#!/usr/bin/env node /** * Purpose: 无浏览器自测二级减速器工具包(目录、分组、前置、参数校验)。 * * Usage: node tools/reducer/self-test.mjs * Exit: 0 全部通过 / 1 首个失败 */ import assert from "node:assert/strict"; import fs from "node:fs"; import path from "node:path"; import { REPO_ROOT, PROJECT_FILE, find, groups, parts, projectManifest, stats } from "./catalog.mjs"; import { preflight } from "./preflight.mjs"; let failures = 0; function check(name, fn) { try { fn(); console.log(`ok ${name}`); } catch (error) { failures += 1; console.error(`FAIL ${name}\n ${error instanceof Error ? error.message.split("\n")[0] : error}`); } } 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`); } check("project manifest is the single source", () => { const manifest = projectManifest(); assert.equal(manifest.parts.length, 23); assert.ok(fs.existsSync(PROJECT_FILE)); }); check("catalog joins steps and replay for every part", () => { const all = parts(); assert.equal(all.length, 23); assert.equal(stats().steps, 104); for (const part of all) { assert.ok(part.steps > 0, `${part.id} steps`); assert.ok(part.volume > 0, `${part.id} volume`); assert.ok(part.reconstructable, `${part.id} reconstructable`); assert.ok(fs.existsSync(path.join(REPO_ROOT, part.stepsFile)), `${part.id} stepsFile`); } }); check("groups cover all 23 parts", () => { const grouped = groups().reduce((sum, group) => sum + group.count, 0); assert.equal(grouped, 23); assert.ok(groups().some((group) => group.id === "bearings" && group.count === 9)); assert.ok(groups().some((group) => group.id === "gears" && group.count === 2)); }); check("find resolves by id and by document", () => { assert.equal(find("P06").document, "Bearing6206_Inner"); assert.equal(find("p06").document, "Bearing6206_Inner"); assert.equal(find("HousingCover").id, "P22"); }); check("find rejects unknown ids and shell fragments", () => { throwsCode(() => find("P99"), "E_PART"); throwsCode(() => find("; rm -rf /"), "E_PART"); throwsCode(() => find("P01 --headed"), "E_PART"); throwsCode(() => find(""), "E_PART"); throwsCode(() => find(undefined), "E_PART"); }); // preflight is async, so it runs before the assertions that depend on it. const report = await preflight(); check("preflight checks are well formed", () => { assert.equal(typeof report.ok, "boolean"); assert.ok(report.checks.length >= 7, `checks ${report.checks.length}`); for (const item of report.checks) assert.equal(typeof item.ok, "boolean", item.id); assert.match(report.origin, /^http:\/\/127\.0\.0\.1:\d+$/); assert.equal(report.stats.parts, 23); for (const id of ["dist", "kernel", "chrome", "display", "playwright", "engine", "parts", "app"]) { assert.ok(report.checks.some((item) => item.id === id), `missing check ${id}`); } }); if (failures) { console.error(`\nself-test: ${failures} failure(s)`); process.exit(1); } console.log(`self-test: OK(${stats().parts} 件 / ${stats().steps} 步;前置 ${report.ok ? "全部满足" : "有缺项"})`);