Files
pplus-core/tests/services.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

101 lines
5.8 KiB
JavaScript

// Runs against the compiled output: `npm test` builds first.
import test from 'node:test';
import assert from 'node:assert/strict';
import { toAscii, toMath, parseSpec, weave, extractSpec } from '../dist/services/speco.js';
import { studyOf, runPseudo, provePseudo } from '../dist/services/pseudo.js';
import { readLingo, runLingo } from '../dist/services/lingo.js';
import { parseAsm, runAsmProgram, parseSetup } from '../dist/services/asm.js';
import { Workspace } from '../dist/common/workspace.js';
import { PROBLEMS, problemById, workForProblem } from '../dist/common/problems.js';
test('every catalog problem parses, runs, and (where it has pseudocode) proves without throwing', () => {
for (const p of PROBLEMS) {
const s = new Workspace(workForProblem(p));
s.allDiagnostics();
const tr = s.trace();
const empty = !Object.values(p.sources).some((src) => src && src.trim());
if (p.id !== 'five-bugs' && !empty) assert.ok(tr, `${p.id} produced no trace: ${s.runError}`);
if (p.sources.pseudo || p.sources.lingo) { s.proof(); s.tests(); }
}
});
test('the known-good examples give the lesson answers', () => {
const by = Object.fromEntries(PROBLEMS.map((p) => [p.id, p]));
const last = (id, lang) => { const st = workForProblem(by[id]); if (lang) st.lang = lang; const s = new Workspace(st); return s.trace().output.at(-1); };
assert.equal(last('count-base', 'pseudo'), 'COUNT_BASE returned 3');
assert.equal(last('count-base', 'lingo'), 'COUNT_BASES returned 3');
assert.equal(last('match', 'pseudo'), 'MATCH returned [1]');
assert.equal(last('revcomp', 'pseudo'), 'REVERSE_COMPLEMENT returned "TGTAATC"');
assert.match(last('asm-first-instructions'), /^rax = 6/);
});
test('the off-by-one bug is caught by the run, the tests, and the proof', () => {
const st = workForProblem(PROBLEMS.find((p) => p.id === 'match-off-by-one')); st.lang = 'pseudo';
const s = new Workspace(st);
assert.equal(s.trace().error.kind, 'index');
assert.ok(s.tests().some((t) => t.failures.length > 0));
assert.ok(s.proof().report.counts.broken >= 1);
});
test('speco: math notation lowers to the pseudocode spelling and back', () => {
assert.equal(toAscii('result = |{ k ∈ [0, |T|) : T[k] = B }|'), 'RESULT == LENGTH({ k IN [0, LENGTH(T)) : T[k] == B })');
assert.equal(toAscii('∀ K ∈ [0, M) : A[K] ≤ B ∧ ¬ (X ≠ Y)'), 'FORALL K IN [0, M) : A[K] <= B AND NOT (X != Y)');
assert.equal(toMath('RESULT == LENGTH({ K IN [0, LENGTH(T)) : T[K] == B })'), 'result = |{ K ∈ [0, |T|) : T[K] = B }|');
assert.equal(toAscii(toMath('FORALL K IN [0, J) : T[I + K] == P[K]')), 'FORALL K IN [0, J) : T[I + K] == P[K]');
});
test('speco: a spec written separately weaves in, proves, and does not duplicate what is already there', () => {
const pseudo = PROBLEMS.find((p) => p.id === 'count-base').sources.pseudo;
const spec = parseSpec(`SPEC COUNT_BASE(B, T)\n ENSURE result = |{ K ∈ [0, |T|) : T[K] = B }|\n LOOP FOR I\n INVARIANT Total = |{ K ∈ [0, I) : T[K] = B }|`);
assert.equal(spec.diagnostics.length, 0);
const w = weave(pseudo, spec);
assert.equal(w.diagnostics.length, 0);
assert.match(w.source, /FUNCTION COUNT_BASE[^\n]*\n\s+ENSURE RESULT == LENGTH/);
assert.match(w.source, /FOR I <- 0 TO N\n\s+INVARIANT Total == LENGTH/);
const st = studyOf(w.source);
assert.ok(st.ok, st.diagnostics.map((d) => d.message).join('; '));
const pr = provePseudo(st);
assert.equal(pr.counts.open + pr.counts.broken, 0, JSON.stringify(pr.counts));
assert.ok(pr.counts.proved >= 3);
// weaving the same spec again adds nothing
const again = weave(w.source, spec);
assert.equal(again.source, w.source);
// and extracting it back gives a spec that round-trips
const ex = extractSpec(w.source);
assert.match(ex, /^SPEC COUNT_BASE\(B, T\)/);
assert.equal(weave(pseudo, parseSpec(ex)).source, w.source);
});
test('speco: a spec for a routine that does not exist is reported, not silently dropped', () => {
const w = weave('FUNCTION F() RETURNS INTEGER\n RETURN 1\nENDFUNCTION', parseSpec('SPEC G()\n ENSURE RESULT == 1'));
assert.equal(w.diagnostics.length, 1);
assert.equal(w.diagnostics[0].id, 'no-routine');
});
test('lingo: English steps lower, run, and report English line numbers', () => {
const r = readLingo('To count the G bases in T:\n Start with Count at 0.\n For each letter b of T:\n If b is \'G\', add 1 to Count.\n The answer is Count.\n');
assert.ok(r.ok, r.diagnostics.map((d) => d.message).join('; '));
const tr = runLingo(r, { call: 'COUNT_BASE("GGA")' }); // a call aimed at another routine is re-aimed
assert.equal(tr.output.at(-1), 'COUNT_G_BASES returned 2');
assert.ok(tr.steps.every((s) => s.line == null || s.line <= 5), 'steps carry English lines');
});
test('lingo: an ambiguous range asks a question with two rewrites', () => {
const r = readLingo('To count in T:\n Start with Count at 0.\n For each i from 0 to the length of T:\n Add 1 to Count.\n The answer is Count.\n');
const q = r.diagnostics.find((d) => d.id === 'range-ends');
assert.ok(q && q.question && q.question.options.length === 2);
assert.match(q.question.options[0].text, /up to but not including/);
});
test('asm: setup lines, assembly errors, and a run', () => {
const setup = parseSetup('rdi = 0x1000\nrsi = 7\ndl = \'A\'\nmemory 0x1000 "GATTACA"\nflags CF');
assert.equal(setup.registers.rdi, 0x1000n); assert.equal(setup.registers.dl, 65n); assert.equal(setup.memory[0].text, 'GATTACA'); assert.equal(setup.flags.CF, true);
const bad = parseAsm(' mov rax, [rbx], [rcx]\n');
assert.equal(bad.ok, false); assert.equal(bad.diagnostics[0].line, 1);
const good = parseAsm(' mov eax, 40\n add eax, 2\n ret\n');
const tr = runAsmProgram(good, {});
assert.equal(tr.count, 3);
assert.match(tr.output.at(-1), /rax = 42/);
});