Baseline: tree as shared

This commit is contained in:
alexm committed 2026-10-05 03:03:38 +00:00
commit 31f2fb8a5a
1679 files changed
+1024562

No files matched your search

+113
View File
@@ -0,0 +1,113 @@
// Runs against the compiled output: `npm test` builds first.
import test from 'node:test';
import assert from 'node:assert/strict';
import { PROBLEMS, stateForProblem } from '../dist/core/catalog.js';
import { Session } from '../dist/core/session.js';
import { encodeState, decodeState, parseRoute, routeFor } from '../dist/core/state.js';
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';
globalThis.localStorage = { getItem() { return null; }, setItem() { }, removeItem() { } };
test('every catalog problem parses, runs, and (where it has pseudocode) proves without throwing', () => {
for (const p of PROBLEMS) {
const s = new Session(stateForProblem(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 = stateForProblem(by[id]); if (lang) st.lang = lang; const s = new Session(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 = stateForProblem(PROBLEMS.find((p) => p.id === 'match-off-by-one')); st.lang = 'pseudo';
const s = new Session(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('state survives a URL round trip and routes parse', async () => {
const st = stateForProblem(PROBLEMS[0]); st.t = 7; st.call = 'COUNT_BASE(\'G\', "GGG")';
const back = await decodeState(await encodeState(st));
assert.deepEqual(back.sources, st.sources);
assert.equal(back.t, 7); assert.equal(back.call, st.call); assert.equal(back.problem, st.problem);
assert.deepEqual(parseRoute('#/bridge/p/count-base'), { kind: 'problem', area: 'bridge', id: 'count-base' });
const r = parseRoute('#/embed/trace/p/match?t=12');
assert.equal(r.kind, 'embed'); assert.equal(r.view, 'trace'); assert.equal(r.problem, 'match'); assert.equal(r.params.get('t'), '12');
assert.equal(routeFor({ kind: 'problem', area: 'dev', id: 'match' }), '#/dev/p/match');
});
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/);
});