Files
pplus-core/tests/runview.test.mjs
T
2026-10-05 03:03:38 +00:00

101 lines
6.1 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 { Session } from '../dist/core/session.js';
import { PROBLEMS, stateForProblem, problemById } from '../dist/core/catalog.js';
import { runColumns, runRows, defaultFollow, valuesAt, rulerCells, gridCells } from '../dist/core/runview.js';
globalThis.localStorage = { getItem() { return null; }, setItem() { }, removeItem() { } };
const traceOf = (p, lang) => {
const st = stateForProblem(p); st.lang = lang;
const s = new Session(st, { area: lang === 'mojo' ? 'dev' : null, storageId: null });
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');
});