const assert = require("node:assert/strict"); const fs = require("node:fs"); const path = require("node:path"); const test = require("node:test"); const root = path.resolve(__dirname, ".."); const viewsDir = path.join(root, "src", "views"); /* * 步骤答案分列存储的回归防线。 * * 背景:历史上多个实训页把整个表单(所有步骤的数据)序列化进 step1Answer, * 导致按步骤读取的链路(AI 逐项评分、student_training_step_answer 明细表、教师分步批阅) * 只能读到步骤一的内容。现统一约定: * 1) 有字段映射表的实训必须走 splitTrainingStepAnswers 分列保存; * 2) 其余页面各自实现"每步写自己的 stepNAnswer 列",登记在下方白名单; * 3) 自定义步骤统一写 dynamicStepAnswers(按步骤 ID 键控)。 * 新增保存调用点而没有分列实现的页面会让本测试失败——这是有意的关卡。 */ // 各页自带"每步一列"保存实现的页面(step1 只含本步骤内容,已逐一人工核对)。 const perStepAllowlist = new Set([ "assessment/diagnosis.vue", "assessment/optimization.vue", "channel/supplier.vue", "demand/business-feasibility.vue", "demand/competitive-environment-analysis.vue", "demand/industry-demand-analysis.vue", "foundation/new-product-survey.vue", "foundation/product-development-factors.vue", "foundation/product-development-process.vue", "product/after-sales-service.vue", "product/hardware-cost.vue", "product/market-opportunity-selection.vue", "product/price.vue", "product/requirements-doc.vue", "product/structure.vue", "test/index.vue", "test/promotion.vue", ]); // 不写 stepNAnswer 列的特殊组件: // - CustomTrainingStepPage:自定义步骤按步骤 ID 写 dynamicStepAnswers; // - TrainingTaskToolbar:仅对无结构页的任务兜底保存 DOM 快照到 dynamicStepAnswers, // 内置任务直接拒绝降级(后端 TrainingStepAnswerOwnership 也会拒绝快照格式)。 const specialAllowlist = new Set([ "components/TrainingTaskToolbar.vue", "training/CustomTrainingStepPage.vue", ]); function collectVueFiles(dir, files = []) { for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { const full = path.join(dir, entry.name); if (entry.isDirectory()) collectVueFiles(full, files); else if (entry.name.endsWith(".vue")) files.push(full); } return files; } function toRelative(file) { return path.relative(viewsDir, file).split(path.sep).join("/"); } test("每个保存学生答案的调用点必须走统一拆分或登记在白名单", () => { const callers = collectVueFiles(viewsDir) .filter((file) => fs.readFileSync(file, "utf8").includes("saveStudentTrainingAnswer(")) .map(toRelative); const offenders = callers.filter((relative) => { if (perStepAllowlist.has(relative) || specialAllowlist.has(relative)) return false; const source = fs.readFileSync(path.join(viewsDir, relative), "utf8"); return !source.includes("splitTrainingStepAnswers"); }); assert.deepEqual( offenders, [], "以下页面调用了 saveStudentTrainingAnswer 但没有分列实现,也未登记白名单:" + "请改用 splitTrainingStepAnswers(并在 trainingStepAnswerFields.json 登记字段映射," + "同时在后端 TrainingStepAnswerOwnership 注册),或补齐每步一列的保存实现后登记白名单", ); }); test("白名单页面必须真实存在且仍调用保存接口", () => { for (const relative of [...perStepAllowlist, ...specialAllowlist]) { const file = path.join(viewsDir, relative); assert.ok(fs.existsSync(file), `白名单页面不存在(请同步维护本测试的清单): ${relative}`); assert.match(fs.readFileSync(file, "utf8"), /saveStudentTrainingAnswer\(/, relative); } }); test("字段映射表不得把同一字段登记到多个步骤", () => { const fields = JSON.parse(fs.readFileSync( path.join(root, "src/views/training/trainingStepAnswerFields.json"), "utf8")); assert.ok(Object.keys(fields).length >= 7, "字段映射表应至少覆盖已迁移的 7 个任务"); for (const [task, panels] of Object.entries(fields)) { const seen = new Map(); panels.forEach((panelFields, index) => { for (const field of panelFields) { assert.ok(!seen.has(field), `${task} 的字段 ${field} 同时登记在第 ${seen.get(field)} 步和第 ${index + 1} 步`); seen.set(field, index + 1); } }); } }); test("后端守卫必须注册字段映射表中的全部任务(跨仓库契约)", () => { const fields = JSON.parse(fs.readFileSync( path.join(root, "src/views/training/trainingStepAnswerFields.json"), "utf8")); const ownership = path.resolve(root, "../link_commerce/src/main/java/com/sztzjy/linkCommerce/training/answer/TrainingStepAnswerOwnership.java"); assert.ok(fs.existsSync(ownership), "后端 TrainingStepAnswerOwnership.java 不存在"); const source = fs.readFileSync(ownership, "utf8"); for (const task of Object.keys(fields)) { assert.ok(source.includes(`register("${task}"`), `后端守卫未注册任务 ${task},前端分列保存会被后端放行而失去保护`); } }); test("已删除的整表单快照页不得复活", () => { const removedPages = [ "demand/index.vue", "demand/environment.vue", "demand/user.vue", "product/index.vue", "product/components/ProductStandardTrainingPage.vue", "assessment/index.vue", "assessment/kano.vue", "assessment/five.vue", "assessment/traffic.vue", "assessment/market.vue", "test/channel.vue", "test/marketing.vue", "components/TrainingLegacyActions.vue", ]; for (const relative of removedPages) { assert.ok(!fs.existsSync(path.join(viewsDir, relative)), `已删除的旧快照页重新出现: ${relative}`); } });