feat(代理): 多套餐余量预警与用户级 jikip 套餐配置

平台自费池由单个 planId/userId 改为 planId:userId 列表逐套餐探测,
套餐余量或账户余额低于阈值时推管理员预警;用户可在密钥设置里为自己
配置多个套餐,巡检跟随密钥检查按用户查余量并推站内通知,欠费状态在
客户端右上角常驻提示(V160 新建用户级套餐表)。

同时把测试里写死的真实代理 key 换成占位串,避免凭证随仓库扩散。
This commit is contained in:
2026-10-21 13:32:56 +08:00
parent 19a291ec39
commit f6aa04d0f2
31 changed files with 1544 additions and 52 deletions
@@ -185,6 +185,122 @@ class NotificationScanSchedulerTest {
assertThat(keyBase.getValue()).matches("service_down:brand-service:\\d{8}");
}
/** 平台自费代理事前预警:套餐余量低于阈值必须推管理员警告,而不是等停摆后报接口不可用。 */
@Test
void lowProxySurplusWarnsAdminsBeforeOutage() {
properties.setBrandServiceUrl("");
properties.setPriceTrackApiUrl("");
properties.setJikipLowSurplusThreshold(100);
when(proxyBalancePort.fetchBalances()).thenReturn(List.of(plan("p-1", "80", "50")));
when(dispatch.pushToAdmins(anyString(), anyString(), anyString(), anyString(), anyString(), any()))
.thenReturn(1);
newScheduler().probeServices();
ArgumentCaptor<String> keyBase = ArgumentCaptor.forClass(String.class);
verify(dispatch).pushToAdmins(eq(NotificationService.SCENE_SERVICE_DOWN),
eq(NotificationService.LEVEL_WARNING), eq("jikip 代理套餐余量不足"), anyString(),
keyBase.capture(), org.mockito.ArgumentMatchers.<Long>isNull());
assertThat(keyBase.getValue()).matches("jikip_low_surplus:p-1:\\d{8}");
}
/** 余量充足不许发预警:阈值默认常开,误报会把真正的停摆通知淹掉。 */
@Test
void healthyProxySurplusDoesNotWarn() {
properties.setBrandServiceUrl("");
properties.setPriceTrackApiUrl("");
properties.setJikipLowSurplusThreshold(100);
when(proxyBalancePort.fetchBalances()).thenReturn(List.of(plan("p-1", "5000", "50")));
newScheduler().probeServices();
verify(dispatch, never()).pushToAdmins(anyString(), anyString(), anyString(), anyString(), anyString(), any());
}
/** 供应商改字段口径(非数字)时宁可不报,不能把「解析失败」当成余量 0 天天误报。 */
@Test
void unparsableSurplusDoesNotWarn() {
properties.setBrandServiceUrl("");
properties.setPriceTrackApiUrl("");
properties.setJikipLowSurplusThreshold(100);
when(proxyBalancePort.fetchBalances()).thenReturn(List.of(plan("p-1", "充足", null)));
newScheduler().probeServices();
verify(dispatch, never()).pushToAdmins(anyString(), anyString(), anyString(), anyString(), anyString(), any());
}
/** 多套餐:每个套餐独立判定、独立去重,不能因一个套餐健康就漏掉另一个。 */
@Test
void eachLowPlanWarnsIndependently() {
properties.setBrandServiceUrl("");
properties.setPriceTrackApiUrl("");
properties.setJikipLowSurplusThreshold(100);
when(proxyBalancePort.fetchBalances()).thenReturn(List.of(
plan("p-a", "50", "50"),
plan("p-b", "80", "50"),
plan("p-c", "5000", "50")));
when(dispatch.pushToAdmins(anyString(), anyString(), anyString(), anyString(), anyString(), any()))
.thenReturn(1);
newScheduler().probeServices();
ArgumentCaptor<String> keyBase = ArgumentCaptor.forClass(String.class);
verify(dispatch, times(2)).pushToAdmins(eq(NotificationService.SCENE_SERVICE_DOWN),
eq(NotificationService.LEVEL_WARNING), eq("jikip 代理套餐余量不足"), anyString(),
keyBase.capture(), org.mockito.ArgumentMatchers.<Long>isNull());
assertThat(keyBase.getAllValues()).anySatisfy(key ->
assertThat(key).matches("jikip_low_surplus:p-a:\\d{8}"));
assertThat(keyBase.getAllValues()).anySatisfy(key ->
assertThat(key).matches("jikip_low_surplus:p-b:\\d{8}"));
}
/** 完全没有配置套餐时跳过,不发告警。 */
@Test
void unconfiguredPlansSkipWithoutWarning() {
properties.setBrandServiceUrl("");
properties.setPriceTrackApiUrl("");
when(proxyBalancePort.fetchBalances()).thenReturn(List.of());
newScheduler().probeServices();
verify(dispatch, never()).pushToAdmins(anyString(), anyString(), anyString(), anyString(), anyString(), any());
}
/** 余额阈值默认关闭(0);显式配置后低于阈值同样预警。 */
@Test
void proxyBalanceWarnsOnlyWhenThresholdEnabled() {
properties.setBrandServiceUrl("");
properties.setPriceTrackApiUrl("");
properties.setJikipLowSurplusThreshold(0);
when(proxyBalancePort.fetchBalances()).thenReturn(List.of(plan("p-1", "5000", "3")));
newScheduler().probeServices();
verify(dispatch, never()).pushToAdmins(anyString(), anyString(), anyString(), anyString(), anyString(), any());
properties.setJikipLowBalanceThreshold(10);
when(dispatch.pushToAdmins(anyString(), anyString(), anyString(), anyString(), anyString(), any()))
.thenReturn(1);
newScheduler().probeServices();
ArgumentCaptor<String> keyBase = ArgumentCaptor.forClass(String.class);
verify(dispatch).pushToAdmins(eq(NotificationService.SCENE_SERVICE_DOWN),
eq(NotificationService.LEVEL_WARNING), eq("jikip 代理账户余额不足"), anyString(),
keyBase.capture(), org.mockito.ArgumentMatchers.<Long>isNull());
assertThat(keyBase.getValue()).matches("jikip_low_balance:p-1:\\d{8}");
}
@Test
void firstNumberParsesSupplierFormats() {
assertThat(NotificationScanScheduler.firstNumber("123")).isEqualTo(123L);
assertThat(NotificationScanScheduler.firstNumber("123次")).isEqualTo(123L);
assertThat(NotificationScanScheduler.firstNumber("12.5")).isEqualTo(12L);
assertThat(NotificationScanScheduler.firstNumber("充足")).isNull();
assertThat(NotificationScanScheduler.firstNumber(null)).isNull();
assertThat(NotificationScanScheduler.firstNumber("123456789012345678901")).isNull();
}
/** 状态码三态口径:4xx 里只有 401/403/404 是「探活不可信」,其余按故障算。 */
@Test
void httpStatusClassifiesIntoThreeProbeVerdicts() {
@@ -348,6 +464,11 @@ class NotificationScanSchedulerTest {
return server;
}
private static ProxyBalancePort.ProxyBalanceEntry plan(String planId, String surplus, String balance) {
return new ProxyBalancePort.ProxyBalanceEntry(planId,
new ProxyBalancePort.ProxyBalanceSnapshot(true, surplus, balance, null));
}
private void stubAdminAudience() {
when(dispatch.prepareAdminAudience()).thenReturn(
new NotificationDispatchService.AdminAudience(List.of(), Set.of(), Map.of()));