fix(brand-label): 品牌标签清洗抽公共件,六模块读写两端统一过出口

PublishBrandLabel 从 publish 私有件搬入 common/util/BrandLabel(三条守卫尺子随迁:
唯一副本、实体写出不许绕过、解析端不许重复实现),品牌列的读入与出件两端都走这一处。

读入端接上:asincache 缓存落库、invalidasin 人工黑名单(先洗再判空——键对不上是静默失效,
比脏值出件更难发现)、appearancepatent resolveBrand(两条取值分支同一个串)、collectdata
入库、shopdatacrawl copyRow/sameRow/rowDedupKey(键与值分家会让同一行跨分片重复出件)、
撞款解析与 re-baseline、dedupe 按表头名只洗品牌列。
写出端接上:publish/similarasin×2/appearancepatent/collectdata/撞款 csv+xlsx/shopdatacrawl。

rows-snapshot golden 里品牌格去掉首尾空格:正本 strip 自 2026-10-08 就含 trim,
Java/Python/MySQL 三处同形,不是这次新加的行为。

门禁:后端 mvn -o test 4594 例 0 红。BrandTaskService(含四张 sheet 出件与解析端匹配)
因同仓另一路在飞改动未动,见 docs/audit/BACKLOG.md B5。
This commit is contained in:
2026-10-20 22:35:25 +08:00
parent e855801237
commit cdef922251
34 changed files with 456 additions and 49 deletions
@@ -0,0 +1,159 @@
package com.nanri.aiimage.common.util;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
/**
* 品牌标签前缀清洗出口。
*
* <p>2026-10-08 的事故链是:Excel 里带 "Brand:" 标签前缀 → WIPO 把它当 Solr 字段限定符
* ({@code undefined field Brand},单日 19,247 次 500)→ 上游错误信封不是合法 Base64、被客户端
* 当瞬时限流重跑满 30 轮 → 品牌全局锁被一个任务持到 1h45m → 同日 16 单里 11 单排队等满 1800s 判死。
*
* <p>第一版只堵了"服务端解析 Excel"那一个入口,漏了更常用的一个:任务完成时
* {@code PublishTaskService.replaceRows} 会用客户端回传的分片整表删后重插,
* 脏值照样落库(现网 7 个脏值来源任务里 5 个的行创建时间 == 任务完成时刻,即由这条路径写入)。
* 所以护栏从"注释里提醒调用方"改成"落在唯一写库出口 + 可实跑的用例"。
*
* <p>本类原在 {@code modules/publish/service} 下,因品牌列的读写两端散在多个模块而出搬为公共件;
* 三条守卫尺子(唯一副本、出口不被绕过、解析端不重复实现)随迁。
*/
class BrandLabelTest {
private static final String OUTLET_CLASS = "BrandLabel.java";
/** 标签正则的特征片段:出现第二处就等于开了第二条会漂移的副本。 */
private static final String REGEX_SIGNATURE = "marque|marka";
@Test
void stripsLabelPrefixButKeepsLookalikes() {
assertThat(BrandLabel.strip("Brand: NIKE")).isEqualTo("NIKE");
assertThat(BrandLabel.strip(" brand : NIKE ")).isEqualTo("NIKE");
assertThat(BrandLabel.strip("品牌:耐克")).isEqualTo("耐克");
assertThat(BrandLabel.strip("MARCA: X")).isEqualTo("X");
// "Brands:" 不是标签;值中间的冒号属于品牌自身内容,只剥行首一次
assertThat(BrandLabel.strip("Brands: NIKE")).isEqualTo("Brands: NIKE");
assertThat(BrandLabel.strip("Brand: NIKE: X")).isEqualTo("NIKE: X");
assertThat(BrandLabel.strip("NIKE Brand")).isEqualTo("NIKE Brand");
// 标签只有出现在行首才是"字段名";句中出现的要原样留着,否则把 ^ 锚摘掉(变成"逢 Brand: 就裁")这条就会红
assertThat(BrandLabel.strip("Super Brand: Nike"))
.isEqualTo("Super Brand: Nike");
assertThat(BrandLabel.strip("BrandNIKE")).isEqualTo("BrandNIKE");
assertThat(BrandLabel.strip("")).isEmpty();
assertThat(BrandLabel.strip(null)).isNull();
}
@Test
void pureLabelOnlyValuesCollapseToEmpty() {
// 现网 1,533 个脏值清洗后 0 个变空串(实测),但 "Brand:" 这种纯标签值确实会洗成空。
// 钉住这一条是因为 PublishTaskService.isBlankRow 要求八个字段同时为空才算空行,
// 单靠 brand 变空不会让一行消失 —— 改了这条判据就会静默丢行。
assertThat(BrandLabel.strip("Brand:")).isEmpty();
assertThat(BrandLabel.strip("Brand: ")).isEmpty();
}
@Test
void everyPublishItemBrandWriteGoesThroughTheOutlet() throws IOException {
List<String> offenders = new ArrayList<>();
List<String> written = new ArrayList<>();
for (Path file : publishSources()) {
if (file.getFileName().toString().equals(OUTLET_CLASS)) {
continue;
}
for (String raw : read(file).split("\r?\n", -1)) {
String line = raw.trim();
if (line.startsWith("//") || line.startsWith("*") || line.startsWith("/*")) {
continue;
}
// 只有写进实体的那一行算出口;DTO 之间的搬运(copyRow/toRowDto)不是
if (line.matches("^entity\\.setBrand\\(.*\\);")) {
written.add(file.getFileName() + ": " + line);
if (!line.contains("BrandLabel.strip(")) {
offenders.add(file.getFileName() + ": " + line);
}
}
}
}
// 取样集非空自检:扫描 0 命中时下面这条恒绿,等于没护栏
assertThat(written).as("publish 模块里写 PublishItemEntity.brand 的行应被扫到").isNotEmpty();
assertThat(offenders)
.as("绕过 BrandLabel.strip 的 brand 写入点(客户端回传 replaceRows 会整表重插,"
+ "少堵一处就等于每完成一个任务把脏值写回去一次)")
.isEmpty();
}
@Test
void labelRegexLivesInExactlyOneMainSource() throws IOException {
List<String> carriers = new ArrayList<>();
for (Path file : mainSources()) {
if (read(file).contains(REGEX_SIGNATURE)) {
carriers.add(file.getFileName().toString());
}
}
assertThat(carriers)
.as("品牌标签正则的副本清单 —— 每多一处,下次加语种就要多改一处")
.containsExactly(OUTLET_CLASS);
}
@Test
void parseSideAlsoDelegatesInsteadOfReimplementing() throws IOException {
String text = read(sourceOf("PublishWorkbookService.java"));
assertThat(text).contains("row.setBrand(BrandLabel.strip(values.get(3)));");
assertThat(text).doesNotContain("stripBrandLabel");
}
private static List<Path> publishSources() throws IOException {
return mainSources().stream()
.filter(p -> p.toString().replace('\\', '/').contains("/modules/publish/"))
.collect(Collectors.toList());
}
private static List<Path> mainSources() throws IOException {
Path root = moduleRoot().resolve("src/main/java");
if (!Files.isDirectory(root)) {
fail("找不到 " + root + " —— 用例依赖以 backend-java 为工作目录运行,别让守卫静默通过");
}
try (Stream<Path> walk = Files.walk(root)) {
return walk.filter(Files::isRegularFile)
.filter(p -> p.getFileName().toString().endsWith(".java"))
.collect(Collectors.toList());
}
}
private static Path sourceOf(String fileName) throws IOException {
return mainSources().stream()
.filter(p -> p.getFileName().toString().equals(fileName))
.findFirst()
.orElseThrow(() -> new IllegalStateException("找不到 " + fileName));
}
/** surefire 的工作目录是模块目录;从仓库根或 IDE 跑时退到 backend-java/。 */
private static Path moduleRoot() {
Path here = Path.of("").toAbsolutePath();
if (Files.isDirectory(here.resolve("src/main/java"))) {
return here;
}
Path nested = here.resolve("backend-java");
if (Files.isDirectory(nested.resolve("src/main/java"))) {
return nested;
}
throw new IllegalStateException("无法定位 backend-java 模块目录(cwd=" + here + ")");
}
private static String read(Path file) {
try {
return Files.readString(file, StandardCharsets.UTF_8);
} catch (IOException e) {
throw new IllegalStateException("读取失败: " + file, e);
}
}
}