From 121ae9b06413cba24cae6a135a91149e1990ec3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E8=87=AA=E8=BE=BE?= <980324341@qq.com> Date: Sun, 18 Oct 2026 19:09:48 +0800 Subject: [PATCH] =?UTF-8?q?test(phase-3):=20=E9=A6=96=E8=BF=81=E5=89=8D?= =?UTF-8?q?=E6=8A=8A=20shopmatch=EF=BC=8Fpricetrack=20=E7=9A=84=E5=9B=9E?= =?UTF-8?q?=E4=BC=A0=E6=AE=B5=E8=90=BD=E5=BA=93=E9=92=89=E6=88=90=20golden?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 蓝图 §5 Phase 0 收口那句「submitResult 出结果段本轮未打快照,Phase 3 要动那一段须先补再动」 是这一笔的全部理由:首迁要替换的正是这一段(取锁→准入→按店名建索引→逐行合并→changed 闸门 →重算状态换成 AbstractShopPayloadTaskPipeline 的模板),没有迁移前的流水,「对外行为未变」就只能嘴上说。 快照渲染的是**调用顺序流水**而不是分组清单——回传段的风险恰恰在副作用先后:先标失败还是先挂文件、 mergedRemove 在不在 enqueue 之后、changed=false 时任务表一次都不许写、taskId 非法时锁一次都不该被碰。 结果行是一份内存表(重读看得见前一次写入),店铺合并载荷是一份 HashMap 接住 get/save/remove, 于是分片回传的合并语义是真的,不是把合并跳过。时钟、雪花 id、OSS 直链一律不进快照。 两支各自 15 例,差异逐条钉住(这些正是迁移时最容易被「顺手统一」掉的):ShopMatch 闸门是 「行数>0 且有 done」,所以 :689 那句「未收到可用于组装的结果数据」在 submitResult 路径上不可达 (只有 tryFinalizeTask 的判序到得了);PriceTrack 闸门只看 success==TRUE,那条可达, 且它多一段排队前的服务端改价次数(基线读不到一律从 0 起算)、终态多一件循环轮次同步、 错误分隔符是半角 "; " 而 ShopMatch 是全角「;」。 golden 生成器用 @EnabledIfEnvironmentVariable 而非 -D:pom 的 是显式配置, 命令行 -DargLine 覆盖不动它,系统属性传不进 forked JVM——第一次跑就是这么发现「写了却没落盘」的。 --- .../PriceTrackSubmitResultSnapshotTest.java | 503 ++++++++++++++++ .../ShopMatchSubmitResultSnapshotTest.java | 540 ++++++++++++++++++ .../golden/submit-result-snapshot.txt | 101 ++++ .../golden/submit-result-snapshot.txt | 76 +++ 4 files changed, 1220 insertions(+) create mode 100644 backend-java/src/test/java/com/nanri/aiimage/modules/pricetrack/service/support/PriceTrackSubmitResultSnapshotTest.java create mode 100644 backend-java/src/test/java/com/nanri/aiimage/modules/shopmatch/service/support/ShopMatchSubmitResultSnapshotTest.java create mode 100644 backend-java/src/test/resources/pricetrack/golden/submit-result-snapshot.txt create mode 100644 backend-java/src/test/resources/shopmatch/golden/submit-result-snapshot.txt diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/pricetrack/service/support/PriceTrackSubmitResultSnapshotTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/pricetrack/service/support/PriceTrackSubmitResultSnapshotTest.java new file mode 100644 index 000000000..c8ef0874a --- /dev/null +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/pricetrack/service/support/PriceTrackSubmitResultSnapshotTest.java @@ -0,0 +1,503 @@ +package com.nanri.aiimage.modules.pricetrack.service.support; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.nanri.aiimage.common.exception.BusinessException; +import com.nanri.aiimage.common.service.ResultDownloadResolver; +import com.nanri.aiimage.config.TaskPressureProperties; +import com.nanri.aiimage.modules.file.service.LocalFileStorageService; +import com.nanri.aiimage.modules.file.service.oss.OssStorageService; +import com.nanri.aiimage.modules.pricetrack.mapper.PriceTrackShopCandidateMapper; +import com.nanri.aiimage.modules.pricetrack.model.dto.PriceTrackSubmitResultRequest; +import com.nanri.aiimage.modules.pricetrack.service.PriceTrackExcelAssemblyService; +import com.nanri.aiimage.modules.pricetrack.service.PriceTrackLoopRunService; +import com.nanri.aiimage.modules.pricetrack.service.PriceTrackTaskCacheService; +import com.nanri.aiimage.modules.pricetrack.service.PriceTrackTaskService; +import com.nanri.aiimage.modules.shopkey.service.SkipPriceAsinService; +import com.nanri.aiimage.modules.task.mapper.FileResultMapper; +import com.nanri.aiimage.modules.task.mapper.FileTaskMapper; +import com.nanri.aiimage.modules.task.model.entity.FileResultEntity; +import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity; +import com.nanri.aiimage.modules.task.service.TaskDistributedLockService; +import com.nanri.aiimage.modules.task.service.TaskFileJobService; +import com.nanri.aiimage.modules.task.service.TaskProgressLightAssembler; +import com.nanri.aiimage.modules.task.service.TaskResultPayloadService; +import com.nanri.aiimage.modules.ziniao.service.ZiniaoShopSwitchService; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; +import org.springframework.transaction.PlatformTransactionManager; + +import java.io.File; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * 跟价回传落库(段 3)+出结果排队(段 4 入口)的现网行为快照——Phase 3 首迁的护身符(孪生支之二)。 + * + *

补这一支的理由与 {@code ShopMatchSubmitResultSnapshotTest} 同:蓝图 §5 写明「{@code submitResult} + * 未打快照,Phase 3 要动那一段先补再动」,而 {@code AbstractShopPayloadTaskPipeline} 的模板 + * 恰好就是这两支的 {@code submitResult} 形状。两支**刻意不是同一份夹具**—— + * 下面这几处差异正是本支存在的理由,也是迁移时最容易被「顺手统一」掉的东西: + *

