/** * Build-time case catalog for the teaching console. * * Derives the real case ids from their sources instead of copying them: * - P0 shell ids and P3 crosscut ids are regex-extracted from the suites; * - P1 chapter ids are `CH--` from web/manual/scenarios; * - P2 module ids are `MOD--01` from the capability registry, minus * the two modules the suite itself skips (OpenSCAD is not implemented on * the Web, AddonManager has no interactive command). * * Importing the suite modules directly would pull in Playwright; the builder * must stay runnable without a browser. */ import fs from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; const HERE = path.dirname(fileURLToPath(import.meta.url)); export const REPO_ROOT = path.resolve(HERE, "../../.."); const SCENARIO_DIR = path.join(HERE, "../../manual/scenarios"); const AND_UP = fs.existsSync(SCENARIO_DIR) ? null : "web/manual/scenarios 不存在"; const REGISTRY = path.join(REPO_ROOT, "docs/current/feature-capability-registry.json"); const SKIP_MODULES = new Set(["OpenSCAD", "AddonManager"]); function readIds(file) { const source = fs.readFileSync(file, "utf8"); return [...source.matchAll(/id:\s*"([A-Z][A-Za-z0-9-]*)"/g)].map((match) => match[1]); } function loadChapters() { const fromScenarios = fs.readdirSync(SCENARIO_DIR) .filter((name) => name.endsWith(".json")) .sort() .map((name) => { const scenario = JSON.parse(fs.readFileSync(path.join(SCENARIO_DIR, name), "utf8")); return { id: `CH-${scenario.chapter}-${scenario.workflow}`, chapter: scenario.chapter, workflow: scenario.workflow, title: scenario.title ?? scenario.workflow, }; }); // Two P1 cases are handwritten (production line + help centre), not scenarios. const source = fs.readFileSync(path.join(REPO_ROOT, "tools/functional-suite/cases/handwritten.mjs"), "utf8"); const handwritten = [...source.matchAll(/id:\s*"(CH-[^"]+)"[\s\S]{0,200}?title:\s*"([^"]+)"/g)].map((match) => { const [, id, title] = match; const rest = id.slice("CH-".length); const separator = rest.indexOf("-"); return { id, chapter: rest.slice(0, separator), workflow: rest.slice(separator + 1), title }; }); return [...fromScenarios, ...handwritten]; } function loadModules() { const registry = JSON.parse(fs.readFileSync(REGISTRY, "utf8")); return registry.modules .map((meta) => meta.module) .filter((name) => !SKIP_MODULES.has(name)) .sort(); } export function loadCatalog() { if (AND_UP) throw new Error(`catalog: ${AND_UP}`); const shell = readIds(path.join(REPO_ROOT, "tools/functional-suite/cases/shell.mjs")); const crosscut = readIds(path.join(REPO_ROOT, "tools/functional-suite/cases/crosscut.mjs")); const chapters = loadChapters(); const modules = loadModules(); const ids = new Set([ ...shell, ...crosscut, ...chapters.map((item) => item.id), ...modules.map((name) => `MOD-${name}-01`), ]); return { schema: "freecad-web/teaching-console-catalog/v1", shell, crosscut, chapters, modules, ids: [...ids].sort(), counts: { shell: shell.length, chapters: chapters.length, modules: modules.length, crosscut: crosscut.length }, }; }