From 660a1b708439d080a6901f4c5722c3c5639d223d 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 11:52:54 +0800 Subject: [PATCH] =?UTF-8?q?test(phase-0):=201B=20=E5=9B=9B=E6=94=AF?= =?UTF-8?q?=E5=B7=A8=E7=9F=B3=E7=8E=B0=E7=BD=91=E8=A1=8C=E4=B8=BA=E5=BF=AB?= =?UTF-8?q?=E7=85=A7=E8=A1=A5=E9=BD=90=E2=80=94=E2=80=94=E5=BB=BA=E5=8D=95?= =?UTF-8?q?/=E8=A7=A3=E6=9E=90/=E8=BD=BD=E8=8D=B7=E4=B8=89=E6=AE=B5?= =?UTF-8?q?=E6=89=93=E8=BF=9B=E7=9C=9F=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 重构蓝图 Phase 0 · 1B:§1B 那句「铺到缺的四支」今天补齐——deletebrand/shopmatch/ pricetrack/collectdata 各一支 *SnapshotTest + golden .txt,沿用仓内已在用的约定 (modules/<域>/service/support/*SnapshotTest 读 src/test/resources/<域>/golden/*.txt 整串对撞, 带 output/reproducible/golden_committed/diff_detected 四件套)。 与旧四支的**关键差别**:快照打的是真服务入口(run/createTask/parseAndCreateTask 一路走到各支 私有内联上下文 DeleteBrandSheetContext 等),不是在测试里照抄一遍管线——照抄的那支对 Phase 3 迁移毫无保护(旧代码消灭了,测试还绿)。落库侧用 Mockito doAnswer 捕获 mapper 的 insert/update 入参、主键顺序发固定值,回传载荷侧捕获 saveParsedPayload/saveLatest;时钟、雪花 id、临时目录 绝对路径一律不进快照(后者擦成 /),所以 golden 跨 JVM 逐字节可比。 钉到的真实形状(举其要): - deletebrand:解析成功但店铺未匹配 ⇒ 整任务 FAILED+「全部文件解析或匹配失败」;表头行被 dataStartRowIndex=1 那条分支当数据行吃进去(ASIN="ASIN");BOM/全角归一化、按归一化键去重、 状态有值而 ASIN 空 ⇒ 丢行;非 Excel 魔数拒绝。 - shopmatch:国家代码只认 ProductRiskCountryCode 五个(**US 不在内**,报「非法国家代码: US」); 心跳只在 RUNNING 打,SCHEDULED 一支不打;建单校验全在 insert 之前 ⇒ 拒绝时一行库都没碰。 - pricetrack:模式二选一、ASIN 文件三种路径形态与「必须落在上传临时目录内」的穿越校验、 表头别名(商品ASIN/最低价→asin/minimumPrice)、按 sheet 名认国家、单国家时 sheet 名兜底。 - collectdata:无关键词列则一条不丢、关键词为空丢行计数、尾部空列不生成占位列(2026-09-26 生产事故那条同源修复)、extraJson 形状、PENDING 建单。 两处当场抓到并改掉的量具缺陷(不改就等于假绿): 1. 替身里写 `String.valueOf(invocation.getArgument(0))` 会因泛型推断挑中 valueOf(char[]) 重载, 运行时 String 强转 [C 抛 ClassCastException,把五格全打成解析失败——已改成显式 String 变量。 2. Spring 注入的 ObjectMapper 不等于 `new ObjectMapper()`:Boot 会注册 classpath 上的 jackson-datatype-jsr310。裸 mapper 下 shopmatch 的 scheduleTimes(List) 序列化失败、被建单段 catch(Exception) 吞成「task 不存在」⇒ 快照会锁在生产不存在的假形状上。 门禁:`mvn -o test` 全量 4417 passed/0 failures/7 skipped(12:42,BUILD SUCCESS); 四支新用例 17 passed;ops `pytest scripts/tests` 500 passed、`pt_worker/tests` 15 passed、 `ruff check .` All checks passed;格式步本项目未纳管 ⇒ 跳过并如实登记。 --- .../support/CollectDataSnapshotTest.java | 328 +++++++++++++++ .../support/DeleteBrandSnapshotTest.java | 394 ++++++++++++++++++ .../support/PriceTrackSnapshotTest.java | 362 ++++++++++++++++ .../support/ShopMatchSnapshotTest.java | 292 +++++++++++++ .../golden/parse-create-snapshot.txt | 38 ++ .../deletebrand/golden/run-snapshot.txt | 74 ++++ .../golden/create-task-snapshot.txt | 99 +++++ .../shopmatch/golden/create-task-snapshot.txt | 45 ++ 8 files changed, 1632 insertions(+) create mode 100644 backend-java/src/test/java/com/nanri/aiimage/modules/collectdata/service/support/CollectDataSnapshotTest.java create mode 100644 backend-java/src/test/java/com/nanri/aiimage/modules/deletebrand/service/support/DeleteBrandSnapshotTest.java create mode 100644 backend-java/src/test/java/com/nanri/aiimage/modules/pricetrack/service/support/PriceTrackSnapshotTest.java create mode 100644 backend-java/src/test/java/com/nanri/aiimage/modules/shopmatch/service/support/ShopMatchSnapshotTest.java create mode 100644 backend-java/src/test/resources/collectdata/golden/parse-create-snapshot.txt create mode 100644 backend-java/src/test/resources/deletebrand/golden/run-snapshot.txt create mode 100644 backend-java/src/test/resources/pricetrack/golden/create-task-snapshot.txt create mode 100644 backend-java/src/test/resources/shopmatch/golden/create-task-snapshot.txt diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/collectdata/service/support/CollectDataSnapshotTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/collectdata/service/support/CollectDataSnapshotTest.java new file mode 100644 index 000000000..57df95254 --- /dev/null +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/collectdata/service/support/CollectDataSnapshotTest.java @@ -0,0 +1,328 @@ +package com.nanri.aiimage.modules.collectdata.service.support; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.nanri.aiimage.modules.collectdata.mapper.CollectDataCountryPrefMapper; +import com.nanri.aiimage.modules.collectdata.mapper.CollectDataItemMapper; +import com.nanri.aiimage.modules.collectdata.model.dto.CollectDataParseRequest; +import com.nanri.aiimage.modules.collectdata.model.dto.CollectDataSourceFileDto; +import com.nanri.aiimage.modules.collectdata.model.entity.CollectDataItemEntity; +import com.nanri.aiimage.modules.collectdata.model.vo.CollectDataParseVo; +import com.nanri.aiimage.modules.collectdata.service.CollectDataBrandRecheckService; +import com.nanri.aiimage.modules.collectdata.service.CollectDataService; +import com.nanri.aiimage.modules.collectdata.util.CollectDataBatchQuery; +import com.nanri.aiimage.modules.collectdata.util.CollectDataBrandBatchFilter; +import com.nanri.aiimage.modules.collectdata.util.CollectDataInvalidAsinBatchWriter; +import com.nanri.aiimage.modules.collectdata.util.CollectDataResultDetailCodec; +import com.nanri.aiimage.modules.collectdata.util.CollectDataResultDetailReader; +import com.nanri.aiimage.modules.collectdata.util.CollectDataResultItemBatchWriter; +import com.nanri.aiimage.modules.file.service.LocalFileStorageService; +import com.nanri.aiimage.modules.invalidasin.mapper.InvalidAsinDataMapper; +import com.nanri.aiimage.modules.task.mapper.FileResultMapper; +import com.nanri.aiimage.modules.task.mapper.FileTaskMapper; +import com.nanri.aiimage.modules.task.mapper.TaskChunkMapper; +import com.nanri.aiimage.modules.task.mapper.TaskResultItemMapper; +import com.nanri.aiimage.modules.task.mapper.TaskScopeStateMapper; +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.file.service.oss.OssStorageService; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.junit.jupiter.api.Test; +import org.mockito.Answers; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.support.TransactionTemplate; + +import java.io.File; +import java.io.FileOutputStream; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.util.ArrayList; +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.anyList; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * 集采建单+解析段的现网行为快照(重构蓝图 Phase 0 · 1B,缺快照的四支之四)。 + * + * 快照打的是真 {@link CollectDataService#parseAndCreateTask}:表头别名认列(关键词/状态)、 + * 无关键词列时「一条都不丢」、关键词为空丢行计数、尾部空列不生成占位列(2026-09-26 生产事故 + * 那条同源修复)、非 Excel 魔数拒绝、{@code taskType} 缺省回落、{@code PENDING} 建单形状, + * 以及逐行 {@code CollectDataItemEntity} 批量落库的 extraJson 形状(Phase 3 迁移后必须仍逐字节相等)。 + * + * 时钟与雪花 taskNo 不进快照;item 主键由 doAnswer 顺序发固定值。 + * + * golden 文件:src/test/resources/collectdata/golden/parse-create-snapshot.txt + */ +class CollectDataSnapshotTest { + + private static final File GOLDEN_PARSE = + new File("src/test/resources/collectdata/golden/parse-create-snapshot.txt"); + + // ---- 夹具 ---- + + private static File workbook(File dir, String name, List> rows) throws Exception { + File file = new File(dir, name); + try (XSSFWorkbook wb = new XSSFWorkbook(); FileOutputStream out = new FileOutputStream(file)) { + Sheet sheet = wb.createSheet("Sheet1"); + for (int r = 0; r < rows.size(); r++) { + Row row = sheet.createRow(r); + List cells = rows.get(r); + for (int c = 0; c < cells.size(); c++) { + row.createCell(c).setCellValue(cells.get(c)); + } + } + wb.write(out); + } + return file; + } + + private static File keywordWorkbook(File dir) throws Exception { + return workbook(dir, "keyword.xlsx", List.of( + List.of("关键词", "asin", "国家", "状态", "价格"), + List.of("wirel less 耳机", "B011", "US", "在售", "19.90"), + List.of("", "B012", "US", "", "9.90"), + List.of("", "", "", "", ""), + List.of(" mouse ", "", "DE", "缺少报价", ""))); + } + + /** 没有关键词列也没有状态列:一条非空行都不该被丢。 */ + private static File noKeywordHeaderWorkbook(File dir) throws Exception { + return workbook(dir, "no-keyword.xlsx", List.of( + List.of("asin", "国家", "价格"), + List.of("B021", "UK", "5.00"), + List.of("B022", "UK", ""))); + } + + /** 表头整行空白 ⇒「Excel 表头为空」。 */ + private static File blankHeaderWorkbook(File dir) throws Exception { + return workbook(dir, "blank-header.xlsx", List.of( + List.of("", "", ""), + List.of("B031", "US", "1.00"))); + } + + /** 关键词列存在但每行关键词都空 ⇒ 有效行 0 ⇒「未解析到有效数据行」。 */ + private static File allDroppedWorkbook(File dir) throws Exception { + return workbook(dir, "all-dropped.xlsx", List.of( + List.of("关键词", "asin"), + List.of("", "B041"), + List.of("", "B042"))); + } + + private static File notExcel(File dir) throws Exception { + File file = new File(dir, "fake.xlsx"); + Files.writeString(file.toPath(), "关键词,asin\nwireless,B0001\n", StandardCharsets.UTF_8); + return file; + } + + // ---- 真服务 + 捕获 ---- + + private static final class Captured { + private final List taskInserts = new ArrayList<>(); + private final List items = new ArrayList<>(); + private final List resultInserts = new ArrayList<>(); + } + + private static CollectDataService newService(LocalFileStorageService storage, Captured captured) { + FileTaskMapper fileTaskMapper = mock(FileTaskMapper.class); + FileResultMapper fileResultMapper = mock(FileResultMapper.class); + CollectDataItemMapper itemMapper = mock(CollectDataItemMapper.class); + ObjectMapper objectMapper = new ObjectMapper(); + + doAnswer(invocation -> { + FileTaskEntity entity = invocation.getArgument(0); + entity.setId(1001L + captured.taskInserts.size()); + captured.taskInserts.add(entity); + return 1; + }).when(fileTaskMapper).insert(any(FileTaskEntity.class)); + doAnswer(invocation -> { + List batch = invocation.getArgument(0); + for (CollectDataItemEntity entity : batch) { + entity.setId(3001L + captured.items.size()); + captured.items.add(entity); + } + return batch.size(); + }).when(itemMapper).insertBatch(anyList()); + doAnswer(invocation -> { + FileResultEntity entity = invocation.getArgument(0); + entity.setId(2001L + captured.resultInserts.size()); + captured.resultInserts.add(entity); + return 1; + }).when(fileResultMapper).insert(any(FileResultEntity.class)); + + return new CollectDataService(storage, fileTaskMapper, fileResultMapper, itemMapper, + mock(CollectDataCountryPrefMapper.class), mock(InvalidAsinDataMapper.class), + mock(TaskChunkMapper.class), mock(TaskScopeStateMapper.class), mock(TaskResultItemMapper.class), + mock(TaskDistributedLockService.class), mock(TaskFileJobService.class), + mock(com.nanri.aiimage.modules.task.service.TaskHeartbeatPositionService.class), + mock(com.nanri.aiimage.modules.task.service.TransientPayloadStorageService.class), + mock(com.nanri.aiimage.modules.collectdata.service.CollectDataExcelAssemblyService.class), + mock(OssStorageService.class), objectMapper, + new TransactionTemplate(mock(PlatformTransactionManager.class)), + mock(com.nanri.aiimage.common.service.DistributedJobLockService.class, Answers.RETURNS_MOCKS), + mock(CollectDataBatchQuery.class), mock(CollectDataBrandBatchFilter.class), + mock(CollectDataInvalidAsinBatchWriter.class), new CollectDataResultDetailCodec(objectMapper), + mock(CollectDataResultItemBatchWriter.class), mock(CollectDataResultDetailReader.class), + mock(TaskProgressLightAssembler.class), + mock(com.nanri.aiimage.modules.notification.service.NotificationDispatchService.class), + mock(com.nanri.aiimage.modules.asincache.service.AsinResultCacheService.class), + mock(CollectDataBrandRecheckService.class), + mock(com.nanri.aiimage.modules.task.service.StaleTaskHeartbeatPolicy.class)); + } + + private static CollectDataParseRequest request(List files, String taskType) { + CollectDataParseRequest request = new CollectDataParseRequest(); + request.setUserId(7L); + request.setFiles(files); + request.setTaskType(taskType); + return request; + } + + private static CollectDataSourceFileDto source(String fileKey, String filename) { + CollectDataSourceFileDto dto = new CollectDataSourceFileDto(); + dto.setFileKey(fileKey); + dto.setOriginalFilename(filename); + return dto; + } + + private static String nullToDash(Object value) { + String text = value == null ? "" : String.valueOf(value); + return text.isBlank() ? "-" : text; + } + + private static String runCase(String caseName, Map filesByFileKey, CollectDataParseRequest request) { + Captured captured = new Captured(); + LocalFileStorageService storage = mock(LocalFileStorageService.class); + when(storage.findLocalSourceFile(anyString())).thenAnswer(invocation -> filesByFileKey.get(invocation.getArgument(0))); + CollectDataService service = newService(storage, captured); + + StringBuilder sb = new StringBuilder(); + sb.append("== case=").append(caseName).append('\n'); + try { + CollectDataParseVo vo = service.parseAndCreateTask(request, "dev-1"); + sb.append("vo: taskId=").append(nullToDash(vo.getTaskId())) + .append(" sourceFilename=").append(nullToDash(vo.getSourceFilename())) + .append(" sourceFileCount=").append(nullToDash(vo.getSourceFileCount())) + .append(" totalRows=").append(nullToDash(vo.getTotalRows())) + .append(" acceptedRows=").append(nullToDash(vo.getAcceptedRows())) + .append(" droppedRows=").append(nullToDash(vo.getDroppedRows())) + .append(" pageSize=").append(nullToDash(vo.getPageSize())) + .append('\n'); + } catch (Exception ex) { + sb.append("thrown: ").append(ex.getClass().getSimpleName()) + .append(" message=").append(nullToDash(ex.getMessage())).append('\n'); + } + for (FileTaskEntity task : captured.taskInserts) { + sb.append("task.insert: id=").append(task.getId()) + .append(" moduleType=").append(nullToDash(task.getModuleType())) + .append(" taskMode=").append(nullToDash(task.getTaskMode())) + .append(" status=").append(nullToDash(task.getStatus())) + .append(" sourceFileCount=").append(nullToDash(task.getSourceFileCount())) + .append(" deviceId=").append(nullToDash(task.getDeviceId())) + .append(" requestJsonTaskTypeDefault=").append(task.getRequestJson() != null + && task.getRequestJson().contains("\"taskType\":\"collect-data\"")) + .append(" requestJsonTaskTypeExplicit=").append(task.getRequestJson() != null + && task.getRequestJson().contains("\"taskType\":\"collect-data-brand\"")) + .append(" resultJsonHasFinalRowCount=").append(task.getResultJson() != null + && task.getResultJson().contains("finalRowCount")) + .append('\n'); + } + for (CollectDataItemEntity item : captured.items) { + sb.append("item: id=").append(item.getId()) + .append(" taskId=").append(nullToDash(item.getTaskId())) + .append(" rowIndex=").append(nullToDash(item.getRowIndex())) + .append(" sourceFileKey=").append(nullToDash(item.getSourceFileKey())) + .append(" sourceFilename=").append(nullToDash(item.getSourceFilename())) + .append(" keyword=").append(nullToDash(item.getKeyword())) + .append(" statusValue=").append(nullToDash(item.getStatusValue())) + .append(" extraJson=").append(nullToDash(item.getExtraJson())) + .append('\n'); + } + for (FileResultEntity result : captured.resultInserts) { + sb.append("result.insert: id=").append(result.getId()) + .append(" taskId=").append(nullToDash(result.getTaskId())) + .append(" moduleType=").append(nullToDash(result.getModuleType())) + .append(" sourceFilename=").append(nullToDash(result.getSourceFilename())) + .append(" sourceFileUrl=").append(nullToDash(result.getSourceFileUrl())) + .append(" rowCount=").append(nullToDash(result.getRowCount())) + .append('\n'); + } + return sb.toString(); + } + + public static String runAll() throws Exception { + File dir = Files.createTempDirectory("collect-data-snapshot-").toFile(); + dir.deleteOnExit(); + File keyword = keywordWorkbook(dir); + File noKeyword = noKeywordHeaderWorkbook(dir); + File blankHeader = blankHeaderWorkbook(dir); + File allDropped = allDroppedWorkbook(dir); + File fake = notExcel(dir); + + StringBuilder sb = new StringBuilder(); + sb.append(runCase("keywordSheet", Map.of("fk-keyword", keyword), + request(List.of(source("fk-keyword", "keyword.xlsx")), null))); + sb.append(runCase("explicitTaskType", Map.of("fk-keyword", keyword), + request(List.of(source("fk-keyword", "keyword.xlsx")), "collect-data-brand"))); + sb.append(runCase("noKeywordHeader", Map.of("fk-no-keyword", noKeyword), + request(List.of(source("fk-no-keyword", "no-keyword.xlsx")), null))); + sb.append(runCase("twoFiles", Map.of("fk-keyword", keyword, "fk-no-keyword", noKeyword), + request(List.of(source("fk-keyword", "keyword.xlsx"), source("fk-no-keyword", "no-keyword.xlsx")), null))); + sb.append(runCase("blankHeader", Map.of("fk-blank", blankHeader), + request(List.of(source("fk-blank", "blank-header.xlsx")), null))); + sb.append(runCase("allRowsDropped", Map.of("fk-dropped", allDropped), + request(List.of(source("fk-dropped", "all-dropped.xlsx")), null))); + sb.append(runCase("notExcel", Map.of("fk-fake", fake), + request(List.of(source("fk-fake", "fake.xlsx")), null))); + sb.append(runCase("missingSourceFile", Map.of(), + request(List.of(source("fk-absent", "absent.xlsx")), null))); + sb.append(runCase("blankFileKeyFiltered", Map.of("fk-keyword", keyword), + request(List.of(source(" ", "blank.xlsx")), null))); + sb.append(runCase("emptyFiles", Map.of(), request(List.of(), 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 parse_create_snapshot_matches_golden() throws Exception { + assertEquals(read(GOLDEN_PARSE), runAll(), "建单+解析输出快照与 golden 一致"); + } + + @Test + void parse_create_snapshot_is_reproducible() throws Exception { + assertEquals(runAll(), runAll(), "同一夹具跑两次结果一致(时钟与雪花 taskNo 不得进快照)"); + } + + @Test + void parse_create_snapshot_golden_is_committed() { + assertTrue(GOLDEN_PARSE.isFile(), "golden 文件必须存在并入库: " + GOLDEN_PARSE.getAbsolutePath()); + assertTrue(GOLDEN_PARSE.length() > 0, "golden 文件非空"); + } + + @Test + void parse_create_snapshot_tamper_is_detected() throws Exception { + String original = read(GOLDEN_PARSE); + assertTrue(original.contains("== case=keywordSheet"), "golden 内容合法"); + try { + Files.writeString(GOLDEN_PARSE.toPath(), original + "\n# tampered", StandardCharsets.UTF_8); + assertTrue(!read(GOLDEN_PARSE).equals(runAll()), "篡改 golden 后断言应失败"); + } finally { + Files.writeString(GOLDEN_PARSE.toPath(), original, StandardCharsets.UTF_8); + } + assertEquals(original, read(GOLDEN_PARSE), "恢复原始 golden"); + } +} diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/deletebrand/service/support/DeleteBrandSnapshotTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/deletebrand/service/support/DeleteBrandSnapshotTest.java new file mode 100644 index 000000000..18b6b8857 --- /dev/null +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/deletebrand/service/support/DeleteBrandSnapshotTest.java @@ -0,0 +1,394 @@ +package com.nanri.aiimage.modules.deletebrand.service.support; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.nanri.aiimage.config.DeleteBrandProgressProperties; +import com.nanri.aiimage.config.TaskPressureProperties; +import com.nanri.aiimage.modules.deletebrand.model.cache.DeleteBrandParsedFileCacheDto; +import com.nanri.aiimage.modules.deletebrand.model.dto.DeleteBrandRunRequest; +import com.nanri.aiimage.modules.deletebrand.model.dto.DeleteBrandSourceFileDto; +import com.nanri.aiimage.modules.deletebrand.model.vo.DeleteBrandCountryAsinVo; +import com.nanri.aiimage.modules.deletebrand.model.vo.DeleteBrandCountryGroupVo; +import com.nanri.aiimage.modules.deletebrand.model.vo.DeleteBrandPreviewRowVo; +import com.nanri.aiimage.modules.deletebrand.model.vo.DeleteBrandResultItemVo; +import com.nanri.aiimage.modules.deletebrand.model.vo.DeleteBrandRunVo; +import com.nanri.aiimage.modules.deletebrand.service.DeleteBrandRunService; +import com.nanri.aiimage.modules.deletebrand.service.DeleteBrandTaskCacheService; +import com.nanri.aiimage.modules.deletebrand.service.DeleteBrandTaskStorageService; +import com.nanri.aiimage.modules.file.service.LocalFileStorageService; +import com.nanri.aiimage.modules.file.service.oss.OssStorageService; +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.ziniao.model.vo.ZiniaoShopMatchResultVo; +import com.nanri.aiimage.modules.ziniao.service.ZiniaoShopSwitchService; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.junit.jupiter.api.Test; +import org.springframework.transaction.PlatformTransactionManager; + +import java.io.File; +import java.io.FileOutputStream; +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.anyBoolean; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * 删除品牌建单+解析段的现网行为快照(重构蓝图 Phase 0 · 1B,缺快照的四支之一)。 + * + * 与 {@code AppearancePatentSnapshotTest} 那套约定的区别,也是这一支的价值所在: + * 快照打的是**真服务**({@link DeleteBrandRunService#run} 一路走到私有内联上下文 + * {@code DeleteBrandSheetContext}),不是在测试里照抄一遍管线——Phase 3 要动的正是这条链, + * 测试里重写的管线会跟着旧代码一起留在原地,对迁移毫无保护。 + * 落库侧用 Mockito 捕获 {@code fileTaskMapper}/{@code fileResultMapper} 的 insert/update 入参, + * 回传载荷侧捕获 {@code saveParsedPayload} 的 map(Phase 3 迁移后必须逐字节仍相等)。 + * + * 时钟/雪花 id 不进快照:taskNo、createdAt/updatedAt/finishedAt 与 requestJson/resultJson 一律不渲染, + * 主键由 doAnswer 按调用顺序发固定值(1001/2001…),所以整串输出可复现。 + * + * golden 文件:src/test/resources/deletebrand/golden/run-snapshot.txt + */ +class DeleteBrandSnapshotTest { + + private static final File GOLDEN_RUN = + new File("src/test/resources/deletebrand/golden/run-snapshot.txt"); + + // ---- 夹具 ---- + + /** 两行表头(国家标题行 + 字段行)+ 数据行;BOM/全角空格/大小写/去重都在这支里吃到。 */ + private static File fixtureMain() throws Exception { + return workbook(List.of( + List.of("美国", "", "德国", ""), + List.of("删除ASIN", "状态", "删除ASIN", "状态"), + List.of("b011 1", "在售", "B021", ""), + List.of("B011 1", "已删除", "", "状态值不该单独成行"), + List.of("B012", "", "B022", "在售"), + List.of("", "", "", ""))); + } + + /** 只有一行表头(没有字段行)⇒ 与旧实现一致的「Excel 表头不完整」。 */ + private static File fixtureHeaderOnly() throws Exception { + return workbook(List.of(List.of("美国", "状态"))); + } + + /** + * 「店铺名」列被标题字面排除、其余列走 dataStartRowIndex=1 那条分支: + * 表头行自身会被当数据行吃进去(ASIN="ASIN"、状态="状态")——今天真实存在的行为, + * Phase 3 把解析段抽走时这条必须仍然相等,所以进快照而不是修掉。 + */ + private static File fixtureSingleHeaderRowShape() throws Exception { + return workbook(List.of( + List.of("店铺名", "英国", ""), + List.of("负责人", "ASIN", "状态"), + List.of("张三", "B031", "在售"), + List.of("李四", "B032", "缺少报价"))); + } + + /** 后缀是 xlsx、内容却是 CSV 文本 ⇒ 魔数拦下,不许被 EasyExcel 静默当 CSV 解析成功。 */ + private static File fixtureNotExcel() throws Exception { + File file = File.createTempFile("delete-brand-snapshot-not-excel-", ".xlsx"); + Files.writeString(file.toPath(), "国家,ASIN\nUS,B0001\n", StandardCharsets.UTF_8); + return file; + } + + private static File workbook(List> rows) throws Exception { + File file = File.createTempFile("delete-brand-snapshot-", ".xlsx"); + try (XSSFWorkbook wb = new XSSFWorkbook(); FileOutputStream out = new FileOutputStream(file)) { + Sheet sheet = wb.createSheet("Sheet1"); + for (int r = 0; r < rows.size(); r++) { + Row row = sheet.createRow(r); + for (int c = 0; c < rows.get(r).size(); c++) { + row.createCell(c).setCellValue(rows.get(r).get(c)); + } + } + wb.write(out); + } + return file; + } + + // ---- 真服务 + 捕获 ---- + + private static final class Captured { + private final List taskInserts = new ArrayList<>(); + private final List taskUpdates = new ArrayList<>(); + private final List resultInserts = new ArrayList<>(); + private final Map payload = new LinkedHashMap<>(); + } + + /** 每个夹具一个「店铺名→紫鸟匹配结果」开关:未匹配的店铺解析成功但不进待跑清单。 */ + private static DeleteBrandRunService newService(Map filesByFileKey, + Captured captured, + List matchedShops) { + FileTaskMapper fileTaskMapper = mock(FileTaskMapper.class); + FileResultMapper fileResultMapper = mock(FileResultMapper.class); + LocalFileStorageService storage = mock(LocalFileStorageService.class); + DeleteBrandTaskCacheService cacheService = mock(DeleteBrandTaskCacheService.class); + DeleteBrandTaskStorageService taskStorage = mock(DeleteBrandTaskStorageService.class); + ZiniaoShopSwitchService ziniao = mock(ZiniaoShopSwitchService.class); + OssStorageService oss = mock(OssStorageService.class); + // ObjectMapper 用真身:mock 它会让 toCompactTaskResultJsonForDb 静默失败(clone is null), + // 快照就锁在了「resultJson 组装失败也能建单」这个假形状上。 + ObjectMapper objectMapper = new ObjectMapper(); + DeleteBrandProgressProperties progressProperties = mock(DeleteBrandProgressProperties.class); + TaskPressureProperties pressureProperties = mock(TaskPressureProperties.class); + TaskDistributedLockService lockService = mock(TaskDistributedLockService.class); + TaskFileJobService taskFileJobService = mock(TaskFileJobService.class); + PlatformTransactionManager transactionManager = mock(PlatformTransactionManager.class); + TaskProgressLightAssembler progressLightAssembler = mock(TaskProgressLightAssembler.class); + + doAnswer(invocation -> { + FileTaskEntity entity = invocation.getArgument(0); + entity.setId(1001L + captured.taskInserts.size()); + captured.taskInserts.add(entity); + return 1; + }).when(fileTaskMapper).insert(any(FileTaskEntity.class)); + doAnswer(invocation -> { + captured.taskUpdates.add(invocation.getArgument(0)); + return 1; + }).when(fileTaskMapper).updateById(any(FileTaskEntity.class)); + doAnswer(invocation -> { + FileResultEntity entity = invocation.getArgument(0); + entity.setId(2001L + captured.resultInserts.size()); + captured.resultInserts.add(entity); + return 1; + }).when(fileResultMapper).insert(any(FileResultEntity.class)); + when(storage.findLocalSourceFile(anyString())) + .thenAnswer(invocation -> filesByFileKey.get(invocation.getArgument(0))); + // 注意别写 `String.valueOf(invocation.getArgument(0))`:泛型 T 会推断成 char[], + // 编译期挑中 valueOf(char[]) 重载,运行时 String 强转 [C 直接抛 ClassCastException。 + when(ziniao.normalizeShopName(any())).thenAnswer(invocation -> { + String shopName = invocation.getArgument(0); + return shopName == null ? "" : shopName; + }); + when(ziniao.findIndexedStoreByName(anyString(), anyBoolean())).thenAnswer(invocation -> { + String shopName = invocation.getArgument(0); + ZiniaoShopMatchResultVo result = new ZiniaoShopMatchResultVo(); + if (!matchedShops.contains(shopName)) { + result.setMatched(false); + result.setMatchStatus("NOT_FOUND"); + result.setMatchMessage("店铺未匹配"); + return result; + } + result.setMatched(true); + result.setMatchStatus("MATCHED"); + result.setShopId("shop-" + shopName); + result.setCompanyName("公司-" + shopName); + result.setPlatform("amazon"); + result.setOpenStoreUrl("https://example.invalid/" + shopName); + return result; + }); + doAnswer(invocation -> { + @SuppressWarnings("unchecked") + Map payload = invocation.getArgument(1); + captured.payload.putAll(payload); + return null; + }).when(taskStorage).saveParsedPayload(any(), any()); + + return new DeleteBrandRunService(fileTaskMapper, fileResultMapper, storage, cacheService, + taskStorage, ziniao, oss, objectMapper, progressProperties, pressureProperties, + lockService, taskFileJobService, transactionManager, progressLightAssembler); + } + + private static DeleteBrandRunRequest request(List fileKeys) { + DeleteBrandRunRequest request = new DeleteBrandRunRequest(); + request.setUserId(7L); + List files = new ArrayList<>(); + for (String fileKey : fileKeys) { + DeleteBrandSourceFileDto sourceFile = new DeleteBrandSourceFileDto(); + sourceFile.setFileKey(fileKey); + sourceFile.setOriginalFilename(fileKey.substring(fileKey.lastIndexOf('/') + 1)); + files.add(sourceFile); + } + request.setFiles(files); + return request; + } + + // ---- 快照渲染 ---- + + private static String nullToDash(Object value) { + String text = value == null ? "" : String.valueOf(value); + return text.isBlank() ? "-" : text; + } + + private static String runCase(String caseName, Map filesByFileKey, + List fileKeys, List matchedShops) { + Captured captured = new Captured(); + DeleteBrandRunService service = newService(filesByFileKey, captured, matchedShops); + DeleteBrandRunVo vo = service.run(request(fileKeys), "dev-1"); + + StringBuilder sb = new StringBuilder(); + sb.append("== case=").append(caseName).append(" files=").append(fileKeys.size()).append('\n'); + for (FileTaskEntity task : captured.taskInserts) { + sb.append("task.insert: id=").append(task.getId()) + .append(" moduleType=").append(nullToDash(task.getModuleType())) + .append(" status=").append(nullToDash(task.getStatus())) + .append(" taskMode=").append(nullToDash(task.getTaskMode())) + .append(" userId=").append(nullToDash(task.getUserId())) + .append(" createdBy=").append(nullToDash(task.getCreatedBy())) + .append(" deviceId=").append(nullToDash(task.getDeviceId())) + .append(" sourceFileCount=").append(nullToDash(task.getSourceFileCount())) + .append('\n'); + } + for (FileTaskEntity task : captured.taskUpdates) { + sb.append("task.update: id=").append(task.getId()) + .append(" status=").append(nullToDash(task.getStatus())) + .append(" sourceFileCount=").append(nullToDash(task.getSourceFileCount())) + .append(" successFileCount=").append(nullToDash(task.getSuccessFileCount())) + .append(" failedFileCount=").append(nullToDash(task.getFailedFileCount())) + .append(" errorMessage=").append(nullToDash(task.getErrorMessage())) + .append('\n'); + } + for (FileResultEntity result : captured.resultInserts) { + sb.append("result.insert: id=").append(result.getId()) + .append(" taskId=").append(nullToDash(result.getTaskId())) + .append(" moduleType=").append(nullToDash(result.getModuleType())) + .append(" sourceFileUrl=").append(nullToDash(result.getSourceFileUrl())) + .append(" rowCount=").append(nullToDash(result.getRowCount())) + .append(" success=").append(nullToDash(result.getSuccess())) + .append(" resultFilename=").append(nullToDash(result.getResultFilename())) + .append(" errorMessage=").append(nullToDash(result.getErrorMessage())) + .append('\n'); + } + sb.append("vo: total=").append(vo.getTotal()) + .append(" successCount=").append(vo.getSuccessCount()) + .append(" failedCount=").append(vo.getFailedCount()).append('\n'); + for (DeleteBrandResultItemVo item : vo.getItems()) { + sb.append("item: resultId=").append(nullToDash(item.getResultId())) + .append(" fileKey=").append(nullToDash(item.getFileKey())) + .append(" shopName=").append(nullToDash(item.getShopName())) + .append(" matched=").append(item.isMatched()) + .append(" matchStatus=").append(nullToDash(item.getMatchStatus())) + .append(" countryCount=").append(nullToDash(item.getCountryCount())) + .append(" totalRows=").append(nullToDash(item.getTotalRows())) + .append(" truncated=").append(item.isTruncated()) + .append(" success=").append(item.isSuccess()) + .append(" error=").append(nullToDash(item.getError())) + .append(" taskStatus=").append(nullToDash(item.getTaskStatus())) + .append('\n'); + renderCountries(sb, " parsed ", item.getCountries()); + for (DeleteBrandPreviewRowVo preview : item.getPreviewRows()) { + sb.append(" preview: row=").append(preview.getRowIndex()) + .append(" country=").append(nullToDash(preview.getCountry())) + .append(" asin=").append(nullToDash(preview.getAsin())) + .append(" status=").append(nullToDash(preview.getStatus())) + .append('\n'); + } + } + sb.append("payload.files=").append(captured.payload.size()).append('\n'); + for (DeleteBrandParsedFileCacheDto dto : captured.payload.values()) { + sb.append(" payload: fileKey=").append(nullToDash(dto.getFileKey())) + .append(" shopId=").append(nullToDash(dto.getShopId())) + .append(" companyName=").append(nullToDash(dto.getCompanyName())) + .append(" platform=").append(nullToDash(dto.getPlatform())) + .append(" totalRows=").append(nullToDash(dto.getTotalRows())) + .append('\n'); + renderCountries(sb, " ", dto.getCountries()); + } + return sb.toString(); + } + + private static void renderCountries(StringBuilder sb, String indent, List countries) { + for (DeleteBrandCountryGroupVo country : countries) { + sb.append(indent).append("country=").append(nullToDash(country.getCountry())) + .append(" asinCount=").append(nullToDash(country.getAsinCount())).append('\n'); + for (DeleteBrandCountryAsinVo asin : country.getItems()) { + sb.append(indent).append(" asin: row=").append(asin.getRowIndex()) + .append(" value=").append(nullToDash(asin.getAsin())) + .append(" status=").append(nullToDash(asin.getStatus())) + .append('\n'); + } + } + } + + static String runAll() { + Map main = Map.of("uploads/魏振峰.xlsx", fixtureMainOrNull()); + StringBuilder sb = new StringBuilder(); + sb.append(runCase("main", main, List.of("uploads/魏振峰.xlsx"), List.of("魏振峰"))); + sb.append(runCase("unmatched", single("uploads/魏振峰.xlsx"), List.of("uploads/魏振峰.xlsx"), List.of())); + sb.append(runCase("headerOnly", single("uploads/缺字段行.xlsx"), List.of("uploads/缺字段行.xlsx"), List.of("缺字段行"))); + sb.append(runCase("shopNameColumnSkipped", single("uploads/英国.xlsx"), List.of("uploads/英国.xlsx"), List.of("英国"))); + sb.append(runCase("notExcel", single("uploads/伪装.csv.xlsx"), List.of("uploads/伪装.csv.xlsx"), List.of("伪装"))); + return sb.toString(); + } + + private static File fixtureMainOrNull() { + try { + return fixtureMain(); + } catch (Exception ex) { + throw new IllegalStateException(ex); + } + } + + private static Map single(String fileKey) { + try { + File file = switch (fileKey) { + case "uploads/缺字段行.xlsx" -> fixtureHeaderOnly(); + case "uploads/英国.xlsx" -> fixtureSingleHeaderRowShape(); + case "uploads/伪装.csv.xlsx" -> fixtureNotExcel(); + default -> fixtureMain(); + }; + return Map.of(fileKey, file); + } catch (Exception ex) { + throw new IllegalStateException(ex); + } + } + + private static String read(File file) throws Exception { + return Files.readString(file.toPath(), StandardCharsets.UTF_8).replace("\r\n", "\n"); + } + + // ---- 用例 ---- + + @Test + void run_snapshot_matches_golden() throws Exception { + assertEquals(read(GOLDEN_RUN), runAll(), "建单+解析输出快照与 golden 一致"); + } + + @Test + void run_snapshot_is_reproducible() { + assertEquals(runAll(), runAll(), "同一夹具跑两次结果一致(时钟与雪花 id 不得进快照)"); + } + + @Test + void run_snapshot_golden_is_committed() { + assertTrue(GOLDEN_RUN.isFile(), "golden 文件必须存在并入库: " + GOLDEN_RUN.getAbsolutePath()); + assertTrue(GOLDEN_RUN.length() > 0, "golden 文件非空"); + } + + @Test + void run_snapshot_tamper_is_detected() throws Exception { + String original = read(GOLDEN_RUN); + assertTrue(original.contains("== case=main"), "golden 内容合法"); + try { + Files.writeString(GOLDEN_RUN.toPath(), original + "\n# tampered", StandardCharsets.UTF_8); + assertTrue(assertDoesNotEqualGolden(), "篡改 golden 后断言应失败"); + } finally { + Files.writeString(GOLDEN_RUN.toPath(), original, StandardCharsets.UTF_8); + } + assertEquals(original, read(GOLDEN_RUN), "恢复原始 golden"); + } + + private static boolean assertDoesNotEqualGolden() { + try { + return !read(GOLDEN_RUN).equals(runAll()); + } catch (Exception ex) { + throw new IllegalStateException(ex); + } + } +} diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/pricetrack/service/support/PriceTrackSnapshotTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/pricetrack/service/support/PriceTrackSnapshotTest.java new file mode 100644 index 000000000..66a0527a1 --- /dev/null +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/pricetrack/service/support/PriceTrackSnapshotTest.java @@ -0,0 +1,362 @@ +package com.nanri.aiimage.modules.pricetrack.service.support; + +import com.fasterxml.jackson.databind.ObjectMapper; +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.PriceTrackCreateTaskRequest; +import com.nanri.aiimage.modules.pricetrack.model.vo.PriceTrackCreateTaskVo; +import com.nanri.aiimage.modules.pricetrack.model.vo.PriceTrackMatchShopsVo; +import com.nanri.aiimage.modules.pricetrack.model.vo.PriceTrackResultItemVo; +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.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.junit.jupiter.api.Test; +import org.springframework.transaction.PlatformTransactionManager; + +import java.io.File; +import java.io.FileOutputStream; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.util.ArrayList; +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.anyString; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * 跟价建单+ASIN 文件解析段的现网行为快照(重构蓝图 Phase 0 · 1B,缺快照的四支之三)。 + * + * 快照打的是真 {@link PriceTrackTaskService#createTask}:模式二选一校验、按归一化店名去重、 + * 未匹配店铺在建单前就拒、ASIN 文件路径的三种形态与「必须落在上传临时目录内」的穿越校验、 + * 表头别名映射(商品ASIN/价格/最低价 → asin/price/minimumPrice)、按 sheet 名认国家、 + * 单国家时的 sheet 名兜底、空 ASIN 行丢弃、BOM 与全角空格归一化,以及解析结果落到 + * {@code TaskResultPayloadService.saveLatest} 的载荷形状——parse 与 report 两段同时钉住。 + * 心跳也在本支钉住:建单即 {@code touchTaskHeartbeat}(蓝图 §1A 那一格,本支从建单侧取证)。 + * + * 时钟与临时目录绝对路径不进快照(后者统一擦成 {@code }),否则逐字节对撞无从谈起。 + * + * golden 文件:src/test/resources/pricetrack/golden/create-task-snapshot.txt + */ +class PriceTrackSnapshotTest { + + private static final File GOLDEN_CREATE = + new File("src/test/resources/pricetrack/golden/create-task-snapshot.txt"); + + // ---- 夹具 ---- + + private record SheetCells(String name, List> rows) { + } + + private static File workbook(File root, String name, List sheets) throws Exception { + File file = new File(root, name); + try (XSSFWorkbook wb = new XSSFWorkbook(); FileOutputStream out = new FileOutputStream(file)) { + for (SheetCells sheet : sheets) { + Sheet created = wb.createSheet(sheet.name()); + for (int r = 0; r < sheet.rows().size(); r++) { + Row row = created.createRow(r); + List cells = sheet.rows().get(r); + for (int c = 0; c < cells.size(); c++) { + row.createCell(c).setCellValue(cells.get(c)); + } + } + } + wb.write(out); + } + return file; + } + + /** 英国 sheet 用「商品ASIN/价格/最低价/备注」四列别名,德国 sheet 用最小两列;含空 ASIN 行与 BOM/全角 ASIN。 */ + private static File asinWorkbookTwoSheets(File root) throws Exception { + return workbook(root, "asin.xlsx", List.of( + new SheetCells("英国", List.of( + List.of("商品ASIN", "价格", "最低价", "备注"), + List.of("B011", "19.90", "18.00", "note"), + List.of("", "9.90", "", ""), + List.of("b012 ", "20.00", "", ""))), + new SheetCells("德国", List.of( + List.of("ASIN", "价格"), + List.of("B021", "5.00"))))); + } + + /** sheet 名不在国家词表里(葡萄牙)——国家归属只能靠 countryCodes 兜底。 */ + private static File unknownSheetWorkbook(File root) throws Exception { + return workbook(root, "unknown-sheet.xlsx", List.of( + new SheetCells("葡萄牙", List.of( + List.of("ASIN", "价格"), + List.of("B099", "1.00"))))); + } + + // ---- 真服务 + 捕获 ---- + + private static final class Captured { + private final List taskInserts = new ArrayList<>(); + private final List taskUpdates = new ArrayList<>(); + private final List resultInserts = new ArrayList<>(); + private final List>>> payloads = new ArrayList<>(); + private int heartbeats; + } + + private static PriceTrackTaskService newService(Captured captured, File root) { + FileTaskMapper fileTaskMapper = mock(FileTaskMapper.class); + FileResultMapper fileResultMapper = mock(FileResultMapper.class); + ZiniaoShopSwitchService ziniao = mock(ZiniaoShopSwitchService.class); + PriceTrackTaskCacheService cacheService = mock(PriceTrackTaskCacheService.class); + LocalFileStorageService storage = mock(LocalFileStorageService.class); + TaskResultPayloadService payloadService = mock(TaskResultPayloadService.class); + + doAnswer(invocation -> { + FileTaskEntity entity = invocation.getArgument(0); + entity.setId(1001L + captured.taskInserts.size()); + captured.taskInserts.add(entity); + return 1; + }).when(fileTaskMapper).insert(any(FileTaskEntity.class)); + doAnswer(invocation -> { + captured.taskUpdates.add(invocation.getArgument(0)); + return 1; + }).when(fileTaskMapper).updateById(any(FileTaskEntity.class)); + doAnswer(invocation -> { + FileResultEntity entity = invocation.getArgument(0); + entity.setId(2001L + captured.resultInserts.size()); + captured.resultInserts.add(entity); + return 1; + }).when(fileResultMapper).insert(any(FileResultEntity.class)); + // 去重键是「归一化后的店名」,这里给一条去首尾空白的最小归一化(紫鸟归一化本身归它自己的套件) + when(ziniao.normalizeShopName(any())).thenAnswer(invocation -> { + String shopName = invocation.getArgument(0); + return shopName == null ? "" : shopName.trim(); + }); + when(storage.localTempRoot()).thenReturn(root); + doAnswer(invocation -> { + captured.heartbeats++; + return null; + }).when(cacheService).touchTaskHeartbeat(any()); + doAnswer(invocation -> { + @SuppressWarnings("unchecked") + Map>> rows = invocation.getArgument(3); + captured.payloads.add(rows); + return null; + }).when(payloadService).saveLatest(any(), anyString(), anyString(), any()); + + return new PriceTrackTaskService(fileTaskMapper, fileResultMapper, + mock(PriceTrackShopCandidateMapper.class), ziniao, mock(OssStorageService.class), + mock(ResultDownloadResolver.class), new ObjectMapper(), mock(SkipPriceAsinService.class), + mock(PriceTrackExcelAssemblyService.class), cacheService, mock(PriceTrackLoopRunService.class), + mock(TaskPressureProperties.class), payloadService, mock(TaskFileJobService.class), + mock(TaskDistributedLockService.class), mock(TaskProgressLightAssembler.class), + storage, mock(PlatformTransactionManager.class)); + } + + private static PriceTrackMatchShopsVo.PriceTrackShopQueueItem shop(String shopName, Boolean matched) { + PriceTrackMatchShopsVo.PriceTrackShopQueueItem item = new PriceTrackMatchShopsVo.PriceTrackShopQueueItem(); + item.setShopName(shopName); + item.setMatched(matched); + item.setShopId(Boolean.TRUE.equals(matched) ? "shop-" + shopName : null); + item.setPlatform("amazon"); + item.setCompanyName("公司-" + shopName); + item.setMatchStatus(Boolean.TRUE.equals(matched) ? "MATCHED" : "NOT_FOUND"); + return item; + } + + private static PriceTrackCreateTaskRequest request(List items, + boolean statusMode, + boolean asinMode, + List asinFiles, + List countryCodes) { + PriceTrackCreateTaskRequest request = new PriceTrackCreateTaskRequest(); + request.setUserId(7L); + request.setItems(items); + request.setStatusMode(statusMode); + request.setAsinMode(asinMode); + request.setAsinFiles(asinFiles); + request.setCountryCodes(countryCodes); + request.setListingStatuses(List.of("在售", "缺少报价")); + return request; + } + + private static String nullToDash(Object value) { + String text = value == null ? "" : String.valueOf(value); + return text.isBlank() ? "-" : text; + } + + private static String runCase(String caseName, PriceTrackCreateTaskRequest request, File root, File outsideRoot) { + Captured captured = new Captured(); + PriceTrackTaskService service = newService(captured, root); + StringBuilder sb = new StringBuilder(); + sb.append("== case=").append(caseName).append('\n'); + try { + PriceTrackCreateTaskVo vo = service.createTask(request, "dev-1"); + sb.append("vo: taskId=").append(nullToDash(vo.getTaskId())) + .append(" items=").append(vo.getItems().size()).append('\n'); + for (PriceTrackResultItemVo item : vo.getItems()) { + sb.append(" item: resultId=").append(nullToDash(item.getResultId())) + .append(" shopName=").append(nullToDash(item.getShopName())) + .append(" matched=").append(nullToDash(item.getMatched())) + .append(" taskStatus=").append(nullToDash(item.getTaskStatus())) + .append(" success=").append(nullToDash(item.getSuccess())) + .append('\n'); + } + // 载荷在 parse 段就落库了;VO 上那三个 map 今天恒空(注释掉的那几行),钉住「别再往回加」 + sb.append("vo: asinRowsByCountry=").append(vo.getAsinRowsByCountry().size()) + .append(" skipAsinsByCountry=").append(vo.getSkipAsinsByCountry().size()) + .append(" minimumPriceByCountry=").append(vo.getMinimumPriceByCountryAndAsin().size()) + .append('\n'); + } catch (Exception ex) { + sb.append("thrown: ").append(ex.getClass().getSimpleName()) + .append(" message=").append(nullToDash(ex.getMessage())).append('\n'); + } + for (FileTaskEntity task : captured.taskInserts) { + sb.append("task.insert: id=").append(task.getId()) + .append(" moduleType=").append(nullToDash(task.getModuleType())) + .append(" taskMode=").append(nullToDash(task.getTaskMode())) + .append(" status=").append(nullToDash(task.getStatus())) + .append(" sourceFileCount=").append(nullToDash(task.getSourceFileCount())) + .append('\n'); + } + for (FileTaskEntity task : captured.taskUpdates) { + sb.append("task.update: id=").append(task.getId()) + .append(" status=").append(nullToDash(task.getStatus())) + .append(" requestJsonHasAsinFiles=").append(task.getRequestJson() != null + && task.getRequestJson().contains("asinFiles")) + .append(" requestJsonHasListingStatuses=").append(task.getRequestJson() != null + && task.getRequestJson().contains("listingStatuses")) + .append(" resultJsonItems=").append(task.getResultJson() == null ? -1 + : count(task.getResultJson(), "\"resultId\"")) + .append('\n'); + } + for (FileResultEntity result : captured.resultInserts) { + sb.append("result.insert: id=").append(result.getId()) + .append(" moduleType=").append(nullToDash(result.getModuleType())) + .append(" sourceFilename=").append(nullToDash(result.getSourceFilename())) + .append(" sourceFileUrl=").append(nullToDash(result.getSourceFileUrl())) + .append(" success=").append(nullToDash(result.getSuccess())) + .append('\n'); + } + sb.append("payloadSaves=").append(captured.payloads.size()).append('\n'); + for (Map>> payload : captured.payloads) { + for (Map.Entry>> entry : payload.entrySet()) { + sb.append(" payload country=").append(entry.getKey()) + .append(" rows=").append(entry.getValue().size()).append('\n'); + for (Map row : entry.getValue()) { + sb.append(" row:"); + for (Map.Entry cell : row.entrySet()) { + sb.append(' ').append(cell.getKey()).append('=').append(nullToDash(cell.getValue())); + } + sb.append('\n'); + } + } + } + sb.append("heartbeatTouches=").append(captured.heartbeats).append('\n'); + return scrub(scrub(sb.toString(), root, ""), outsideRoot, ""); + } + + 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 String scrub(String text, File dir, String token) { + return text.replace(dir.getAbsolutePath(), token); + } + + public static String runAll() throws Exception { + File root = Files.createTempDirectory("price-track-snapshot-root-").toFile(); + root.deleteOnExit(); + File outsideRoot = Files.createTempDirectory("price-track-snapshot-outside-").toFile(); + outsideRoot.deleteOnExit(); + asinWorkbookTwoSheets(root); + unknownSheetWorkbook(root); + File outside = new File(outsideRoot, "outside.xlsx"); + try (FileOutputStream ignored = new FileOutputStream(outside)) { + ignored.write("PK\u0003\u0004-not-inside-temp-root".getBytes(StandardCharsets.UTF_8)); + } + + List twoShops = + List.of(shop("店铺A", true), shop(" 店铺A ", true), shop("店铺B", true)); + List oneShop = List.of(shop("店铺A", true)); + + StringBuilder sb = new StringBuilder(); + sb.append(runCase("asinModeTwoSheets", + request(twoShops, false, true, List.of("asin.xlsx"), List.of("UK", "DE")), root, outsideRoot)); + sb.append(runCase("singleSheetFallback", + request(oneShop, false, true, List.of("unknown-sheet.xlsx"), List.of("FR")), root, outsideRoot)); + sb.append(runCase("unknownSheetWithoutFallback", + request(oneShop, false, true, List.of("unknown-sheet.xlsx"), List.of("UK", "DE")), root, outsideRoot)); + sb.append(runCase("multipleFilesSameCountryMerged", + request(oneShop, false, true, List.of("asin.xlsx", "unknown-sheet.xlsx"), List.of("FR")), root, outsideRoot)); + sb.append(runCase("fileOutsideTempRootRejected", + request(oneShop, false, true, List.of(outside.getAbsolutePath()), List.of("UK")), root, outsideRoot)); + sb.append(runCase("missingFile", + request(oneShop, false, true, List.of("不存在的.xlsx"), List.of("UK")), root, outsideRoot)); + sb.append(runCase("blankPathSkipped", + request(oneShop, false, true, List.of("", "asin.xlsx"), List.of("FR")), root, outsideRoot)); + sb.append(runCase("statusMode", request(oneShop, true, false, List.of(), List.of("UK")), root, outsideRoot)); + sb.append(runCase("bothModes", + request(oneShop, true, true, List.of("asin.xlsx"), List.of("UK")), root, outsideRoot)); + sb.append(runCase("neitherMode", request(oneShop, false, false, List.of(), List.of("UK")), root, outsideRoot)); + sb.append(runCase("asinModeWithoutFiles", request(oneShop, false, true, List.of(), List.of("UK")), root, outsideRoot)); + sb.append(runCase("unmatchedShop", request(List.of(shop("店铺C", false)), true, false, List.of(), List.of("UK")), root, outsideRoot)); + 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 create_task_snapshot_matches_golden() throws Exception { + assertEquals(read(GOLDEN_CREATE), runAll(), "建单+ASIN 解析输出快照与 golden 一致"); + } + + @Test + void create_task_snapshot_is_reproducible() throws Exception { + assertEquals(runAll(), runAll(), "同一夹具跑两次结果一致(时钟与临时目录路径不得进快照)"); + } + + @Test + void create_task_snapshot_golden_is_committed() { + assertTrue(GOLDEN_CREATE.isFile(), "golden 文件必须存在并入库: " + GOLDEN_CREATE.getAbsolutePath()); + assertTrue(GOLDEN_CREATE.length() > 0, "golden 文件非空"); + } + + @Test + void create_task_snapshot_tamper_is_detected() throws Exception { + String original = read(GOLDEN_CREATE); + assertTrue(original.contains("== case=asinModeTwoSheets"), "golden 内容合法"); + try { + Files.writeString(GOLDEN_CREATE.toPath(), original + "\n# tampered", StandardCharsets.UTF_8); + assertTrue(!read(GOLDEN_CREATE).equals(runAll()), "篡改 golden 后断言应失败"); + } finally { + Files.writeString(GOLDEN_CREATE.toPath(), original, StandardCharsets.UTF_8); + } + assertEquals(original, read(GOLDEN_CREATE), "恢复原始 golden"); + } +} diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/shopmatch/service/support/ShopMatchSnapshotTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/shopmatch/service/support/ShopMatchSnapshotTest.java new file mode 100644 index 000000000..f3ec158da --- /dev/null +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/shopmatch/service/support/ShopMatchSnapshotTest.java @@ -0,0 +1,292 @@ +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.service.ResultDownloadResolver; +import com.nanri.aiimage.config.TaskPressureProperties; +import com.nanri.aiimage.modules.file.service.oss.OssStorageService; +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.ShopMatchTaskItemDto; +import com.nanri.aiimage.modules.shopmatch.model.vo.ShopMatchCreateTaskVo; +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.springframework.transaction.PlatformTransactionManager; + +import java.io.File; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.List; + +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.Mockito.doAnswer; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * 匹配店铺建单段的现网行为快照(重构蓝图 Phase 0 · 1B,缺快照的四支之二)。 + * + * 本模块此前只有 1 支测试/264 行,对 3,275 行源码——蓝图把 3C(collectdata+shopmatch) + * 的开工红线钉在「快照补齐之前不得动」,这一支就是那条红线的解。 + * 快照打的是真 {@link ShopMatchTaskService#createTask}:建单校验的顺序、按归一化店名去重、 + * 国家代码白名单、排期分出的 SCHEDULED/RUNNING 两条状态,以及「心跳只在 RUNNING 时打」 + * 这条跨模块契约(蓝图 §1A 要接的心跳,本支在建单侧一并钉住)。 + * 落库侧捕获 fileTaskMapper/fileResultMapper 的 insert/update 入参;时钟不进快照—— + * scheduleTimes 用「现在 + N 天」构造,scheduledAt 只渲染「有没有」。 + * + * 这支没有 Excel 输入:它的「解析」是把 {@code ShopMatchTaskItemDto} 队列归一化成任务项 + * ({@code toQueueItems → dedupeQueueItems → toTaskItems}),所以夹具直接喂请求对象。 + * + * golden 文件:src/test/resources/shopmatch/golden/create-task-snapshot.txt + */ +class ShopMatchSnapshotTest { + + private static final File GOLDEN_CREATE = + new File("src/test/resources/shopmatch/golden/create-task-snapshot.txt"); + + private static final class Captured { + private final List taskInserts = new ArrayList<>(); + private final List taskUpdates = new ArrayList<>(); + private final List resultInserts = new ArrayList<>(); + private int heartbeats; + } + + /** + * Spring 注入的那只 ObjectMapper 不是 {@code new ObjectMapper()}:Boot 会把 classpath 上的 + * jackson-datatype-jsr310 注册进去(本仓 2.18.11 在),并把 WRITE_DATES_AS_TIMESTAMPS 关掉。 + * 用裸 mapper 会让 {@code scheduleTimes}(List<LocalDateTime>)序列化失败,被建单那段 + * 的 {@code catch (Exception)} 吞成「task 不存在」——快照就会锁在一个生产里不存在的假形状上。 + */ + private static ObjectMapper bootLikeObjectMapper() { + return JsonMapper.builder() + .addModule(new JavaTimeModule()) + .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) + .build(); + } + + private static ShopMatchTaskService newService(Captured captured) { + FileTaskMapper fileTaskMapper = mock(FileTaskMapper.class); + FileResultMapper fileResultMapper = mock(FileResultMapper.class); + ZiniaoShopSwitchService ziniao = mock(ZiniaoShopSwitchService.class); + ShopMatchTaskCacheService cacheService = mock(ShopMatchTaskCacheService.class); + + doAnswer(invocation -> { + FileTaskEntity entity = invocation.getArgument(0); + entity.setId(1001L + captured.taskInserts.size()); + captured.taskInserts.add(entity); + return 1; + }).when(fileTaskMapper).insert(any(FileTaskEntity.class)); + doAnswer(invocation -> { + captured.taskUpdates.add(invocation.getArgument(0)); + return 1; + }).when(fileTaskMapper).updateById(any(FileTaskEntity.class)); + doAnswer(invocation -> { + FileResultEntity entity = invocation.getArgument(0); + entity.setId(2001L + captured.resultInserts.size()); + captured.resultInserts.add(entity); + return 1; + }).when(fileResultMapper).insert(any(FileResultEntity.class)); + // 去重键是「归一化后的店名」,这里给一条去首尾空白的最小归一化: + // 本支要钉的是「按归一化名去重、不按原始名」这条形状,紫鸟归一化本身归它自己的套件。 + when(ziniao.normalizeShopName(any())).thenAnswer(invocation -> { + String shopName = invocation.getArgument(0); + return shopName == null ? "" : shopName.trim(); + }); + doAnswer(invocation -> { + captured.heartbeats++; + return null; + }).when(cacheService).touchTaskHeartbeat(any()); + + return new ShopMatchTaskService(fileTaskMapper, fileResultMapper, + mock(ShopMatchShopCandidateMapper.class), ziniao, + mock(ShopMatchExcelAssemblyService.class), mock(OssStorageService.class), + mock(ResultDownloadResolver.class), bootLikeObjectMapper(), cacheService, + mock(TaskPressureProperties.class), mock(TaskResultPayloadService.class), + mock(TaskFileJobService.class), mock(TaskDistributedLockService.class), + mock(SkipPriceAsinService.class), mock(QueryAsinMapper.class), + mock(TaskProgressLightAssembler.class), mock(PlatformTransactionManager.class)); + } + + private static ShopMatchTaskItemDto shop(String shopName, boolean matched) { + ShopMatchTaskItemDto item = new ShopMatchTaskItemDto(); + item.setShopName(shopName); + item.setMatched(matched); + item.setShopId(matched ? "shop-" + shopName.trim() : null); + item.setPlatform("amazon"); + item.setCompanyName("公司-" + shopName.trim()); + item.setOpenStoreUrl("https://example.invalid/" + shopName.trim()); + item.setMatchedUserId(9L); + item.setMatchStatus(matched ? "MATCHED" : "NOT_FOUND"); + item.setMatchMessage(matched ? "" : "店铺未匹配"); + return item; + } + + private static ShopMatchCreateTaskRequest request(List items, + List countryCodes, + List scheduleTimes) { + ShopMatchCreateTaskRequest request = new ShopMatchCreateTaskRequest(); + request.setUserId(7L); + request.setItems(items); + request.setCountryCodes(countryCodes); + request.setScheduleTimes(scheduleTimes); + return request; + } + + private static String nullToDash(Object value) { + String text = value == null ? "" : String.valueOf(value); + return text.isBlank() ? "-" : text; + } + + private static int countOccurrences(String text, String needle) { + int count = 0; + int index = text.indexOf(needle); + while (index >= 0) { + count++; + index = text.indexOf(needle, index + needle.length()); + } + return count; + } + + private static String runCase(String caseName, ShopMatchCreateTaskRequest request) { + Captured captured = new Captured(); + ShopMatchTaskService service = newService(captured); + StringBuilder sb = new StringBuilder(); + sb.append("== case=").append(caseName).append('\n'); + try { + ShopMatchCreateTaskVo vo = service.createTask(request, "dev-1"); + sb.append("vo: taskId=").append(nullToDash(vo.getTaskId())) + .append(" items=").append(vo.getItems().size()).append('\n'); + for (ShopMatchCreateTaskVo.Item item : vo.getItems()) { + sb.append(" item: resultId=").append(nullToDash(item.getResultId())) + .append(" shopName=").append(nullToDash(item.getShopName())) + .append(" shopId=").append(nullToDash(item.getShopId())) + .append(" matched=").append(item.isMatched()) + .append(" matchStatus=").append(nullToDash(item.getMatchStatus())) + .append(" taskStatus=").append(nullToDash(item.getTaskStatus())) + .append(" success=").append(nullToDash(item.getSuccess())) + .append(" scheduledAtPresent=").append(item.getScheduledAt() != null) + .append('\n'); + } + } catch (Exception ex) { + sb.append("thrown: ").append(ex.getClass().getSimpleName()) + .append(" message=").append(nullToDash(ex.getMessage())).append('\n'); + } + for (FileTaskEntity task : captured.taskInserts) { + sb.append("task.insert: id=").append(task.getId()) + .append(" moduleType=").append(nullToDash(task.getModuleType())) + .append(" taskMode=").append(nullToDash(task.getTaskMode())) + .append(" status=").append(nullToDash(task.getStatus())) + .append(" sourceFileCount=").append(nullToDash(task.getSourceFileCount())) + .append(" deviceId=").append(nullToDash(task.getDeviceId())) + .append(" scheduledAtPresent=").append(task.getScheduledAt() != null) + .append('\n'); + } + for (FileTaskEntity task : captured.taskUpdates) { + sb.append("task.update: id=").append(task.getId()) + .append(" status=").append(nullToDash(task.getStatus())) + .append(" requestJsonStageIndexZero=").append(task.getRequestJson() != null + && task.getRequestJson().contains("\"current_stage_index\":0")) + .append(" requestJsonStageIndexNull=").append(task.getRequestJson() != null + && task.getRequestJson().contains("\"current_stage_index\":null")) + .append(" resultJsonResultIds=").append(task.getResultJson() == null ? -1 + : countOccurrences(task.getResultJson(), "\"resultId\"")) + .append('\n'); + } + for (FileResultEntity result : captured.resultInserts) { + sb.append("result.insert: id=").append(result.getId()) + .append(" taskId=").append(nullToDash(result.getTaskId())) + .append(" moduleType=").append(nullToDash(result.getModuleType())) + .append(" sourceFilename=").append(nullToDash(result.getSourceFilename())) + .append(" sourceFileUrl=").append(nullToDash(result.getSourceFileUrl())) + .append(" success=").append(nullToDash(result.getSuccess())) + .append('\n'); + } + sb.append("heartbeatTouches=").append(captured.heartbeats).append('\n'); + return sb.toString(); + } + + public static String runAll() { + List twoShops = List.of(shop("店铺A", true), shop("店铺B", true)); + StringBuilder sb = new StringBuilder(); + // 国家代码:去空白+转大写+去重,且只认 ProductRiskCountryCode 那五个(US 不在内) + sb.append(runCase("immediate", request(twoShops, List.of("uk", "UK", " uk ", ""), List.of()))); + sb.append(runCase("usNotSupportedCountry", request(twoShops, List.of("US"), List.of()))); + sb.append(runCase("dedupedByNormalizedName", request( + List.of(shop("店铺A", true), shop(" 店铺A ", true), shop("店铺B", true)), + List.of("DE"), List.of()))); + sb.append(runCase("scheduled", request(twoShops, List.of("UK"), + List.of(LocalDateTime.now().plusDays(1), LocalDateTime.now().plusDays(2))))); + sb.append(runCase("unmatchedShop", request( + List.of(shop("店铺A", true), shop("店铺C", false)), List.of("UK"), List.of()))); + sb.append(runCase("badCountryCode", request(twoShops, List.of("CN"), List.of()))); + sb.append(runCase("emptyCountryCodes", request(twoShops, List.of(), List.of()))); + sb.append(runCase("scheduleInPast", request(twoShops, List.of("UK"), + List.of(LocalDateTime.now().minusDays(1))))); + sb.append(runCase("scheduleNotIncreasing", request(twoShops, List.of("UK"), + List.of(LocalDateTime.now().plusDays(2), LocalDateTime.now().plusDays(1))))); + 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 create_task_snapshot_matches_golden() throws Exception { + assertEquals(read(GOLDEN_CREATE), runAll(), "建单输出快照与 golden 一致"); + } + + @Test + void create_task_snapshot_is_reproducible() { + assertEquals(runAll(), runAll(), "同一夹具跑两次结果一致(时钟与雪花 id 不得进快照)"); + } + + @Test + void create_task_snapshot_golden_is_committed() { + assertTrue(GOLDEN_CREATE.isFile(), "golden 文件必须存在并入库: " + GOLDEN_CREATE.getAbsolutePath()); + assertTrue(GOLDEN_CREATE.length() > 0, "golden 文件非空"); + } + + @Test + void create_task_snapshot_tamper_is_detected() throws Exception { + String original = read(GOLDEN_CREATE); + assertTrue(original.contains("== case=immediate"), "golden 内容合法"); + try { + Files.writeString(GOLDEN_CREATE.toPath(), original + "\n# tampered", StandardCharsets.UTF_8); + assertTrue(!read(GOLDEN_CREATE).equals(runAll()), "篡改 golden 后断言应失败"); + } finally { + Files.writeString(GOLDEN_CREATE.toPath(), original, StandardCharsets.UTF_8); + } + assertEquals(original, read(GOLDEN_CREATE), "恢复原始 golden"); + } + + /** 逐行 insert 的自增主键回填是 resultId 的来源——改成批量插入会让快照 resultId 变 null。 */ + @Test + void create_task_backfills_result_id_per_row() { + Captured captured = new Captured(); + ShopMatchCreateTaskVo vo = newService(captured).createTask( + request(List.of(shop("店铺A", true), shop("店铺B", true)), List.of("UK"), List.of()), "dev-1"); + assertEquals(2, vo.getItems().size()); + assertEquals(2001L, vo.getItems().get(0).getResultId()); + assertEquals(2002L, vo.getItems().get(1).getResultId()); + } +} diff --git a/backend-java/src/test/resources/collectdata/golden/parse-create-snapshot.txt b/backend-java/src/test/resources/collectdata/golden/parse-create-snapshot.txt new file mode 100644 index 000000000..75dbf6256 --- /dev/null +++ b/backend-java/src/test/resources/collectdata/golden/parse-create-snapshot.txt @@ -0,0 +1,38 @@ +== case=keywordSheet +vo: taskId=1001 sourceFilename=keyword.xlsx sourceFileCount=1 totalRows=3 acceptedRows=2 droppedRows=1 pageSize=50 +task.insert: id=1001 moduleType=COLLECT_DATA taskMode=PYTHON_QUEUE status=PENDING sourceFileCount=1 deviceId=dev-1 requestJsonTaskTypeDefault=true requestJsonTaskTypeExplicit=false resultJsonHasFinalRowCount=true +item: id=3001 taskId=1001 rowIndex=1 sourceFileKey=fk-keyword sourceFilename=keyword.xlsx keyword=wirel less 耳机 statusValue=在售 extraJson={"asin":"B011","国家":"US","价格":"19.90"} +item: id=3002 taskId=1001 rowIndex=2 sourceFileKey=fk-keyword sourceFilename=keyword.xlsx keyword=mouse statusValue=缺少报价 extraJson={"asin":"","国家":"DE","价格":""} +result.insert: id=2001 taskId=1001 moduleType=COLLECT_DATA sourceFilename=keyword.xlsx sourceFileUrl=fk-keyword rowCount=2 +== case=explicitTaskType +vo: taskId=1001 sourceFilename=keyword.xlsx sourceFileCount=1 totalRows=3 acceptedRows=2 droppedRows=1 pageSize=50 +task.insert: id=1001 moduleType=COLLECT_DATA taskMode=PYTHON_QUEUE status=PENDING sourceFileCount=1 deviceId=dev-1 requestJsonTaskTypeDefault=false requestJsonTaskTypeExplicit=true resultJsonHasFinalRowCount=true +item: id=3001 taskId=1001 rowIndex=1 sourceFileKey=fk-keyword sourceFilename=keyword.xlsx keyword=wirel less 耳机 statusValue=在售 extraJson={"asin":"B011","国家":"US","价格":"19.90"} +item: id=3002 taskId=1001 rowIndex=2 sourceFileKey=fk-keyword sourceFilename=keyword.xlsx keyword=mouse statusValue=缺少报价 extraJson={"asin":"","国家":"DE","价格":""} +result.insert: id=2001 taskId=1001 moduleType=COLLECT_DATA sourceFilename=keyword.xlsx sourceFileUrl=fk-keyword rowCount=2 +== case=noKeywordHeader +vo: taskId=1001 sourceFilename=no-keyword.xlsx sourceFileCount=1 totalRows=2 acceptedRows=2 droppedRows=0 pageSize=50 +task.insert: id=1001 moduleType=COLLECT_DATA taskMode=PYTHON_QUEUE status=PENDING sourceFileCount=1 deviceId=dev-1 requestJsonTaskTypeDefault=true requestJsonTaskTypeExplicit=false resultJsonHasFinalRowCount=true +item: id=3001 taskId=1001 rowIndex=1 sourceFileKey=fk-no-keyword sourceFilename=no-keyword.xlsx keyword=- statusValue=- extraJson={"asin":"B021","国家":"UK","价格":"5.00"} +item: id=3002 taskId=1001 rowIndex=2 sourceFileKey=fk-no-keyword sourceFilename=no-keyword.xlsx keyword=- statusValue=- extraJson={"asin":"B022","国家":"UK","价格":""} +result.insert: id=2001 taskId=1001 moduleType=COLLECT_DATA sourceFilename=no-keyword.xlsx sourceFileUrl=fk-no-keyword rowCount=2 +== case=twoFiles +vo: taskId=1001 sourceFilename=keyword.xlsx 等 2 个文件 sourceFileCount=2 totalRows=5 acceptedRows=4 droppedRows=1 pageSize=50 +task.insert: id=1001 moduleType=COLLECT_DATA taskMode=PYTHON_QUEUE status=PENDING sourceFileCount=2 deviceId=dev-1 requestJsonTaskTypeDefault=true requestJsonTaskTypeExplicit=false resultJsonHasFinalRowCount=true +item: id=3001 taskId=1001 rowIndex=1 sourceFileKey=fk-keyword sourceFilename=keyword.xlsx keyword=wirel less 耳机 statusValue=在售 extraJson={"asin":"B011","国家":"US","价格":"19.90"} +item: id=3002 taskId=1001 rowIndex=2 sourceFileKey=fk-keyword sourceFilename=keyword.xlsx keyword=mouse statusValue=缺少报价 extraJson={"asin":"","国家":"DE","价格":""} +item: id=3003 taskId=1001 rowIndex=3 sourceFileKey=fk-no-keyword sourceFilename=no-keyword.xlsx keyword=- statusValue=- extraJson={"asin":"B021","国家":"UK","价格":"5.00"} +item: id=3004 taskId=1001 rowIndex=4 sourceFileKey=fk-no-keyword sourceFilename=no-keyword.xlsx keyword=- statusValue=- extraJson={"asin":"B022","国家":"UK","价格":""} +result.insert: id=2001 taskId=1001 moduleType=COLLECT_DATA sourceFilename=keyword.xlsx 等 2 个文件 sourceFileUrl=fk-keyword|fk-no-keyword rowCount=4 +== case=blankHeader +thrown: BusinessException message=Excel 表头为空 +== case=allRowsDropped +thrown: BusinessException message=未解析到有效数据行 +== case=notExcel +thrown: BusinessException message=解析 Excel 失败 +== case=missingSourceFile +thrown: BusinessException message=源文件不存在 +== case=blankFileKeyFiltered +thrown: BusinessException message=请先上传 Excel 文件 +== case=emptyFiles +thrown: BusinessException message=请先上传 Excel 文件 diff --git a/backend-java/src/test/resources/deletebrand/golden/run-snapshot.txt b/backend-java/src/test/resources/deletebrand/golden/run-snapshot.txt new file mode 100644 index 000000000..5c4600f7d --- /dev/null +++ b/backend-java/src/test/resources/deletebrand/golden/run-snapshot.txt @@ -0,0 +1,74 @@ +== case=main files=1 +task.insert: id=1001 moduleType=DELETE_BRAND status=RUNNING taskMode=IMMEDIATE userId=7 createdBy=user:7 deviceId=dev-1 sourceFileCount=1 +task.update: id=1001 status=RUNNING sourceFileCount=1 successFileCount=1 failedFileCount=0 errorMessage=- +result.insert: id=2001 taskId=1001 moduleType=DELETE_BRAND sourceFileUrl=uploads/魏振峰.xlsx rowCount=4 success=0 resultFilename=- errorMessage=- +vo: total=1 successCount=1 failedCount=0 +item: resultId=2001 fileKey=uploads/魏振峰.xlsx shopName=魏振峰 matched=true matchStatus=MATCHED countryCount=2 totalRows=4 truncated=false success=true error=- taskStatus=RUNNING + parsed country=美国 asinCount=2 + parsed asin: row=3 value=b011 1 status=在售 + parsed asin: row=5 value=B012 status=- + parsed country=德国 asinCount=2 + parsed asin: row=3 value=B021 status=- + parsed asin: row=5 value=B022 status=在售 + preview: row=3 country=美国 asin=b011 1 status=在售 + preview: row=5 country=美国 asin=B012 status=- + preview: row=3 country=德国 asin=B021 status=- + preview: row=5 country=德国 asin=B022 status=在售 +payload.files=1 + payload: fileKey=uploads/魏振峰.xlsx shopId=shop-魏振峰 companyName=公司-魏振峰 platform=amazon totalRows=4 + country=美国 asinCount=2 + asin: row=3 value=b011 1 status=在售 + asin: row=5 value=B012 status=- + country=德国 asinCount=2 + asin: row=3 value=B021 status=- + asin: row=5 value=B022 status=在售 +== case=unmatched files=1 +task.insert: id=1001 moduleType=DELETE_BRAND status=FAILED taskMode=IMMEDIATE userId=7 createdBy=user:7 deviceId=dev-1 sourceFileCount=0 +task.update: id=1001 status=FAILED sourceFileCount=0 successFileCount=0 failedFileCount=1 errorMessage=全部文件解析或匹配失败 +result.insert: id=2001 taskId=1001 moduleType=DELETE_BRAND sourceFileUrl=uploads/魏振峰.xlsx rowCount=4 success=0 resultFilename=- errorMessage=- +vo: total=1 successCount=0 failedCount=1 +item: resultId=2001 fileKey=uploads/魏振峰.xlsx shopName=魏振峰 matched=false matchStatus=NOT_FOUND countryCount=2 totalRows=4 truncated=false success=true error=- taskStatus=RUNNING + parsed country=美国 asinCount=2 + parsed asin: row=3 value=b011 1 status=在售 + parsed asin: row=5 value=B012 status=- + parsed country=德国 asinCount=2 + parsed asin: row=3 value=B021 status=- + parsed asin: row=5 value=B022 status=在售 + preview: row=3 country=美国 asin=b011 1 status=在售 + preview: row=5 country=美国 asin=B012 status=- + preview: row=3 country=德国 asin=B021 status=- + preview: row=5 country=德国 asin=B022 status=在售 +payload.files=0 +== case=headerOnly files=1 +task.insert: id=1001 moduleType=DELETE_BRAND status=FAILED taskMode=IMMEDIATE userId=7 createdBy=user:7 deviceId=dev-1 sourceFileCount=0 +task.update: id=1001 status=FAILED sourceFileCount=0 successFileCount=0 failedFileCount=1 errorMessage=全部文件解析或匹配失败 +result.insert: id=2001 taskId=1001 moduleType=DELETE_BRAND sourceFileUrl=uploads/缺字段行.xlsx rowCount=- success=0 resultFilename=- errorMessage=Excel 表头不完整 +vo: total=1 successCount=0 failedCount=1 +item: resultId=2001 fileKey=uploads/缺字段行.xlsx shopName=缺字段行 matched=false matchStatus=- countryCount=- totalRows=- truncated=false success=false error=Excel 表头不完整 taskStatus=RUNNING +payload.files=0 +== case=shopNameColumnSkipped files=1 +task.insert: id=1001 moduleType=DELETE_BRAND status=RUNNING taskMode=IMMEDIATE userId=7 createdBy=user:7 deviceId=dev-1 sourceFileCount=1 +task.update: id=1001 status=RUNNING sourceFileCount=1 successFileCount=1 failedFileCount=0 errorMessage=- +result.insert: id=2001 taskId=1001 moduleType=DELETE_BRAND sourceFileUrl=uploads/英国.xlsx rowCount=3 success=0 resultFilename=- errorMessage=- +vo: total=1 successCount=1 failedCount=0 +item: resultId=2001 fileKey=uploads/英国.xlsx shopName=英国 matched=true matchStatus=MATCHED countryCount=1 totalRows=3 truncated=false success=true error=- taskStatus=RUNNING + parsed country=英国 asinCount=3 + parsed asin: row=2 value=ASIN status=状态 + parsed asin: row=3 value=B031 status=在售 + parsed asin: row=4 value=B032 status=缺少报价 + preview: row=2 country=英国 asin=ASIN status=状态 + preview: row=3 country=英国 asin=B031 status=在售 + preview: row=4 country=英国 asin=B032 status=缺少报价 +payload.files=1 + payload: fileKey=uploads/英国.xlsx shopId=shop-英国 companyName=公司-英国 platform=amazon totalRows=3 + country=英国 asinCount=3 + asin: row=2 value=ASIN status=状态 + asin: row=3 value=B031 status=在售 + asin: row=4 value=B032 status=缺少报价 +== case=notExcel files=1 +task.insert: id=1001 moduleType=DELETE_BRAND status=FAILED taskMode=IMMEDIATE userId=7 createdBy=user:7 deviceId=dev-1 sourceFileCount=0 +task.update: id=1001 status=FAILED sourceFileCount=0 successFileCount=0 failedFileCount=1 errorMessage=全部文件解析或匹配失败 +result.insert: id=2001 taskId=1001 moduleType=DELETE_BRAND sourceFileUrl=uploads/伪装.csv.xlsx rowCount=- success=0 resultFilename=- errorMessage=读取删除品牌 Excel 失败 +vo: total=1 successCount=0 failedCount=1 +item: resultId=2001 fileKey=uploads/伪装.csv.xlsx shopName=伪装.csv matched=false matchStatus=- countryCount=- totalRows=- truncated=false success=false error=读取删除品牌 Excel 失败 taskStatus=RUNNING +payload.files=0 diff --git a/backend-java/src/test/resources/pricetrack/golden/create-task-snapshot.txt b/backend-java/src/test/resources/pricetrack/golden/create-task-snapshot.txt new file mode 100644 index 000000000..9821fdfa1 --- /dev/null +++ b/backend-java/src/test/resources/pricetrack/golden/create-task-snapshot.txt @@ -0,0 +1,99 @@ +== case=asinModeTwoSheets +vo: taskId=1001 items=2 + item: resultId=2001 shopName=店铺A matched=true taskStatus=RUNNING success=false + item: resultId=2002 shopName=店铺B matched=true taskStatus=RUNNING success=false +vo: asinRowsByCountry=0 skipAsinsByCountry=0 minimumPriceByCountry=0 +task.insert: id=1001 moduleType=PRICE_TRACK taskMode=PYTHON_QUEUE status=RUNNING sourceFileCount=2 +task.update: id=1001 status=RUNNING requestJsonHasAsinFiles=true requestJsonHasListingStatuses=true resultJsonItems=2 +result.insert: id=2001 moduleType=PRICE_TRACK sourceFilename=店铺A sourceFileUrl=shop-店铺A success=0 +result.insert: id=2002 moduleType=PRICE_TRACK sourceFilename=店铺B sourceFileUrl=shop-店铺B success=0 +payloadSaves=1 + payload country=UK rows=2 + row: asin=B011 price=19.90 minimumPrice=18.00 + row: asin=b012 price=20.00 minimumPrice=- + payload country=DE rows=1 + row: asin=B021 price=5.00 +heartbeatTouches=1 +== case=singleSheetFallback +vo: taskId=1001 items=1 + item: resultId=2001 shopName=店铺A matched=true taskStatus=RUNNING success=false +vo: asinRowsByCountry=0 skipAsinsByCountry=0 minimumPriceByCountry=0 +task.insert: id=1001 moduleType=PRICE_TRACK taskMode=PYTHON_QUEUE status=RUNNING sourceFileCount=1 +task.update: id=1001 status=RUNNING requestJsonHasAsinFiles=true requestJsonHasListingStatuses=true resultJsonItems=1 +result.insert: id=2001 moduleType=PRICE_TRACK sourceFilename=店铺A sourceFileUrl=shop-店铺A success=0 +payloadSaves=1 + payload country=FR rows=1 + row: asin=B099 price=1.00 +heartbeatTouches=1 +== case=unknownSheetWithoutFallback +vo: taskId=1001 items=1 + item: resultId=2001 shopName=店铺A matched=true taskStatus=RUNNING success=false +vo: asinRowsByCountry=0 skipAsinsByCountry=0 minimumPriceByCountry=0 +task.insert: id=1001 moduleType=PRICE_TRACK taskMode=PYTHON_QUEUE status=RUNNING sourceFileCount=1 +task.update: id=1001 status=RUNNING requestJsonHasAsinFiles=true requestJsonHasListingStatuses=true resultJsonItems=1 +result.insert: id=2001 moduleType=PRICE_TRACK sourceFilename=店铺A sourceFileUrl=shop-店铺A success=0 +payloadSaves=1 +heartbeatTouches=1 +== case=multipleFilesSameCountryMerged +vo: taskId=1001 items=1 + item: resultId=2001 shopName=店铺A matched=true taskStatus=RUNNING success=false +vo: asinRowsByCountry=0 skipAsinsByCountry=0 minimumPriceByCountry=0 +task.insert: id=1001 moduleType=PRICE_TRACK taskMode=PYTHON_QUEUE status=RUNNING sourceFileCount=1 +task.update: id=1001 status=RUNNING requestJsonHasAsinFiles=true requestJsonHasListingStatuses=true resultJsonItems=1 +result.insert: id=2001 moduleType=PRICE_TRACK sourceFilename=店铺A sourceFileUrl=shop-店铺A success=0 +payloadSaves=1 + payload country=UK rows=2 + row: asin=B011 price=19.90 minimumPrice=18.00 + row: asin=b012 price=20.00 minimumPrice=- + payload country=DE rows=1 + row: asin=B021 price=5.00 + payload country=FR rows=1 + row: asin=B099 price=1.00 +heartbeatTouches=1 +== case=fileOutsideTempRootRejected +thrown: BusinessException message=ASIN 文件不存在或不可读: \outside.xlsx +payloadSaves=0 +heartbeatTouches=0 +== case=missingFile +thrown: BusinessException message=ASIN 文件不存在或不可读: 不存在的.xlsx +payloadSaves=0 +heartbeatTouches=0 +== case=blankPathSkipped +vo: taskId=1001 items=1 + item: resultId=2001 shopName=店铺A matched=true taskStatus=RUNNING success=false +vo: asinRowsByCountry=0 skipAsinsByCountry=0 minimumPriceByCountry=0 +task.insert: id=1001 moduleType=PRICE_TRACK taskMode=PYTHON_QUEUE status=RUNNING sourceFileCount=1 +task.update: id=1001 status=RUNNING requestJsonHasAsinFiles=true requestJsonHasListingStatuses=true resultJsonItems=1 +result.insert: id=2001 moduleType=PRICE_TRACK sourceFilename=店铺A sourceFileUrl=shop-店铺A success=0 +payloadSaves=1 + payload country=UK rows=2 + row: asin=B011 price=19.90 minimumPrice=18.00 + row: asin=b012 price=20.00 minimumPrice=- + payload country=DE rows=1 + row: asin=B021 price=5.00 +heartbeatTouches=1 +== case=statusMode +vo: taskId=1001 items=1 + item: resultId=2001 shopName=店铺A matched=true taskStatus=RUNNING success=false +vo: asinRowsByCountry=0 skipAsinsByCountry=0 minimumPriceByCountry=0 +task.insert: id=1001 moduleType=PRICE_TRACK taskMode=PYTHON_QUEUE status=RUNNING sourceFileCount=1 +task.update: id=1001 status=RUNNING requestJsonHasAsinFiles=false requestJsonHasListingStatuses=true resultJsonItems=1 +result.insert: id=2001 moduleType=PRICE_TRACK sourceFilename=店铺A sourceFileUrl=shop-店铺A success=0 +payloadSaves=0 +heartbeatTouches=1 +== case=bothModes +thrown: BusinessException message=跟价模式必须二选一 +payloadSaves=0 +heartbeatTouches=0 +== case=neitherMode +thrown: BusinessException message=跟价模式必须二选一 +payloadSaves=0 +heartbeatTouches=0 +== case=asinModeWithoutFiles +thrown: BusinessException message=自定义 ASIN 模式必须上传 ASIN 文件 +payloadSaves=0 +heartbeatTouches=0 +== case=unmatchedShop +thrown: BusinessException message=存在未匹配店铺,无法创建任务: 店铺C +payloadSaves=0 +heartbeatTouches=0 diff --git a/backend-java/src/test/resources/shopmatch/golden/create-task-snapshot.txt b/backend-java/src/test/resources/shopmatch/golden/create-task-snapshot.txt new file mode 100644 index 000000000..08647ab64 --- /dev/null +++ b/backend-java/src/test/resources/shopmatch/golden/create-task-snapshot.txt @@ -0,0 +1,45 @@ +== case=immediate +vo: taskId=1001 items=2 + item: resultId=2001 shopName=店铺A shopId=shop-店铺A matched=true matchStatus=MATCHED taskStatus=RUNNING success=false scheduledAtPresent=false + item: resultId=2002 shopName=店铺B shopId=shop-店铺B matched=true matchStatus=MATCHED taskStatus=RUNNING success=false scheduledAtPresent=false +task.insert: id=1001 moduleType=SHOP_MATCH taskMode=PYTHON_QUEUE status=RUNNING sourceFileCount=2 deviceId=dev-1 scheduledAtPresent=false +task.update: id=1001 status=RUNNING requestJsonStageIndexZero=false requestJsonStageIndexNull=true resultJsonResultIds=2 +result.insert: id=2001 taskId=1001 moduleType=SHOP_MATCH sourceFilename=店铺A sourceFileUrl=shop-店铺A success=0 +result.insert: id=2002 taskId=1001 moduleType=SHOP_MATCH sourceFilename=店铺B sourceFileUrl=shop-店铺B success=0 +heartbeatTouches=1 +== case=usNotSupportedCountry +thrown: BusinessException message=非法国家代码: US +heartbeatTouches=0 +== case=dedupedByNormalizedName +vo: taskId=1001 items=2 + item: resultId=2001 shopName=店铺A shopId=shop-店铺A matched=true matchStatus=MATCHED taskStatus=RUNNING success=false scheduledAtPresent=false + item: resultId=2002 shopName=店铺B shopId=shop-店铺B matched=true matchStatus=MATCHED taskStatus=RUNNING success=false scheduledAtPresent=false +task.insert: id=1001 moduleType=SHOP_MATCH taskMode=PYTHON_QUEUE status=RUNNING sourceFileCount=2 deviceId=dev-1 scheduledAtPresent=false +task.update: id=1001 status=RUNNING requestJsonStageIndexZero=false requestJsonStageIndexNull=true resultJsonResultIds=2 +result.insert: id=2001 taskId=1001 moduleType=SHOP_MATCH sourceFilename=店铺A sourceFileUrl=shop-店铺A success=0 +result.insert: id=2002 taskId=1001 moduleType=SHOP_MATCH sourceFilename=店铺B sourceFileUrl=shop-店铺B success=0 +heartbeatTouches=1 +== case=scheduled +vo: taskId=1001 items=2 + item: resultId=2001 shopName=店铺A shopId=shop-店铺A matched=true matchStatus=MATCHED taskStatus=SCHEDULED success=false scheduledAtPresent=false + item: resultId=2002 shopName=店铺B shopId=shop-店铺B matched=true matchStatus=MATCHED taskStatus=SCHEDULED success=false scheduledAtPresent=false +task.insert: id=1001 moduleType=SHOP_MATCH taskMode=PYTHON_QUEUE status=SCHEDULED sourceFileCount=2 deviceId=dev-1 scheduledAtPresent=true +task.update: id=1001 status=SCHEDULED requestJsonStageIndexZero=true requestJsonStageIndexNull=false resultJsonResultIds=2 +result.insert: id=2001 taskId=1001 moduleType=SHOP_MATCH sourceFilename=店铺A sourceFileUrl=shop-店铺A success=0 +result.insert: id=2002 taskId=1001 moduleType=SHOP_MATCH sourceFilename=店铺B sourceFileUrl=shop-店铺B success=0 +heartbeatTouches=0 +== case=unmatchedShop +thrown: BusinessException message=存在未匹配店铺,无法创建任务 +heartbeatTouches=0 +== case=badCountryCode +thrown: BusinessException message=非法国家代码: CN +heartbeatTouches=0 +== case=emptyCountryCodes +thrown: BusinessException message=country_codes 不能为空 +heartbeatTouches=0 +== case=scheduleInPast +thrown: BusinessException message=schedule_times 必须晚于当前时间 +heartbeatTouches=0 +== case=scheduleNotIncreasing +thrown: BusinessException message=schedule_times 必须按时间递增 +heartbeatTouches=0