#!/usr/bin/env node /** * Purpose: 无浏览器自测航空母舰工具包的数据契约。 * * 只检查磁盘上的清单与证据是否自洽:spec 是唯一零件来源、project.json 与 * spec 一致、分组完整、标识解析严格、ui-steps 与 replay 步数对应、体积 * 正数且与目录一致。真正的界面重建由 create-part / create-all 负责。 * * Usage: node tools/carrier/self-test.mjs [--json] * Exit: 0 全部通过 / 1 有失败 */ import assert from "node:assert/strict"; import fs from "node:fs"; import path from "node:path"; import { REPO_ROOT, SPEC_FILE, PROJECT_FILE, find, groups, parts, projectManifest, spec, stats } from "./catalog.mjs"; const asJson = process.argv.includes("--json"); const results = []; let failures = 0; function check(name, fn) { try { fn(); results.push({ name, ok: true }); if (!asJson) console.log(`ok ${name}`); } catch (error) { failures += 1; const message = error instanceof Error ? error.message.split("\n")[0] : String(error); results.push({ name, ok: false, message }); if (!asJson) console.error(`FAIL ${name}\n ${message}`); } } function throwsCode(fn, code) { try { fn(); } catch (error) { assert.equal(error.code, code, `expected ${code}, got ${error.code ?? error.message}`); return; } throw new Error(`expected ${code}, nothing thrown`); } const readJson = file => JSON.parse(fs.readFileSync(file, "utf8")); check("spec 是唯一零件来源且格式完整", () => { const manifest = spec(); assert.ok(fs.existsSync(SPEC_FILE)); assert.equal(manifest.parts.length, 20, `expected 20 parts, got ${manifest.parts.length}`); const ids = manifest.parts.map(part => part.id); const documents = manifest.parts.map(part => part.document); assert.equal(new Set(ids).size, ids.length, "duplicate part ids"); assert.equal(new Set(documents).size, documents.length, "duplicate documents"); ids.forEach(id => assert.match(id, /^P\d{2}$/, `bad id ${id}`)); for (const part of manifest.parts) { assert.ok(part.title && part.system && Array.isArray(part.featureTypes) && part.featureTypes.length, `${part.id} 缺少 title/system/featureTypes`); assert.ok(Object.hasOwn(manifest.systems ?? {}, part.system), `${part.id} 的 system ${part.system} 不在 systems 里`); } }); check("project.json 与 spec 一致", () => { const manifest = projectManifest(); const wanted = spec().parts.map(part => `${part.id}:${part.document}`).sort(); const got = manifest.parts.map(part => `${part.id}:${part.document}`).sort(); assert.deepEqual(got, wanted); for (const key of ["replayDir", "sampleDir", "outDir", "stillsDir"]) { assert.ok(typeof manifest.paths?.[key] === "string" && manifest.paths[key], `paths.${key} 缺失`); } assert.match(manifest.appOrigin, /^http:\/\/127\.0\.0\.1:\d+$/); assert.ok(fs.existsSync(PROJECT_FILE)); }); check("分组覆盖全部零件且不重复", () => { const all = parts(); const grouped = groups().flatMap(group => group.ids); assert.equal(grouped.length, all.length); assert.deepEqual([...grouped].sort(), all.map(part => part.id).sort()); for (const group of groups()) assert.ok(group.label && group.count > 0, `分组 ${group.id} 无标签或为空`); }); check("目录统计与零件列表一致", () => { const summary = stats(); const all = parts(); assert.equal(summary.parts, all.length); assert.equal(summary.steps, all.reduce((sum, part) => sum + part.steps, 0)); assert.equal(summary.reconstructable, all.filter(part => part.reconstructable).length); }); check("标识解析:id / Document 大小写不敏感,非法输入拒绝", () => { const first = parts()[0]; assert.equal(find(first.id).id, first.id); assert.equal(find(first.id.toLowerCase()).id, first.id); assert.equal(find(first.document).id, first.id); throwsCode(() => find("P999"), "E_PART"); throwsCode(() => find("P02; rm -rf /"), "E_PART"); throwsCode(() => find(""), "E_PART"); throwsCode(() => find(undefined), "E_PART"); }); check("有界面步骤的零件必须有对应构建记录,且步数一致", () => { for (const part of parts()) { if (!part.steps) continue; assert.ok(fs.existsSync(path.join(REPO_ROOT, part.stepsFile)), `${part.id} 缺 ${part.stepsFile}`); const steps = readJson(path.join(REPO_ROOT, part.stepsFile)).steps; assert.equal(steps.length, part.steps, `${part.id} steps 字段与文件不一致`); assert.ok(part.reconstructable, `${part.id} 有界面步骤但目录判为不可重建`); assert.ok(part.volume > 0, `${part.id} 没有正的构建记录体积`); } }); check("ui-steps 与 replay 逐步对应(步数、顺序、种类)", () => { const KIND = new Set(["sketch", "feature", "dressup", "transform", "primitive"]); for (const part of parts().filter(item => item.reconstructable)) { const uiSteps = readJson(path.join(REPO_ROOT, part.stepsFile)).steps; const replay = readJson(path.join(REPO_ROOT, "web/teaching/cases/carrier-partdesign/out/replay", `${part.document}.json`)); assert.ok(Array.isArray(replay?.steps), `${part.document} replay 缺 steps`); assert.equal(uiSteps.length, replay.steps.length, `${part.document} 步数不等`); uiSteps.forEach((step, index) => { assert.ok(KIND.has(step.kind), `${part.document}#${index + 1} 未知 kind ${step.kind}`); assert.equal(step.step, index + 1, `${part.document} 步骤序号不连续`); assert.ok(step.command, `${part.document}#${step.step} 缺 command`); }); const kinds = new Set(uiSteps.map(step => step.kind)); for (const featureType of part.featureTypes) { const mapped = featureType === "Pad" || featureType === "Pocket" || featureType === "Loft" || featureType === "Pipe" || featureType === "Hole" || featureType === "Revolution" ? kinds.has("feature") : featureType === "Fillet" || featureType === "Chamfer" ? kinds.has("dressup") : featureType === "Mirrored" || featureType === "LinearPattern" || featureType === "PolarPattern" ? kinds.has("transform") : true; assert.ok(mapped, `${part.document} 宣称 ${featureType} 但界面步骤里没有对应动作`); } } }); if (asJson) { console.log(JSON.stringify({ schema: "freecad-web/carrier-toolkit/self-test/v1", ok: failures === 0, checks: results }, null, 2)); } else { const summary = stats(); console.log(`\n${failures === 0 ? "全部通过" : `${failures} 项失败`}:${summary.parts} 件 / ${summary.steps} 步 / 可界面重建 ${summary.reconstructable} 件 / 体积合计 ${summary.volume.toFixed(0)} m³`); } process.exit(failures === 0 ? 0 : 1);