+ * + * golden:src/test/resources/pricetrack/golden/submit-result-snapshot.txt + * 重新生成:{@code SHUFU_SNAPSHOT_WRITE=true mvn -o test -Dtest=PriceTrackSubmitResultSnapshotTest} + */ +class PriceTrackSubmitResultSnapshotTest { + + private static final File GOLDEN_SUBMIT = + new File("src/test/resources/pricetrack/golden/submit-result-snapshot.txt"); + private static final long TASK_ID = 1001L; + + private static final class Fixture { + private final List events = new ArrayList<>(); + private final Map mergedPayloads = new LinkedHashMap<>(); + private final FileTaskEntity task = new FileTaskEntity(); + private final List rows = new ArrayList<>(); + private PriceTrackTaskService service; + private int heartbeats; + private int lockCloses; + + private Fixture withRows(String... shopNames) { + long id = 2001L; + for (String shopName : shopNames) { + FileResultEntity row = new FileResultEntity(); + row.setId(id++); + row.setTaskId(TASK_ID); + row.setModuleType("PRICE_TRACK"); + row.setSourceFilename(shopName); + row.setSourceFileUrl("shop-" + shopName); + row.setUserId(7L); + row.setSuccess(0); + rows.add(row); + } + return this; + } + + private Fixture withTask(String status, String moduleType) { + task.setId(TASK_ID); + task.setUserId(7L); + task.setModuleType(moduleType); + task.setStatus(status); + // 本支的 loadTaskForExecution 要求缓存行 requestJson 非空才认这条缓存(蓝图 §1A 点名的非同构变体) + task.setRequestJson("{}"); + return this; + } + + private PriceTrackTaskService service() { + if (service != null) { + return service; + } + FileTaskMapper fileTaskMapper = mock(FileTaskMapper.class); + FileResultMapper fileResultMapper = mock(FileResultMapper.class); + PriceTrackTaskCacheService cacheService = mock(PriceTrackTaskCacheService.class); + ZiniaoShopSwitchService ziniao = mock(ZiniaoShopSwitchService.class); + PriceTrackExcelAssemblyService assembly = mock(PriceTrackExcelAssemblyService.class); + TaskResultPayloadService payloadService = mock(TaskResultPayloadService.class); + TaskFileJobService fileJobService = mock(TaskFileJobService.class); + TaskDistributedLockService lockService = mock(TaskDistributedLockService.class); + PriceTrackLoopRunService loopRunService = mock(PriceTrackLoopRunService.class); + TaskDistributedLockService.LockHandle lock = mock(TaskDistributedLockService.LockHandle.class); + + when(fileResultMapper.selectList(any())).thenAnswer(invocation -> List.copyOf(rows)); + doAnswer(invocation -> { + events.add("result.update: " + renderRow(invocation.getArgument(0))); + return 1; + }).when(fileResultMapper).updateById(any(FileResultEntity.class)); + doAnswer(invocation -> { + events.add("task.update: " + renderTask(invocation.getArgument(0))); + return 1; + }).when(fileTaskMapper).updateById(any(FileTaskEntity.class)); + + when(lockService.acquire(anyString(), anyLong())).thenReturn(lock); + doAnswer(invocation -> { + lockCloses++; + return null; + }).when(lock).close(); + + when(cacheService.getTaskCacheBatch(any())).thenAnswer(invocation -> Map.of(TASK_ID, task)); + doAnswer(invocation -> { + heartbeats++; + return null; + }).when(cacheService).touchTaskHeartbeat(any()); + when(cacheService.getShopMergedPayload(anyLong(), anyString())) + .thenAnswer(invocation -> mergedPayloads.get(invocation.getArgument(1))); + doAnswer(invocation -> { + String shopKey = invocation.getArgument(1); + PriceTrackSubmitResultRequest.ShopResult payload = invocation.getArgument(2); + mergedPayloads.put(shopKey, payload); + events.add("mergedSave: taskId=" + invocation.getArgument(0) + + " shopKey=" + shopKey + " " + renderPayload(payload)); + return null; + }).when(cacheService).saveShopMergedPayload(anyLong(), anyString(), any()); + doAnswer(invocation -> { + events.add("mergedRemove: taskId=" + invocation.getArgument(0) + + " shopKey=" + invocation.getArgument(1)); + mergedPayloads.remove(invocation.getArgument(1)); + return null; + }).when(cacheService).removeShopMergedPayload(anyLong(), anyString()); + doAnswer(invocation -> { + events.add("taskCacheDelete: taskId=" + invocation.getArgument(0)); + return null; + }).when(cacheService).deleteTaskCache(any()); + + doAnswer(invocation -> { + events.add("payloadSave: taskId=" + invocation.getArgument(0) + + " moduleType=" + invocation.getArgument(1) + + " scopeKey=" + invocation.getArgument(2) + + " " + renderPayload(invocation.getArgument(3))); + return null; + }).when(payloadService).saveLatest(anyLong(), anyString(), anyString(), any()); + // 原 ASIN 载荷读不到 ⇒ 改价次数基线一律 0(本支另一条同名用例钉的就是这条回落) + when(payloadService.getLatest(anyLong(), anyString(), anyString(), any())).thenReturn(null); + when(fileJobService.findAssembleJobsByResultIds(anyString(), any())).thenReturn(Map.of()); + when(fileJobService.findAssembleJob(any(), anyString(), any())).thenReturn(null); + doAnswer(invocation -> { + events.add("enqueue: taskId=" + invocation.getArgument(0) + + " moduleType=" + invocation.getArgument(1) + + " resultId=" + invocation.getArgument(2) + + " scopeKey=" + invocation.getArgument(3)); + return null; + }).when(fileJobService).enqueueAssembleResult(anyLong(), anyString(), anyLong(), anyString()); + doAnswer(invocation -> { + events.add("loopRunSync: taskId=" + invocation.getArgument(0)); + return null; + }).when(loopRunService).syncLoopRunAfterChildTerminal(anyLong()); + + when(ziniao.normalizeShopName(any())).thenAnswer(invocation -> { + String shopName = invocation.getArgument(0); + return shopName == null ? "" : shopName.trim(); + }); + when(assembly.normalizeCountriesMap(any())).thenAnswer(invocation -> invocation.getArgument(0)); + when(assembly.countRows(any())).thenAnswer(invocation -> { + Map> countries = invocation.getArgument(0); + int count = 0; + if (countries != null) { + for (List list : countries.values()) { + count += list == null ? 0 : list.size(); + } + } + return count; + }); + + service = new PriceTrackTaskService(fileTaskMapper, fileResultMapper, + mock(PriceTrackShopCandidateMapper.class), ziniao, mock(OssStorageService.class), + mock(ResultDownloadResolver.class), new ObjectMapper(), mock(SkipPriceAsinService.class), + assembly, cacheService, loopRunService, mock(TaskPressureProperties.class), + payloadService, fileJobService, lockService, mock(TaskProgressLightAssembler.class), + mock(LocalFileStorageService.class), mock(PlatformTransactionManager.class)); + return service; + } + } + + private static String renderRow(FileResultEntity row) { + return "id=" + row.getId() + + " success=" + row.getSuccess() + + " error=" + dash(row.getErrorMessage()) + + " filename=" + dash(row.getResultFilename()) + + " urlPresent=" + (row.getResultFileUrl() != null && !row.getResultFileUrl().isBlank()) + + " size=" + row.getResultFileSize() + + " contentType=" + dash(row.getResultContentType()) + + " rowCount=" + row.getRowCount(); + } + + private static String renderTask(FileTaskEntity task) { + return "id=" + task.getId() + + " status=" + dash(task.getStatus()) + + " ok=" + task.getSuccessFileCount() + + " fail=" + task.getFailedFileCount() + + " sourceFileCount=" + task.getSourceFileCount() + + " error=" + dash(task.getErrorMessage()) + + " resultJsonItems=" + (task.getResultJson() == null ? -1 + : count(task.getResultJson(), "\"resultId\"")) + + " finishedPresent=" + (task.getFinishedAt() != null); + } + + private static String renderPayload(Object payload) { + if (!(payload instanceof PriceTrackSubmitResultRequest.ShopResult result)) { + return "payload=" + (payload == null ? "" : "<" + payload.getClass().getSimpleName() + ">"); + } + StringBuilder sb = new StringBuilder(); + sb.append("shopName=").append(dash(result.getShopName())) + .append(" error=").append(dash(result.getError())) + .append(" success=").append(dash(result.getSuccess())); + Map> countries = result.getCountries(); + sb.append(" countries=").append(countries == null ? 0 : countries.size()); + int rows = 0; + if (countries != null) { + for (Map.Entry> entry : countries.entrySet()) { + List list = entry.getValue(); + rows += list == null ? 0 : list.size(); + if (list == null) { + continue; + } + for (PriceTrackSubmitResultRequest.AsinResult row : list) { + sb.append("\n row: country=").append(entry.getKey()) + .append(" asin=").append(dash(row == null ? null : row.getAsin())) + .append(" price=").append(dash(row == null ? null : row.getPrice())) + .append(" changeStatus=").append(dash(row == null ? null : row.getPriceChangeStatus())) + .append(" modifyCount=").append(dash(row == null ? null : row.getModifyCount())); + } + } + } + return sb.append(" rows=").append(rows).toString(); + } + + private static String dash(Object value) { + String text = value == null ? "" : String.valueOf(value); + return text.isBlank() ? "-" : text; + } + + private static int count(String text, String needle) { + int occurrences = 0; + int index = text.indexOf(needle); + while (index >= 0) { + occurrences++; + index = text.indexOf(needle, index + needle.length()); + } + return occurrences; + } + + // ------------------------------------------------------------------ + // 请求构造 + // ------------------------------------------------------------------ + + private static PriceTrackSubmitResultRequest.AsinResult asinRow(String asin, String price, String changeStatus) { + PriceTrackSubmitResultRequest.AsinResult row = new PriceTrackSubmitResultRequest.AsinResult(); + row.setShopMallName("店铺商城名"); + row.setAsin(asin); + row.setPrice(price); + row.setStatus("在售"); + row.setPriceChangeStatus(changeStatus); + return row; + } + + private static PriceTrackSubmitResultRequest.ShopResult shop(String shopName, String error, Boolean success, + String country, + PriceTrackSubmitResultRequest.AsinResult... rows) { + PriceTrackSubmitResultRequest.ShopResult result = new PriceTrackSubmitResultRequest.ShopResult(); + result.setShopName(shopName); + result.setError(error); + result.setSuccess(success); + Map> countries = new LinkedHashMap<>(); + if (country != null) { + countries.put(country, List.of(rows)); + } + result.setCountries(countries); + return result; + } + + private static PriceTrackSubmitResultRequest request(PriceTrackSubmitResultRequest.ShopResult... shops) { + PriceTrackSubmitResultRequest request = new PriceTrackSubmitResultRequest(); + request.setShops(List.of(shops)); + return request; + } + + private interface Emit { + String render() throws Exception; + } + + private static void appendCase(StringBuilder sb, String caseName, Emit emit) { + sb.append("== case=").append(caseName).append('\n'); + try { + sb.append(emit.render()); + } catch (Exception ex) { + sb.append("thrown: ").append(ex.getClass().getSimpleName()) + .append(" code=").append(ex instanceof BusinessException businessException + ? dash(businessException.getCode()) : "-") + .append(" message=").append(dash(ex.getMessage())).append('\n'); + } + } + + private static String counters(Fixture fixture) { + return "events=" + fixture.events.size() + + " heartbeatTouches=" + fixture.heartbeats + + " lockCloses=" + fixture.lockCloses + '\n'; + } + + private static String submit(Fixture fixture, PriceTrackSubmitResultRequest request) { + fixture.service().submitResult(TASK_ID, request); + StringBuilder sb = new StringBuilder(); + fixture.events.forEach(line -> sb.append(line).append('\n')); + return sb.append(counters(fixture)).toString(); + } + + private static Fixture runningFixture() { + Fixture fixture = new Fixture(); + fixture.withRows("店铺A", "店铺B").withTask("RUNNING", "PRICE_TRACK"); + return fixture; + } + + public static String runAll() throws Exception { + StringBuilder sb = new StringBuilder(); + + // 1. 单店跑完:合并→改价次数→载荷落库→行标「文件名已定、文件待组装」→Job 排队→清理合并载荷 + // 任务表留在 RUNNING(Job 未落地不许提前终态);改价成功那行 modifyCount 从 0 基线抬到 1 + Fixture happy = runningFixture(); + appendCase(sb, "happyShop", () -> submit(happy, request( + shop("店铺A", null, true, "UK", asinRow("B001", "19.90", "改价成功"))))); + + // 2. 回传的店铺不在任务行里 ⇒ changed=false ⇒ 任务表一次都不许写 + Fixture unknown = runningFixture(); + appendCase(sb, "unknownShopWritesNothing", () -> submit(unknown, request( + shop("店铺Z", null, true, "UK", asinRow("B009", "1.00", null))))); + + // 3. success=false(没有 error)⇒ 走失败收尾,但已有行仍产出部分结果(error 文案是硬编码 "shop processing failed") + Fixture softFail = runningFixture(); + appendCase(sb, "successFalseWithRowsProducesPartial", () -> submit(softFail, request( + shop("店铺A", null, false, "UK", asinRow("B001", "19.90", null))))); + + // 4. error 非空 ⇒ 第一道闸门就拦下(合并仍发生在前,因为 finalizeFailedShop 里还要 merge 一次) + Fixture errored = runningFixture(); + appendCase(sb, "errorWithRows", () -> submit(errored, request( + shop("店铺A", "店铺登录失败", true, "UK", asinRow("B001", "19.90", null))))); + + // 5. error 且一行都没有 ⇒ 只标失败、不排队(文件名必须清空) + Fixture erroredEmpty = runningFixture(); + appendCase(sb, "errorWithoutRows", () -> submit(erroredEmpty, request( + shop("店铺A", "店铺登录失败", true, null)))); + + // 6. success=true 但 ASIN 全空 ⇒ 本支可达(闸门只看 success,不看行数)⇒ 落「未收到有效跟价数据」 + Fixture noUsable = runningFixture(); + appendCase(sb, "completedWithoutUsableRows", () -> submit(noUsable, request( + shop("店铺A", null, true, "DE", asinRow("", "1.00", null))))); + + // 7. error 是纯空白:第一道判据不认(isBlank),落到无可用行那条文案 + // ——钉住「buildNoUsableRowsMessage 里读 payload.error 的那一支在现网拿不到非空白 error」 + Fixture blankError = runningFixture(); + appendCase(sb, "blankErrorFallsBackToNoUsableRowsMessage", () -> submit(blankError, request( + shop("店铺A", " ", true, "DE", asinRow("", "1.00", null))))); + + // 8. 有行但 success 缺失 ⇒ 等更多分片:只有 mergedSave,没有排队、没有清理 + Fixture waiting = runningFixture(); + appendCase(sb, "chunkWithoutSuccessWaits", () -> submit(waiting, request( + shop("店铺A", null, null, "UK", asinRow("B001", "19.90", null))))); + + // 9. 同一店铺分两片:第一片没 success、第二片带同一 ASIN 的价格并 success=true + // 合并必须读缓存里那份(第二片不许把第一片的行丢掉),改价次数在排队前按合并后的行重算 + Fixture chunked = runningFixture(); + chunked.service().submitResult(TASK_ID, request( + shop("店铺A", null, null, "UK", asinRow("B001", null, null)))); + chunked.events.clear(); + appendCase(sb, "chunkedMergeAcrossTwoSubmissions", () -> submit(chunked, request( + shop("店铺A", null, true, "UK", asinRow("B001", "19.90", "UPDATED"))))); + + // 10. 删除跳过 ASIN 的信号只是日志(今天不回传任何写副作用),不得改变本行结局 + Fixture signal = runningFixture(); + PriceTrackSubmitResultRequest.AsinResult deleteSignal = asinRow("B002", "8.00", null); + deleteSignal.setDeleteSkipAsin(true); + deleteSignal.setRemoveAsin("B002"); + deleteSignal.setDeleteReason("已下架"); + appendCase(sb, "skipAsinDeleteSignalChangesNothing", () -> submit(signal, request( + shop("店铺A", null, true, "UK", deleteSignal)))); + + // 11. 两店都 success=false 且无行 ⇒ 每行都有交代 ⇒ FAILED(半角 "; " 连接) + // + sourceFileCount 被重算 + 缓存清理 + 循环轮次同步(ShopMatch 没有最后这一件) + Fixture bothFailed = runningFixture(); + appendCase(sb, "allShopsFailedFinalizesTask", () -> submit(bothFailed, request( + shop("店铺A", null, false, null), + shop("店铺B", null, false, null)))); + + // 12. 任务已终态 ⇒ 40901(幂等忽略并停止重试) + Fixture terminal = new Fixture(); + terminal.withRows("店铺A").withTask("SUCCESS", "PRICE_TRACK"); + appendCase(sb, "terminalTaskRejectedWith40901", () -> submit(terminal, request( + shop("店铺A", null, true, "UK", asinRow("B001", "1.00", null))))); + + // 13. 缓存里的行属于别的模块 ⇒ 「任务不存在」(不带 code) + Fixture wrongModule = new Fixture(); + wrongModule.withRows("店铺A").withTask("RUNNING", "SHOP_DATA_CRAWL"); + appendCase(sb, "wrongModuleTaskNotFound", () -> submit(wrongModule, request( + shop("店铺A", null, true, "UK", asinRow("B001", "1.00", null))))); + + // 14. taskId 非法 ⇒ 抢锁之前就拒:锁一次都不该被碰 + Fixture badId = runningFixture(); + badId.service(); + appendCase(sb, "invalidTaskIdRejectedBeforeLock", () -> { + StringBuilder inner = new StringBuilder(); + try { + badId.service.submitResult(0L, request(shop("店铺A", null, true, "UK", + asinRow("B001", "1.00", null)))); + inner.append("thrown: none\n"); + } catch (Exception ex) { + inner.append("thrown: ").append(ex.getClass().getSimpleName()) + .append(" code=").append(ex instanceof BusinessException businessException + ? dash(businessException.getCode()) : "-") + .append(" message=").append(dash(ex.getMessage())).append('\n'); + } + return inner.append(counters(badId)).toString(); + }); + + // 15. shops 为空 / 整个请求为 null ⇒ 同一句「shops 不能为空」,且都在抢锁之前 + Fixture emptyShops = runningFixture(); + appendCase(sb, "emptyShops", () -> submit(emptyShops, request())); + Fixture nullRequest = runningFixture(); + appendCase(sb, "nullRequest", () -> submit(nullRequest, null)); + + return sb.toString(); + } + + private static String read(File file) throws Exception { + return Files.readString(file.toPath(), StandardCharsets.UTF_8).replace("\r\n", "\n"); + } + + @Test + void submit_result_snapshot_matches_golden() throws Exception { + assertEquals(read(GOLDEN_SUBMIT), runAll(), "回传段落库流水与 golden 一致"); + } + + @Test + void submit_result_snapshot_is_reproducible() throws Exception { + assertEquals(runAll(), runAll(), "同一夹具跑两次流水一致(时钟与雪花 id 不得进快照)"); + } + + @Test + void submit_result_snapshot_golden_is_committed() { + assertTrue(GOLDEN_SUBMIT.isFile(), "golden 文件必须存在并入库: " + GOLDEN_SUBMIT.getAbsolutePath()); + assertTrue(GOLDEN_SUBMIT.length() > 0, "golden 文件非空"); + } + + @Test + void submit_result_snapshot_tamper_is_detected() throws Exception { + String original = read(GOLDEN_SUBMIT); + assertTrue(original.contains("== case=happyShop"), "golden 内容合法"); + try { + Files.writeString(GOLDEN_SUBMIT.toPath(), original + "\n# tampered", StandardCharsets.UTF_8); + assertTrue(!read(GOLDEN_SUBMIT).equals(runAll()), "篡改 golden 后断言应失败"); + } finally { + Files.writeString(GOLDEN_SUBMIT.toPath(), original, StandardCharsets.UTF_8); + } + assertEquals(original, read(GOLDEN_SUBMIT), "恢复原始 golden"); + } + + /** 只在显式点名时写 golden:默认跑测试永远不许改是非源(pom 的 argLine 是显式配置,-D 传不进 fork,故用环境变量)。 */ + @Test + @EnabledIfEnvironmentVariable(named = "SHUFU_SNAPSHOT_WRITE", matches = "true") + void write_golden_on_demand() throws Exception { + Files.createDirectories(GOLDEN_SUBMIT.getParentFile().toPath()); + Files.writeString(GOLDEN_SUBMIT.toPath(), runAll(), StandardCharsets.UTF_8); + } +} diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/shopmatch/service/support/ShopMatchSubmitResultSnapshotTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/shopmatch/service/support/ShopMatchSubmitResultSnapshotTest.java new file mode 100644 index 000000000..5660bed7b --- /dev/null +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/shopmatch/service/support/ShopMatchSubmitResultSnapshotTest.java @@ -0,0 +1,540 @@ +package com.nanri.aiimage.modules.shopmatch.service.support; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.databind.json.JsonMapper; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import com.nanri.aiimage.common.exception.BusinessException; +import com.nanri.aiimage.common.service.ResultDownloadResolver; +import com.nanri.aiimage.config.TaskPressureProperties; +import com.nanri.aiimage.modules.file.service.oss.OssStorageService; +import com.nanri.aiimage.modules.productrisk.model.enums.ProductRiskCountryCode; +import com.nanri.aiimage.modules.shopkey.mapper.QueryAsinMapper; +import com.nanri.aiimage.modules.shopkey.service.SkipPriceAsinService; +import com.nanri.aiimage.modules.shopmatch.mapper.ShopMatchShopCandidateMapper; +import com.nanri.aiimage.modules.shopmatch.model.dto.ShopMatchCreateTaskRequest; +import com.nanri.aiimage.modules.shopmatch.model.dto.ShopMatchRowDto; +import com.nanri.aiimage.modules.shopmatch.model.dto.ShopMatchShopPayloadDto; +import com.nanri.aiimage.modules.shopmatch.model.dto.ShopMatchSubmitResultRequest; +import com.nanri.aiimage.modules.shopmatch.model.dto.ShopMatchTaskItemDto; +import com.nanri.aiimage.modules.shopmatch.service.ShopMatchExcelAssemblyService; +import com.nanri.aiimage.modules.shopmatch.service.ShopMatchTaskCacheService; +import com.nanri.aiimage.modules.shopmatch.service.ShopMatchTaskService; +import com.nanri.aiimage.modules.task.mapper.FileResultMapper; +import com.nanri.aiimage.modules.task.mapper.FileTaskMapper; +import com.nanri.aiimage.modules.task.model.entity.FileResultEntity; +import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity; +import com.nanri.aiimage.modules.task.service.TaskDistributedLockService; +import com.nanri.aiimage.modules.task.service.TaskFileJobService; +import com.nanri.aiimage.modules.task.service.TaskProgressLightAssembler; +import com.nanri.aiimage.modules.task.service.TaskResultPayloadService; +import com.nanri.aiimage.modules.ziniao.service.ZiniaoShopSwitchService; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; +import org.springframework.transaction.PlatformTransactionManager; + +import java.io.File; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * 匹配店铺回传落库(段 3)+出结果排队(段 4 入口)的现网行为快照——Phase 3 首迁的护身符。 + * + *

为什么补这一支。Phase 0 的 {@link ShopMatchSnapshotTest} 只锁到「建单+解析+载荷」, + * 蓝图 §5 收口实录明文写着「{@code submitResult} 出结果段本轮未打快照——Phase 3 要动那一段,先补再动」。 + * Phase 3 首迁要改的正是那一段(取锁→准入→按店名建索引→逐行合并→changed 闸门→重算状态换成 + * {@code AbstractShopPayloadTaskPipeline.submitShopScopedResults}),所以这一支必须落在迁移**之前**: + * 迁移后 golden 逐字节不动,才谈得上「对外行为未变」。 + * + *

快照吃的是事件流水,不是分组清单。建单快照按类型分组渲染够用(建单只写一次库); + * 回传段的风险恰恰在副作用**先后**:先标失败还是先挂文件、mergedRemove 在不在 enqueue 之后、 + * changed=false 时任务表一次都不许写。所以这里把 result.update / task.update / payloadSave / + * enqueue / mergedSave / mergedRemove / taskCacheSave / taskCacheDelete 全按真实调用顺序落成一条流水, + * 顺序换了就红——那正是「抽公共段」最容易悄悄改掉的东西。 + * + *

替身只实现这条链真吃到的部分:结果行是一份内存表({@code selectList} 每次返回同一批活实体, + * 所以重读天然看见上一次 update 的写入——模拟的是同一次调用内的重读,不是数据库隔离级别); + * 店铺合并载荷是一份 HashMap 接住 get/save/remove,于是分片回传的合并语义是真的 + * (第二片确实读得到第一片),不是把合并跳过。时钟、雪花 id、OSS 直链一律不进快照。 + * + * golden:src/test/resources/shopmatch/golden/submit-result-snapshot.txt + * 重新生成:{@code SHUFU_SNAPSHOT_WRITE=true mvn -o test -Dtest=ShopMatchSubmitResultSnapshotTest} + * (用环境变量而不是 -D:pom 里 {@code } 是显式配置,命令行 -DargLine 覆盖不动它, + * 系统属性传不进 fork;forked JVM 继承父进程环境,所以这条才真能点名) + * (只在被迁支改动**之前**生成一次,此后这条 golden 就是不可动的是非源) + */ +class ShopMatchSubmitResultSnapshotTest { + + private static final File GOLDEN_SUBMIT = + new File("src/test/resources/shopmatch/golden/submit-result-snapshot.txt"); + private static final long TASK_ID = 1001L; + + /** Spring 注入的那只 ObjectMapper ≠ {@code new ObjectMapper()}:Boot 会挂上 jsr310(Phase 0 实测教训)。 */ + private static ObjectMapper bootLikeObjectMapper() { + return JsonMapper.builder() + .addModule(new JavaTimeModule()) + .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) + .build(); + } + + // ------------------------------------------------------------------ + // 夹具 + // ------------------------------------------------------------------ + + private static final class Fixture { + private final ObjectMapper objectMapper = bootLikeObjectMapper(); + private final List events = new ArrayList<>(); + private final Map mergedPayloads = new LinkedHashMap<>(); + private final FileTaskEntity task = new FileTaskEntity(); + private final List rows = new ArrayList<>(); + private ShopMatchTaskService service; + private int heartbeats; + private int lockCloses; + + private Fixture withRows(String... shopNames) { + long id = 2001L; + for (String shopName : shopNames) { + FileResultEntity row = new FileResultEntity(); + row.setId(id++); + row.setTaskId(TASK_ID); + row.setModuleType("SHOP_MATCH"); + row.setSourceFilename(shopName); + row.setSourceFileUrl("shop-" + shopName); + row.setUserId(7L); + row.setSuccess(0); + rows.add(row); + } + return this; + } + + private Fixture withTask(String status, String moduleType, String requestJson) { + task.setId(TASK_ID); + task.setUserId(7L); + task.setModuleType(moduleType); + task.setStatus(status); + task.setRequestJson(requestJson); + return this; + } + + private ShopMatchTaskService service() { + if (service != null) { + return service; + } + FileTaskMapper fileTaskMapper = mock(FileTaskMapper.class); + FileResultMapper fileResultMapper = mock(FileResultMapper.class); + ShopMatchTaskCacheService cacheService = mock(ShopMatchTaskCacheService.class); + ZiniaoShopSwitchService ziniao = mock(ZiniaoShopSwitchService.class); + ShopMatchExcelAssemblyService assembly = mock(ShopMatchExcelAssemblyService.class); + TaskResultPayloadService payloadService = mock(TaskResultPayloadService.class); + TaskFileJobService fileJobService = mock(TaskFileJobService.class); + TaskDistributedLockService lockService = mock(TaskDistributedLockService.class); + TaskDistributedLockService.LockHandle lock = mock(TaskDistributedLockService.LockHandle.class); + + when(fileResultMapper.selectList(any())).thenAnswer(invocation -> List.copyOf(rows)); + doAnswer(invocation -> { + events.add("result.update: " + renderRow(invocation.getArgument(0))); + return 1; + }).when(fileResultMapper).updateById(any(FileResultEntity.class)); + doAnswer(invocation -> { + events.add("task.update: " + renderTask(invocation.getArgument(0))); + return 1; + }).when(fileTaskMapper).updateById(any(FileTaskEntity.class)); + + when(lockService.acquire(anyString(), anyLong())).thenReturn(lock); + doAnswer(invocation -> { + lockCloses++; + return null; + }).when(lock).close(); + + when(cacheService.getTaskCacheBatch(any())).thenAnswer(invocation -> Map.of(TASK_ID, task)); + doAnswer(invocation -> { + heartbeats++; + return null; + }).when(cacheService).touchTaskHeartbeat(any()); + when(cacheService.getShopMergedPayload(anyLong(), anyString())) + .thenAnswer(invocation -> mergedPayloads.get(invocation.getArgument(1))); + doAnswer(invocation -> { + String shopKey = invocation.getArgument(1); + ShopMatchShopPayloadDto payload = invocation.getArgument(2); + mergedPayloads.put(shopKey, payload); + events.add("mergedSave: taskId=" + invocation.getArgument(0) + + " shopKey=" + shopKey + " " + renderPayload(payload)); + return null; + }).when(cacheService).saveShopMergedPayload(anyLong(), anyString(), any()); + doAnswer(invocation -> { + events.add("mergedRemove: taskId=" + invocation.getArgument(0) + + " shopKey=" + invocation.getArgument(1)); + mergedPayloads.remove(invocation.getArgument(1)); + return null; + }).when(cacheService).removeShopMergedPayload(anyLong(), anyString()); + doAnswer(invocation -> { + events.add("taskCacheSave: id=" + ((FileTaskEntity) invocation.getArgument(0)).getId()); + return null; + }).when(cacheService).saveTaskCache(any(FileTaskEntity.class)); + doAnswer(invocation -> { + events.add("taskCacheDelete: taskId=" + invocation.getArgument(0)); + return null; + }).when(cacheService).deleteTaskCache(any()); + + doAnswer(invocation -> { + events.add("payloadSave: taskId=" + invocation.getArgument(0) + + " moduleType=" + invocation.getArgument(1) + + " scopeKey=" + invocation.getArgument(2) + + " " + renderPayload(invocation.getArgument(3))); + return null; + }).when(payloadService).saveLatest(anyLong(), anyString(), anyString(), any()); + // 组装 Job 一律「查不到」:提交时刚排队、Job 还没跑完,任务因此必须留在 RUNNING + when(fileJobService.findAssembleJobsByResultIds(anyString(), any())).thenReturn(Map.of()); + when(fileJobService.findAssembleJob(any(), anyString(), any())).thenReturn(null); + doAnswer(invocation -> { + events.add("enqueue: taskId=" + invocation.getArgument(0) + + " moduleType=" + invocation.getArgument(1) + + " resultId=" + invocation.getArgument(2) + + " scopeKey=" + invocation.getArgument(3)); + return null; + }).when(fileJobService).enqueueAssembleResult(anyLong(), anyString(), anyLong(), anyString()); + + when(ziniao.normalizeShopName(any())).thenAnswer(invocation -> { + String shopName = invocation.getArgument(0); + return shopName == null ? "" : shopName.trim(); + }); + when(assembly.normalizeCountriesMap(any())).thenAnswer(invocation -> { + Map> raw = invocation.getArgument(0); + Map> out = new LinkedHashMap<>(); + if (raw != null) { + raw.forEach((country, list) -> out.put(String.valueOf(country), list)); + } + return out; + }); + when(assembly.countRows(any())).thenAnswer(invocation -> { + Map> countries = invocation.getArgument(0); + int count = 0; + for (List list : countries.values()) { + count += list == null ? 0 : list.size(); + } + return count; + }); + + service = new ShopMatchTaskService(fileTaskMapper, fileResultMapper, + mock(ShopMatchShopCandidateMapper.class), ziniao, assembly, + mock(OssStorageService.class), mock(ResultDownloadResolver.class), objectMapper, + cacheService, mock(TaskPressureProperties.class), payloadService, fileJobService, + lockService, mock(SkipPriceAsinService.class), mock(QueryAsinMapper.class), + mock(TaskProgressLightAssembler.class), mock(PlatformTransactionManager.class)); + return service; + } + } + + // ------------------------------------------------------------------ + // 渲染(时钟/雪花 id/OSS 直链不进快照) + // ------------------------------------------------------------------ + + private static String renderRow(FileResultEntity row) { + return "id=" + row.getId() + + " success=" + row.getSuccess() + + " error=" + dash(row.getErrorMessage()) + + " filename=" + dash(row.getResultFilename()) + + " urlPresent=" + (row.getResultFileUrl() != null && !row.getResultFileUrl().isBlank()) + + " size=" + row.getResultFileSize() + + " contentType=" + dash(row.getResultContentType()) + + " rowCount=" + row.getRowCount(); + } + + private static String renderTask(FileTaskEntity task) { + return "id=" + task.getId() + + " status=" + dash(task.getStatus()) + + " ok=" + task.getSuccessFileCount() + + " fail=" + task.getFailedFileCount() + + " sourceFileCount=" + task.getSourceFileCount() + + " error=" + dash(task.getErrorMessage()) + + " resultJsonItems=" + (task.getResultJson() == null ? -1 + : count(task.getResultJson(), "\"resultId\"")) + + " finishedPresent=" + (task.getFinishedAt() != null); + } + + private static String renderPayload(Object payload) { + if (!(payload instanceof ShopMatchShopPayloadDto dto)) { + return "payload=" + (payload == null ? "" : "<" + payload.getClass().getSimpleName() + ">"); + } + int countries = dto.getCountries() == null ? 0 : dto.getCountries().size(); + int rows = 0; + if (dto.getCountries() != null) { + for (List list : dto.getCountries().values()) { + rows += list == null ? 0 : list.size(); + } + } + int skipDetails = dto.getSkipAsinDetailsByCountry() == null ? 0 : dto.getSkipAsinDetailsByCountry().size(); + return "shopName=" + dash(dto.getShopName()) + " error=" + dash(dto.getError()) + + " countries=" + countries + " rows=" + rows + " skipDetailCountries=" + skipDetails; + } + + private static String dash(Object value) { + String text = value == null ? "" : String.valueOf(value); + return text.isBlank() ? "-" : text; + } + + private static int count(String text, String needle) { + int occurrences = 0; + int index = text.indexOf(needle); + while (index >= 0) { + occurrences++; + index = text.indexOf(needle, index + needle.length()); + } + return occurrences; + } + + // ------------------------------------------------------------------ + // 请求构造 + // ------------------------------------------------------------------ + + private static ShopMatchRowDto row(String asin, String minimumPrice, boolean done) { + ShopMatchRowDto row = new ShopMatchRowDto(); + row.setAsin(asin); + row.setMinimumPrice(minimumPrice); + row.setStatus("在售"); + row.setDone(done); + return row; + } + + private static ShopMatchShopPayloadDto shop(String shopName, String error, + ProductRiskCountryCode country, ShopMatchRowDto... rows) { + ShopMatchShopPayloadDto dto = new ShopMatchShopPayloadDto(); + dto.setShopName(shopName); + dto.setError(error); + Map> countries = new LinkedHashMap<>(); + if (country != null) { + countries.put(country, List.of(rows)); + } + dto.setCountries(countries); + return dto; + } + + private static ShopMatchSubmitResultRequest request(ShopMatchShopPayloadDto... shops) { + ShopMatchSubmitResultRequest request = new ShopMatchSubmitResultRequest(); + request.setShops(List.of(shops)); + return request; + } + + /** 队列里两个店各带一条明细:{@code mergeQueueFieldsFromRequest} 会按归一化店名回填这些字段。 */ + private static String requestJsonOf(ObjectMapper objectMapper, List shopNames) throws Exception { + ShopMatchCreateTaskRequest persisted = new ShopMatchCreateTaskRequest(); + persisted.setUserId(7L); + List items = new ArrayList<>(); + for (String shopName : shopNames) { + ShopMatchTaskItemDto item = new ShopMatchTaskItemDto(); + item.setShopName(shopName); + item.setMatched(true); + item.setShopId("shop-" + shopName); + item.setPlatform("amazon"); + item.setCompanyName("公司-" + shopName); + item.setMatchedUserId(9L); + item.setMatchStatus("MATCHED"); + item.setMatchMessage(""); + items.add(item); + } + persisted.setItems(items); + persisted.setCountryCodes(List.of("UK")); + persisted.setScheduleTimes(List.of()); + return objectMapper.writeValueAsString(persisted); + } + + // ------------------------------------------------------------------ + // 用例 + // ------------------------------------------------------------------ + + private interface Emit { + String render() throws Exception; + } + + private static void appendCase(StringBuilder sb, String caseName, Emit emit) { + sb.append("== case=").append(caseName).append('\n'); + try { + sb.append(emit.render()); + } catch (Exception ex) { + sb.append("thrown: ").append(ex.getClass().getSimpleName()) + .append(" code=").append(ex instanceof BusinessException businessException + ? dash(businessException.getCode()) : "-") + .append(" message=").append(dash(ex.getMessage())).append('\n'); + } + } + + /** 一次回传的全部可观测面:调用顺序流水+心跳与锁收尾计数。 */ + private static String submit(Fixture fixture, ShopMatchSubmitResultRequest request) { + fixture.service().submitResult(TASK_ID, request); + StringBuilder sb = new StringBuilder(); + fixture.events.forEach(line -> sb.append(line).append('\n')); + return sb.append(counters(fixture)).toString(); + } + + private static String counters(Fixture fixture) { + return "events=" + fixture.events.size() + + " heartbeatTouches=" + fixture.heartbeats + + " lockCloses=" + fixture.lockCloses + '\n'; + } + + /** 每个用例一套全新夹具:默认「RUNNING + 店铺A/店铺B 两行 + 合法 requestJson」。 */ + private static Fixture runningFixture() throws Exception { + Fixture fixture = new Fixture(); + fixture.withRows("店铺A", "店铺B") + .withTask("RUNNING", "SHOP_MATCH", + requestJsonOf(fixture.objectMapper, List.of("店铺A", "店铺B"))); + return fixture; + } + + public static String runAll() throws Exception { + StringBuilder sb = new StringBuilder(); + + // 1. 两店都跑完:合并→载荷落库→行标「文件名已定、文件待组装」→Job 排队→合并载荷清理 + // → 任务表**必须**留在 RUNNING:Job 没落地就标终态,前端一停轮询、下载按钮永远不出现 + Fixture happy = runningFixture(); + appendCase(sb, "happyTwoShops", () -> submit(happy, request( + shop("店铺A", null, ProductRiskCountryCode.UK, row("B001", "19.90", true)), + shop("店铺B", null, ProductRiskCountryCode.UK, row("B002", "20.00", true))))); + + // 2. 回传的店铺不在任务行里 ⇒ changed=false ⇒ 任务表一次都不许写(心跳照打) + Fixture unknown = runningFixture(); + appendCase(sb, "unknownShopWritesNothing", () -> submit(unknown, request( + shop("店铺Z", null, ProductRiskCountryCode.UK, row("B009", "1.00", true))))); + + // 3. shops 里夹 null 与空白店名:建索引时双双跳过 ⇒ 走「没命中任何行」同一条出口 + Fixture blanks = runningFixture(); + List withNulls = new ArrayList<>(); + withNulls.add(null); + withNulls.add(shop(" ", null, ProductRiskCountryCode.UK, row("B010", "1.00", true))); + withNulls.add(null); + ShopMatchSubmitResultRequest nullish = new ShopMatchSubmitResultRequest(); + nullish.setShops(withNulls); + appendCase(sb, "nullAndBlankShopSkipped", () -> submit(blanks, nullish)); + + // 4. 店铺报错但已有可用行 ⇒ 先标失败、再把部分结果排队(preserveFailure:success 保持 0、原因保留) + Fixture partial = runningFixture(); + appendCase(sb, "failedShopWithPartialRows", () -> submit(partial, request( + shop("店铺A", "会话掉线", ProductRiskCountryCode.UK, row("B001", "19.90", false))))); + + // 5. 报错且一行都没有 ⇒ 不排队,只标失败(文件名清空,否则下载按钮指向空文件) + Fixture failedOnly = runningFixture(); + appendCase(sb, "failedShopWithoutRows", () -> submit(failedOnly, request( + shop("店铺A", "店铺打不开", null)))); + + // 6. 两个店铺都失败且都没行 ⇒ 每行都有交代 ⇒ 任务终态 FAILED + 缓存清理(分隔符是全角「;」) + Fixture bothFailed = runningFixture(); + appendCase(sb, "allShopsFailedFinalizesTask", () -> submit(bothFailed, request( + shop("店铺A", "店铺打不开", null), + shop("店铺B", "国家页超时", null)))); + + // 7. done=true 但行里 ASIN 全空 ⇒ 本支闸门是 countPayloadRows>0 && anyDone, + // 所以这条落的是「等更多分片」,**:689 那句「未收到可用于组装的结果数据」在 submitResult 路径上到不了** + // (它只在 tryFinalizeTask 的不同判序下可达)。本例把这层不可达钉住:迁移若改掉了闸门形状,这里立刻红。 + Fixture noUsable = runningFixture(); + appendCase(sb, "blankAsinWithDoneFlagStillWaits", () -> submit(noUsable, request( + shop("店铺A", null, ProductRiskCountryCode.UK, row("", "1.00", true))))); + + // 8. 有行但没 done ⇒ 闸门在「等更多分片」:不排队、不清合并载荷 + Fixture waiting = runningFixture(); + appendCase(sb, "partialChunkWaitsForMore", () -> submit(waiting, request( + shop("店铺A", null, ProductRiskCountryCode.UK, row("B001", "19.90", false))))); + + // 9. 同一店铺分两片回传:第一片没 done,第二片补同一 ASIN 的最低价并 done。 + // 合并必须发生在缓存里那份合并载荷上(不是本次入参),否则第二片会把第一片的行丢掉。 + Fixture chunked = runningFixture(); + chunked.service().submitResult(TASK_ID, request( + shop("店铺A", null, ProductRiskCountryCode.UK, row("B001", null, false)))); + chunked.events.clear(); + appendCase(sb, "chunkedMergeAcrossTwoSubmissions", () -> submit(chunked, request( + shop("店铺A", null, ProductRiskCountryCode.UK, row("B001", "19.90", true))))); + + // 10. 任务已终态 ⇒ 40901(Python 侧按幂等忽略并停止重试;换成无 code 就是另一种线协议) + Fixture terminal = new Fixture(); + terminal.withRows("店铺A").withTask("SUCCESS", "SHOP_MATCH", + requestJsonOf(terminal.objectMapper, List.of("店铺A"))); + appendCase(sb, "terminalTaskRejectedWith40901", () -> submit(terminal, request( + shop("店铺A", null, ProductRiskCountryCode.UK, row("B001", "1.00", true))))); + + // 11. 缓存里的行属于别的模块 ⇒ 「任务不存在」(不带 code,与上一例正好是两种线协议) + Fixture wrongModule = new Fixture(); + wrongModule.withRows("店铺A").withTask("RUNNING", "APPEARANCE_PATENT", + requestJsonOf(wrongModule.objectMapper, List.of("店铺A"))); + appendCase(sb, "wrongModuleTaskNotFound", () -> submit(wrongModule, request( + shop("店铺A", null, ProductRiskCountryCode.UK, row("B001", "1.00", true))))); + + // 12. taskId 非法 ⇒ 抢锁之前就拒:锁一次都不该被碰(自己吞掉异常,否则计数行渲染不出来) + Fixture badId = runningFixture(); + badId.service(); + appendCase(sb, "invalidTaskIdRejectedBeforeLock", () -> { + StringBuilder inner = new StringBuilder(); + try { + badId.service.submitResult(0L, request(shop("店铺A", null, ProductRiskCountryCode.UK, + row("B001", "1.00", true)))); + inner.append("thrown: none\n"); + } catch (Exception ex) { + inner.append("thrown: ").append(ex.getClass().getSimpleName()) + .append(" code=").append(ex instanceof BusinessException businessException + ? dash(businessException.getCode()) : "-") + .append(" message=").append(dash(ex.getMessage())).append('\n'); + } + return inner.append(counters(badId)).toString(); + }); + + // 13. shops 为空 / 整个请求为 null ⇒ 同一句「shops 不能为空」 + Fixture emptyShops = runningFixture(); + appendCase(sb, "emptyShops", () -> submit(emptyShops, request())); + Fixture nullRequest = runningFixture(); + appendCase(sb, "nullRequest", () -> submit(nullRequest, null)); + + return sb.toString(); + } + + private static String read(File file) throws Exception { + return Files.readString(file.toPath(), StandardCharsets.UTF_8).replace("\r\n", "\n"); + } + + @Test + void submit_result_snapshot_matches_golden() throws Exception { + assertEquals(read(GOLDEN_SUBMIT), runAll(), "回传段落库流水与 golden 一致"); + } + + @Test + void submit_result_snapshot_is_reproducible() throws Exception { + assertEquals(runAll(), runAll(), "同一夹具跑两次流水一致(时钟与雪花 id 不得进快照)"); + } + + @Test + void submit_result_snapshot_golden_is_committed() { + assertTrue(GOLDEN_SUBMIT.isFile(), "golden 文件必须存在并入库: " + GOLDEN_SUBMIT.getAbsolutePath()); + assertTrue(GOLDEN_SUBMIT.length() > 0, "golden 文件非空"); + } + + @Test + void submit_result_snapshot_tamper_is_detected() throws Exception { + String original = read(GOLDEN_SUBMIT); + assertTrue(original.contains("== case=happyTwoShops"), "golden 内容合法"); + try { + Files.writeString(GOLDEN_SUBMIT.toPath(), original + "\n# tampered", StandardCharsets.UTF_8); + assertTrue(!read(GOLDEN_SUBMIT).equals(runAll()), "篡改 golden 后断言应失败"); + } finally { + Files.writeString(GOLDEN_SUBMIT.toPath(), original, StandardCharsets.UTF_8); + } + assertEquals(original, read(GOLDEN_SUBMIT), "恢复原始 golden"); + } + + /** 只在显式点名时写 golden:默认跑测试永远不许改是非源(否则改坏了顺手把 golden 也改掉)。 */ + @Test + @EnabledIfEnvironmentVariable(named = "SHUFU_SNAPSHOT_WRITE", matches = "true") + void write_golden_on_demand() throws Exception { + Files.createDirectories(GOLDEN_SUBMIT.getParentFile().toPath()); + Files.writeString(GOLDEN_SUBMIT.toPath(), runAll(), StandardCharsets.UTF_8); + } +} diff --git a/backend-java/src/test/resources/pricetrack/golden/submit-result-snapshot.txt b/backend-java/src/test/resources/pricetrack/golden/submit-result-snapshot.txt new file mode 100644 index 000000000..366bbe200 --- /dev/null +++ b/backend-java/src/test/resources/pricetrack/golden/submit-result-snapshot.txt @@ -0,0 +1,101 @@ +== case=happyShop +mergedSave: taskId=1001 shopKey=店铺A shopName=店铺A error=- success=true countries=1 + row: country=UK asin=B001 price=19.90 changeStatus=改价成功 modifyCount=- rows=1 +payloadSave: taskId=1001 moduleType=PRICE_TRACK scopeKey=店铺A shopName=店铺A error=- success=true countries=1 + row: country=UK asin=B001 price=19.90 changeStatus=改价成功 modifyCount=1 rows=1 +result.update: id=2001 success=1 error=- filename=店铺A.xlsx urlPresent=false size=0 contentType=application/vnd.openxmlformats-officedocument.spreadsheetml.sheet rowCount=1 +enqueue: taskId=1001 moduleType=PRICE_TRACK resultId=2001 scopeKey=店铺A +mergedRemove: taskId=1001 shopKey=店铺A +task.update: id=1001 status=RUNNING ok=0 fail=0 sourceFileCount=2 error=- resultJsonItems=2 finishedPresent=false +events=6 heartbeatTouches=1 lockCloses=1 +== case=unknownShopWritesNothing +events=0 heartbeatTouches=1 lockCloses=1 +== case=successFalseWithRowsProducesPartial +mergedSave: taskId=1001 shopKey=店铺A shopName=店铺A error=- success=false countries=1 + row: country=UK asin=B001 price=19.90 changeStatus=- modifyCount=- rows=1 +result.update: id=2001 success=0 error=shop processing failed filename=- urlPresent=false size=0 contentType=- rowCount=0 +payloadSave: taskId=1001 moduleType=PRICE_TRACK scopeKey=店铺A shopName=店铺A error=- success=false countries=1 + row: country=UK asin=B001 price=19.90 changeStatus=- modifyCount=0 rows=1 +result.update: id=2001 success=0 error=shop processing failed filename=店铺A.xlsx urlPresent=false size=0 contentType=application/vnd.openxmlformats-officedocument.spreadsheetml.sheet rowCount=1 +enqueue: taskId=1001 moduleType=PRICE_TRACK resultId=2001 scopeKey=店铺A +mergedRemove: taskId=1001 shopKey=店铺A +task.update: id=1001 status=RUNNING ok=0 fail=0 sourceFileCount=2 error=- resultJsonItems=2 finishedPresent=false +events=7 heartbeatTouches=1 lockCloses=1 +== case=errorWithRows +mergedSave: taskId=1001 shopKey=店铺A shopName=店铺A error=店铺登录失败 success=true countries=1 + row: country=UK asin=B001 price=19.90 changeStatus=- modifyCount=- rows=1 +result.update: id=2001 success=0 error=店铺登录失败 filename=- urlPresent=false size=0 contentType=- rowCount=0 +payloadSave: taskId=1001 moduleType=PRICE_TRACK scopeKey=店铺A shopName=店铺A error=店铺登录失败 success=true countries=1 + row: country=UK asin=B001 price=19.90 changeStatus=- modifyCount=0 rows=1 +result.update: id=2001 success=0 error=店铺登录失败 filename=店铺A.xlsx urlPresent=false size=0 contentType=application/vnd.openxmlformats-officedocument.spreadsheetml.sheet rowCount=1 +enqueue: taskId=1001 moduleType=PRICE_TRACK resultId=2001 scopeKey=店铺A +mergedRemove: taskId=1001 shopKey=店铺A +task.update: id=1001 status=RUNNING ok=0 fail=0 sourceFileCount=2 error=- resultJsonItems=2 finishedPresent=false +events=7 heartbeatTouches=1 lockCloses=1 +== case=errorWithoutRows +mergedSave: taskId=1001 shopKey=店铺A shopName=店铺A error=店铺登录失败 success=true countries=0 rows=0 +result.update: id=2001 success=0 error=店铺登录失败 filename=- urlPresent=false size=0 contentType=- rowCount=0 +mergedRemove: taskId=1001 shopKey=店铺A +task.update: id=1001 status=RUNNING ok=0 fail=1 sourceFileCount=2 error=- resultJsonItems=2 finishedPresent=false +events=4 heartbeatTouches=1 lockCloses=1 +== case=completedWithoutUsableRows +mergedSave: taskId=1001 shopKey=店铺A shopName=店铺A error=- success=true countries=1 + row: country=DE asin=- price=1.00 changeStatus=- modifyCount=- rows=1 +result.update: id=2001 success=0 error=未收到有效跟价数据,未生成结果文件 filename=- urlPresent=false size=0 contentType=- rowCount=0 +mergedRemove: taskId=1001 shopKey=店铺A +task.update: id=1001 status=RUNNING ok=0 fail=1 sourceFileCount=2 error=- resultJsonItems=2 finishedPresent=false +events=4 heartbeatTouches=1 lockCloses=1 +== case=blankErrorFallsBackToNoUsableRowsMessage +mergedSave: taskId=1001 shopKey=店铺A shopName=店铺A error=- success=true countries=1 + row: country=DE asin=- price=1.00 changeStatus=- modifyCount=- rows=1 +result.update: id=2001 success=0 error=未收到有效跟价数据,未生成结果文件 filename=- urlPresent=false size=0 contentType=- rowCount=0 +mergedRemove: taskId=1001 shopKey=店铺A +task.update: id=1001 status=RUNNING ok=0 fail=1 sourceFileCount=2 error=- resultJsonItems=2 finishedPresent=false +events=4 heartbeatTouches=1 lockCloses=1 +== case=chunkWithoutSuccessWaits +mergedSave: taskId=1001 shopKey=店铺A shopName=店铺A error=- success=- countries=1 + row: country=UK asin=B001 price=19.90 changeStatus=- modifyCount=- rows=1 +task.update: id=1001 status=RUNNING ok=0 fail=0 sourceFileCount=2 error=- resultJsonItems=2 finishedPresent=false +events=2 heartbeatTouches=1 lockCloses=1 +== case=chunkedMergeAcrossTwoSubmissions +mergedSave: taskId=1001 shopKey=店铺A shopName=店铺A error=- success=true countries=1 + row: country=UK asin=B001 price=19.90 changeStatus=UPDATED modifyCount=- rows=1 +payloadSave: taskId=1001 moduleType=PRICE_TRACK scopeKey=店铺A shopName=店铺A error=- success=true countries=1 + row: country=UK asin=B001 price=19.90 changeStatus=UPDATED modifyCount=1 rows=1 +result.update: id=2001 success=1 error=- filename=店铺A.xlsx urlPresent=false size=0 contentType=application/vnd.openxmlformats-officedocument.spreadsheetml.sheet rowCount=1 +enqueue: taskId=1001 moduleType=PRICE_TRACK resultId=2001 scopeKey=店铺A +mergedRemove: taskId=1001 shopKey=店铺A +task.update: id=1001 status=RUNNING ok=0 fail=0 sourceFileCount=2 error=- resultJsonItems=2 finishedPresent=false +events=6 heartbeatTouches=2 lockCloses=2 +== case=skipAsinDeleteSignalChangesNothing +mergedSave: taskId=1001 shopKey=店铺A shopName=店铺A error=- success=true countries=1 + row: country=UK asin=B002 price=8.00 changeStatus=- modifyCount=- rows=1 +payloadSave: taskId=1001 moduleType=PRICE_TRACK scopeKey=店铺A shopName=店铺A error=- success=true countries=1 + row: country=UK asin=B002 price=8.00 changeStatus=- modifyCount=0 rows=1 +result.update: id=2001 success=1 error=- filename=店铺A.xlsx urlPresent=false size=0 contentType=application/vnd.openxmlformats-officedocument.spreadsheetml.sheet rowCount=1 +enqueue: taskId=1001 moduleType=PRICE_TRACK resultId=2001 scopeKey=店铺A +mergedRemove: taskId=1001 shopKey=店铺A +task.update: id=1001 status=RUNNING ok=0 fail=0 sourceFileCount=2 error=- resultJsonItems=2 finishedPresent=false +events=6 heartbeatTouches=1 lockCloses=1 +== case=allShopsFailedFinalizesTask +mergedSave: taskId=1001 shopKey=店铺A shopName=店铺A error=- success=false countries=0 rows=0 +result.update: id=2001 success=0 error=shop processing failed filename=- urlPresent=false size=0 contentType=- rowCount=0 +mergedRemove: taskId=1001 shopKey=店铺A +mergedSave: taskId=1001 shopKey=店铺B shopName=店铺B error=- success=false countries=0 rows=0 +result.update: id=2002 success=0 error=shop processing failed filename=- urlPresent=false size=0 contentType=- rowCount=0 +mergedRemove: taskId=1001 shopKey=店铺B +task.update: id=1001 status=FAILED ok=0 fail=2 sourceFileCount=2 error=店铺A: shop processing failed; 店铺B: shop processing failed resultJsonItems=2 finishedPresent=true +taskCacheDelete: taskId=1001 +loopRunSync: taskId=1001 +events=9 heartbeatTouches=1 lockCloses=1 +== case=terminalTaskRejectedWith40901 +thrown: BusinessException code=40901 message=任务已结束,拒绝重复提交 +== case=wrongModuleTaskNotFound +thrown: BusinessException code=- message=任务不存在 +== case=invalidTaskIdRejectedBeforeLock +thrown: BusinessException code=- message=taskId 不合法 +events=0 heartbeatTouches=0 lockCloses=0 +== case=emptyShops +thrown: BusinessException code=- message=shops 不能为空 +== case=nullRequest +thrown: BusinessException code=- message=shops 不能为空 diff --git a/backend-java/src/test/resources/shopmatch/golden/submit-result-snapshot.txt b/backend-java/src/test/resources/shopmatch/golden/submit-result-snapshot.txt new file mode 100644 index 000000000..987e3ff81 --- /dev/null +++ b/backend-java/src/test/resources/shopmatch/golden/submit-result-snapshot.txt @@ -0,0 +1,76 @@ +== case=happyTwoShops +mergedSave: taskId=1001 shopKey=店铺A shopName=店铺A error=- countries=1 rows=1 skipDetailCountries=0 +payloadSave: taskId=1001 moduleType=SHOP_MATCH scopeKey=店铺A shopName=店铺A error=- countries=1 rows=1 skipDetailCountries=0 +result.update: id=2001 success=1 error=- filename=店铺A.xlsx urlPresent=false size=0 contentType=application/vnd.openxmlformats-officedocument.spreadsheetml.sheet rowCount=1 +enqueue: taskId=1001 moduleType=SHOP_MATCH resultId=2001 scopeKey=店铺A +mergedRemove: taskId=1001 shopKey=店铺A +mergedSave: taskId=1001 shopKey=店铺B shopName=店铺B error=- countries=1 rows=1 skipDetailCountries=0 +payloadSave: taskId=1001 moduleType=SHOP_MATCH scopeKey=店铺B shopName=店铺B error=- countries=1 rows=1 skipDetailCountries=0 +result.update: id=2002 success=1 error=- filename=店铺B.xlsx urlPresent=false size=0 contentType=application/vnd.openxmlformats-officedocument.spreadsheetml.sheet rowCount=1 +enqueue: taskId=1001 moduleType=SHOP_MATCH resultId=2002 scopeKey=店铺B +mergedRemove: taskId=1001 shopKey=店铺B +task.update: id=1001 status=RUNNING ok=0 fail=0 sourceFileCount=null error=- resultJsonItems=2 finishedPresent=false +taskCacheSave: id=1001 +events=12 heartbeatTouches=1 lockCloses=1 +== case=unknownShopWritesNothing +events=0 heartbeatTouches=1 lockCloses=1 +== case=nullAndBlankShopSkipped +events=0 heartbeatTouches=1 lockCloses=1 +== case=failedShopWithPartialRows +mergedSave: taskId=1001 shopKey=店铺A shopName=店铺A error=会话掉线 countries=1 rows=1 skipDetailCountries=0 +result.update: id=2001 success=0 error=会话掉线 filename=- urlPresent=false size=0 contentType=- rowCount=0 +payloadSave: taskId=1001 moduleType=SHOP_MATCH scopeKey=店铺A shopName=店铺A error=会话掉线 countries=1 rows=1 skipDetailCountries=0 +result.update: id=2001 success=0 error=会话掉线 filename=店铺A.xlsx urlPresent=false size=0 contentType=application/vnd.openxmlformats-officedocument.spreadsheetml.sheet rowCount=1 +enqueue: taskId=1001 moduleType=SHOP_MATCH resultId=2001 scopeKey=店铺A +mergedRemove: taskId=1001 shopKey=店铺A +task.update: id=1001 status=RUNNING ok=0 fail=0 sourceFileCount=null error=- resultJsonItems=2 finishedPresent=false +taskCacheSave: id=1001 +events=8 heartbeatTouches=1 lockCloses=1 +== case=failedShopWithoutRows +mergedSave: taskId=1001 shopKey=店铺A shopName=店铺A error=店铺打不开 countries=0 rows=0 skipDetailCountries=0 +result.update: id=2001 success=0 error=店铺打不开 filename=- urlPresent=false size=0 contentType=- rowCount=0 +mergedRemove: taskId=1001 shopKey=店铺A +task.update: id=1001 status=RUNNING ok=0 fail=1 sourceFileCount=null error=- resultJsonItems=2 finishedPresent=false +taskCacheSave: id=1001 +events=5 heartbeatTouches=1 lockCloses=1 +== case=allShopsFailedFinalizesTask +mergedSave: taskId=1001 shopKey=店铺A shopName=店铺A error=店铺打不开 countries=0 rows=0 skipDetailCountries=0 +result.update: id=2001 success=0 error=店铺打不开 filename=- urlPresent=false size=0 contentType=- rowCount=0 +mergedRemove: taskId=1001 shopKey=店铺A +mergedSave: taskId=1001 shopKey=店铺B shopName=店铺B error=国家页超时 countries=0 rows=0 skipDetailCountries=0 +result.update: id=2002 success=0 error=国家页超时 filename=- urlPresent=false size=0 contentType=- rowCount=0 +mergedRemove: taskId=1001 shopKey=店铺B +task.update: id=1001 status=FAILED ok=0 fail=2 sourceFileCount=null error=店铺A: 店铺打不开;店铺B: 国家页超时 resultJsonItems=2 finishedPresent=true +taskCacheSave: id=1001 +taskCacheDelete: taskId=1001 +events=9 heartbeatTouches=1 lockCloses=1 +== case=blankAsinWithDoneFlagStillWaits +mergedSave: taskId=1001 shopKey=店铺A shopName=店铺A error=- countries=1 rows=1 skipDetailCountries=0 +task.update: id=1001 status=RUNNING ok=0 fail=0 sourceFileCount=null error=- resultJsonItems=2 finishedPresent=false +taskCacheSave: id=1001 +events=3 heartbeatTouches=1 lockCloses=1 +== case=partialChunkWaitsForMore +mergedSave: taskId=1001 shopKey=店铺A shopName=店铺A error=- countries=1 rows=1 skipDetailCountries=0 +task.update: id=1001 status=RUNNING ok=0 fail=0 sourceFileCount=null error=- resultJsonItems=2 finishedPresent=false +taskCacheSave: id=1001 +events=3 heartbeatTouches=1 lockCloses=1 +== case=chunkedMergeAcrossTwoSubmissions +mergedSave: taskId=1001 shopKey=店铺A shopName=店铺A error=- countries=1 rows=1 skipDetailCountries=0 +payloadSave: taskId=1001 moduleType=SHOP_MATCH scopeKey=店铺A shopName=店铺A error=- countries=1 rows=1 skipDetailCountries=0 +result.update: id=2001 success=1 error=- filename=店铺A.xlsx urlPresent=false size=0 contentType=application/vnd.openxmlformats-officedocument.spreadsheetml.sheet rowCount=1 +enqueue: taskId=1001 moduleType=SHOP_MATCH resultId=2001 scopeKey=店铺A +mergedRemove: taskId=1001 shopKey=店铺A +task.update: id=1001 status=RUNNING ok=0 fail=0 sourceFileCount=null error=- resultJsonItems=2 finishedPresent=false +taskCacheSave: id=1001 +events=7 heartbeatTouches=2 lockCloses=2 +== case=terminalTaskRejectedWith40901 +thrown: BusinessException code=40901 message=任务已结束,拒绝重复提交 +== case=wrongModuleTaskNotFound +thrown: BusinessException code=- message=任务不存在 +== case=invalidTaskIdRejectedBeforeLock +thrown: BusinessException code=- message=taskId 不合法 +events=0 heartbeatTouches=0 lockCloses=0 +== case=emptyShops +thrown: BusinessException code=- message=shops 不能为空 +== case=nullRequest +thrown: BusinessException code=- message=shops 不能为空