package com.nanri.aiimage; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.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.LinkedHashSet; import java.util.List; import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; import org.junit.jupiter.api.Test; /** * 全仓尺子:凡是会进用户界面的文案出口({@code new BusinessException(...)} 与 * {@code saveFileBuildProgress(...)}),首段必须是中文。 * *

起因是 2026-10-09 一张货源查询任务截图:同一个卡片上既有中文"仅完成 102/112 行", * 又有一句英文 "Similar ASIN result rows are empty; please retry result generation"。 * 那次拆分只翻译了 legacy 装配路径,bounded 路径的 8 句英文异常原样进了 * {@code biz_task_file_job.error_message},前端 {@code pendingResultHint} 又不加转换地渲染。 * 全仓普查当时数出 1,213 个带字面量的出口、其中 112 个首段没有中文。 * *

判据是"首段字面量含中文",不是"每段都含中文":拼接里的单位片段 * ({@code " bytes"}、{@code " 分钟)"})本就不该有中文。 * *

豁免表必须条条被用到——用不到的豁免就是常开的洞,所以 {@link #noDeadExemptions} 专门反查。 */ class UserFacingMessageChineseGuardTest { /** 扫描到的出口总数下限:低于此值说明抽取器什么都没抓到,那时的"零违规"是假的。 */ private static final int MIN_OUTLETS_SCANNED = 1200; /** * 逐条点名豁免,格式 {@code 文件后缀 :: 首段字面量}。 * 每一条都要在下面写清为什么可以留英文,以及什么时候该摘掉。 */ private static final List EXEMPTIONS = List.of( // JSON 键名,不是消息:真正的兜底文案在同一个参数列表的最后一段,已是中文 "modules/imagevideo/service/ImageVideoCozeService.java::msg"); @Test void everyUserFacingMessageOutletStartsWithChinese() throws IOException { List offenders = new ArrayList<>(); Set usedExemptions = new LinkedHashSet<>(); int scanned = 0; for (Path file : mainSources()) { String source = read(file); String relative = relativeName(file); for (String arguments : messageArguments(source)) { String first = firstLiteral(arguments); if (first.isBlank()) { continue; } scanned++; if (containsChinese(first)) { continue; } String key = relative + "::" + first; if (EXEMPTIONS.contains(key)) { usedExemptions.add(key); continue; } offenders.add(relative + " -> \"" + first + "\""); } } assertThatListIsEmpty(offenders, "这些文案出口的首段不是中文,会原样出现在用户界面上"); assertTrue(scanned >= MIN_OUTLETS_SCANNED, "只扫到 " + scanned + " 个带字面量的出口(下限 " + MIN_OUTLETS_SCANNED + ")——抽取器失效了,此刻的\"零违规\"不作数"); } /** 用不到的豁免要摘掉,否则它会慢慢变成一个"谁都能往里塞一条"的常开门。 */ @Test void noDeadExemptions() throws IOException { Set seen = new LinkedHashSet<>(); for (Path file : mainSources()) { String relative = relativeName(file); for (String arguments : messageArguments(read(file))) { String first = firstLiteral(arguments); if (!first.isBlank()) { seen.add(relative + "::" + first); } } } List dead = EXEMPTIONS.stream().filter(key -> !seen.contains(key)).toList(); assertThatListIsEmpty(dead, "这些豁免已经没有对应的英文出口了,请从 EXEMPTIONS 里删掉"); } private static void assertThatListIsEmpty(List items, String message) { if (!items.isEmpty()) { fail(message + ":\n " + String.join("\n ", items)); } } private static final List MARKERS = List.of("new BusinessException(", "saveFileBuildProgress("); /** 每个标记后面那段配平的括号内容,就是文案所在的地方。 */ private static List messageArguments(String source) { List arguments = new ArrayList<>(); for (String marker : MARKERS) { int from = 0; while (true) { int at = source.indexOf(marker, from); if (at < 0) { break; } from = at + marker.length(); int open = source.indexOf('(', at); String body = balancedArguments(source, open); if (body == null) { fail("括号没配平,扫描器会在 " + marker + " 处漏判(offset=" + at + ")"); } arguments.add(body); } } return arguments; } private static String balancedArguments(String source, int openParen) { int depth = 0; boolean inString = false; for (int i = openParen; i < source.length(); i++) { char c = source.charAt(i); if (inString) { if (c == '\\') { i++; } else if (c == '"') { inString = false; } continue; } if (c == '"') { inString = true; } else if (c == '(') { depth++; } else if (c == ')') { depth--; if (depth == 0) { return source.substring(openParen + 1, i); } } } return null; } private static String firstLiteral(String arguments) { int start = -1; boolean inString = false; for (int i = 0; i < arguments.length(); i++) { char c = arguments.charAt(i); if (inString) { if (c == '\\') { i++; continue; } if (c == '"') { return arguments.substring(start + 1, i); } continue; } if (c == '"') { inString = true; start = i; } } return ""; } private static boolean containsChinese(String text) { for (int i = 0; i < text.length(); i++) { char c = text.charAt(i); if (c >= 0x4E00 && c <= 0x9FFF) { return true; } } return false; } /** 相对 src/main/java/com/nanri/aiimage 的路径,豁免表用它点名,行号会漂所以不进键。 */ private static String relativeName(Path file) { String path = file.toString().replace('\\', '/'); int cut = path.indexOf("/com/nanri/aiimage/"); return cut < 0 ? path : path.substring(cut + "/com/nanri/aiimage/".length()); } private static List mainSources() throws IOException { Path root = moduleRoot().resolve("src/main/java"); if (!Files.isDirectory(root)) { fail("找不到 " + root + " —— 用例依赖以 backend-java 为工作目录运行,别让守卫静默通过"); } try (Stream walk = Files.walk(root)) { return walk.filter(Files::isRegularFile) .filter(p -> p.getFileName().toString().endsWith(".java")) .collect(Collectors.toList()); } } /** 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); } } }