From 6c14f29a87a592e64349855d0a9aa16f167fafbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E8=87=AA=E8=BE=BE?= <980324341@qq.com> Date: Tue, 20 Oct 2026 18:14:52 +0800 Subject: [PATCH] =?UTF-8?q?feat(brand-check):=20=E5=93=81=E7=89=8C?= =?UTF-8?q?=E6=A3=80=E6=B5=8B=E6=8C=89=E4=BB=BB=E5=8A=A1=E5=BD=92=E5=B1=9E?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E5=8F=96=E4=BB=A3=E7=90=86=E5=B9=B6=E8=AE=A1?= =?UTF-8?q?=E7=94=A8=E9=87=8F=E2=80=94=E2=80=94=E4=B8=89=E5=A4=84=E8=B0=83?= =?UTF-8?q?=E7=94=A8=E6=8E=A5=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 零错误 --- .../client/AppearancePatentLlmClient.java | 8 +- .../brand/client/BrandCheckClient.java | 161 ++++++++++++++---- .../CollectDataBrandRecheckScheduler.java | 8 +- .../service/CollectDataService.java | 8 +- .../util/CollectDataBrandBatchFilter.java | 49 ++++-- .../config/HttpClientConnectionReuseTest.java | 16 +- .../ExternalCallMetricsRecorderTest.java | 32 ++-- .../AppearancePatentBrandBatchTest.java | 21 +-- .../AppearancePatentLlmClientHttpTest.java | 2 +- ...earancePatentLlmClientImageDedupeTest.java | 2 +- .../client/AppearancePatentLlmClientTest.java | 35 ++-- ...pearancePatentLlmClientTokenUsageTest.java | 2 +- .../brand/client/BrandCheckClientTest.java | 38 +++-- .../BrandCheckClientTitleBatchTest.java | 17 +- .../CollectDataBrandRecheckSchedulerTest.java | 26 +-- .../CollectDataServiceTxBoundaryTest.java | 16 +- .../CollectDataSubmitResultSnapshotTest.java | 4 +- .../util/CollectDataBrandBatchFilterTest.java | 96 +++++------ .../util/CollectDataBrandCacheTest.java | 70 ++++---- .../DuplicateSubmissionContractTest.java | 4 +- 20 files changed, 395 insertions(+), 220 deletions(-) diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/appearancepatent/client/AppearancePatentLlmClient.java b/backend-java/src/main/java/com/nanri/aiimage/modules/appearancepatent/client/AppearancePatentLlmClient.java index eaf948282..c316cd688 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/appearancepatent/client/AppearancePatentLlmClient.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/appearancepatent/client/AppearancePatentLlmClient.java @@ -1042,7 +1042,13 @@ public class AppearancePatentLlmClient { BrandCheckClient.BrandCheckBatchResult[] byRowIndex = new BrandCheckClient.BrandCheckBatchResult[results.size()]; if (!pending.isEmpty()) { - List batch = brandCheckClient.checkTitleTexts(titles, "Terms"); + // 品牌检查用的代理=这批结果归属那个用户自己的提取链接(16890 不再兜底平台 key)。 + // 取不到任务上下文就是无人归属,此时不去打上游:上游要么用别人的套餐、要么裸 IP 被拒, + // 两种都把一次配置缺失变成一笔算不清的账。 + SecretUsageContext.Context usage = SecretUsageContext.current(); + Long ownerUserId = usage == null ? null : usage.userId(); + List batch = + brandCheckClient.checkTitleTexts(titles, "Terms", ownerUserId); for (int k = 0; k < pending.size(); k++) { byRowIndex[pending.get(k)] = batch.get(k); } diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/brand/client/BrandCheckClient.java b/backend-java/src/main/java/com/nanri/aiimage/modules/brand/client/BrandCheckClient.java index d31a9ff20..ab2c35ce2 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/brand/client/BrandCheckClient.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/brand/client/BrandCheckClient.java @@ -2,6 +2,8 @@ package com.nanri.aiimage.modules.brand.client; import com.fasterxml.jackson.annotation.JsonAlias; import com.nanri.aiimage.config.BrandCheckProperties; +import com.nanri.aiimage.modules.usersecret.service.UserApiSecretService; +import com.nanri.aiimage.modules.usersecret.support.UserSecretModule; import jakarta.annotation.PreDestroy; import lombok.Data; import lombok.extern.slf4j.Slf4j; @@ -35,14 +37,20 @@ public class BrandCheckClient { /** 品牌检查并发度:避免一次批量检查把第三方服务打满。 */ private static final int BRAND_CHECK_CONCURRENCY = 3; + /** 用户没配代理时交回的原因文案;采集侧与补测队列都按它把行判查询失败。 */ + public static final String NO_PROXY_REASON = "用户未配置代理"; + private final BrandCheckProperties properties; private final com.nanri.aiimage.metrics.ExternalCallMetricsRecorder externalCallMetrics; + private final UserApiSecretService userApiSecretService; private volatile RestClient sharedRestClient; public BrandCheckClient(BrandCheckProperties properties, - com.nanri.aiimage.metrics.ExternalCallMetricsRecorder externalCallMetrics) { + com.nanri.aiimage.metrics.ExternalCallMetricsRecorder externalCallMetrics, + UserApiSecretService userApiSecretService) { this.properties = properties; this.externalCallMetrics = externalCallMetrics; + this.userApiSecretService = userApiSecretService; } // 有界队列 + CallerRuns:大批量(几千品牌)检查时队列洪峰不无界堆内存, @@ -68,18 +76,32 @@ public class BrandCheckClient { }; } - public BrandCheckResponse check(String brand) { - return check(brand, properties.getDefaultStrategy()); + /** + * 单品牌查一次上游。 + * + *

{@code userId} 决定这次烧谁的代理:16890 不再自带平台 key(那把 key 的套餐 + * 2026-10-19 过期,全体用户的品牌检测一起静默失效,而用户在后台只看到「没采到数据」)。 + * 用户没配代理时**一次上游都不打**,当场按查询失败交回原因——重试救不回缺代理, + * 十条阶梯只会把一个人的配置问题摊成上游十分钟的重试流量。 + */ + public BrandCheckResponse check(String brand, String strategy, Long userId) { + return postCheck(brand, strategy, userId, proxyUrlFor(userId)); } - public BrandCheckResponse check(String brand, String strategy) { + private BrandCheckResponse postCheck(String brand, String strategy, Long userId, String proxyUrl) { String normalizedBrand = normalize(brand); if (normalizedBrand.isBlank()) { return BrandCheckResponse.empty(); } + if (proxyUrl.isBlank()) { + log.warn("[brand-check] {} userId={}(不打上游)", NO_PROXY_REASON, userId); + return BrandCheckResponse.noProxy(normalizedBrand); + } Map body = new LinkedHashMap<>(); body.put("brand", normalizedBrand); body.put("strategy", normalize(strategy).isBlank() ? "Terms" : normalize(strategy)); + body.put("user_id", userId); + body.put("proxy_url", proxyUrl); return restClient().post() .uri(joinUrl(properties.getBaseUrl(), properties.getPath())) .headers(headers -> { @@ -94,8 +116,19 @@ public class BrandCheckClient { .body(BrandCheckResponse.class); } - public BrandCheckBatchResult checkAll(List brands, String strategy) { - return checkAll(brands, strategy, properties.getTotalTimeoutMillis()); + /** + * 该用户的代理提取链接(module=proxy 明文,支持 {@code url;mode;url;mode} 多 key 写法)。 + * findPlainValue 自身不抛:未配置/解密失败一律空串。 + */ + private String proxyUrlFor(Long userId) { + if (userId == null || userId <= 0L || userApiSecretService == null) { + return ""; + } + return normalize(userApiSecretService.findPlainValue(userId, UserSecretModule.PROXY.key())); + } + + public BrandCheckBatchResult checkAll(List brands, String strategy, Long userId) { + return checkAll(brands, strategy, properties.getTotalTimeoutMillis(), userId); } /** @@ -106,15 +139,26 @@ public class BrandCheckClient { * 是给后台批量任务(外观合批、品牌补测调度)的预算,同步等响应的桌面客户端不该替它扛 * ——上游卡死时按批各领一个 90 秒,一个 chunk 十几批能叠到十几分钟。 */ - public BrandCheckBatchResult checkAll(List brands, String strategy, long budgetMillis) { + public BrandCheckBatchResult checkAll(List brands, String strategy, + long budgetMillis, Long userId) { List distinctBrands = distinctNonBlank(brands); List failedData = new ArrayList<>(); List queryFailedData = new ArrayList<>(); - for (BrandCheckOutcome outcome : checkWords(distinctBrands, strategy, deadlineNanos(budgetMillis)).values()) { + Map outcomes = + checkWords(distinctBrands, strategy, deadlineNanos(budgetMillis), userId); + List reasons = new ArrayList<>(); + for (String brand : distinctBrands) { + BrandCheckOutcome outcome = outcomes.get(brand); + if (outcome == null) { + continue; + } failedData.addAll(outcome.failedData()); queryFailedData.addAll(outcome.queryFailedData()); + if (!outcome.reason().isBlank() && !reasons.contains(outcome.reason())) { + reasons.add(outcome.reason()); + } } - return new BrandCheckBatchResult(distinctBrands, failedData, queryFailedData); + return new BrandCheckBatchResult(distinctBrands, failedData, queryFailedData, String.join(";", reasons)); } /** @@ -126,7 +170,7 @@ public class BrandCheckClient { * 与货源侧 CollectDataBrandBatchFilter 已上线的口径一致。 * 返回值与入参同序同长,空标题行返回空词集合(调用方据此回退成标题原文)。 */ - public List checkTitleTexts(List titles, String strategy) { + public List checkTitleTexts(List titles, String strategy, Long userId) { if (titles == null || titles.isEmpty()) { return List.of(); } @@ -138,19 +182,24 @@ public class BrandCheckClient { distinctWords.addAll(words); } Map outcomes = checkWords( - new ArrayList<>(distinctWords), strategy, deadlineNanos(properties.getTotalTimeoutMillis())); + new ArrayList<>(distinctWords), strategy, + deadlineNanos(properties.getTotalTimeoutMillis()), userId); List results = new ArrayList<>(titles.size()); for (List words : wordsByTitle) { List failedData = new ArrayList<>(); List queryFailedData = new ArrayList<>(); + List reasons = new ArrayList<>(); for (String word : words) { BrandCheckOutcome outcome = outcomes.get(word); if (outcome != null) { failedData.addAll(outcome.failedData()); queryFailedData.addAll(outcome.queryFailedData()); + if (!outcome.reason().isBlank() && !reasons.contains(outcome.reason())) { + reasons.add(outcome.reason()); + } } } - results.add(new BrandCheckBatchResult(words, failedData, queryFailedData)); + results.add(new BrandCheckBatchResult(words, failedData, queryFailedData, String.join(";", reasons))); } return results; } @@ -159,17 +208,29 @@ public class BrandCheckClient { * 并发查一批**已去重**的词,整批共用调用方给定的截止时刻。 * 键顺序即入参顺序,checkAll 的并集顺序因此与改动前一致。 */ - private Map checkWords(List brands, String strategy, long deadlineNanos) { + private Map checkWords(List brands, String strategy, + long deadlineNanos, Long userId) { // 整批共用一个耗时预算:上游 16890 卡死时,单品牌 10 次重试曾把一次分片回传拖到 // 103.5 秒(taskId 28599),客户端重试预算耗尽后中止了整个采集。预算用尽即停止重试。 Map outcomes = new LinkedHashMap<>(); + String proxyUrl = proxyUrlFor(userId); + if (proxyUrl.isBlank()) { + // 缺代理整批一个请求都不发:这是配置问题,十条重试阶梯在这里只会白烧, + // 而 16890 拿不到调用方的代理就只能裸打 WIPO——裸 IP 必被拒(2026-10-19 现网)。 + log.warn("[brand-check] {},整批不打上游 userId={} 品牌数={}", NO_PROXY_REASON, userId, brands.size()); + for (String brand : brands) { + outcomes.put(brand, new BrandCheckOutcome(List.of(), List.of(brand), NO_PROXY_REASON)); + } + return outcomes; + } List> futures = new ArrayList<>(brands.size()); // 一次批量一份判定:本批里只要有一个品牌跑满阶梯仍被判查询失败,其余品牌就不再 // 各自烧完整条重试阶梯(见 checkOneBrand 的短路分支)。 AtomicBoolean upstreamRejecting = new AtomicBoolean(false); for (String brand : brands) { futures.add(CompletableFuture.supplyAsync( - () -> checkOneBrand(brand, strategy, deadlineNanos, upstreamRejecting), checkExecutor)); + () -> checkOneBrand(brand, strategy, deadlineNanos, upstreamRejecting, proxyUrl, userId), + checkExecutor)); } for (int index = 0; index < brands.size(); index++) { outcomes.put(brands.get(index), awaitOutcome(futures.get(index))); @@ -183,10 +244,11 @@ public class BrandCheckClient { } private BrandCheckOutcome checkOneBrand(String brand, String strategy, long deadlineNanos, - AtomicBoolean upstreamRejecting) { + AtomicBoolean upstreamRejecting, String proxyUrl, Long userId) { int attempts = Math.max(1, properties.getRetryTimes()); BrandCheckResponse response = null; Exception lastFailure = null; + String reason = ""; // 只有「把整条重试阶梯走完」才算拿到"上游在确定性拒绝"的证据。 // 被预算或短路掐断的轮次不是证据,拿它去短路同批其余品牌会把偶发抖动放大成整批放弃。 boolean ladderCompleted = true; @@ -212,7 +274,7 @@ public class BrandCheckClient { break; } try { - response = check(brand, strategy); + response = postCheck(brand, strategy, userId, proxyUrl); } catch (Exception ex) { lastFailure = ex; response = null; @@ -226,7 +288,20 @@ public class BrandCheckClient { } if (response != null && nullToEmpty(response.getQueryFaildData()).isEmpty()) { // 本次查询完成且无失败项:命中侵权(faild_data)属有效结论,不重试。 - return new BrandCheckOutcome(nullToEmpty(response.getFaildData()), List.of()); + return new BrandCheckOutcome(nullToEmpty(response.getFaildData()), List.of(), ""); + } + // 走到这里 response 必非 null:抛异常的那一轮在 catch 里已经 continue 掉了。 + if (response.getReason() != null && !response.getReason().isBlank()) { + reason = response.getReason().trim(); + } + if (response != null && !response.isRetryable()) { + // 上游当场判定"重试也不会变好"(供应商拒付、套餐过期、这类结论是确定的), + // 阶梯到此为止并把同批一起短路——旧版这里要跑满 10 轮、约 45~65 秒才知道 + // 同一个坏消息,而它烧掉的正是后面那些品牌的机会。 + upstreamRejecting.set(true); + ladderCompleted = false; + log.warn("[brand-check] 上游判定确定性拒绝,停止重试 brand={} reason={}", brand, reason); + break; } if (attempt < attempts) { long retryDelayMillis = retryDelayMillis(attempt); @@ -242,11 +317,11 @@ public class BrandCheckClient { && !nullToEmpty(response.getQueryFaildData()).isEmpty()) { upstreamRejecting.set(true); } - log.warn("[brand-check] 重试耗尽判定查询失败 brand={} attempts={} lastErr={}", + log.warn("[brand-check] 重试耗尽判定查询失败 brand={} attempts={} lastErr={} reason={}", brand, attempts, - lastFailure == null ? "query_faild_data 持续非空" : lastFailure.getMessage()); + lastFailure == null ? "query_faild_data 持续非空" : lastFailure.getMessage(), reason); return new BrandCheckOutcome(List.of(), - response == null ? List.of(brand) : nullToEmpty(response.getQueryFaildData())); + response == null ? List.of(brand) : nullToEmpty(response.getQueryFaildData()), reason); } /** @@ -278,19 +353,22 @@ public class BrandCheckClient { } catch (CompletionException ex) { log.warn("[brand-check] unexpected batch failure err={}", ex.getCause() == null ? ex.getMessage() : String.valueOf(ex.getCause().getMessage())); - return new BrandCheckOutcome(List.of(), List.of()); + return new BrandCheckOutcome(List.of(), List.of(), ""); } } - private record BrandCheckOutcome(List failedData, List queryFailedData) { + /** + * 单个品牌的结论。 + * + * @param reason 查询失败时的可展示原因(供应商拒付 / 用户未配置代理 / 上游拒绝代理出口); + * 结论正常时为空串。它存在的意义是让采集侧与补测队列能把「为什么查不了」 + * 写进任务,而不是只留一句 {@code query_faild_data 持续非空}。 + */ + private record BrandCheckOutcome(List failedData, List queryFailedData, String reason) { } - public BrandCheckBatchResult checkTitleText(String titleText) { - return checkTitleText(titleText, properties.getDefaultStrategy()); - } - - public BrandCheckBatchResult checkTitleText(String titleText, String strategy) { - return checkAll(splitTitleText(titleText), strategy); + public BrandCheckBatchResult checkTitleText(String titleText, String strategy, Long userId) { + return checkAll(splitTitleText(titleText), strategy, userId); } public List splitTitleText(String titleText) { @@ -392,15 +470,35 @@ public class BrandCheckClient { @JsonAlias({"query_faild_data", "query_failed_data"}) private List queryFaildData = new ArrayList<>(); + /** 查询失败的可展示原因;上游旧版本不回传该字段时为空串,行为与改造前一致。 */ + private String reason = ""; + + /** + * false=上游判定这个结论重也不会变好(供应商拒付、用户没配代理), + * 重试阶梯应当当场停下。缺省 true 是刻意的:旧版 16890 不带这个字段时, + * 不能把"没带"读成"别重试"。 + */ + private boolean retryable = true; + private static BrandCheckResponse empty() { return new BrandCheckResponse(); } + + /** 用户没配代理:Java 侧本地就能定论,一次上游都不该打。 */ + private static BrandCheckResponse noProxy(String brand) { + BrandCheckResponse response = new BrandCheckResponse(); + response.setQueryFaildData(new ArrayList<>(List.of(brand))); + response.setReason(NO_PROXY_REASON); + response.setRetryable(false); + return response; + } } public record BrandCheckBatchResult( List brands, List faildData, - List queryFaildData + List queryFaildData, + String reason ) { public boolean hasFailedData() { return faildData != null && !faildData.isEmpty(); @@ -409,5 +507,10 @@ public class BrandCheckClient { public boolean hasQueryFailedData() { return queryFaildData != null && !queryFaildData.isEmpty(); } + + /** 查询失败原因(多个用;拼接);结论正常时为空串。 */ + public String reason() { + return reason == null ? "" : reason; + } } } diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/collectdata/service/CollectDataBrandRecheckScheduler.java b/backend-java/src/main/java/com/nanri/aiimage/modules/collectdata/service/CollectDataBrandRecheckScheduler.java index e84bc6c31..98a0b41ed 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/collectdata/service/CollectDataBrandRecheckScheduler.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/collectdata/service/CollectDataBrandRecheckScheduler.java @@ -144,7 +144,7 @@ public class CollectDataBrandRecheckScheduler { List brands = distinctBrands(entries); BrandCheckClient.BrandCheckBatchResult check; try { - check = brandCheckClient.checkAll(brands, "Terms"); + check = brandCheckClient.checkAll(brands, "Terms", task.getUserId()); } catch (RuntimeException ex) { // 整批检查异常(网络/服务级故障):按实测失败计次退避,下轮重试 log.warn("[brand-recheck] 批量品牌检测异常,按失败计次 taskId={} brands={} err={}", @@ -194,6 +194,10 @@ public class CollectDataBrandRecheckScheduler { } int exhaustedThisRound = 0; + // 补测这一轮到底为什么还是查不了:上游给的原因(供应商拒付/用户未配置代理/出口被拒) + // 直接写进条目,收口的条目才读得懂是"查不到"还是"没得查"。上游没给原因时保留旧文案。 + String checkReason = check == null ? "" : check.reason(); + String stillFailedReason = checkReason.isBlank() ? "query_faild_data 持续非空" : checkReason; for (CollectDataBrandRecheckEntity entry : cleanEntries) { recheckService.markInjected(entry); } @@ -201,7 +205,7 @@ public class CollectDataBrandRecheckScheduler { recheckService.markRejected(entry); } for (CollectDataBrandRecheckEntity entry : stillFailedEntries) { - if (recheckService.markAttempted(entry, "query_faild_data 持续非空")) { + if (recheckService.markAttempted(entry, stillFailedReason)) { // updateQuietly 失败时下轮按 PENDING 重放,不算收口 if (CollectDataBrandRecheckEntity.STATUS_EXHAUSTED.equals(entry.getStatus())) { exhaustedThisRound++; diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/collectdata/service/CollectDataService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/collectdata/service/CollectDataService.java index fc292ca9f..bf648e3f3 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/collectdata/service/CollectDataService.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/collectdata/service/CollectDataService.java @@ -823,9 +823,11 @@ public class CollectDataService extends AbstractFileTaskPipeline rows) { + public BrandBatchOutcome filter(List rows, Long userId) { List rejected = new ArrayList<>(); List queryFailed = new ArrayList<>(); List accepted = new ArrayList<>(); + List reasons = new ArrayList<>(); if (rows == null || rows.isEmpty()) { - return new BrandBatchOutcome(rejected, queryFailed, accepted); + return new BrandBatchOutcome(rejected, queryFailed, accepted, ""); } long deadlineNanos = chunkBudgetMillis > 0L ? System.nanoTime() + chunkBudgetMillis * 1_000_000L : Long.MAX_VALUE; @@ -124,9 +128,13 @@ public class CollectDataBrandBatchFilter { } Map batchVerdicts = new LinkedHashMap<>(cachedVerdicts); try { - batchVerdicts.putAll(checkAndCache(uncachedBrands, remainingBudgetMillis(deadlineNanos))); + BatchVerdicts checked = checkAndCache(uncachedBrands, + remainingBudgetMillis(deadlineNanos), userId); + batchVerdicts.putAll(checked.verdicts()); + addReason(reasons, checked.reason()); } catch (RuntimeException ex) { log.warn("[collect-data] brand check batch failed, degrade batch to queryFailed err={}", ex.getMessage()); + addReason(reasons, "品牌检查批次异常: " + ex.getMessage()); for (CollectDataResultRowVo row : batch) { if (row != null) { queryFailed.add(row); @@ -140,7 +148,14 @@ public class CollectDataBrandBatchFilter { log.info("[collect-data] 品牌检查预算用尽,剩余批次转入补测队列 预算={}ms 延后行={} 总行={}", chunkBudgetMillis, budgetCutRows, rows.size()); } - return new BrandBatchOutcome(rejected, queryFailed, accepted); + return new BrandBatchOutcome(rejected, queryFailed, accepted, String.join(";", reasons)); + } + + /** 原因清单去重累积(同一次任务里同一个坏消息不该在日志里重复三遍)。 */ + private static void addReason(List reasons, String reason) { + if (reason != null && !reason.isBlank() && !reasons.contains(reason)) { + reasons.add(reason); + } } /** 本批次还能用的毫秒数;0 表示不限制(交给 checkAll 按配置预算走)。 */ @@ -151,9 +166,14 @@ public class CollectDataBrandBatchFilter { return Math.max(1L, (deadlineNanos - System.nanoTime()) / 1_000_000L); } - /** 远程检查未缓存品牌并写入缓存;返回新查品牌(小写标准化)→ 判定映射。 */ - private Map checkAndCache(List uncachedBrands, long budgetMillis) { - BrandCheckClient.BrandCheckBatchResult check = brandCheckClient.checkAll(uncachedBrands, "Terms", budgetMillis); + /** 本批次远程检查的结论与原因(原因=这批为什么查不了,可能为空串)。 */ + private record BatchVerdicts(Map verdicts, String reason) { + } + + /** 远程检查未缓存品牌并写入缓存;返回新查品牌(小写标准化)→ 判定映射与失败原因。 */ + private BatchVerdicts checkAndCache(List uncachedBrands, long budgetMillis, Long userId) { + BrandCheckClient.BrandCheckBatchResult check = + brandCheckClient.checkAll(uncachedBrands, "Terms", budgetMillis, userId); Set failedBrands = normalizeObjectSet(check == null ? null : check.faildData()); Set queryFailedBrands = normalizeObjectSet(check == null ? null : check.queryFaildData()); Map verdicts = new LinkedHashMap<>(); @@ -170,7 +190,7 @@ public class CollectDataBrandBatchFilter { putBounded(normalized, verdict); verdicts.put(normalized, verdict); } - return verdicts; + return new BatchVerdicts(verdicts, check == null ? "" : check.reason()); } private Map snapshotVerdicts(List brands) { @@ -249,10 +269,19 @@ public class CollectDataBrandBatchFilter { } } - /** 品牌检查分类结果:三类行互斥,顺序与输入一致。 */ + /** + * 品牌检查分类结果:三类行互斥,顺序与输入一致。 + * + * @param reason 这批为什么查不了(供应商拒付 / 用户未配置代理 / 批次异常),多个用;拼接; + * 全部有结论时为空串。它存在的意义是让「只看到没采到数据」变成可归因的一句话。 + */ public record BrandBatchOutcome(List rejected, List queryFailed, - List accepted) { + List accepted, + String reason) { + public String reason() { + return reason == null ? "" : reason; + } } private static List distinctNonBlank(List values) { diff --git a/backend-java/src/test/java/com/nanri/aiimage/config/HttpClientConnectionReuseTest.java b/backend-java/src/test/java/com/nanri/aiimage/config/HttpClientConnectionReuseTest.java index 3f8d917d3..37969d8ca 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/config/HttpClientConnectionReuseTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/config/HttpClientConnectionReuseTest.java @@ -96,8 +96,8 @@ class HttpClientConnectionReuseTest { void test_task_077_brand_normal_default_path() throws Exception { // 默认路径:品牌检查客户端通过共享池创建单例 RestClient, // 工厂为带 keep-alive 连接池的 JdkClientHttpRequestFactory。 - BrandCheckClient client = new BrandCheckClient(new BrandCheckProperties(), null); - BrandCheckBatchResult result = client.checkTitleText(" "); + BrandCheckClient client = new BrandCheckClient(new BrandCheckProperties(), null, null); + BrandCheckBatchResult result = client.checkTitleText(" ", null, null); assertTrue(result.brands().isEmpty(), "空标题安全跳过,不创建无效资源"); assertPooled(factoryOf(restClientOf(client))); @@ -108,7 +108,7 @@ class HttpClientConnectionReuseTest { // 批量场景:LLM/品牌/紫鸟三个客户端各自持有独立 RestClient, // 但底层连接池共用同一 HttpClient 实例,不重复创建。 SimilarAsinLlmClient llm = new SimilarAsinLlmClient(new SimilarAsinProperties(), new ObjectMapper(), null, null); - BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null); + BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null, null); ZiniaoClientImpl ziniao = new ZiniaoClientImpl(new ZiniaoProperties(), new ObjectMapper()); HttpClient llmClient = clientOf(factoryOf(restClientOf(llm))); @@ -124,7 +124,7 @@ class HttpClientConnectionReuseTest { void test_task_077_brand_normal_repeated_operation_is_idempotent() throws Exception { // 幂等:同一客户端重复触发请求创建逻辑只产生一个 RestClient, // 重复调用返回同一实例,不重复创建客户端对象。 - BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null); + BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null, null); assertSame(restClientOf(brand), restClientOf(brand), "品牌客户端复用同一 RestClient"); ZiniaoClientImpl ziniao = new ZiniaoClientImpl(new ZiniaoProperties(), new ObjectMapper()); @@ -134,8 +134,8 @@ class HttpClientConnectionReuseTest { @Test void test_task_077_brand_boundary_empty_input() throws Exception { // 空输入:空品牌列表不发起任何 HTTP 请求、不创建客户端资源。 - BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null); - BrandCheckBatchResult result = brand.checkAll(List.of(), "Terms"); + BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null, null); + BrandCheckBatchResult result = brand.checkAll(List.of(), "Terms", null); assertTrue(result.brands().isEmpty()); assertTrue(result.faildData().isEmpty()); @@ -145,7 +145,7 @@ class HttpClientConnectionReuseTest { @Test void test_task_077_brand_boundary_single_item() throws Exception { // 单元素:单客户端单请求走共享池,工厂带连接池,行为与批量一致。 - BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null); + BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null, null); assertPooled(factoryOf(restClientOf(brand))); } @@ -155,7 +155,7 @@ class HttpClientConnectionReuseTest { // 不随实例数量线性增长连接资源。 int instances = 8; for (int i = 0; i < instances; i++) { - restClientOf(new BrandCheckClient(new BrandCheckProperties(), null)); + restClientOf(new BrandCheckClient(new BrandCheckProperties(), null, null)); restClientOf(new ZiniaoClientImpl(new ZiniaoProperties(), new ObjectMapper())); } assertSame(HttpClientPool.sharedHttpClient(), HttpClientPool.sharedHttpClient(), diff --git a/backend-java/src/test/java/com/nanri/aiimage/metrics/ExternalCallMetricsRecorderTest.java b/backend-java/src/test/java/com/nanri/aiimage/metrics/ExternalCallMetricsRecorderTest.java index e713a20c1..f51e37c33 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/metrics/ExternalCallMetricsRecorderTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/metrics/ExternalCallMetricsRecorderTest.java @@ -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); diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/client/AppearancePatentBrandBatchTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/client/AppearancePatentBrandBatchTest.java index 6eb548532..c2af37bb9 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/client/AppearancePatentBrandBatchTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/client/AppearancePatentBrandBatchTest.java @@ -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 titles = invocation.getArgument(0); List results = new ArrayList<>(); for (String title : titles) { List 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 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 titles = invocation.getArgument(0); List results = new ArrayList<>(); for (String title : titles) { List 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> 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); diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/client/AppearancePatentLlmClientHttpTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/client/AppearancePatentLlmClientHttpTest.java index 84aeef316..92384ce42 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/client/AppearancePatentLlmClientHttpTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/client/AppearancePatentLlmClientHttpTest.java @@ -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 { diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/client/AppearancePatentLlmClientImageDedupeTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/client/AppearancePatentLlmClientImageDedupeTest.java index 72de08274..7fc9304dc 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/client/AppearancePatentLlmClientImageDedupeTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/client/AppearancePatentLlmClientImageDedupeTest.java @@ -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 diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/client/AppearancePatentLlmClientTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/client/AppearancePatentLlmClientTest.java index d218ee773..f6a57fc11 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/client/AppearancePatentLlmClientTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/client/AppearancePatentLlmClientTest.java @@ -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 diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/client/AppearancePatentLlmClientTokenUsageTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/client/AppearancePatentLlmClientTokenUsageTest.java index 72c2dcb57..1bab55f5f 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/client/AppearancePatentLlmClientTokenUsageTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/appearancepatent/client/AppearancePatentLlmClientTokenUsageTest.java @@ -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 diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/brand/client/BrandCheckClientTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/brand/client/BrandCheckClientTest.java index 895edf813..28a596e73 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/modules/brand/client/BrandCheckClientTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/brand/client/BrandCheckClientTest.java @@ -1,6 +1,7 @@ package com.nanri.aiimage.modules.brand.client; import com.nanri.aiimage.config.BrandCheckProperties; +import com.nanri.aiimage.modules.usersecret.service.UserApiSecretService; import com.sun.net.httpserver.HttpServer; import org.junit.jupiter.api.Test; @@ -12,12 +13,16 @@ import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; class BrandCheckClientTest { @Test void splitTitleTextSupportsCommonSeparatorsAndQuotes() { - BrandCheckClient client = new BrandCheckClient(new BrandCheckProperties(), null); + BrandCheckClient client = new BrandCheckClient(new BrandCheckProperties(), null, null); List brands = client.splitTitleText("'阿凡达,任天堂'; Disney、LEGO\nSony"); @@ -26,7 +31,7 @@ class BrandCheckClientTest { @Test void splitTitleTextDeduplicatesBlankValues() { - BrandCheckClient client = new BrandCheckClient(new BrandCheckProperties(), null); + BrandCheckClient client = new BrandCheckClient(new BrandCheckProperties(), null, null); List brands = client.splitTitleText(" 任天堂, ,任天堂,Sony "); @@ -46,7 +51,7 @@ class BrandCheckClientTest { try { BrandCheckClient client = newClient(server, 3); - BrandCheckClient.BrandCheckBatchResult result = client.checkAll(List.of("Apple"), "Terms"); + BrandCheckClient.BrandCheckBatchResult result = client.checkAll(List.of("Apple"), "Terms", 7L); assertThat(attempts.get()).as("前两次失败后应重试到成功").isEqualTo(3); assertThat(result.hasFailedData()).isFalse(); @@ -66,7 +71,7 @@ class BrandCheckClientTest { try { BrandCheckClient client = newClient(server, 3); - BrandCheckClient.BrandCheckBatchResult result = client.checkAll(List.of("Apple"), "Terms"); + BrandCheckClient.BrandCheckBatchResult result = client.checkAll(List.of("Apple"), "Terms", 7L); assertThat(attempts.get()).as("持续失败应耗尽重试次数").isEqualTo(3); assertThat(result.hasFailedData()).isFalse(); @@ -87,7 +92,7 @@ class BrandCheckClientTest { try { BrandCheckClient client = newClient(server, 3); - BrandCheckClient.BrandCheckBatchResult result = client.checkAll(List.of("Apple"), "Terms"); + BrandCheckClient.BrandCheckBatchResult result = client.checkAll(List.of("Apple"), "Terms", 7L); assertThat(attempts.get()).as("命中侵权属有效结论,不应重试").isEqualTo(1); assertThat(result.hasFailedData()).isTrue(); @@ -115,7 +120,7 @@ class BrandCheckClientTest { BrandCheckClient client = newClient(server, 3); List brands = List.of("b1", "b2", "b3", "b4", "b5", "b6", "b7", "b8"); - BrandCheckClient.BrandCheckBatchResult result = client.checkAll(brands, "Terms"); + BrandCheckClient.BrandCheckBatchResult result = client.checkAll(brands, "Terms", 7L); assertThat(requests.get()).as("首轮始终执行:短路不许让任何品牌一次都没被查过") .isGreaterThanOrEqualTo(brands.size()); @@ -149,7 +154,7 @@ class BrandCheckClientTest { BrandCheckClient client = newClient(server, 3); BrandCheckClient.BrandCheckBatchResult result = - client.checkAll(List.of("ok1", "bad", "ok2", "ok3"), "Terms"); + client.checkAll(List.of("ok1", "bad", "ok2", "ok3"), "Terms", 7L); assertThat(badAttempts.get()).as("同批有成功品牌时,失败品牌仍跑满整条阶梯").isEqualTo(3); assertThat(result.queryFaildData()).containsExactly("bad"); @@ -164,7 +169,7 @@ class BrandCheckClientTest { props.setPath("/brand_check"); props.setRetryTimes(retryTimes); props.setRetryIntervalMillis(1); - return new BrandCheckClient(props, null); + return new BrandCheckClient(props, null, stubbedUserSecrets()); } /** 重试阶梯可调的构造:用于验证「耗时预算掐的是重试,不是首轮」。 */ @@ -175,7 +180,14 @@ class BrandCheckClientTest { props.setRetryTimes(retryTimes); props.setRetryIntervalMillis(retryIntervalMillis); props.setTotalTimeoutMillis(totalTimeoutMillis); - return new BrandCheckClient(props, null); + return new BrandCheckClient(props, null, stubbedUserSecrets()); + } + + /** 有真实 HTTP 断言的用例必须桩出非空代理,否则客户端零请求短路、阶梯与指标全测不到。 */ + private UserApiSecretService stubbedUserSecrets() { + UserApiSecretService secrets = mock(UserApiSecretService.class); + when(secrets.findPlainValue(anyLong(), anyString())).thenReturn("http://user:pass@127.0.0.1:9"); + return secrets; } @Test @@ -188,12 +200,12 @@ class BrandCheckClientTest { try { // 阶梯 20/40/60…:跑满 10 次要 0.9 秒,60ms 预算只够跑到第 2~3 次。 BrandCheckClient unlimited = newClient(server, 10, 20, 0); - unlimited.checkAll(List.of("Apple"), "Terms"); + unlimited.checkAll(List.of("Apple"), "Terms", 7L); int attemptsWithoutBudget = attempts.get(); attempts.set(0); BrandCheckClient client = newClient(server, 10, 20, 0); - BrandCheckClient.BrandCheckBatchResult result = client.checkAll(List.of("Apple"), "Terms", 60L); + BrandCheckClient.BrandCheckBatchResult result = client.checkAll(List.of("Apple"), "Terms", 60L, 7L); assertThat(attemptsWithoutBudget).as("不限制时跑满整条重试阶梯").isEqualTo(10); assertThat(attempts.get()).as("预算用尽即停重试,但首轮始终执行") @@ -215,7 +227,7 @@ class BrandCheckClientTest { try { BrandCheckClient client = newClient(server, 10, 20, 60); - client.checkAll(List.of("Apple"), "Terms"); + client.checkAll(List.of("Apple"), "Terms", 7L); assertThat(attempts.get()).as("两参重载按 total-timeout-millis 兜底,未退化也不越界") .isBetween(1, 9); @@ -238,7 +250,7 @@ class BrandCheckClientTest { BrandCheckClient client = newClient(server, 3, 200, 0); List brands = List.of("b1", "b2", "b3", "b4"); - BrandCheckClient.BrandCheckBatchResult result = client.checkAll(brands, "Terms", 1L); + BrandCheckClient.BrandCheckBatchResult result = client.checkAll(brands, "Terms", 1L, 7L); assertThat(requests.get()).as("预算过期后每个品牌仍各发一次真实请求").isEqualTo(brands.size()); assertThat(result.brands()).as("降级不丢品牌,入参集合原样带回").containsExactlyElementsOf(brands); diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/brand/client/BrandCheckClientTitleBatchTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/brand/client/BrandCheckClientTitleBatchTest.java index 5fed43c3a..661abee15 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/modules/brand/client/BrandCheckClientTitleBatchTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/brand/client/BrandCheckClientTitleBatchTest.java @@ -1,6 +1,7 @@ package com.nanri.aiimage.modules.brand.client; import com.nanri.aiimage.config.BrandCheckProperties; +import com.nanri.aiimage.modules.usersecret.service.UserApiSecretService; import com.sun.net.httpserver.HttpServer; import org.junit.jupiter.api.Test; @@ -14,6 +15,10 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicInteger; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; /** * 一批标题的品牌校验(外观专利热路径:原先每行各调一次 checkTitleText, @@ -34,7 +39,7 @@ class BrandCheckClientTitleBatchTest { HttpServer server = startBrandCheckServer(brandResponses(hits)); try { List results = - newClient(server, 3).checkTitleTexts(List.of("阿凡达,玩具", "小米,充电宝"), "Terms"); + newClient(server, 3).checkTitleTexts(List.of("阿凡达,玩具", "小米,充电宝"), "Terms", 7L); assertThat(results).hasSize(2); assertThat(results.get(0).hasFailedData()).as("第 1 行含侵权词").isTrue(); @@ -59,7 +64,7 @@ class BrandCheckClientTitleBatchTest { }); try { List results = - newClient(server, 3).checkTitleTexts(List.of("阿凡达,玩具", "阿凡达,手机", "阿凡达,周边"), "Terms"); + newClient(server, 3).checkTitleTexts(List.of("阿凡达,玩具", "阿凡达,手机", "阿凡达,周边"), "Terms", 7L); assertThat(counters.get("阿凡达").get()).as("跨行同一个词只查一次").isEqualTo(1); assertThat(counters).hasSize(4); @@ -80,7 +85,7 @@ class BrandCheckClientTitleBatchTest { Map.of("阿凡达", "{\"faild_data\":[\"阿凡达\"],\"query_faild_data\":[]}"))); try { List results = - newClient(server, 3).checkTitleTexts(List.of("", "阿凡达"), "Terms"); + newClient(server, 3).checkTitleTexts(List.of("", "阿凡达"), "Terms", 7L); assertThat(results.get(0).brands()).as("空标题行的词集合必须为空,否则判定从标题原文变成无侵权").isEmpty(); assertThat(results.get(0).hasFailedData()).isFalse(); @@ -100,7 +105,7 @@ class BrandCheckClientTitleBatchTest { HttpServer server = startBrandCheckServer(brandResponses(hits)); try { List results = - newClient(server, 2).checkTitleTexts(List.of("任天堂,主机", "小米,充电宝"), "Terms"); + newClient(server, 2).checkTitleTexts(List.of("任天堂,主机", "小米,充电宝"), "Terms", 7L); assertThat(results.get(0).hasQueryFailedData()).isTrue(); assertThat(results.get(1).hasQueryFailedData()).as("别行的查询失败不得扩散").isFalse(); @@ -130,7 +135,9 @@ class BrandCheckClientTitleBatchTest { props.setPath("/brand_check"); props.setRetryTimes(retryTimes); props.setRetryIntervalMillis(1); - return new BrandCheckClient(props, null); + UserApiSecretService secrets = mock(UserApiSecretService.class); + when(secrets.findPlainValue(anyLong(), anyString())).thenReturn("http://user:pass@127.0.0.1:9"); + return new BrandCheckClient(props, null, secrets); } private HttpServer startBrandCheckServer(com.sun.net.httpserver.HttpHandler handler) throws IOException { diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/collectdata/service/CollectDataBrandRecheckSchedulerTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/collectdata/service/CollectDataBrandRecheckSchedulerTest.java index ec6842746..4999f07da 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/modules/collectdata/service/CollectDataBrandRecheckSchedulerTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/collectdata/service/CollectDataBrandRecheckSchedulerTest.java @@ -117,7 +117,7 @@ class CollectDataBrandRecheckSchedulerTest { scheduler.runRound(); verify(recheckService).postponeWithoutAttempt(List.of(e1), 30); - verify(brandCheckClient, never()).checkAll(anyList(), anyString()); + verify(brandCheckClient, never()).checkAll(anyList(), anyString(), any()); verify(notificationDispatchService, never()).pushRecheckOutcome( any(), any(), anyString(), anyString(), anyInt(), anyInt(), anyInt(), anyInt()); } @@ -131,7 +131,7 @@ class CollectDataBrandRecheckSchedulerTest { scheduler.runRound(); verify(recheckService).markExhausted(e1, "任务不存在(可能已被清理)"); - verify(brandCheckClient, never()).checkAll(anyList(), anyString()); + verify(brandCheckClient, never()).checkAll(anyList(), anyString(), any()); } @Test @@ -144,7 +144,7 @@ class CollectDataBrandRecheckSchedulerTest { scheduler.runRound(); verify(recheckService).postponeWithoutAttempt(List.of(e1), 30); - verify(brandCheckClient, never()).checkAll(anyList(), anyString()); + verify(brandCheckClient, never()).checkAll(anyList(), anyString(), any()); } @Test @@ -155,8 +155,8 @@ class CollectDataBrandRecheckSchedulerTest { when(fileTaskMapper.selectById(100L)).thenReturn(task(100L, "SUCCESS")); when(taskDistributedLockService.acquire("COLLECT_DATA", 100L, 2000L)).thenReturn(taskLock); // "Nike " 带尾随空格:验证取品牌时按归一化口径去查(入队时已归一化,防御性兜底) - when(brandCheckClient.checkAll(anyList(), eq("Terms"))).thenReturn( - new BrandCheckClient.BrandCheckBatchResult(List.of(), List.of("Zara"), List.of())); + when(brandCheckClient.checkAll(anyList(), eq("Terms"), any())).thenReturn( + new BrandCheckClient.BrandCheckBatchResult(List.of(), List.of("Zara"), List.of(), "")); when(injector.apply(any(FileTaskEntity.class), any())).thenReturn( new GroupOutcome(9L, 3, 1)); @@ -176,8 +176,8 @@ class CollectDataBrandRecheckSchedulerTest { when(recheckService.listDue(any(), anyInt())).thenReturn(List.of(e1)); when(fileTaskMapper.selectById(100L)).thenReturn(task(100L, "SUCCESS")); when(taskDistributedLockService.acquire("COLLECT_DATA", 100L, 2000L)).thenReturn(taskLock); - when(brandCheckClient.checkAll(anyList(), eq("Terms"))).thenReturn( - new BrandCheckClient.BrandCheckBatchResult(List.of(), List.of(), List.of("zara"))); + when(brandCheckClient.checkAll(anyList(), eq("Terms"), any())).thenReturn( + new BrandCheckClient.BrandCheckBatchResult(List.of(), List.of(), List.of("zara"), "")); scheduler.runRound(); @@ -194,7 +194,7 @@ class CollectDataBrandRecheckSchedulerTest { when(recheckService.listDue(any(), anyInt())).thenReturn(List.of(e1)); when(fileTaskMapper.selectById(100L)).thenReturn(task(100L, "FAILED")); when(taskDistributedLockService.acquire("COLLECT_DATA", 100L, 2000L)).thenReturn(taskLock); - when(brandCheckClient.checkAll(anyList(), eq("Terms"))) + when(brandCheckClient.checkAll(anyList(), eq("Terms"), any())) .thenThrow(new RuntimeException("connection reset")); scheduler.runRound(); @@ -209,8 +209,8 @@ class CollectDataBrandRecheckSchedulerTest { when(recheckService.listDue(any(), anyInt())).thenReturn(List.of(e1)); when(fileTaskMapper.selectById(100L)).thenReturn(task(100L, "SUCCESS")); when(taskDistributedLockService.acquire("COLLECT_DATA", 100L, 2000L)).thenReturn(taskLock); - when(brandCheckClient.checkAll(anyList(), eq("Terms"))).thenReturn( - new BrandCheckClient.BrandCheckBatchResult(List.of(), List.of(), List.of())); + when(brandCheckClient.checkAll(anyList(), eq("Terms"), any())).thenReturn( + new BrandCheckClient.BrandCheckBatchResult(List.of(), List.of(), List.of(), "")); when(injector.apply(any(FileTaskEntity.class), any())) .thenThrow(new RuntimeException("结果明细写入失败")); @@ -232,9 +232,9 @@ class CollectDataBrandRecheckSchedulerTest { when(recheckService.listDue(any(), anyInt())).thenReturn(List.of(f1, f2, f3, later)); when(fileTaskMapper.selectById(100L)).thenReturn(task(100L, "SUCCESS")); when(taskDistributedLockService.acquire("COLLECT_DATA", 100L, 2000L)).thenReturn(taskLock); - when(brandCheckClient.checkAll(anyList(), eq("Terms"))).thenReturn( + when(brandCheckClient.checkAll(anyList(), eq("Terms"), any())).thenReturn( new BrandCheckClient.BrandCheckBatchResult(List.of(), List.of(), - List.of("bad-one", "bad-two", "bad-three"))); + List.of("bad-one", "bad-two", "bad-three"), "")); scheduler.runRound(); @@ -243,7 +243,7 @@ class CollectDataBrandRecheckSchedulerTest { verify(recheckService).markAttempted(f2, "query_faild_data 持续非空"); verify(recheckService).markAttempted(f3, "query_faild_data 持续非空"); // 第二组整组顺延:不实测、不计数 - verify(brandCheckClient, org.mockito.Mockito.times(1)).checkAll(anyList(), anyString()); + verify(brandCheckClient, org.mockito.Mockito.times(1)).checkAll(anyList(), anyString(), any()); verify(recheckService).postponeWithoutAttempt(List.of(later), 30); verify(recheckService, never()).markInjected(any()); verify(recheckService, never()).markRejected(any()); diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/collectdata/service/CollectDataServiceTxBoundaryTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/collectdata/service/CollectDataServiceTxBoundaryTest.java index 3206e55e8..a0edafabe 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/modules/collectdata/service/CollectDataServiceTxBoundaryTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/collectdata/service/CollectDataServiceTxBoundaryTest.java @@ -143,8 +143,8 @@ class CollectDataServiceTxBoundaryTest { row.setAsin("B0COLLECT1"); lenient().when(collectDataBatchQuery.filter(any(), any())).thenReturn( new CollectDataBatchQuery.FilterResult(List.of(row), 0, 0)); - lenient().when(brandBatchFilter.filter(any())).thenReturn( - new CollectDataBrandBatchFilter.BrandBatchOutcome(List.of(), List.of(), List.of(row))); + lenient().when(brandBatchFilter.filter(any(), any())).thenReturn( + new CollectDataBrandBatchFilter.BrandBatchOutcome(List.of(), List.of(), List.of(row), "")); lenient().when(resultItemBatchWriter.upsertAccepted(anyLong(), anyLong(), anyString(), anyInt(), any(), anyString())) .thenReturn(new CollectDataResultItemBatchWriter.UpsertCounts(1, 0, 1)); lenient().when(resultDetailCodec.encodeChunk(any())).thenReturn("{\"chunk\":1}"); @@ -199,8 +199,8 @@ class CollectDataServiceTxBoundaryTest { void brandRejectedRowsWrittenToInvalidWriter() { CollectDataResultRowVo bad = new CollectDataResultRowVo(); bad.setAsin("B0INVALID"); - when(brandBatchFilter.filter(any())).thenReturn( - new CollectDataBrandBatchFilter.BrandBatchOutcome(List.of(bad), List.of(), List.of())); + when(brandBatchFilter.filter(any(), any())).thenReturn( + new CollectDataBrandBatchFilter.BrandBatchOutcome(List.of(bad), List.of(), List.of(), "")); service.submitResult(TASK_ID, submitRequest()); @@ -211,8 +211,8 @@ class CollectDataServiceTxBoundaryTest { void brandQueryFailedRowsNotWrittenToInvalidWriter() { CollectDataResultRowVo queryFailed = new CollectDataResultRowVo(); queryFailed.setAsin("B0QUERYFAIL"); - when(brandBatchFilter.filter(any())).thenReturn( - new CollectDataBrandBatchFilter.BrandBatchOutcome(List.of(), List.of(queryFailed), List.of())); + when(brandBatchFilter.filter(any(), any())).thenReturn( + new CollectDataBrandBatchFilter.BrandBatchOutcome(List.of(), List.of(queryFailed), List.of(), "")); service.submitResult(TASK_ID, submitRequest()); @@ -223,8 +223,8 @@ class CollectDataServiceTxBoundaryTest { void emptyRejectedOutcomeSkipsInvalidWriter() { CollectDataResultRowVo accepted = new CollectDataResultRowVo(); accepted.setAsin("B0OK"); - when(brandBatchFilter.filter(any())).thenReturn( - new CollectDataBrandBatchFilter.BrandBatchOutcome(List.of(), List.of(), List.of(accepted))); + when(brandBatchFilter.filter(any(), any())).thenReturn( + new CollectDataBrandBatchFilter.BrandBatchOutcome(List.of(), List.of(), List.of(accepted), "")); service.submitResult(TASK_ID, submitRequest()); diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/collectdata/service/support/CollectDataSubmitResultSnapshotTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/collectdata/service/support/CollectDataSubmitResultSnapshotTest.java index f7673313e..4c815ed05 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/modules/collectdata/service/support/CollectDataSubmitResultSnapshotTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/collectdata/service/support/CollectDataSubmitResultSnapshotTest.java @@ -337,11 +337,11 @@ class CollectDataSubmitResultSnapshotTest { + " dedupe=0 invalid=0"); return new CollectDataBatchQuery.FilterResult(kept, 0, 0); }); - when(brandFilter.filter(anyList())).thenAnswer(invocation -> { + when(brandFilter.filter(anyList(), any())).thenAnswer(invocation -> { @SuppressWarnings("unchecked") List rows = invocation.getArgument(0); f.events.add("outsideLock brandFilter accepted=" + rows.size() + " rejected=0 queryFailed=0"); - return new CollectDataBrandBatchFilter.BrandBatchOutcome(List.of(), List.of(), rows); + return new CollectDataBrandBatchFilter.BrandBatchOutcome(List.of(), List.of(), rows, ""); }); doAnswer(invocation -> { @SuppressWarnings("unchecked") diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/collectdata/util/CollectDataBrandBatchFilterTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/collectdata/util/CollectDataBrandBatchFilterTest.java index b89c44109..ee815dff0 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/modules/collectdata/util/CollectDataBrandBatchFilterTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/collectdata/util/CollectDataBrandBatchFilterTest.java @@ -42,22 +42,22 @@ class CollectDataBrandBatchFilterTest { @Test void test_task_046_brand_normal_default_path() { // 正常输入:非空品牌批次发起检查,失败品牌行 rejected、其余 accepted。 - when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn( + when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenReturn( new BrandCheckClient.BrandCheckBatchResult( - List.of(), List.of("Zara"), List.of())); + List.of(), List.of("Zara"), List.of(), "")); CollectDataBrandBatchFilter.BrandBatchOutcome outcome = filter.filter(List.of( row("B000000001", "Zara"), row("B000000002", "Nike"), row("B000000003", "H&M") - )); + ), 7L); assertEquals(1, outcome.rejected().size(), "失败品牌行进入 rejected"); assertEquals("B000000001", outcome.rejected().get(0).getAsin()); assertEquals(2, outcome.accepted().size(), "其余行 accepted"); assertEquals("B000000002", outcome.accepted().get(0).getAsin(), "accepted 顺序稳定"); assertEquals(0, outcome.queryFailed().size(), "无查询失败行"); - verify(brandCheckClient).checkAll(anyList(), any(), anyLong()); + verify(brandCheckClient).checkAll(anyList(), any(), anyLong(), any()); } @Test @@ -67,74 +67,74 @@ class CollectDataBrandBatchFilterTest { for (int i = 0; i < 120; i++) { rows.add(row("B" + String.format("%09d", i + 1), i % 10 == 0 ? "bad-brand" : "good-" + (i / 10))); } - when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn( + when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenReturn( new BrandCheckClient.BrandCheckBatchResult( - List.of(), List.of("bad-brand"), List.of())); + List.of(), List.of("bad-brand"), List.of(), "")); - CollectDataBrandBatchFilter.BrandBatchOutcome outcome = filter.filter(rows); + CollectDataBrandBatchFilter.BrandBatchOutcome outcome = filter.filter(rows, 7L); assertEquals(12, outcome.rejected().size(), "每批次 1 个失败品牌行"); assertEquals(108, outcome.accepted().size(), "其余行 accepted"); assertEquals("B000000001", outcome.rejected().get(0).getAsin(), "rejected 顺序稳定"); assertEquals("B000000002", outcome.accepted().get(0).getAsin(), "accepted 顺序稳定"); - verify(brandCheckClient, times(12)).checkAll(anyList(), any(), anyLong()); + verify(brandCheckClient, times(12)).checkAll(anyList(), any(), anyLong(), any()); } @Test void test_task_046_brand_normal_repeated_operation_is_idempotent() { // 幂等:同一输入重复执行结果一致,不产生重复对象。 - when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn( + when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenReturn( new BrandCheckClient.BrandCheckBatchResult( - List.of(), List.of("Nike"), List.of())); + List.of(), List.of("Nike"), List.of(), "")); List rows = List.of( row("B000000001", "Nike"), row("B000000002", "Adidas") ); - CollectDataBrandBatchFilter.BrandBatchOutcome first = filter.filter(rows); - CollectDataBrandBatchFilter.BrandBatchOutcome second = filter.filter(rows); + CollectDataBrandBatchFilter.BrandBatchOutcome first = filter.filter(rows, 7L); + CollectDataBrandBatchFilter.BrandBatchOutcome second = filter.filter(rows, 7L); assertEquals(first.rejected(), second.rejected(), "重复执行 rejected 一致"); assertEquals(first.accepted(), second.accepted(), "重复执行 accepted 一致"); // 缓存生效:第二次执行全部命中缓存,不再发起远程调用。 - verify(brandCheckClient, times(1)).checkAll(anyList(), any(), anyLong()); + verify(brandCheckClient, times(1)).checkAll(anyList(), any(), anyLong(), any()); } @Test void test_task_046_brand_boundary_empty_input() { // 空输入:空列表返回空结果;全空品牌批次不发起远程检查,行全部 rejected。 - CollectDataBrandBatchFilter.BrandBatchOutcome empty = filter.filter(List.of()); + CollectDataBrandBatchFilter.BrandBatchOutcome empty = filter.filter(List.of(), 7L); assertEquals(0, empty.rejected().size() + empty.accepted().size() + empty.queryFailed().size(), "空列表返回空结果"); - verify(brandCheckClient, never()).checkAll(anyList(), any(), anyLong()); + verify(brandCheckClient, never()).checkAll(anyList(), any(), anyLong(), any()); CollectDataBrandBatchFilter.BrandBatchOutcome blankBatch = filter.filter(List.of( row("B000000001", ""), row("B000000002", null), row("B000000003", " ") - )); + ), 7L); assertEquals(3, blankBatch.rejected().size(), "空品牌批次行全部 rejected"); assertEquals(0, blankBatch.accepted().size(), "空品牌批次无 accepted"); - verify(brandCheckClient, never()).checkAll(anyList(), any(), anyLong()); + verify(brandCheckClient, never()).checkAll(anyList(), any(), anyLong(), any()); } @Test void test_task_046_brand_boundary_single_item() { // 单元素:单行单品牌批次正常检查;单行空品牌批次不发远程调用。 - when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn( + when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenReturn( new BrandCheckClient.BrandCheckBatchResult( - List.of(), List.of(), List.of())); + List.of(), List.of(), List.of(), "")); CollectDataBrandBatchFilter.BrandBatchOutcome single = filter.filter(List.of( row("B000000001", "solo") - )); + ), 7L); assertEquals(1, single.accepted().size(), "单行未命中失败归 accepted"); - verify(brandCheckClient).checkAll(anyList(), any(), anyLong()); + verify(brandCheckClient).checkAll(anyList(), any(), anyLong(), any()); CollectDataBrandBatchFilter.BrandBatchOutcome blank = filter.filter(List.of( row("B000000002", "") - )); + ), 7L); assertEquals(1, blank.rejected().size(), "单行空品牌归 rejected"); // 同实例缓存生效:solo 已查过,空品牌批次无远程调用,总调用保持 1 次。 - verify(brandCheckClient, times(1)).checkAll(anyList(), any(), anyLong()); + verify(brandCheckClient, times(1)).checkAll(anyList(), any(), anyLong(), any()); } @Test @@ -145,50 +145,50 @@ class CollectDataBrandBatchFilterTest { rows.add(row("B" + String.format("%09d", i + 1), "brand-" + (i % 100))); } rows.add(row("B999999999", " ")); - when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn( + when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenReturn( new BrandCheckClient.BrandCheckBatchResult( - List.of(), List.of(), List.of())); + List.of(), List.of(), List.of(), "")); - CollectDataBrandBatchFilter.BrandBatchOutcome outcome = filter.filter(rows); + CollectDataBrandBatchFilter.BrandBatchOutcome outcome = filter.filter(rows, 7L); assertEquals(1, outcome.rejected().size(), "空品牌行归 rejected"); assertEquals(5000, outcome.accepted().size(), "非空品牌行全 accepted"); // 每批次 10 行恰好引入 10 个新品牌,10 批次后 100 品牌全部缓存,后续批次零调用。 - verify(brandCheckClient, times(10)).checkAll(anyList(), any(), anyLong()); + verify(brandCheckClient, times(10)).checkAll(anyList(), any(), anyLong(), any()); } @Test void test_task_046_brand_invalid_input_rejected() { // 非法参数:checkAll 返回 null 按无失败处理;null 行安全跳过不计数。 - when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn(null); + when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenReturn(null); List rows = new ArrayList<>(); rows.add(null); rows.add(row("B000000001", "Nike")); rows.add(row("B000000002", "Zara")); - CollectDataBrandBatchFilter.BrandBatchOutcome outcome = filter.filter(rows); + CollectDataBrandBatchFilter.BrandBatchOutcome outcome = filter.filter(rows, 7L); assertEquals(2, outcome.accepted().size(), "null 结果按无失败处理,行全 accepted"); assertEquals(0, outcome.rejected().size(), "null 行不计数"); - verify(brandCheckClient).checkAll(anyList(), any(), anyLong()); + verify(brandCheckClient).checkAll(anyList(), any(), anyLong(), any()); } @Test void test_task_046_brand_dependency_failure_releases_resources() { // 依赖失败:远程检查抛错时批次整组降级 queryFailed,可恢复;恢复后重新检查成功。 - when(brandCheckClient.checkAll(anyList(), any(), anyLong())) + when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())) .thenThrow(new RuntimeException("brand service down")) .thenReturn(new BrandCheckClient.BrandCheckBatchResult( - List.of(), List.of("Zara"), List.of())); + List.of(), List.of("Zara"), List.of(), "")); List rows = List.of( row("B000000001", "Zara"), row("B000000002", "Nike")); - CollectDataBrandBatchFilter.BrandBatchOutcome failed = filter.filter(rows); + CollectDataBrandBatchFilter.BrandBatchOutcome failed = filter.filter(rows, 7L); assertEquals(2, failed.queryFailed().size(), "依赖失败批次整组降级 queryFailed"); assertEquals(0, failed.accepted().size() + failed.rejected().size(), "降级后无其他分类"); - CollectDataBrandBatchFilter.BrandBatchOutcome recovered = filter.filter(rows); + CollectDataBrandBatchFilter.BrandBatchOutcome recovered = filter.filter(rows, 7L); assertEquals(1, recovered.rejected().size(), "恢复后失败品牌重新分类"); assertEquals(1, recovered.accepted().size(), "恢复后其余行 accepted"); assertTrue(recovered.queryFailed().isEmpty(), "恢复后无残留 queryFailed"); @@ -202,17 +202,17 @@ class CollectDataBrandBatchFilterTest { @Test void test_chunk_budget_exhausted_stops_calling_upstream() { AtomicInteger calls = new AtomicInteger(); - when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenAnswer(invocation -> { + when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenAnswer(invocation -> { calls.incrementAndGet(); Thread.sleep(30); - return new BrandCheckClient.BrandCheckBatchResult(List.of(), List.of(), List.of()); + return new BrandCheckClient.BrandCheckBatchResult(List.of(), List.of(), List.of(), ""); }); CollectDataBrandBatchFilter tight = new CollectDataBrandBatchFilter(brandCheckClient, 2, 512, 1L); CollectDataBrandBatchFilter.BrandBatchOutcome outcome = tight.filter(List.of( row("B000000001", "brand-1"), row("B000000002", "brand-2"), row("B000000003", "brand-3"), row("B000000004", "brand-4"), - row("B000000005", "brand-5"), row("B000000006", "brand-6"))); + row("B000000005", "brand-5"), row("B000000006", "brand-6")), 7L); assertEquals(1, calls.get(), "预算用尽后不再为剩余批次发起远程检查"); assertEquals(2, outcome.accepted().size(), "首个已检查批次照常分类"); @@ -224,18 +224,18 @@ class CollectDataBrandBatchFilterTest { void test_cached_verdict_still_applies_and_blank_brand_rejects_after_budget_exhausted() { // 预算用尽只做「不再花代价」,不改分类口径:缓存里有结论的仍按结论走, // 空品牌行仍归 rejected(补测队列按品牌入队,空品牌进去就永远回不来)。 - when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenAnswer(invocation -> { + when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenAnswer(invocation -> { Thread.sleep(30); List brands = invocation.getArgument(0); List hits = new ArrayList<>(brands.stream().filter("bad-brand"::equals).toList()); - return new BrandCheckClient.BrandCheckBatchResult(List.of(), hits, List.of()); + return new BrandCheckClient.BrandCheckBatchResult(List.of(), hits, List.of(), ""); }); CollectDataBrandBatchFilter tight = new CollectDataBrandBatchFilter(brandCheckClient, 2, 512, 1L); CollectDataBrandBatchFilter.BrandBatchOutcome outcome = tight.filter(List.of( row("B000000001", "bad-brand"), row("B000000002", "seen-ok"), row("B000000003", "bad-brand"), row("B000000004", "brand-4"), - row("B000000005", "brand-5"), row("B000000006", " "))); + row("B000000005", "brand-5"), row("B000000006", " ")), 7L); assertEquals(List.of("B000000001", "B000000003", "B000000006"), asins(outcome.rejected()), "侵权品牌与空品牌行归 rejected"); @@ -247,19 +247,19 @@ class CollectDataBrandBatchFilterTest { @Test void test_each_batch_gets_only_the_remaining_chunk_budget() { // 共用一个截止时刻,而不是每批各领一份预算。 - when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenAnswer(invocation -> { + when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenAnswer(invocation -> { Thread.sleep(40); - return new BrandCheckClient.BrandCheckBatchResult(List.of(), List.of(), List.of()); + return new BrandCheckClient.BrandCheckBatchResult(List.of(), List.of(), List.of(), ""); }); CollectDataBrandBatchFilter bounded = new CollectDataBrandBatchFilter(brandCheckClient, 2, 512, 5_000L); bounded.filter(List.of( row("B000000001", "brand-1"), row("B000000002", "brand-2"), row("B000000003", "brand-3"), row("B000000004", "brand-4"), - row("B000000005", "brand-5"), row("B000000006", "brand-6"))); + row("B000000005", "brand-5"), row("B000000006", "brand-6")), 7L); ArgumentCaptor budgets = ArgumentCaptor.forClass(Long.class); - verify(brandCheckClient, times(3)).checkAll(anyList(), any(), budgets.capture()); + verify(brandCheckClient, times(3)).checkAll(anyList(), any(), budgets.capture(), any()); List values = budgets.getAllValues(); assertTrue(values.get(0) <= 5_000L, "首批拿到的不超过 chunk 预算"); assertTrue(values.get(1) < values.get(0), "第二批只能用剩余的"); @@ -269,15 +269,15 @@ class CollectDataBrandBatchFilterTest { @Test void test_zero_budget_passes_through_as_unlimited() { // 逃生阀:预算配 0 表示不掐批间,把每批的预算交给 checkAll 按配置走(旧口径)。 - when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn( - new BrandCheckClient.BrandCheckBatchResult(List.of(), List.of(), List.of())); + when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenReturn( + new BrandCheckClient.BrandCheckBatchResult(List.of(), List.of(), List.of(), "")); CollectDataBrandBatchFilter unbounded = new CollectDataBrandBatchFilter(brandCheckClient, 2, 512, 0L); CollectDataBrandBatchFilter.BrandBatchOutcome outcome = unbounded.filter(List.of( - row("B000000001", "brand-1"), row("B000000002", "brand-2"), row("B000000003", "brand-3"))); + row("B000000001", "brand-1"), row("B000000002", "brand-2"), row("B000000003", "brand-3")), 7L); ArgumentCaptor budgets = ArgumentCaptor.forClass(Long.class); - verify(brandCheckClient, times(2)).checkAll(anyList(), any(), budgets.capture()); + verify(brandCheckClient, times(2)).checkAll(anyList(), any(), budgets.capture(), any()); assertEquals(List.of(0L, 0L), budgets.getAllValues(), "预算 0 逐批透传 0(不限制)"); assertEquals(3, outcome.accepted().size(), "不限制时没有行被延后"); } diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/collectdata/util/CollectDataBrandCacheTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/collectdata/util/CollectDataBrandCacheTest.java index befd20d5d..180640b00 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/modules/collectdata/util/CollectDataBrandCacheTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/collectdata/util/CollectDataBrandCacheTest.java @@ -41,32 +41,32 @@ class CollectDataBrandCacheTest { @Test void test_task_047_cache_brand_normal_default_path() { // 正常输入:首查品牌发起远程检查并缓存判定,分类正确。 - when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn( + when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenReturn( new BrandCheckClient.BrandCheckBatchResult( - List.of(), List.of("Zara"), List.of())); + List.of(), List.of("Zara"), List.of(), "")); CollectDataBrandBatchFilter.BrandBatchOutcome outcome = filter.filter(List.of( row("B000000001", "Zara"), row("B000000002", "Nike") - )); + ), 7L); assertEquals(1, outcome.rejected().size(), "失败品牌行 rejected"); assertEquals("B000000001", outcome.rejected().get(0).getAsin()); assertEquals(1, outcome.accepted().size(), "其余行 accepted"); - verify(brandCheckClient).checkAll(anyList(), any(), anyLong()); + verify(brandCheckClient).checkAll(anyList(), any(), anyLong(), any()); } @Test void test_task_047_cache_brand_normal_multiple_items() { // 批量场景:多 chunk 提交时同品牌只远程检查一次,每 chunk 只查未缓存品牌。 - when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn( + when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenReturn( new BrandCheckClient.BrandCheckBatchResult( - List.of(), List.of("Zara"), List.of())); - filter.filter(List.of(row("B000000001", "Zara"), row("B000000002", "Nike"))); + List.of(), List.of("Zara"), List.of(), "")); + filter.filter(List.of(row("B000000001", "Zara"), row("B000000002", "Nike")), 7L); CollectDataBrandBatchFilter.BrandBatchOutcome second = filter.filter(List.of( row("B000000001", "Zara"), row("B000000003", "Adidas") - )); + ), 7L); assertEquals(1, second.rejected().size(), "chunk2 Zara 命中缓存仍 rejected"); assertEquals(1, second.accepted().size(), "chunk2 新品牌 Adidas accepted"); @@ -78,63 +78,63 @@ class CollectDataBrandCacheTest { @Test void test_task_047_cache_brand_normal_repeated_operation_is_idempotent() { // 幂等:同一输入重复执行结果一致,第二次全部命中缓存不发远程调用。 - when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn( + when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenReturn( new BrandCheckClient.BrandCheckBatchResult( - List.of(), List.of("Nike"), List.of())); + List.of(), List.of("Nike"), List.of(), "")); List rows = List.of( row("B000000001", "Nike"), row("B000000002", "Adidas") ); - CollectDataBrandBatchFilter.BrandBatchOutcome first = filter.filter(rows); - CollectDataBrandBatchFilter.BrandBatchOutcome second = filter.filter(rows); + CollectDataBrandBatchFilter.BrandBatchOutcome first = filter.filter(rows, 7L); + CollectDataBrandBatchFilter.BrandBatchOutcome second = filter.filter(rows, 7L); assertEquals(first.rejected(), second.rejected(), "重复执行 rejected 一致"); assertEquals(first.accepted(), second.accepted(), "重复执行 accepted 一致"); - verify(brandCheckClient, times(1)).checkAll(anyList(), any(), anyLong()); + verify(brandCheckClient, times(1)).checkAll(anyList(), any(), anyLong(), any()); } @Test void test_task_047_cache_brand_boundary_empty_input() { // 空输入:空列表与全空品牌批次均不发起远程调用,也不写缓存。 - CollectDataBrandBatchFilter.BrandBatchOutcome empty = filter.filter(List.of()); + CollectDataBrandBatchFilter.BrandBatchOutcome empty = filter.filter(List.of(), 7L); assertEquals(0, empty.rejected().size() + empty.accepted().size() + empty.queryFailed().size(), "空列表返回空结果"); - verify(brandCheckClient, never()).checkAll(anyList(), any(), anyLong()); + verify(brandCheckClient, never()).checkAll(anyList(), any(), anyLong(), any()); CollectDataBrandBatchFilter.BrandBatchOutcome blank = filter.filter(List.of( row("B000000001", "") - )); + ), 7L); assertEquals(1, blank.rejected().size(), "空品牌行 rejected"); - verify(brandCheckClient, never()).checkAll(anyList(), any(), anyLong()); + verify(brandCheckClient, never()).checkAll(anyList(), any(), anyLong(), any()); } @Test void test_task_047_cache_brand_boundary_single_item() { // 单元素:单品牌首查一次;同品牌再出现时命中缓存零调用。 - when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn( + when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenReturn( new BrandCheckClient.BrandCheckBatchResult( - List.of(), List.of(), List.of())); + List.of(), List.of(), List.of(), "")); CollectDataBrandBatchFilter.BrandBatchOutcome single = filter.filter(List.of( row("B000000001", "solo") - )); + ), 7L); assertEquals(1, single.accepted().size(), "单品牌未命中失败 accepted"); CollectDataBrandBatchFilter.BrandBatchOutcome again = filter.filter(List.of( row("B000000002", "solo") - )); + ), 7L); assertEquals(1, again.accepted().size(), "同品牌再次出现结果一致"); - verify(brandCheckClient, times(1)).checkAll(anyList(), any(), anyLong()); + verify(brandCheckClient, times(1)).checkAll(anyList(), any(), anyLong(), any()); } @Test void test_task_047_cache_brand_boundary_limit_and_overflow() { // 上限/超限:缓存容量 8,首批 10 个品牌淘汰最旧 2 个;第二批同品牌 // 只重查被淘汰的 2 个,其余 8 个命中缓存;无无界增长。 - when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn( + when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenReturn( new BrandCheckClient.BrandCheckBatchResult( - List.of(), List.of(), List.of())); + List.of(), List.of(), List.of(), "")); CollectDataBrandBatchFilter smallCache = new CollectDataBrandBatchFilter(brandCheckClient, 10, 8); List first = new ArrayList<>(); List second = new ArrayList<>(); @@ -143,8 +143,8 @@ class CollectDataBrandCacheTest { second.add(row("B" + (100 + i), "brand-" + i)); } - smallCache.filter(first); - CollectDataBrandBatchFilter.BrandBatchOutcome out = smallCache.filter(second); + smallCache.filter(first, 7L); + CollectDataBrandBatchFilter.BrandBatchOutcome out = smallCache.filter(second, 7L); assertEquals(10, out.accepted().size(), "第二批 10 行全部 accepted"); ArgumentCaptor captor = ArgumentCaptor.forClass(List.class); @@ -157,39 +157,39 @@ class CollectDataBrandCacheTest { @Test void test_task_047_cache_brand_invalid_input_rejected() { // 非法参数:checkAll 返回 null 按无失败处理并缓存;null 行安全跳过。 - when(brandCheckClient.checkAll(anyList(), any(), anyLong())).thenReturn(null); + when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())).thenReturn(null); List rows = new ArrayList<>(); rows.add(null); rows.add(row("B000000001", "Nike")); - CollectDataBrandBatchFilter.BrandBatchOutcome first = filter.filter(rows); + CollectDataBrandBatchFilter.BrandBatchOutcome first = filter.filter(rows, 7L); assertEquals(1, first.accepted().size(), "null 结果按无失败处理"); assertEquals(0, first.rejected().size(), "null 行不计数"); - CollectDataBrandBatchFilter.BrandBatchOutcome second = filter.filter(rows); + CollectDataBrandBatchFilter.BrandBatchOutcome second = filter.filter(rows, 7L); assertEquals(1, second.accepted().size(), "重复执行结果一致"); - verify(brandCheckClient, times(1)).checkAll(anyList(), any(), anyLong()); + verify(brandCheckClient, times(1)).checkAll(anyList(), any(), anyLong(), any()); } @Test void test_task_047_cache_brand_dependency_failure_releases_resources() { // 依赖失败:远程抛错整批 queryFailed 且不缓存;恢复后重查成功,无残留状态。 - when(brandCheckClient.checkAll(anyList(), any(), anyLong())) + when(brandCheckClient.checkAll(anyList(), any(), anyLong(), any())) .thenThrow(new RuntimeException("brand service down")) .thenReturn(new BrandCheckClient.BrandCheckBatchResult( - List.of(), List.of("Zara"), List.of())); + List.of(), List.of("Zara"), List.of(), "")); List rows = List.of( row("B000000001", "Zara"), row("B000000002", "Nike")); - CollectDataBrandBatchFilter.BrandBatchOutcome failed = filter.filter(rows); + CollectDataBrandBatchFilter.BrandBatchOutcome failed = filter.filter(rows, 7L); assertEquals(2, failed.queryFailed().size(), "失败批次整组降级 queryFailed"); - CollectDataBrandBatchFilter.BrandBatchOutcome recovered = filter.filter(rows); + CollectDataBrandBatchFilter.BrandBatchOutcome recovered = filter.filter(rows, 7L); assertEquals(1, recovered.rejected().size(), "恢复后重新检查并分类"); assertEquals(1, recovered.accepted().size(), "恢复后 accepted 正确"); assertTrue(recovered.queryFailed().isEmpty(), "恢复后无残留 queryFailed"); - verify(brandCheckClient, times(2)).checkAll(anyList(), any(), anyLong()); + verify(brandCheckClient, times(2)).checkAll(anyList(), any(), anyLong(), any()); } private static CollectDataResultRowVo row(String asin, String brand) { diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/task/contract/DuplicateSubmissionContractTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/task/contract/DuplicateSubmissionContractTest.java index 6a8820da0..0fc8f6c39 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/modules/task/contract/DuplicateSubmissionContractTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/task/contract/DuplicateSubmissionContractTest.java @@ -140,8 +140,8 @@ class DuplicateSubmissionContractTest { row.setAsin("B0DUP1234"); lenient().when(collectDataBatchQuery.filter(any(), any())).thenReturn( new CollectDataBatchQuery.FilterResult(List.of(row), 0, 0)); - lenient().when(brandBatchFilter.filter(any())).thenReturn( - new CollectDataBrandBatchFilter.BrandBatchOutcome(List.of(), List.of(), List.of(row))); + lenient().when(brandBatchFilter.filter(any(), any())).thenReturn( + new CollectDataBrandBatchFilter.BrandBatchOutcome(List.of(), List.of(), List.of(row), "")); lenient().when(resultItemBatchWriter.upsertAccepted(anyLong(), anyLong(), anyString(), anyInt(), any(), anyString())) .thenReturn(new CollectDataResultItemBatchWriter.UpsertCounts(1, 0, 1)); lenient().when(resultDetailCodec.encodeChunk(any())).thenReturn("{\"chunk\":1}");