feat(brand-check): 品牌检测按任务归属用户取代理并计用量——三处调用接入

- BrandCheckClient 契约扩展:构造器收 UserApiSecretService,check/checkAll/
  checkTitleText(s) 全系带 userId,请求体新增 user_id + proxy_url(proxy 取自
  用户密钥库 module=proxy);响应新增 reason/retryable,retryable=false(供应商
  拒付、用户没配代理)当场停重试阶梯并短路整批
- 三处调用方接入:CollectDataService 品牌过滤、CollectDataBrandRecheckScheduler
  补测(markAttempted 带真实原因)、AppearancePatentLlmClient 标题查标(取
  SecretUsageContext 归属用户)
- 用户未配置代理:整批本地定论「用户未配置代理」,零 HTTP,不再有平台钥匙兜底
- 测试迁移:15 支测试文件按新契约迁移(3 参构造、userId 实参、BrandCheckBatchResult
  第 4 位 reason、Mockito 匹配器加宽);HTTP 类测试 stub findPlainValue 非空代理
  以绕开零 HTTP 短路。13 类 119 例全绿(1 skip 为既有快照写盘门),test-compile 零错误
This commit is contained in:
2026-10-20 18:14:52 +08:00
parent 492a27270a
commit 6c14f29a87
20 changed files with 395 additions and 220 deletions
@@ -7,6 +7,7 @@ import com.nanri.aiimage.config.ZiniaoProperties;
import com.nanri.aiimage.metrics.ExternalCallMetricsRecorder;
import com.nanri.aiimage.modules.brand.client.BrandCheckClient;
import com.nanri.aiimage.modules.similarasin.client.SimilarAsinLlmClient;
import com.nanri.aiimage.modules.usersecret.service.UserApiSecretService;
import com.nanri.aiimage.modules.ziniao.client.ZiniaoClientImpl;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpServer;
@@ -34,6 +35,7 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.nanri.aiimage.modules.usersecret.service.UserSecretUsageService;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* Task 78:为所有外部调用(LLM / 品牌检查 / 紫鸟)统一增加耗时、重试、
@@ -190,8 +192,8 @@ class ExternalCallMetricsRecorderTest {
"client", "llm", "result", "success"), "三次成功全部记录");
// 全部外部客户端(LLM / 品牌检查 / 紫鸟)在同一次批量中各自记录指标
BrandCheckClient brand = new BrandCheckClient(brandProps(), new ExternalCallMetricsRecorder(registry));
brand.checkAll(List.of("Nintendo", "LEGO", "Sony"), "Terms");
BrandCheckClient brand = new BrandCheckClient(brandProps(), new ExternalCallMetricsRecorder(registry), stubbedUserSecrets());
brand.checkAll(List.of("Nintendo", "LEGO", "Sony"), "Terms", 7L);
ZiniaoClientImpl ziniao = new ZiniaoClientImpl(ziniaoProps(), objectMapper, new ExternalCallMetricsRecorder(registry));
assertEquals(1001L, ziniao.getCompanyIdByApiKey("test-api-key"));
@@ -208,10 +210,10 @@ class ExternalCallMetricsRecorderTest {
@Test
void test_task_078_payload_metrics_normal_repeated_operation_is_idempotent() throws Exception {
BrandCheckProperties props = brandProps();
BrandCheckClient client = new BrandCheckClient(props, new ExternalCallMetricsRecorder(registry));
BrandCheckClient client = new BrandCheckClient(props, new ExternalCallMetricsRecorder(registry), stubbedUserSecrets());
client.check("Nintendo");
client.check("Nintendo");
client.check("Nintendo", "Terms", 7L);
client.check("Nintendo", "Terms", 7L);
assertEquals(2, brandCount.get(), "两次请求真实发出");
assertEquals(2.0, counterCount("aiimage.external-call.total",
@@ -225,9 +227,9 @@ class ExternalCallMetricsRecorderTest {
@Test
void test_task_078_payload_metrics_boundary_empty_input() throws Exception {
BrandCheckProperties props = brandProps();
BrandCheckClient client = new BrandCheckClient(props, new ExternalCallMetricsRecorder(registry));
BrandCheckClient client = new BrandCheckClient(props, new ExternalCallMetricsRecorder(registry), stubbedUserSecrets());
BrandCheckClient.BrandCheckBatchResult result = client.checkAll(List.of(), "Terms");
BrandCheckClient.BrandCheckBatchResult result = client.checkAll(List.of(), "Terms", 7L);
assertEquals(0, brandCount.get(), "空输入不发起外部调用");
assertTrue(result.brands().isEmpty());
@@ -240,9 +242,9 @@ class ExternalCallMetricsRecorderTest {
@Test
void test_task_078_payload_metrics_boundary_single_item() throws Exception {
BrandCheckProperties props = brandProps();
BrandCheckClient client = new BrandCheckClient(props, new ExternalCallMetricsRecorder(registry));
BrandCheckClient client = new BrandCheckClient(props, new ExternalCallMetricsRecorder(registry), stubbedUserSecrets());
BrandCheckClient.BrandCheckResponse response = client.check("LEGO");
BrandCheckClient.BrandCheckResponse response = client.check("LEGO", "Terms", 7L);
assertNotNull(response);
awaitMetric("aiimage.external-call.duration", "client", "brand");
@@ -295,9 +297,9 @@ class ExternalCallMetricsRecorderTest {
@Test
void test_task_078_payload_metrics_invalid_input_rejected() throws Exception {
BrandCheckProperties props = brandProps();
BrandCheckClient client = new BrandCheckClient(props, new ExternalCallMetricsRecorder(registry));
BrandCheckClient client = new BrandCheckClient(props, new ExternalCallMetricsRecorder(registry), stubbedUserSecrets());
BrandCheckClient.BrandCheckBatchResult result = client.checkAll(null, "Terms");
BrandCheckClient.BrandCheckBatchResult result = client.checkAll(null, "Terms", 7L);
assertTrue(result.brands().isEmpty(), "null 列表安全跳过");
assertEquals(0, brandCount.get(), "非法输入不发起外部调用");
@@ -345,6 +347,14 @@ class ExternalCallMetricsRecorderTest {
return props;
}
/** 品牌检查按 userId 取用户自己的代理:不桩一个非空代理就一次上游都不会打。 */
private UserApiSecretService stubbedUserSecrets() {
UserApiSecretService secrets = mock(UserApiSecretService.class);
when(secrets.findPlainValue(org.mockito.ArgumentMatchers.anyLong(),
org.mockito.ArgumentMatchers.anyString())).thenReturn("http://user:pass@127.0.0.1:9");
return secrets;
}
private ZiniaoProperties ziniaoProps() {
ZiniaoProperties props = new ZiniaoProperties();
props.setBaseUrl("http://127.0.0.1:" + port);