Files
pplus-web/tests/areas.test.mjs
T
alexmandClaude 82e18f7966 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

152 lines
11 KiB
JavaScript

// Stage 9: three areas, each with its own route, panes, views and layout. A problem is one state shared by all
// three; the area comes from the route. Anything that is not a route in the plan is home.
import test from 'node:test';
import assert from 'node:assert/strict';
import { areasFor, AREAS, AREA_ORDER, defaultAreaFor, paneForArea, isAreaId, areaHasPane } from '../dist/core/areas.js';
import { parseRoute, routeFor, normalize, emptyState, applyEmbedParams } from '../dist/core/state.js';
import { PROBLEMS, stateForProblem } from '../dist/core/catalog.js';
import { Session } from '../dist/core/session.js';
import { EXERCISES } from '../core/dist/exercises/index.js';
globalThis.localStorage = { getItem() { return null; }, setItem() { }, removeItem() { } };
test('every route shape parses and prints back', () => {
for (const area of AREA_ORDER) {
assert.deepEqual(parseRoute(`#/${area}/p/count-base`), { kind: 'problem', area, id: 'count-base' });
assert.equal(routeFor({ kind: 'problem', area, id: 'count-base' }), `#/${area}/p/count-base`);
assert.deepEqual(parseRoute(`#/${area}/s/~abc`), { kind: 'shared', area, blob: '~abc' });
assert.equal(routeFor({ kind: 'shared', area, blob: '~abc' }), `#/${area}/s/~abc`);
}
assert.deepEqual(parseRoute(''), { kind: 'home' });
assert.deepEqual(parseRoute('#/'), { kind: 'home' });
const e = parseRoute('#/embed/trace/p/match?t=12');
assert.equal(e.kind, 'embed'); assert.equal(e.view, 'trace'); assert.equal(e.problem, 'match'); assert.equal(e.params.get('t'), '12');
const b = parseRoute('#/embed/life/~blob?area=dev');
assert.equal(b.kind, 'embed'); assert.equal(b.blob, '~blob'); assert.equal(b.params.get('area'), 'dev');
assert.equal(routeFor(b), '#/embed/life/~blob?area=dev');
assert.equal(routeFor(parseRoute('#/embed/playground/p/demo?panel=life')), '#/embed/playground/p/demo?panel=life');
assert.deepEqual(parseRoute('#/bridge/p/a%20b'), { kind: 'problem', area: 'bridge', id: 'a b' });
});
test('an unknown route is home: the old links and the retired names do not work', () => {
for (const h of ['#/p/count-base', '#/s/~abc', '#/learn', '#/learn/loops', '#/playground/p/demo', '#/bridge', '#/bridge/p', '#/bridge/x/y', '#/dev/s', '#/nowhere', '#/#problems', '#/#reference'])
assert.deepEqual(parseRoute(h), { kind: 'home' }, h);
});
test('the area table: three areas with the panes, views and docks of the plan', () => {
assert.deepEqual(AREA_ORDER, ['bridge', 'dev', 'hw']);
assert.ok(AREA_ORDER.every(isAreaId) && !isAreaId('learn') && !isAreaId('playground'));
for (const id of AREA_ORDER) { assert.equal(AREAS[id].id, id); assert.equal(AREAS[id].route, id); assert.ok(AREAS[id].name && AREAS[id].tagline); }
assert.deepEqual(AREAS.bridge.panes, ['lingo', 'matho', 'pseudo', 'speco', 'mojo']);
assert.deepEqual(AREAS.dev.panes, ['mojo']);
assert.deepEqual(AREAS.hw.panes, ['mojo', 'asm']);
assert.ok(AREAS.dev.inspect.includes('trace') && AREAS.bridge.inspect.includes('trace'), 'both have the Run view, which shows the variables and their lifetimes');
assert.ok(!AREAS.dev.inspect.includes('table') && !AREAS.dev.inspect.includes('life') && !AREAS.bridge.inspect.includes('table'), 'the Run view replaces the table of values and the lifetimes');
assert.ok(AREAS.bridge.inspect.includes('proof') && !AREAS.dev.inspect.includes('proof'));
assert.equal(AREAS.hw.workspace, 'board', 'Hardware is two panes: the code, and the board');
assert.ok(AREAS.bridge.workspace === 'ide' && AREAS.dev.workspace === 'ide');
assert.deepEqual([AREAS.hw.inspect, AREAS.hw.dock], [[], []], 'its views are cards on the board, not tabs');
assert.ok(!AREAS.bridge.dock.includes('asm') && !AREAS.dev.dock.includes('asm'));
assert.ok(areaHasPane(AREAS.hw, 'asm') && !areaHasPane(AREAS.dev, 'pseudo'));
});
test('each problem names its area: Hardware for asm-*, Developer for Mojo-only, Bridge otherwise', () => {
for (const e of EXERCISES) {
const p = PROBLEMS.find((x) => x.id === e.id);
assert.ok(isAreaId(p.area), e.id);
const raw = (l) => !!(e.sources[l] ?? '').trim();
if (e.area) assert.equal(p.area, e.area, e.id);
else if (e.id.startsWith('asm-')) assert.equal(p.area, 'hw', e.id);
else if ((raw('mojo') || e.mojoProgram) && !raw('pseudo') && !raw('lingo') && !raw('matho')) assert.equal(p.area, 'dev', e.id);
else assert.equal(p.area, 'bridge', e.id);
}
assert.ok(PROBLEMS.some((p) => p.area === 'bridge') && PROBLEMS.some((p) => p.area === 'hw'));
assert.equal(defaultAreaFor({ id: 'x', sources: { pseudo: 'OUTPUT 1' }, area: 'hw' }), 'hw', 'an exercise that says so wins');
});
test('the pane a state opens on follows the area', () => {
const count = stateForProblem(PROBLEMS.find((p) => p.id === 'count-base'));
assert.equal(count.lang, 'mojo', 'the exercise\'s own default is its Mojo');
assert.equal(paneForArea(AREAS.bridge, count, true), 'pseudo', 'a problem opening in the Bridge begins with the idea');
assert.equal(paneForArea(AREAS.bridge, { ...count, lang: 'speco' }, true), 'speco', 'a linked pane the student left open is kept');
assert.equal(paneForArea(AREAS.bridge, { ...count, lang: 'mojo' }), 'mojo', 'a shared link at the Mojo tab keeps it');
assert.equal(paneForArea(AREAS.dev, { ...count, lang: 'pseudo' }), 'mojo');
const asm = stateForProblem(PROBLEMS.find((p) => p.id.startsWith('asm-')));
assert.equal(paneForArea(AREAS.hw, { ...asm, lang: 'pseudo' }), 'asm');
assert.equal(paneForArea(AREAS.hw, { ...count, lang: 'pseudo', sources: { ...count.sources, asm: '' } }), 'mojo', 'no assembly: Mojo');
assert.equal(paneForArea(AREAS.hw, { ...asm, lang: 'mojo' }), 'mojo', 'a pane of the area is kept');
});
test('layout is kept per area, and a state normalizes it that way', () => {
const s = normalize({ v: 3, layout: { bridge: { split: 0.4, dockTab: 'problems' }, dev: { split: 0.7, dockCollapsed: true }, hw: { dock: 0.5, right: 7 }, nowhere: { split: 0.1 }, split: 0.9 } });
assert.deepEqual(Object.keys(s.layout).sort(), ['bridge', 'dev', 'hw']);
assert.equal(s.layout.bridge.split, 0.4); assert.equal(s.layout.bridge.dockTab, 'problems');
assert.equal(s.layout.dev.split, 0.7); assert.equal(s.layout.dev.dockCollapsed, true);
assert.equal(s.layout.hw.dock, 0.5); assert.equal(s.layout.hw.right, undefined, 'a fraction outside 0..1 is dropped');
assert.equal(normalize({ v: 3, layout: { split: 0.5, dockTab: 'output' } }).layout, undefined, 'a layout that is not per area is not read');
assert.equal(emptyState().layout, undefined);
});
test('a session lays out the area it is open in, and the other areas keep their own', () => {
const st = stateForProblem(PROBLEMS.find((p) => p.id === 'count-base'));
const bridge = new Session(st, { area: 'bridge', storageId: null });
bridge.setLayout({ split: 0.3 });
assert.equal(bridge.layout().split, 0.3);
const dev = new Session(st, { area: 'dev', storageId: null });
assert.equal(dev.layout().split, undefined, 'the Developer area has not been arranged yet');
dev.setLayout({ split: 0.7, dockTab: 'compiler' });
assert.equal(bridge.layout().split, 0.3, 'the same state: the Bridge arrangement is still there');
assert.equal(st.layout.dev.dockTab, 'compiler');
assert.equal(new Session(st, { storageId: null }).area, null, 'a session with no area is not a workspace: every pane counts');
});
test('an embed query arranges one area, chosen with area=', () => {
const s = applyEmbedParams(emptyState(), new URLSearchParams('editor=0.6&dock=closed'), 'dev');
assert.deepEqual(s.layout, { dev: { split: 0.6, dockCollapsed: true } });
});
test('allDiagnostics reports only the panes of the area', () => {
const st = stateForProblem(PROBLEMS.find((p) => p.id === 'count-base'));
st.sources.mojo = 'def main(:\n';
st.sources.pseudo = 'FUNCTION BROKEN(\n';
const dev = new Session(structuredClone(st), { area: 'dev', storageId: null });
assert.ok(dev.allDiagnostics().every((d) => d.lang === 'mojo'), 'Developer: Mojo only');
assert.ok(dev.allDiagnostics().length);
const bridge = new Session(structuredClone(st), { area: 'bridge', storageId: null });
assert.ok(bridge.allDiagnostics().some((d) => d.lang === 'pseudo'));
const hw = new Session(structuredClone(st), { area: 'hw', storageId: null });
assert.ok(hw.allDiagnostics().every((d) => d.lang === 'mojo' || d.lang === 'asm'));
});
test('the toolbar offers only the areas that would show the open pane exactly as it is', async () => {
const { safeSwitches } = await import('../dist/core/areas.js');
const open = (id, area) => { const st = stateForProblem(PROBLEMS.find((p) => p.id === id)); return new Session(st, { area, storageId: null }); };
const s = open('count-base', 'dev');
const shown = (a, l) => s.shownIn(a, l);
assert.deepEqual(safeSwitches('dev', 'mojo', shown), ['hw'], 'Developer and Hardware share the Mojo');
assert.deepEqual(safeSwitches('hw', 'mojo', shown), ['dev']);
assert.deepEqual(safeSwitches('hw', 'asm', shown), [], 'only Hardware has x86-64');
assert.deepEqual(safeSwitches('hw', 'mojo', () => ''), [], 'an empty pane offers no switch: the move would show other work');
for (const l of ['lingo', 'matho', 'pseudo', 'speco']) assert.deepEqual(safeSwitches('bridge', l, shown), [], `only the Bridge has ${l}`);
assert.notEqual(s.shownIn('bridge', 'mojo'), s.shownIn('dev', 'mojo'), 'the exercise\'s own Mojo is not the translation of its pseudo');
assert.deepEqual(safeSwitches('bridge', 'mojo', shown), [], 'so the Bridge\'s Mojo tab does not offer Developer');
// once the generated Mojo is copied into the Developer area, the Bridge and the others show the same text
const b = open('count-base', 'bridge');
assert.ok(b.adoptGeneratedMojo());
assert.deepEqual(safeSwitches('bridge', 'mojo', (a, l) => b.shownIn(a, l)), ['dev', 'hw']);
assert.deepEqual(safeSwitches('dev', 'lingo', shown), [], 'a pane the area does not have offers nothing');
});
test('each problem opens in its own area first, then every other area it has something to show in', () => {
for (const p of PROBLEMS) {
const a = areasFor(p);
assert.equal(a[0], p.area, p.id);
assert.equal(new Set(a).size, a.length, `${p.id}: each area once`);
assert.equal(a.includes('hw'), p.area === 'hw' || !!(p.sources.asm ?? '').trim(), `${p.id}: Hardware when it has x86-64`);
assert.equal(a.includes('bridge'), p.area === 'bridge' || ['pseudo', 'lingo', 'matho', 'speco'].some((l) => (p.sources[l] ?? '').trim()), `${p.id}: the Bridge when it has an idea to translate`);
}
assert.deepEqual(areasFor({ area: 'bridge', sources: { pseudo: 'OUTPUT 1', mojo: 'def main(): pass', asm: 'ret' } }), ['bridge', 'dev', 'hw']);
assert.deepEqual(areasFor({ area: 'hw', sources: { asm: 'ret' } }), ['hw']);
assert.deepEqual(areasFor({ area: 'dev', sources: {}, mojo: { source: 'def main(): pass' } }), ['dev']);
});