Files
pplus-core/tests/curriculum.test.mjs
T
alexmandClaude c5157fcad5 Move the engine core into core/, a project of its own (pplus-core)
What programs mean (the languages, translators, Mojo checker and
interpreter, prover, fuzzer, machine model, catalog problems and the Mojo
corpus with godbolt's stored answers) was mixed into the site's tree, so a
change to the prover and a change to a button went through the same gate
and the same docs. This moves all of it under core/, laid out as its own
repository, ready to be extracted with its history and pinned here as a
git submodule.

- core/: src/{engines,services,sync,mojo,exercises,curriculum,common},
  mojo/, syntax/mojo.xml, its tests (with Workspace and workForProblem in
  place of the site's Session), scripts (corpus pipeline, play, the
  exercise and probe starters, engines export/diff), and its docs
  (engines, trust, mojo-corpus, exercises, coverage). Its own pixi.toml,
  package.json, README, AGENTS.md and CI workflow.
- core/tests/boundary.test.mjs: library modules use no DOM, import no
  page widget, and import nothing outside src/.
- core/scripts/cli.mjs: pixi run cli check|run|prove|fuzz|translate.
- core/scripts/dafny.py: Dafny 4.11.0 pinned by version and SHA-256 per
  platform, fetched into .tools/ (pixi run dafny-fetch, pixi run dafny).
- The site imports core only as #core/<path>.js: an import map in
  index.html, "imports" in package.json for node, core's declarations for
  tsc. The site's TypeScript builds core first (npm run build).
- assemble.sh, the Quarto engine copy, the render cache key, check-pages,
  quarto-export and quarto-css read core/dist and core/mojo; quarto-css
  copies core's syntax/mojo.xml into the extension.
- Site tests that covered engines moved to core; the rest keep Session.
- README, AGENTS.md, docs/app.md, docs/deploy.md and the CI workflow
  describe the split (checkout with submodules; core's tests and corpus
  status run against the pinned commit).

Gate: site 110 pass; core 307 pass; corpus status --strict passes.
Manual check: the assembled site opens Bridge (count-base-proved),
Developer and Hardware (count-base) with the editor, the stepper and the
board, and no module errors.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FgvbRdoEheAHqsRmr2WuKZ
2026-10-05 05:43:58 +00:00

118 lines
7.0 KiB
JavaScript

// The lesson corpus verifies the interpreter: the dependency map must be strict and every program must run.
import test from 'node:test';
import assert from 'node:assert/strict';
import { LESSONS, CONCEPTS, CONCEPT_BY_ID, FLAVORS, teacherOf, prerequisitesOf, depthOf, allPrerequisitesOf, statusOf, solutionOf, starterOf } from '../dist/curriculum/index.js';
import { studyOf, runPseudo } from '../dist/services/pseudo.js';
import { problemById } from '../dist/common/problems.js';
const conceptIds = new Set(CONCEPTS.map((c) => c.id));
const flavorIds = new Set(FLAVORS.map((f) => f.id));
test('lesson ids are unique and every concept id is real', () => {
const ids = new Set();
for (const L of LESSONS) {
assert.ok(!ids.has(L.id), `duplicate lesson id ${L.id}`); ids.add(L.id);
for (const c of [...L.teaches, ...L.requires]) assert.ok(conceptIds.has(c), `${L.id} names unknown concept ${c}`);
assert.ok(L.teaches.length >= 1, `${L.id} teaches nothing`);
assert.ok(L.purpose.length > 20 && L.explanation.length >= 3 && L.checks.length >= 2 && L.flavors.length >= 2, `${L.id} is thin`);
for (const f of L.flavors) assert.ok(flavorIds.has(f.id), `${L.id} has unknown flavor ${f.id}`);
for (const c of L.checks) assert.ok(c.answer >= 0 && c.answer < c.options.length, `${L.id} check answer out of range`);
for (const p of L.problems || []) assert.ok(problemById(p), `${L.id} points at missing problem ${p}`);
if (L.showAsmOf) assert.ok(problemById(L.showAsmOf)?.sources.asm, `${L.id} shows asm of a problem without any`);
}
});
test('every concept is taught by exactly one lesson, and every concept is used', () => {
for (const c of CONCEPTS) {
const teachers = LESSONS.filter((l) => l.teaches.includes(c.id));
assert.equal(teachers.length, 1, `${c.id} is taught by ${teachers.map((l) => l.id).join(', ') || 'nobody'}`);
}
// every foundations and core concept must feed a later lesson; only advanced concepts may be frontier
const required = new Set(LESSONS.flatMap((l) => l.requires));
for (const L of LESSONS.filter((l) => l.stage !== 'advanced')) for (const c of L.teaches) assert.ok(required.has(c), `${c} (from ${L.id}) is never required by a later lesson`);
});
test('the dependency map is strict: every requirement is taught by an earlier lesson, and there are no cycles', () => {
const seen = new Set();
for (const L of LESSONS) {
for (const c of L.requires) {
const t = teacherOf(c);
assert.ok(t, `${L.id} requires ${c}, which nothing teaches`);
assert.ok(seen.has(t.id), `${L.id} requires ${c}, taught by ${t.id}, which comes later in the order`);
assert.ok(!L.teaches.includes(c), `${L.id} both teaches and requires ${c}`);
}
seen.add(L.id);
}
for (const L of LESSONS) assert.ok(depthOf(L) >= 0, `${L.id} is in a cycle`);
const roots = LESSONS.filter((l) => l.requires.length === 0);
assert.equal(roots.length, 1, `exactly one lesson should assume nothing: ${roots.map((l) => l.id).join(', ')}`);
for (const L of LESSONS.slice(1)) assert.ok(allPrerequisitesOf(L).some((p) => p.id === roots[0].id), `${L.id} does not trace back to ${roots[0].id}`);
});
test('a lesson is available exactly when every prerequisite lesson is done', () => {
const none = { done: [], checks: {} };
const first = LESSONS[0];
assert.equal(statusOf(first, none), 'available');
assert.equal(statusOf(LESSONS[LESSONS.length - 1], none), 'locked');
const last = LESSONS[LESSONS.length - 1];
const all = { done: allPrerequisitesOf(last).map((l) => l.id), checks: {} };
assert.equal(statusOf(last, all), 'available');
const direct = prerequisitesOf(last)[0].id;
const butOne = { done: all.done.filter((id) => id !== direct), checks: {} };
assert.notEqual(statusOf(last, butOne), 'available');
const order = ['foundations', 'core', 'advanced'];
for (const L of LESSONS) for (const p of prerequisitesOf(L)) assert.ok(order.indexOf(p.stage) <= order.indexOf(L.stage), `${L.id} depends on a later stage`);
});
function runProgram(label, prog) {
const st = studyOf(solutionOf(prog));
const errs = st.diagnostics.filter((d) => d.severity === 'error');
assert.equal(errs.length, 0, `${label}: ${errs.map((d) => `L${d.line} ${d.id}: ${d.message}`).join(' | ')}`);
const tr = runPseudo(st, { input: prog.input || [], maxSteps: 200000 });
const out = tr.output.filter((l) => !/^\w+ returned /.test(l));
if (prog.stops) { assert.equal(tr.error?.kind, prog.stops, `${label}: expected a ${prog.stops} stop, got ${tr.error?.kind || 'none'}`); }
else assert.equal(tr.error, null, `${label}: ${tr.error?.message}`);
assert.deepEqual(out, prog.expect, label);
return st;
}
for (const L of LESSONS) {
test(`lesson ${L.id}: the example and every flavor run and print what they say`, () => {
runProgram(`${L.id}/example`, L.example);
for (const f of L.flavors) {
runProgram(`${L.id}/${f.id}`, f.solution);
const starter = f.starter ?? starterOf(f.solution);
assert.ok(starter.length > 0);
if (!f.starter) assert.ok(/\/\/ TODO:/.test(starter) || !/\/\/ ~/.test(f.solution.pseudo), `${L.id}/${f.id}: starter has no blanks`);
assert.ok(!/\/\/ ~/.test(starter), `${L.id}/${f.id}: marker leaked into the starter`);
assert.ok(!/\/\/ ~/.test(solutionOf(f.solution)), `${L.id}/${f.id}: marker leaked into the solution`);
}
});
}
test('a starter blanks exactly the marked lines', () => {
const p = { pseudo: 'A <- 1\nB <- 2 // ~ set B\nC <- 3', expect: [] };
assert.equal(starterOf(p), 'A <- 1\n// TODO: set B\nC <- 3');
assert.equal(solutionOf(p), 'A <- 1\nB <- 2\nC <- 3');
});
test('every lesson uses only keywords its prerequisites (or it) have introduced', () => {
// a rough but real check: the statement keyword that introduces a concept must not appear
// in a lesson that neither teaches nor (transitively) requires that concept
const introduces = {
'IF': 'selection', 'FOR': 'for-loop', 'WHILE': 'while', 'REPEAT': 'repeat', 'FUNCTION': 'function', 'PROCEDURE': 'procedure-sub', 'BYREF': 'byref', 'CASE': 'case',
'HASHMAP': 'hashmap', 'SET': 'set', 'TYPE': 'record', 'ARRAY': 'array', 'INVARIANT': 'invariant', 'ENSURE': 'specification', 'BREAK': 'early-exit', 'CONTINUE': 'early-exit', 'INPUT': 'io',
};
const exempt = { 'sets': ['TYPE'], 'what-stops-a-run': ['FUNCTION'] };
const violations = []; // TYPE … = SET OF is the set lesson's own form; the stops lesson reviews a function
for (const L of LESSONS) {
const held = new Set([...L.teaches, ...allPrerequisitesOf(L).flatMap((p) => p.teaches), ...L.requires]);
const programs = [L.example.pseudo, ...L.flavors.flatMap((f) => [f.solution.pseudo, f.starter || ''])].join('\n');
for (const [kw, concept] of Object.entries(introduces)) {
if ((exempt[L.id] || []).includes(kw)) continue;
if (new RegExp(`(^|[^\\w])${kw}\\b`, 'm').test(programs.replace(/"[^"]*"/g, '""').replace(/\/\/.*$/gm, '')) && !held.has(concept)) violations.push(`${L.id} uses ${kw} but neither teaches nor requires ${concept}`);
}
}
assert.deepEqual(violations, []);
});