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

100 lines
6.0 KiB
JavaScript

// The Run view (core/runview.ts): the run top to bottom, one row per step, the variables the student follows as
// columns with their value after each step, and the lifecycle of the value each names. Checked against the
// interpreters' own traces for every exercise, in pseudo and in Mojo.
import test from 'node:test';
import assert from 'node:assert/strict';
import { runColumns, runRows, defaultFollow, valuesAt, rulerCells, gridCells } from '../dist/common/runview.js';
import { Workspace } from '../dist/common/workspace.js';
import { PROBLEMS, problemById, workForProblem } from '../dist/common/problems.js';
const traceOf = (p, lang) => {
const st = workForProblem(p); st.lang = lang;
const s = new Workspace(st);
return s.trace();
};
const RUNS = [];
for (const p of PROBLEMS) for (const lang of ['pseudo', 'mojo']) {
if (!(p.sources?.[lang] ?? '').trim()) continue;
const tr = traceOf(p, lang);
if (tr && tr.count) RUNS.push({ id: `${p.id} (${lang})`, tr });
}
test('every run with a trace has its variables as columns, and a suggestion to follow', () => {
assert.ok(RUNS.length >= 25, `the exercises give runs to check (${RUNS.length})`);
for (const { id, tr } of RUNS) {
const cols = runColumns(tr);
const keys = new Set(cols.map((c) => c.key));
assert.equal(keys.size, cols.length, `${id}: one column per routine and name`);
const follow = defaultFollow(cols);
assert.ok(follow.length <= 5 && follow.every((k) => keys.has(k)), `${id}: the suggestion is at most five of the run's variables`);
if (cols.some((c) => c.writes && c.name !== 'self' && c.name !== '_')) assert.ok(follow.length, `${id}: something is suggested when something changes`);
assert.ok(!follow.some((k) => k.endsWith('.self') || k.endsWith('._')), `${id}: never self or _`);
assert.equal(new Set(follow.map((k) => k.split('.')[0])).size <= 1, true, `${id}: the suggestion is one routine's variables`);
}
});
test('each row is one step, and each cell is the variable\'s value after that step', () => {
for (const { id, tr } of RUNS) {
const cols = runColumns(tr);
const follow = cols.map((c) => c.key).slice(0, 12);
const rows = runRows(tr, follow);
assert.equal(rows.length, tr.count, `${id}: a row per step`);
rows.forEach((r, i) => {
assert.equal(r.step, i);
assert.equal(r.line, tr.steps[i].line ?? null);
const after = valuesAt(tr, follow, i + 1);
for (const v of after) {
const c = r.cells[v.key];
assert.equal(c.text, v.inScope ? v.text : null, `${id} step ${i + 1} ${v.key}: the cell is the value after the step`);
if (c.wrote) assert.ok((tr.steps[i].writes ?? []).some((w) => w.name === v.key.split('.').slice(1).join('.')), `${id} step ${i + 1}: ${v.key} marked written only when the step writes it`);
}
});
// a window of the run is the same rows
const mid = Math.floor(tr.count / 2);
assert.deepEqual(runRows(tr, follow, mid, mid + 3).map((r) => r.step), [mid, mid + 1, mid + 2].filter((x) => x < tr.count));
// only the steps that change something: a subset, and every step that writes is kept
const few = runRows(tr, follow, 0, tr.count, { onlyWrites: true });
assert.ok(few.length <= rows.length);
for (let i = 0; i < tr.count; i++) if ((tr.steps[i].writes ?? []).length) assert.ok(few.some((r) => r.step === i), `${id}: step ${i + 1} writes, so it stays`);
// what each step printed adds up to the run's output
assert.deepEqual(rows.flatMap((r) => r.printed), tr.output.slice(0, tr.states[tr.count]?.outputLength ?? tr.output.length));
}
});
test('the lifecycle of the value a variable names is in that variable\'s column', () => {
const tr = traceOf(problemById('demo'), 'mojo');
const events = tr.steps.flatMap((s) => (s.life ?? []).filter((e) => e.kind !== 'named'));
assert.ok(events.length > 0, 'the demo makes, moves and destroys values');
const cols = runColumns(tr);
const all = cols.map((c) => c.key);
const rows = runRows(tr, all);
const marks = rows.flatMap((r) => Object.values(r.cells).flatMap((c) => c.life));
const others = rows.flatMap((r) => r.otherLife);
assert.ok(marks.length > 0, 'some events land on a named variable');
// every event is shown once for each place it belongs: a move is a value moved in and a value moved out
const expected = events.reduce((n, e) => n + (e.kind === 'moved' && e.from != null ? 2 : 1), 0);
const named = new Map();
for (const s of tr.steps) for (const e of s.life ?? []) if (e.kind === 'named') named.set(e.id, (named.get(e.id) ?? 0) + 1);
assert.ok(marks.length + others.length >= expected, `every event is shown (${marks.length} in columns, ${others.length} beside the step, ${expected} events)`);
for (const r of rows) for (const c of Object.values(r.cells)) for (const m of c.life) assert.match(m.kind, /^(made|copied|moved-in|moved-out|destroyed)$/);
// following no variable puts every event beside its step
const bare = runRows(tr, []);
assert.equal(bare.reduce((n, r) => n + r.otherLife.length, 0), expected);
});
test('strings and lists get a ruler, tables a grid', () => {
assert.deepEqual(rulerCells({ value: 'GAT', type: 'STRING' }), ['G', 'A', 'T']);
assert.equal(rulerCells({ value: 'G', type: 'CHAR' }), null, 'one character is not a ruler');
assert.deepEqual(rulerCells({ text: '"GAT"', value: { k: 'str', v: 'GAT' } }), ['G', 'A', 'T'], 'a Mojo String');
assert.deepEqual(rulerCells({ text: '[1, 2]', value: { k: 'list', items: [{ k: 'int', v: 1n }, { k: 'int', v: 2n }] } }), ['1', '2'], 'a Mojo List[Int]');
assert.equal(rulerCells({ value: 'x'.repeat(81) }), null, 'too long to draw');
const ed = traceOf(problemById('edit-distance'), 'pseudo');
const d = runColumns(ed).find((c) => c.name === 'D');
assert.ok(d, 'edit-distance has its table D');
const [v] = valuesAt(ed, [d.key], ed.count - 1);
assert.ok(v.grid && v.grid.length > 1 && v.grid[0].length > 1, 'D is drawn as a grid');
assert.ok(v.grid.flat().some((x) => /^\d+$/.test(x)), 'with its numbers in the cells');
assert.equal(gridCells({ value: [[1, 2], 3] }), null, 'not a table');
});