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
@@ -17,6 +17,7 @@ import java.util.ArrayList;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
@@ -50,12 +51,12 @@ class AppearancePatentBrandBatchTest {
@Test
void mergedBatchAsksBrandCheckOnceAndAttributesPerRow() throws Exception {
// 桩按「本行的词」返回:含阿凡达的行 failedData 非空,另一行为空。
when(brandCheckClient.checkTitleTexts(anyList(), eq("Terms"))).thenAnswer(invocation -> {
when(brandCheckClient.checkTitleTexts(anyList(), eq("Terms"), any())).thenAnswer(invocation -> {
List<String> titles = invocation.getArgument(0);
List<BrandCheckClient.BrandCheckBatchResult> results = new ArrayList<>();
for (String title : titles) {
List<Object> failed = title.contains("阿凡达") ? List.of("阿凡达") : List.of();
results.add(new BrandCheckClient.BrandCheckBatchResult(List.of(title), failed, List.of()));
results.add(new BrandCheckClient.BrandCheckBatchResult(List.of(title), failed, List.of(), ""));
}
return results;
});
@@ -69,8 +70,8 @@ class AppearancePatentBrandBatchTest {
List<AppearancePatentResultRowDto> merged = client.mergeRowsFromDataText(rows, dataText);
verify(brandCheckClient, times(1)).checkTitleTexts(anyList(), eq("Terms"));
verify(brandCheckClient, never()).checkTitleText(anyString(), anyString());
verify(brandCheckClient, times(1)).checkTitleTexts(anyList(), eq("Terms"), any());
verify(brandCheckClient, never()).checkTitleText(anyString(), anyString(), any());
assertThat(merged).hasSize(2);
assertThat(merged.get(0).getTitleRisk()).isEqualTo("侵权");
assertThat(merged.get(1).getTitleRisk()).as("干净行不得继承别行的侵权词").isEqualTo("无侵权");
@@ -85,8 +86,8 @@ class AppearancePatentBrandBatchTest {
client.mergeRowsFromDataText(rows, dataText);
verify(brandCheckClient, never()).checkTitleTexts(anyList(), anyString());
verify(brandCheckClient, never()).checkTitleText(anyString(), anyString());
verify(brandCheckClient, never()).checkTitleTexts(anyList(), anyString(), any());
verify(brandCheckClient, never()).checkTitleText(anyString(), anyString(), any());
}
/**
@@ -98,12 +99,12 @@ class AppearancePatentBrandBatchTest {
*/
@Test
void fallbackRowsAlsoAskBrandCheckOnceForTheWholeBatch() throws Exception {
when(brandCheckClient.checkTitleTexts(anyList(), eq("Terms"))).thenAnswer(invocation -> {
when(brandCheckClient.checkTitleTexts(anyList(), eq("Terms"), any())).thenAnswer(invocation -> {
List<String> titles = invocation.getArgument(0);
List<BrandCheckClient.BrandCheckBatchResult> results = new ArrayList<>();
for (String title : titles) {
List<Object> failed = title.contains("阿凡达") ? List.of("阿凡达") : List.of();
results.add(new BrandCheckClient.BrandCheckBatchResult(List.of(title), failed, List.of()));
results.add(new BrandCheckClient.BrandCheckBatchResult(List.of(title), failed, List.of(), ""));
}
return results;
});
@@ -126,8 +127,8 @@ class AppearancePatentBrandBatchTest {
@SuppressWarnings("unchecked")
ArgumentCaptor<List<String>> titlesCaptor = ArgumentCaptor.forClass(List.class);
verify(brandCheckClient, times(1)).checkTitleTexts(titlesCaptor.capture(), eq("Terms"));
verify(brandCheckClient, never()).checkTitleText(anyString(), anyString());
verify(brandCheckClient, times(1)).checkTitleTexts(titlesCaptor.capture(), eq("Terms"), any());
verify(brandCheckClient, never()).checkTitleText(anyString(), anyString(), any());
assertThat(titlesCaptor.getValue())
.containsExactly("阿凡达 玩具 手办 正版", "小米 充电宝 20000mAh 快充");
assertThat(rows).hasSize(2);
@@ -85,7 +85,7 @@ class AppearancePatentLlmClientHttpTest {
brand.setRetryTimes(1);
brand.setRetryIntervalMillis(0);
brand.setTotalTimeoutMillis(5000);
return new BrandCheckClient(brand, null);
return new BrandCheckClient(brand, null, null);
}
private void handleBrandCheck(HttpExchange exchange) throws IOException {
@@ -77,7 +77,7 @@ class AppearancePatentLlmClientImageDedupeTest {
brand.setRetryTimes(1);
brand.setRetryIntervalMillis(0);
brand.setTotalTimeoutMillis(5000);
return new BrandCheckClient(brand, null);
return new BrandCheckClient(brand, null, null);
}
@AfterEach
@@ -10,6 +10,7 @@ import org.junit.jupiter.api.Test;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
@@ -25,7 +26,7 @@ class AppearancePatentLlmClientTest {
new AppearancePatentProperties(),
new ObjectMapper(),
null,
new BrandCheckClient(new BrandCheckProperties(), null),
new BrandCheckClient(new BrandCheckProperties(), null, null),
mock(UserSecretUsageService.class)
);
@@ -50,7 +51,7 @@ class AppearancePatentLlmClientTest {
@Test
void titleRiskIsInfringementWhenBrandCheckHasFailedData() {
BrandCheckClient.BrandCheckBatchResult result =
new BrandCheckClient.BrandCheckBatchResult(List.of("阿凡达"), List.of("阿凡达"), List.of());
new BrandCheckClient.BrandCheckBatchResult(List.of("阿凡达"), List.of("阿凡达"), List.of(), "");
assertThat(client.buildTitleRisk("阿凡达", result)).isEqualTo("侵权");
}
@@ -88,7 +89,7 @@ class AppearancePatentLlmClientTest {
@Test
void titleRiskNamesUpstreamOutageOnlyWhenWholeBatchFailed() {
BrandCheckClient.BrandCheckBatchResult failed =
new BrandCheckClient.BrandCheckBatchResult(List.of("nike"), List.of(), List.of("nike"));
new BrandCheckClient.BrandCheckBatchResult(List.of("nike"), List.of(), List.of("nike"), "");
assertThat(client.buildTitleRisk("nike air", failed, true))
.isEqualTo(AppearancePatentLlmClient.BRAND_UPSTREAM_UNAVAILABLE);
@@ -100,7 +101,7 @@ class AppearancePatentLlmClientTest {
@Test
void infringementStillWinsOverWholeBatchOutageMessage() {
BrandCheckClient.BrandCheckBatchResult hit =
new BrandCheckClient.BrandCheckBatchResult(List.of("阿凡达"), List.of("阿凡达"), List.of());
new BrandCheckClient.BrandCheckBatchResult(List.of("阿凡达"), List.of("阿凡达"), List.of(), "");
assertThat(client.buildTitleRisk("阿凡达 联名", hit, true)).isEqualTo("侵权");
}
@@ -134,7 +135,7 @@ class AppearancePatentLlmClientTest {
assertThat(merged).hasSize(1);
assertThat(merged.get(0).getTitleRisk()).isEqualTo("无侵权");
assertThat(merged.get(0).getConclusion()).isEqualTo("无侵权");
verify(brandCheckClient, never()).checkTitleText(anyString(), anyString());
verify(brandCheckClient, never()).checkTitleText(anyString(), anyString(), any());
}
private static AppearancePatentLlmClient clientWith(BrandCheckClient brandCheckClient) {
@@ -152,9 +153,9 @@ class AppearancePatentLlmClientTest {
@Test
void wholeBatchQueryFailureEscalatesEveryRow() throws Exception {
BrandCheckClient brandCheckClient = mock(BrandCheckClient.class);
when(brandCheckClient.checkTitleTexts(anyList(), eq("Terms"))).thenReturn(List.of(
new BrandCheckClient.BrandCheckBatchResult(List.of("nike"), List.of(), List.of("nike")),
new BrandCheckClient.BrandCheckBatchResult(List.of("puma"), List.of(), List.of("puma"))));
when(brandCheckClient.checkTitleTexts(anyList(), eq("Terms"), any())).thenReturn(List.of(
new BrandCheckClient.BrandCheckBatchResult(List.of("nike"), List.of(), List.of("nike"), ""),
new BrandCheckClient.BrandCheckBatchResult(List.of("puma"), List.of(), List.of("puma"), "")));
String dataText = """
{"data":[{"row_id":"1","title":"nike","title_reason":"nike","appearance":"外观识别异常","result":""},
{"row_id":"2","title":"puma","title_reason":"puma","appearance":"外观识别异常","result":""}]}
@@ -175,9 +176,9 @@ class AppearancePatentLlmClientTest {
@Test
void oneSuccessfulRowKeepsOthersOnPerRowFailureText() throws Exception {
BrandCheckClient brandCheckClient = mock(BrandCheckClient.class);
when(brandCheckClient.checkTitleTexts(anyList(), eq("Terms"))).thenReturn(List.of(
new BrandCheckClient.BrandCheckBatchResult(List.of("nike"), List.of(), List.of("nike")),
new BrandCheckClient.BrandCheckBatchResult(List.of("puma"), List.of(), List.of())));
when(brandCheckClient.checkTitleTexts(anyList(), eq("Terms"), any())).thenReturn(List.of(
new BrandCheckClient.BrandCheckBatchResult(List.of("nike"), List.of(), List.of("nike"), ""),
new BrandCheckClient.BrandCheckBatchResult(List.of("puma"), List.of(), List.of(), "")));
String dataText = """
{"data":[{"row_id":"1","title":"nike","title_reason":"nike","appearance":"外观识别异常","result":""},
{"row_id":"2","title":"puma","title_reason":"puma","appearance":"外观识别异常","result":""}]}
@@ -194,9 +195,9 @@ class AppearancePatentLlmClientTest {
void callBrandCheckOnlyWhenTitleAndAppearanceArePresent() throws Exception {
BrandCheckClient brandCheckClient = mock(BrandCheckClient.class);
// 一行也走批接口:逐行调用等于把 90 秒耗时预算按行复制
when(brandCheckClient.checkTitleTexts(anyList(), eq("Terms")))
when(brandCheckClient.checkTitleTexts(anyList(), eq("Terms"), any()))
.thenReturn(List.of(new BrandCheckClient.BrandCheckBatchResult(
List.of("阿凡达"), List.of(), List.of())));
List.of("阿凡达"), List.of(), List.of(), "")));
AppearancePatentLlmClient llmClient = new AppearancePatentLlmClient(
new AppearancePatentProperties(),
new ObjectMapper(),
@@ -214,8 +215,8 @@ class AppearancePatentLlmClientTest {
assertThat(merged).hasSize(1);
assertThat(merged.get(0).getTitleRisk()).isEqualTo("无侵权");
verify(brandCheckClient).checkTitleTexts(List.of("阿凡达"), "Terms");
verify(brandCheckClient, never()).checkTitleText(anyString(), anyString());
verify(brandCheckClient).checkTitleTexts(eq(List.of("阿凡达")), eq("Terms"), any());
verify(brandCheckClient, never()).checkTitleText(anyString(), anyString(), any());
}
@Test
@@ -238,7 +239,7 @@ class AppearancePatentLlmClientTest {
assertThat(merged).hasSize(1);
assertThat(merged.get(0).getTitleRisk()).isEmpty();
verify(brandCheckClient, never()).checkTitleText(anyString(), anyString());
verify(brandCheckClient, never()).checkTitleText(anyString(), anyString(), any());
}
@Test
@@ -262,7 +263,7 @@ class AppearancePatentLlmClientTest {
assertThat(merged).hasSize(1);
assertThat(merged.get(0).getTitleRisk()).isEmpty();
assertThat(merged.get(0).getAppearanceRisk()).isNullOrEmpty();
verify(brandCheckClient, never()).checkTitleText(anyString(), anyString());
verify(brandCheckClient, never()).checkTitleText(anyString(), anyString(), any());
}
@Test
@@ -68,7 +68,7 @@ class AppearancePatentLlmClientTokenUsageTest {
brand.setRetryTimes(1);
brand.setRetryIntervalMillis(0);
brand.setTotalTimeoutMillis(5000);
return new BrandCheckClient(brand, null);
return new BrandCheckClient(brand, null, null);
}
@AfterEach