// 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/); });