fix(文案出口): 用户可见提示语一律走 UserMessages 把关,并补结果作业租约围栏
两件事同源:2026-10-09 任务 30456 的卡片上同时出现中文进度和一句英文异常原文。 围栏:派发链在执行前重读 job 行,让 activateRunningClaim 的 CAS 恒真,撞锁方 deferRunning 把在跑的行翻回 PENDING,markSuccess 静默写 0 行——成功被判成失败。 claim 快照现在贯穿到 runClaimed 不再回读,markSuccess 返回布尔并补 stage=SUCCESS_LOST 日志,让这次丢失可观测。 文案:普查数出 61 处把 ex.getMessage() 拼进用户可见字段(异常响应、任务 error_message、 结果作业 error_msg、看板 message),逐条翻译追不上新增速度,改为出口把关—— UserMessages.with(中文主干, 异常) 只拼接中文详情、orFallback(原文, 兜底句) 只放行含中文的串, 被换掉的原文进日志;GlobalExceptionHandler 按码分档兜底(401/4011/40902 各说各的话)。 另把 chunk-read-failed[N] 摘要写成"第 N 分片读取失败"、TASK_LOCK_BUSY 换成中文(40902 码不动, 客户端按码分支)。数字人两处与结果作业 worker 故意仍存原文:孤儿判定与三条 preserves raw 用例按 error_message 读它,界面侧由前端 userNotice 兜住,已在新尺子里点名豁免并反查豁免是否还被用到。 尺子:RawExceptionMessageSinkGuardTest(出口行数下限硬钉 1500+内存造违例自证)、UserMessagesTest、 UserFacingMessageChineseGuardTest 豁免表收到只剩 JSON 键 msg 一条。mvn -o test 4205 例 0 失败。
This commit is contained in:
@@ -0,0 +1,137 @@
|
||||
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.regex.Pattern;
|
||||
import java.util.stream.Stream;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* 全仓尺子:用户可见文案出口不许把底层异常原文直接拼进去。
|
||||
*
|
||||
* <p>起因是 2026-10-09 那张货源查询截图:卡片上那句英文不是写错的文案,而是
|
||||
* {@code biz_task_file_job.error_message} 里存的异常原文。普查数出 61 处出口
|
||||
* ({@code new BusinessException("中文: " + ex.getMessage())}、
|
||||
* {@code setErrorMessage(ex.getMessage())}、{@code ApiResponse.fail(ex.getMessage())} 这类)
|
||||
* 把 Java 栈名、Coze/MinIO 报错原样交给界面——逐条翻译追不上新增速度,所以把规矩做进代码:
|
||||
* 出口只接受 {@link com.nanri.aiimage.common.util.UserMessages} 把关后的串。
|
||||
*
|
||||
* <p>判据按行匹配(与同目录 {@code UserFacingMessageChineseGuardTest} 的括号配平法互补):
|
||||
* 一行里既有文案出口、又出现 {@code getMessage()/messageOf()},却没有 {@code UserMessages.}
|
||||
* 也没有 {@code log.},就是违例。日志行不算——日志正是留着英文原文查根因的地方。
|
||||
*
|
||||
* <p>分母硬钉:扫到的出口行数低于下限,说明正则或目录树坏了,那时的"零违例"不作数。
|
||||
*/
|
||||
class RawExceptionMessageSinkGuardTest {
|
||||
|
||||
/** 本轮实测 1595 行出口,下限取 1500:明显缩水即判抽取器失效。 */
|
||||
private static final int MIN_SINK_LINES = 1500;
|
||||
|
||||
/**
|
||||
* 逐条点名的豁免:格式 {@code 文件后缀 :: 语句}(空白归一后整句比对,行号会漂所以不进键)。
|
||||
* 数字人任务把 Coze 返回的业务错原文留在 error_message 里,是给支持同学对着
|
||||
* ImageVideoAsyncTaskServiceTest 那三条钉子查根因用的;界面上的中文由前端
|
||||
* {@code userNotice} 兜住,所以这里放行、不再要求写侧把关。
|
||||
*/
|
||||
private static final List<String> EXEMPTIONS = List.of(
|
||||
"modules/imagevideo/service/ImageVideoAsyncTaskService.java::"
|
||||
+ "task.setErrorMessage(truncate(messageOf(ex)));");
|
||||
|
||||
private static final Pattern SINK = Pattern.compile(
|
||||
"new BusinessException\\(|ApiResponse\\.fail\\(|set(?:ErrorMessage|Error|MatchMessage|FileError"
|
||||
+ "|Message|FailureReason)\\(|markFailed|markFailureFinalized|saveBrandProgressSnapshot"
|
||||
+ "|saveFileBuildProgress");
|
||||
|
||||
private static final Pattern RAW_EXCEPTION_TEXT = Pattern.compile("\\.getMessage\\(\\)|messageOf\\(");
|
||||
|
||||
private static final Pattern GUARDED = Pattern.compile("UserMessages\\.|\\blog\\.\\w+\\(");
|
||||
|
||||
@Test
|
||||
void userVisibleSinksNeverEmbedRawExceptionText() throws IOException {
|
||||
List<String> offenders = new ArrayList<>();
|
||||
Set<String> usedExemptions = new LinkedHashSet<>();
|
||||
int sinkLines = 0;
|
||||
for (Path file : mainSources()) {
|
||||
String relative = relativeName(file);
|
||||
List<String> lines = Files.readAllLines(file, StandardCharsets.UTF_8);
|
||||
for (int i = 0; i < lines.size(); i++) {
|
||||
String line = lines.get(i).strip();
|
||||
if (line.startsWith("//") || line.startsWith("*") || line.startsWith("/*")) {
|
||||
continue;
|
||||
}
|
||||
if (!SINK.matcher(line).find()) {
|
||||
continue;
|
||||
}
|
||||
sinkLines++;
|
||||
if (RAW_EXCEPTION_TEXT.matcher(line).find() && !GUARDED.matcher(line).find()) {
|
||||
String key = relative + "::" + line;
|
||||
if (EXEMPTIONS.contains(key)) {
|
||||
usedExemptions.add(key);
|
||||
continue;
|
||||
}
|
||||
offenders.add(relative + ":" + (i + 1) + " " + shorten(line));
|
||||
}
|
||||
}
|
||||
}
|
||||
assertTrue(sinkLines >= MIN_SINK_LINES,
|
||||
"只扫到 " + sinkLines + " 行文案出口(下限 " + MIN_SINK_LINES + ")——正则或源码树坏了,"
|
||||
+ "此刻的\"零违例\"不作数");
|
||||
if (!offenders.isEmpty()) {
|
||||
fail("这些出口把底层异常原文交给了界面,必须走 UserMessages.with/orFallback:\n "
|
||||
+ String.join("\n ", offenders));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 自证:把一条违例样本喂给同一套判据,必须被抓住。
|
||||
* 判据抓不到自己造的违例,就说明它是个恒绿的空壳。
|
||||
*/
|
||||
@Test
|
||||
void detectorCatchesAPlantedViolation() {
|
||||
String planted = "throw new BusinessException(\"生成结果失败: \" + ex.getMessage());";
|
||||
assertTrue(SINK.matcher(planted).find(), "样本没被认成文案出口,抽取器对这类写法失明");
|
||||
assertTrue(RAW_EXCEPTION_TEXT.matcher(planted).find(), "样本没被认出异常原文");
|
||||
assertTrue(!GUARDED.matcher(planted).find(), "样本不该算已把关");
|
||||
|
||||
String guarded = "throw new BusinessException(UserMessages.with(\"生成结果失败\", ex));";
|
||||
assertTrue(SINK.matcher(guarded).find());
|
||||
assertTrue(GUARDED.matcher(guarded).find(), "把关写法被误判成违例");
|
||||
|
||||
String logged = "log.warn(\"assemble failed msg={}\", ex.getMessage());";
|
||||
assertTrue(GUARDED.matcher(logged).find(), "日志行不该进违例");
|
||||
}
|
||||
|
||||
private static String shorten(String line) {
|
||||
return line.length() <= 140 ? line : line.substring(0, 140) + "...";
|
||||
}
|
||||
|
||||
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<Path> mainSources() throws IOException {
|
||||
Path root = moduleRoot().resolve("src/main/java");
|
||||
if (!Files.isDirectory(root)) {
|
||||
fail("找不到源码目录:" + root);
|
||||
}
|
||||
try (Stream<Path> stream = Files.walk(root)) {
|
||||
return stream.filter(p -> p.toString().endsWith(".java")).sorted().toList();
|
||||
}
|
||||
}
|
||||
|
||||
private static Path moduleRoot() {
|
||||
Path here = Path.of("").toAbsolutePath();
|
||||
return here.getFileName().toString().equals("backend-java") ? here : here.getParent();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,223 @@
|
||||
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(...)}),首段必须是中文。
|
||||
*
|
||||
* <p>起因是 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 个首段没有中文。
|
||||
*
|
||||
* <p>判据是"首段字面量含中文",不是"每段都含中文":拼接里的单位片段
|
||||
* ({@code " bytes"}、{@code " 分钟)"})本就不该有中文。
|
||||
*
|
||||
* <p>豁免表必须条条被用到——用不到的豁免就是常开的洞,所以 {@link #noDeadExemptions} 专门反查。
|
||||
*/
|
||||
class UserFacingMessageChineseGuardTest {
|
||||
|
||||
/** 扫描到的出口总数下限:低于此值说明抽取器什么都没抓到,那时的"零违规"是假的。 */
|
||||
private static final int MIN_OUTLETS_SCANNED = 1200;
|
||||
|
||||
/**
|
||||
* 逐条点名豁免,格式 {@code 文件后缀 :: 首段字面量}。
|
||||
* 每一条都要在下面写清为什么可以留英文,以及什么时候该摘掉。
|
||||
*/
|
||||
private static final List<String> EXEMPTIONS = List.of(
|
||||
// JSON 键名,不是消息:真正的兜底文案在同一个参数列表的最后一段,已是中文
|
||||
"modules/imagevideo/service/ImageVideoCozeService.java::msg");
|
||||
|
||||
@Test
|
||||
void everyUserFacingMessageOutletStartsWithChinese() throws IOException {
|
||||
List<String> offenders = new ArrayList<>();
|
||||
Set<String> 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<String> 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<String> dead = EXEMPTIONS.stream().filter(key -> !seen.contains(key)).toList();
|
||||
assertThatListIsEmpty(dead, "这些豁免已经没有对应的英文出口了,请从 EXEMPTIONS 里删掉");
|
||||
}
|
||||
|
||||
private static void assertThatListIsEmpty(List<String> items, String message) {
|
||||
if (!items.isEmpty()) {
|
||||
fail(message + ":\n " + String.join("\n ", items));
|
||||
}
|
||||
}
|
||||
|
||||
private static final List<String> MARKERS = List.of("new BusinessException(", "saveFileBuildProgress(");
|
||||
|
||||
/** 每个标记后面那段配平的括号内容,就是文案所在的地方。 */
|
||||
private static List<String> messageArguments(String source) {
|
||||
List<String> 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<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());
|
||||
}
|
||||
}
|
||||
|
||||
/** 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.util;
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -99,8 +99,9 @@ class TaskResultFileJobWorkerLogAuditTest {
|
||||
assertFalse(l.contains("module="), "不应使用 module= 漂移字段: " + l.trim());
|
||||
}
|
||||
// 阶段字面量来自允许集合(防新增阶段拼写漂移)
|
||||
// SUCCESS_LOST:结果文件已生成但作业行不在本次执行手里(2026-10-09 任务 30456 的静默丢失)
|
||||
List<String> allowed = List.of("SKIP_OWNER", "REQUEUE", "WAIT_LLM", "DEFER", "SUCCESS",
|
||||
"ORPHAN", "FAILED", "FINALIZE", "HEARTBEAT", "FALLBACK");
|
||||
"SUCCESS_LOST", "ORPHAN", "FAILED", "FINALIZE", "HEARTBEAT", "FALLBACK");
|
||||
for (String l : perJob) {
|
||||
if (l.contains("stage=")) {
|
||||
int idx = l.indexOf("stage=");
|
||||
|
||||
+2
-2
@@ -207,7 +207,7 @@ class ImageVideoAsyncTaskServiceTest {
|
||||
ImageVideoAsyncTaskEntity task = waitingWorkflowTask();
|
||||
task.setStatus("FAILED");
|
||||
task.setCozeStatus("RUNNING");
|
||||
task.setErrorMessage("Coze workflow finished with status RUNNING");
|
||||
task.setErrorMessage("Coze 工作流已结束,状态 RUNNING");
|
||||
when(taskMapper.selectOne(any())).thenReturn(task);
|
||||
|
||||
var result = service.getTask(98L, 1L);
|
||||
@@ -367,7 +367,7 @@ class ImageVideoAsyncTaskServiceTest {
|
||||
|
||||
assertEquals("FAILED", task.getStatus());
|
||||
assertEquals("SUCCESS", task.getCozeStatus());
|
||||
assertEquals("Coze workflow succeeded but video_url is empty", task.getErrorMessage());
|
||||
assertEquals("Coze 工作流成功但没有返回视频地址", task.getErrorMessage());
|
||||
assertNotNull(task.getCompletedAt());
|
||||
assertTrue(task.getResultJson().contains("video_url"));
|
||||
verify(archiveService, never()).enrichCompletedTask(any(), any());
|
||||
|
||||
+2
-2
@@ -174,7 +174,7 @@ class SimilarAsinTaskServiceChunkKeysetTest {
|
||||
when(taskChunkMapper.selectList(any())).thenThrow(new IllegalStateException("db down"));
|
||||
BusinessException ex = assertThrows(BusinessException.class,
|
||||
() -> SimilarAsinChunkMergeSupport.loadChunksKeyset(taskChunkMapper, 7004L, MODULE, 500));
|
||||
assertTrue(ex.getMessage() != null && ex.getMessage().contains("chunk"),
|
||||
assertTrue(ex.getMessage() != null && ex.getMessage().contains("分片"),
|
||||
"异常消息必须可识别,实际: " + ex.getMessage());
|
||||
}
|
||||
|
||||
@@ -194,7 +194,7 @@ class SimilarAsinTaskServiceChunkKeysetTest {
|
||||
}).when(taskChunkMapper).selectList(any());
|
||||
BusinessException ex = assertThrows(BusinessException.class,
|
||||
() -> SimilarAsinChunkMergeSupport.loadChunksKeyset(taskChunkMapper, 7004L, MODULE, 3));
|
||||
assertTrue(ex.getMessage() != null && ex.getMessage().contains("chunk"),
|
||||
assertTrue(ex.getMessage() != null && ex.getMessage().contains("分片"),
|
||||
"异常消息必须可识别,实际: " + ex.getMessage());
|
||||
// 恢复后重试成功:5 个 chunk 全部取回
|
||||
stubKeysetPages(all, 3);
|
||||
|
||||
+1
-1
@@ -357,7 +357,7 @@ class SimilarAsinTaskServiceChunkMergeLimitTest {
|
||||
}
|
||||
});
|
||||
assertNotNull(ex.getMessage());
|
||||
assertTrue(ex.getMessage().contains("chunk"),
|
||||
assertTrue(ex.getMessage().contains("分片"),
|
||||
"chunk 读取失败消息必须可识别,实际: " + ex.getMessage());
|
||||
verify(transientPayloadStorageService, times(0)).storeChunkPayloadVersioned(anyString(), any(), anyString(), any(), anyString());
|
||||
verify(taskScopeStateMapper, times(0)).insert(any(TaskScopeStateEntity.class));
|
||||
|
||||
+1
-1
@@ -309,7 +309,7 @@ class SimilarAsinTaskServiceRowKeyDedupeTest {
|
||||
throw e.getCause();
|
||||
}
|
||||
});
|
||||
assertTrue(ex.getMessage() != null && ex.getMessage().contains("chunk"),
|
||||
assertTrue(ex.getMessage() != null && ex.getMessage().contains("分片"),
|
||||
"chunk 读取失败消息必须可识别,实际: " + ex.getMessage());
|
||||
verify(transientPayloadStorageService, times(0)).storeChunkPayloadVersioned(anyString(), any(), anyString(), any(), anyString());
|
||||
// 恢复后重试成功:只写一次,无重复记录
|
||||
|
||||
+1
-1
@@ -359,7 +359,7 @@ class SimilarAsinTaskServiceRowKeyIndexTest {
|
||||
throw e.getCause();
|
||||
}
|
||||
});
|
||||
assertTrue(ex.getMessage() != null && ex.getMessage().contains("chunk"),
|
||||
assertTrue(ex.getMessage() != null && ex.getMessage().contains("分片"),
|
||||
"chunk 读取失败消息必须可识别,实际: " + ex.getMessage());
|
||||
verify(transientPayloadStorageService, times(0)).storeChunkPayloadVersioned(anyString(), any(), anyString(), any(), anyString());
|
||||
// 恢复后重试成功:行合并到正确 chunk
|
||||
|
||||
+222
@@ -0,0 +1,222 @@
|
||||
package com.nanri.aiimage.modules.similarasin.service.support;
|
||||
|
||||
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 org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* 货源查询(SIMILAR_ASIN)结果出口的提示语必须是中文。
|
||||
*
|
||||
* <p>线上任务 30456 的界面同时出现"仅完成 102/112 行"和一句英文
|
||||
* "Similar ASIN result rows are empty; please retry result generation":前者是 legacy 装配路径写的,
|
||||
* 后者是 bounded 装配路径写的。两条路径是同一次拆分(9d92fb4a)时只翻译了 legacy 那一半,
|
||||
* bounded 那一半的 8 句英文异常原样进了 {@code biz_task_file_job.error_message},
|
||||
* 前端 {@code pendingResultHint} 又把这一列不加转换地渲染出来。
|
||||
*
|
||||
* <p>所以这里钉两件事:
|
||||
* <ol>
|
||||
* <li>那 8 句英文不许回来(排他断言,逐句点名);</li>
|
||||
* <li>两个文件里每个 {@code new BusinessException(...)} / {@code saveFileBuildProgress(...)}
|
||||
* 出口的第一段文案必须含中文——新增出口自动受约束。</li>
|
||||
* </ol>
|
||||
*
|
||||
* <p>第二条的判据是"首段字面量含中文",不是"每段都含中文":拼接里的单位片段
|
||||
* ({@code " bytes"}、{@code " 分钟)"})本就不该有中文,2026-10-09 用两个文件的全部
|
||||
* 62 个出口校准过这个口径。分母自证用例防止扫描器一旦抓不到就恒绿。
|
||||
*/
|
||||
class SimilarAsinChineseMessageGuardTest {
|
||||
|
||||
private static final List<String> SCANNED_FILES = List.of(
|
||||
"modules/similarasin/service/SimilarAsinTaskService.java",
|
||||
"modules/similarasin/service/support/SimilarAsinResultWorkbookAssembler.java");
|
||||
|
||||
/** 2026-10-09 实测:TaskService 42 个出口 + Assembler 20 个出口。新增/删除出口要同步改这里。 */
|
||||
private static final int SCANNED_SITE_COUNT = 62;
|
||||
|
||||
/** 文案来自变量、字面量判据覆盖不到的出口,按构造点名(行号会漂)。 */
|
||||
private static final List<String> VARIABLE_FED_OUTLETS = List.of(
|
||||
"task, job, 1, 1, failureMessage",
|
||||
"FailedStatusRowFilter.noMatchedRowsMessage()",
|
||||
"message");
|
||||
|
||||
private static final List<String> ENGLISH_LEAKS = List.of(
|
||||
"Similar ASIN result rows are empty",
|
||||
"failed to create result directory",
|
||||
"result workbook assembly interrupted",
|
||||
"result workbook assembly timed out",
|
||||
"result workbook assembly cancelled",
|
||||
"result workbook assembly failed",
|
||||
"create Similar ASIN image spool failed",
|
||||
"no valid similar ASIN rows",
|
||||
"Assembling xlsx",
|
||||
"Uploading result file",
|
||||
"Result file generated",
|
||||
"LLM result is still processing");
|
||||
|
||||
@Test
|
||||
void theEnglishMessagesThatReachedTheUiAreGone() throws IOException {
|
||||
for (String fragment : ENGLISH_LEAKS) {
|
||||
for (String file : SCANNED_FILES) {
|
||||
assertThat(readSource(file))
|
||||
.as("%s 里不许出现以 \"%s\" 开头的用户可见文案,改成中文", file, fragment)
|
||||
.doesNotContain(fragment);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void everyMessageOutletStartsWithChinese() throws IOException {
|
||||
List<String> offenders = new ArrayList<>();
|
||||
List<String> variableFed = new ArrayList<>();
|
||||
int scanned = 0;
|
||||
for (String file : SCANNED_FILES) {
|
||||
String source = readSource(file);
|
||||
for (Outlet outlet : outlets(source)) {
|
||||
scanned++;
|
||||
if (!outlet.hasFirstLiteral()) {
|
||||
variableFed.add(outlet.arguments().trim());
|
||||
continue;
|
||||
}
|
||||
if (!containsChinese(outlet.firstLiteral())) {
|
||||
offenders.add(file + ":" + outlet.line() + " -> \"" + outlet.firstLiteral() + "\"");
|
||||
}
|
||||
}
|
||||
}
|
||||
assertThat(offenders)
|
||||
.as("这些出口的首段文案没有中文,会原样出现在用户界面上")
|
||||
.isEmpty();
|
||||
// 分母自证:扫描器一旦什么都抓不到,上面的断言就恒绿,等于没有护栏。
|
||||
// 新增出口要同时改这个数字;文案来自变量时也要确认变量本身是中文。
|
||||
assertThat(scanned)
|
||||
.as("两个文件里 new BusinessException(...) 与 saveFileBuildProgress(...) 的出口总数")
|
||||
.isEqualTo(SCANNED_SITE_COUNT);
|
||||
assertThat(variableFed)
|
||||
.as("文案来自变量、本用例判不了的出口——改动这几处时要自己确认落到界面上是中文")
|
||||
.containsExactlyInAnyOrderElementsOf(VARIABLE_FED_OUTLETS);
|
||||
}
|
||||
|
||||
private record Outlet(int line, String arguments) {
|
||||
boolean hasFirstLiteral() {
|
||||
return !firstLiteral().isEmpty();
|
||||
}
|
||||
|
||||
String firstLiteral() {
|
||||
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 List<Outlet> outlets(String source) {
|
||||
List<Outlet> outlets = new ArrayList<>();
|
||||
for (String marker : List.of("new BusinessException(", "saveFileBuildProgress(")) {
|
||||
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 arguments = balancedArguments(source, open);
|
||||
if (arguments == null) {
|
||||
fail("括号没配平,扫描器会在 " + marker + " 处漏判(offset=" + at + ")");
|
||||
}
|
||||
outlets.add(new Outlet(lineOf(source, at), arguments));
|
||||
}
|
||||
}
|
||||
return outlets;
|
||||
}
|
||||
|
||||
private static int lineOf(String source, int offset) {
|
||||
int line = 1;
|
||||
for (int i = 0; i < offset; i++) {
|
||||
if (source.charAt(i) == '\n') {
|
||||
line++;
|
||||
}
|
||||
}
|
||||
return line;
|
||||
}
|
||||
|
||||
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 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;
|
||||
}
|
||||
|
||||
private static String readSource(String moduleRelativePath) throws IOException {
|
||||
Path file = mainJavaRoot().resolve("com/nanri/aiimage/" + moduleRelativePath);
|
||||
if (!Files.isRegularFile(file)) {
|
||||
fail("找不到 " + file + " —— 用例依赖以 backend-java 为工作目录运行,别让守卫静默通过");
|
||||
}
|
||||
return Files.readString(file, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
/** surefire 的工作目录是模块目录;从仓库根或 IDE 跑时退到 backend-java/。 */
|
||||
private static Path mainJavaRoot() {
|
||||
Path here = Path.of("").toAbsolutePath();
|
||||
if (Files.isDirectory(here.resolve("src/main/java"))) {
|
||||
return here.resolve("src/main/java");
|
||||
}
|
||||
Path nested = here.resolve("backend-java");
|
||||
if (Files.isDirectory(nested.resolve("src/main/java"))) {
|
||||
return nested.resolve("src/main/java");
|
||||
}
|
||||
throw new IllegalStateException("无法定位 backend-java 模块目录(cwd=" + here + ")");
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -370,7 +370,7 @@ class SimilarAsinHistoryAssemblerTest {
|
||||
|
||||
BusinessException thrown = assertThrows(BusinessException.class,
|
||||
() -> assembler.buildHistoryItems(List.of(row), Map.of(10L, task(10L, "RUNNING", null)), jobMap()));
|
||||
assertTrue(thrown.getMessage().contains("chunk=2"), "异常附带 chunk 索引,实际: " + thrown.getMessage());
|
||||
assertTrue(thrown.getMessage().contains("分片=2"), "异常附带分片索引,实际: " + thrown.getMessage());
|
||||
|
||||
ArgumentCaptor<LambdaUpdateWrapper<TaskScopeStateEntity>> captor =
|
||||
ArgumentCaptor.forClass(LambdaUpdateWrapper.class);
|
||||
|
||||
+28
@@ -4,6 +4,7 @@ import com.nanri.aiimage.modules.task.mapper.TaskFileJobMapper;
|
||||
import com.nanri.aiimage.modules.task.model.entity.TaskFileJobEntity;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.core.task.TaskExecutor;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
@@ -13,6 +14,7 @@ import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
@@ -86,6 +88,32 @@ class TaskFileJobLocalDispatcherTest {
|
||||
verify(executor, times(1)).execute(any(Runnable.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* 已 claim 的派发必须把 claim 那一刻的行快照原样带到执行处,且这条路径一次都不回读数据库。
|
||||
*
|
||||
* <p>线上任务 30456(2026-10-09)的反例:执行前 {@code selectById} 重读,读到的
|
||||
* {@code updated_at} 就是当前值,{@code activateRunningClaim} 的 CAS 于是恒真 ——
|
||||
* 排队排久了的旧派发能冒充成新持有者,撞锁后再用 {@code deferRunning} 把真持有者
|
||||
* 正在跑的行翻回 PENDING,那次的 {@code markSuccess} 静默 0 行更新、成功丢失。
|
||||
*/
|
||||
@Test
|
||||
void claimedDispatchCarriesTheSnapshotInsteadOfReReadingTheRow() {
|
||||
TaskFileJobEntity claim = job(7L);
|
||||
claim.setStatus("RUNNING");
|
||||
TaskFileJobEntity rowChangedAfterTheClaim = job(7L);
|
||||
rowChangedAfterTheClaim.setStatus("PENDING");
|
||||
when(taskFileJobMapper.selectById(7L)).thenReturn(rowChangedAfterTheClaim);
|
||||
|
||||
List<Runnable> captured = captureRunnables();
|
||||
assertTrue(dispatcher.dispatchClaimed(claim));
|
||||
captured.forEach(Runnable::run);
|
||||
|
||||
ArgumentCaptor<TaskFileJobEntity> passed = ArgumentCaptor.forClass(TaskFileJobEntity.class);
|
||||
verify(worker).processClaimed(passed.capture());
|
||||
assertSame(claim, passed.getValue(), "必须用 claim 快照,重读就等于没有围栏");
|
||||
verify(taskFileJobMapper, never()).selectById(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_task_073_dispatch_normal_multiple_items() throws Exception {
|
||||
// 批量场景:多个不同 jobId 依次 dispatch,互不冲突,全部受理。
|
||||
|
||||
+18
@@ -176,6 +176,24 @@ class TaskFileJobServiceTest {
|
||||
assertTrue(update.getValue().getParamNameValuePairs().containsValue("RUNNING"));
|
||||
}
|
||||
|
||||
/**
|
||||
* 围栏写没命中必须让调用方知道。2026-10-09 任务 30456 就是这里静默 0 行更新:
|
||||
* 结果文件已生成、可下载,作业行却被别人翻回了 PENDING,最后被重试打满判成 FAILED,
|
||||
* 界面上一个成功的任务显示成"结果生成失败"。
|
||||
*/
|
||||
@Test
|
||||
void markSuccessReportsWhetherTheFencedWriteLanded() {
|
||||
TaskFileJobEntity job = runningJob(109L, 1, LocalDateTime.now());
|
||||
TaskFileJobService service = new TaskFileJobService(taskFileJobMapper, applicationEventPublisher);
|
||||
|
||||
when(taskFileJobMapper.update(isNull(), any(LambdaUpdateWrapper.class))).thenReturn(0);
|
||||
assertFalse(service.markSuccess(job, "result.xlsx"),
|
||||
"行已不在本次执行手里时必须报未落库,不能静默");
|
||||
|
||||
when(taskFileJobMapper.update(isNull(), any(LambdaUpdateWrapper.class))).thenReturn(1);
|
||||
assertTrue(service.markSuccess(job, "result.xlsx"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void similarAsinHeartbeatOnlyTouchesStaleRunningAssembleJobs() {
|
||||
TaskFileJobService service = new TaskFileJobService(taskFileJobMapper, applicationEventPublisher);
|
||||
|
||||
+30
@@ -158,6 +158,36 @@ class TaskResultFileJobWorkerOrphanTest {
|
||||
verify(taskFileJobService).markFailedPermanent(job, "result record not found");
|
||||
}
|
||||
|
||||
/**
|
||||
* 2026-10-09 把各模块的"任务不存在/结果记录不存在"从英文换成中文后,孤儿判据必须照样命中:
|
||||
* 这两句是 {@code isOrphanJobFailure} 的匹配串,漏了这些作业就会对着已经不存在的任务白重试 5 次。
|
||||
*/
|
||||
@Test
|
||||
void orphanTaskNotFoundChineseMessage() throws Exception {
|
||||
TaskResultFileJobWorker worker = buildWorker();
|
||||
TaskFileJobEntity job = job("SHOP_MATCH", 3L, 13L);
|
||||
allowClaim(job);
|
||||
allowLock(job.getModuleType(), job.getTaskId());
|
||||
doThrow(new BusinessException("任务不存在")).when(shopMatch).processResultFileJob(job);
|
||||
|
||||
worker.process(job);
|
||||
|
||||
verify(taskFileJobService).markFailedPermanent(job, "任务不存在");
|
||||
}
|
||||
|
||||
@Test
|
||||
void orphanResultRecordNotFoundChineseMessage() throws Exception {
|
||||
TaskResultFileJobWorker worker = buildWorker();
|
||||
TaskFileJobEntity job = job("SHOP_MATCH", 4L, 14L);
|
||||
allowClaim(job);
|
||||
allowLock(job.getModuleType(), job.getTaskId());
|
||||
doThrow(new BusinessException("结果记录不存在")).when(shopMatch).processResultFileJob(job);
|
||||
|
||||
worker.process(job);
|
||||
|
||||
verify(taskFileJobService).markFailedPermanent(job, "结果记录不存在");
|
||||
}
|
||||
|
||||
@Test
|
||||
void orphanChineseMessages() throws Exception {
|
||||
TaskResultFileJobWorker worker = buildWorker();
|
||||
|
||||
+3
-3
@@ -125,7 +125,7 @@ class TaskResultFileJobWorkerOwnerRoutingTest {
|
||||
TaskFileJobEntity claim = claimedJob(PublishTaskService.MODULE_TYPE, 18524L, 26402L, "task:26402:owner:instance-a");
|
||||
when(taskFileJobService.claimRunnableJobsForOwner(20, "instance-a")).thenReturn(List.of(claim));
|
||||
when(taskFileJobService.activateRunningClaim(claim)).thenReturn(true);
|
||||
when(localDispatcher.dispatchClaimed(claim.getId(), claim.getTaskId(), claim.getModuleType())).thenReturn(false);
|
||||
when(localDispatcher.dispatchClaimed(claim)).thenReturn(false);
|
||||
when(taskDistributedLockService.acquire(PublishTaskService.MODULE_TYPE, claim.getTaskId(),
|
||||
TaskDistributedLockService.DEFAULT_WAIT_MILLIS)).thenReturn(lock);
|
||||
|
||||
@@ -142,11 +142,11 @@ class TaskResultFileJobWorkerOwnerRoutingTest {
|
||||
when(instanceMetadata.getInstanceId()).thenReturn("instance-a");
|
||||
TaskFileJobEntity claim = claimedJob(PublishTaskService.MODULE_TYPE, 18525L, 26403L, "task:26403:owner:instance-a");
|
||||
when(taskFileJobService.claimRunnableJobsForOwner(20, "instance-a")).thenReturn(List.of(claim));
|
||||
when(localDispatcher.dispatchClaimed(claim.getId(), claim.getTaskId(), claim.getModuleType())).thenReturn(true);
|
||||
when(localDispatcher.dispatchClaimed(claim)).thenReturn(true);
|
||||
|
||||
worker.runPendingJobs();
|
||||
|
||||
verify(localDispatcher).dispatchClaimed(claim.getId(), claim.getTaskId(), claim.getModuleType());
|
||||
verify(localDispatcher).dispatchClaimed(claim);
|
||||
verify(localDispatcher, never()).dispatch(anyLong(), anyLong(), any());
|
||||
verify(taskFileJobService, never()).claimRunning(anyLong());
|
||||
verify(publish, never()).processResultFileJob(any());
|
||||
|
||||
Reference in New Issue
Block a user