refactor(phase-4): 解散万能 util——similarasin/util 与 common/util 按域归位
similarasin/util 7 支: - 域内件(SimilarAsinImageEmbedder/PuzzleImageMerger/SimilarAsinLogSupport/ BoundedImageCache)并入既有 service/support 先例; - 通用 Excel 件(ExcelCellImageWriter/WpsCellImageWriter)归 common/excel; - 仅测试用的 SimilarAsinPerfFixture 移出生产源到 src/test。 common/util 13 支解散为四个关注点包:excel(读/写/单元格图片)、 text(BrandLabel/SecretMasking/UserMessages)、task(行过滤/分组传播/判死文案/ 删除台账/锁跳过留痕)、support(LRU/下载头/线程池)。老 util 目录不再存在。 纯粹的包搬迁与 import 重写(diff 219 增 / 217 删),零行为改动: 搬迁前后由 SimilarAsinCreateTaskSnapshotTest 的建单入口 golden 对撞相等; 新增 MovedComponentScanTest 钉住三枚 @Component 仍被根包扫描发现、老包零残留。
This commit is contained in:
@@ -0,0 +1,159 @@
|
||||
package com.nanri.aiimage.common.text;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.nanri.aiimage.common.text;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* 文案把关工具的行为钉子:中文照原样过、英文换兜底句、详情只在中文时才拼接。
|
||||
*/
|
||||
class UserMessagesTest {
|
||||
|
||||
@Test
|
||||
void chineseMessagePassesThroughUntouched() {
|
||||
assertEquals("这份文件没有解析到可导出的行",
|
||||
UserMessages.orFallback("这份文件没有解析到可导出的行", "操作未能完成"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void englishAndBlankFallBackToChinese() {
|
||||
assertEquals("操作未能完成",
|
||||
UserMessages.orFallback("Similar ASIN result rows are empty", "操作未能完成"));
|
||||
assertEquals("操作未能完成", UserMessages.orFallback(null, "操作未能完成"));
|
||||
assertEquals("操作未能完成", UserMessages.orFallback(" ", "操作未能完成"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void chineseDetailIsAppendedAndEnglishDetailIsDropped() {
|
||||
assertEquals("转换结果打包失败:临时目录已满",
|
||||
UserMessages.with("转换结果打包失败", new RuntimeException("临时目录已满")));
|
||||
assertEquals("转换结果打包失败",
|
||||
UserMessages.with("转换结果打包失败", new RuntimeException("No space left on device")));
|
||||
assertEquals("转换结果打包失败", UserMessages.with("转换结果打包失败", (String) null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void headlineKeepsItsOwnColonWhenDetailIsGone() {
|
||||
String text = UserMessages.with("去重结果打包失败: ", new IllegalStateException("boom"));
|
||||
assertEquals("去重结果打包失败", text);
|
||||
assertFalse(text.endsWith(":"), "尾巴被丢掉后不该留下悬空冒号");
|
||||
}
|
||||
|
||||
@Test
|
||||
void onlyHanCharactersCountAsChinese() {
|
||||
assertTrue(UserMessages.hasChinese("401 未登录"));
|
||||
assertFalse(UserMessages.hasChinese("TASK_LOCK_BUSY"));
|
||||
assertFalse(UserMessages.hasChinese(":()—"), "全角标点不等于中文文案");
|
||||
assertFalse(UserMessages.hasChinese(null));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user