#!/usr/bin/env node /** * No-browser self test for the teaching console core. * * Covers the catalog, the curriculum wiring, every validation path that guards * spawn(), and the argv templates. It never starts a browser or a suite run — * the live gate is check-console.mjs. * * Usage: node web/tools/teaching-console/self-test.mjs * Exit: 0 all assertions pass, 1 the first failure. */ import assert from "node:assert/strict"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { loadCatalog } from "./catalog.mjs"; import { buildLessons } from "./lessons.mjs"; import { REPO_ROOT, buildJobArgv, catalog, parseProgressLine, preflight, validateCaseIds, validateProbeUrl, } from "../../teaching/suite-runner.mjs"; const HERE = path.dirname(fileURLToPath(import.meta.url)); let failures = 0; function check(name, fn) { try { fn(); console.log(`ok ${name}`); } catch (error) { failures += 1; console.error(`FAIL ${name}`); console.error(` ${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`); } const cases = loadCatalog(); const curriculum = buildLessons(cases); const byId = new Map(curriculum.lessons.map((lesson) => [lesson.id, lesson])); check("catalog derives all four layers", () => { assert.equal(cases.shell.length, 5); assert.equal(cases.chapters.length, 29); assert.equal(cases.modules.length, 31); assert.equal(cases.crosscut.length, 6); assert.equal(new Set(cases.ids).size, cases.ids.length); }); check("every lesson references catalog ids only", () => { const known = new Set(cases.ids); for (const lesson of curriculum.lessons) { if (lesson.job.kind !== "suite" || !lesson.job.only) continue; for (const id of lesson.job.only) assert.ok(known.has(id), `${lesson.id} → ${id}`); } }); check("lesson ids are unique and groups exist", () => { assert.equal(new Set(curriculum.lessons.map((lesson) => lesson.id)).size, curriculum.lessons.length); const groups = new Set(curriculum.groups.map((group) => group.id)); for (const lesson of curriculum.lessons) assert.ok(groups.has(lesson.groupId), `${lesson.id} → ${lesson.groupId}`); assert.ok(curriculum.lessons.length >= 40, `only ${curriculum.lessons.length} lessons`); }); check("validateCaseIds accepts catalog ids", () => { assert.deepEqual(validateCaseIds(["SHELL-01", "MOD-Part-01"], cases, 8), ["SHELL-01", "MOD-Part-01"]); }); check("validateCaseIds rejects unknown ids", () => { throwsCode(() => validateCaseIds(["SHELL-99"], cases, 8), "E_PARAM"); }); check("validateCaseIds rejects injection and empty input", () => { throwsCode(() => validateCaseIds(["; rm -rf /"], cases, 8), "E_PARAM"); throwsCode(() => validateCaseIds([], cases, 8), "E_PARAM"); throwsCode(() => validateCaseIds("SHELL-01", cases, 8), "E_PARAM"); }); check("validateCaseIds enforces the cap", () => { throwsCode(() => validateCaseIds(["SHELL-01", "SHELL-02", "SHELL-03"], cases, 2), "E_PARAM"); }); check("validateProbeUrl whitelists pages", () => { assert.equal(validateProbeUrl("/partdesign.html"), "/partdesign.html"); assert.equal(validateProbeUrl("/workbench.html?module=Robot"), "/workbench.html?module=Robot"); throwsCode(() => validateProbeUrl("/../../etc/passwd"), "E_PARAM"); throwsCode(() => validateProbeUrl("http://evil.example/"), "E_PARAM"); throwsCode(() => validateProbeUrl("/workbench.html?module=OpenSCAD"), "E_PARAM"); }); check("suite argv carries only whitelisted flags", () => { const lesson = byId.get("lesson-part-boolean"); const { argv } = buildJobArgv(lesson, {}, { jobDir: "/tmp/job-x" }); assert.equal(argv[0], process.execPath); assert.ok(argv.includes("--only")); assert.equal(argv[argv.indexOf("--only") + 1], "CH-ch02-part-primitives-boolean"); assert.ok(argv.includes("--out")); assert.ok(String(argv[argv.indexOf("--out") + 1]).startsWith("/tmp/job-x")); for (const part of argv) assert.ok(!part.includes(";"), `unexpected shell token in ${part}`); }); check("probe argv pins root, url and viewport", () => { const lesson = byId.get("lesson-02-layout-probe"); const { argv } = buildJobArgv(lesson, {}, { jobDir: "/tmp/job-y" }); assert.ok(String(argv[argv.indexOf("--root") + 1]).endsWith(path.join("web", "dist"))); assert.equal(argv[argv.indexOf("--url") + 1], "/partdesign.html"); assert.equal(argv[argv.indexOf("--viewport") + 1], "1440x1000"); }); check("free-practice honours bounded parameters", () => { const lesson = byId.get("lesson-practice"); const { argv } = buildJobArgv(lesson, { only: ["SHELL-01"], slow: 300, highlight: true, screenshots: "all" }, { jobDir: "/tmp/job-z" }); assert.equal(argv[argv.indexOf("--slow") + 1], "300"); assert.ok(argv.includes("--highlight")); assert.equal(argv[argv.indexOf("--screenshots") + 1], "all"); throwsCode(() => buildJobArgv(lesson, { only: ["SHELL-01"], slow: 99999 }, { jobDir: "/tmp/job-z" }), "E_PARAM"); throwsCode(() => buildJobArgv(lesson, { only: ["NOPE-01"] }, { jobDir: "/tmp/job-z" }), "E_PARAM"); }); check("host argv binds an ephemeral port", () => { const lesson = byId.get("lesson-01-static-host"); const { argv } = buildJobArgv(lesson, {}, { jobDir: "/tmp/job-h" }); assert.equal(argv[argv.indexOf("--port") + 1], "0"); }); check("preflight report has boolean checks", () => { const report = preflight(); assert.equal(typeof report.ok, "boolean"); assert.ok(report.checks.length >= 6); for (const item of report.checks) assert.equal(typeof item.ok, "boolean", item.id); }); check("progress lines parse without trusting text", () => { assert.deepEqual(parseProgressLine("SHELL-01: passed (3.2s)"), { id: "SHELL-01", status: "passed" }); assert.deepEqual(parseProgressLine("MOD-Fem-01: blocked (0s) [fixture]"), { id: "MOD-Fem-01", status: "blocked" }); assert.equal(parseProgressLine("fatal: boom"), null); assert.equal(parseProgressLine("legacy: 3 case(s) imported"), null); }); check("catalog() caches and exposes lessons by id", () => { assert.equal(catalog().byId.get("lesson-shell")?.job.kind, "suite"); assert.ok(REPO_ROOT.endsWith(path.join("working", "FreeCAD"))); }); if (failures) { console.error(`\nself-test: ${failures} failure(s)`); process.exit(1); } console.log(`self-test: OK (${curriculum.lessons.length} lessons, ${cases.ids.length} cases)`);