Files
pplus-web/tests/badges.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

130 lines
6.5 KiB
JavaScript

// Stage 12: the Mojo badges. The generated label (where the Mojo came from) and the godbolt badge (whether the real
// compiler was asked about exactly this source) are independent, both are data a view reads from the session, and
// neither can come from a link or a file.
import test from 'node:test';
import assert from 'node:assert/strict';
import { Session } from '../dist/core/session.js';
import { stateForProblem, problemById, PROBLEMS } from '../dist/core/catalog.js';
import { mojoProvenance } from '../dist/core/provenance.js';
import { compiledStatus, compileHash, forgetCompiles } from '../dist/core/compiles.js';
import { normalize } from '../dist/core/state.js';
import { GENERATED_HEADER } from '../core/dist/sync/header.js';
import { mojoFromPseudo } from '../core/dist/sync/hub.js';
const store = new Map();
globalThis.localStorage = { getItem: (k) => store.get(k) ?? null, setItem: (k, v) => store.set(k, String(v)), removeItem: (k) => store.delete(k) };
const open = (id, area = 'bridge') => new Session(stateForProblem(problemById(id)), { area, storageId: null });
const answer = (over = {}) => async () => ({ error: null, seconds: 1, answer: { compiled: true, buildStderr: '', executed: true, exitCode: 0, stdout: '1\n', stderr: '', timedOut: false, asm: [], ...over } });
test('the Bridge\'s Mojo is always generated; elsewhere the state says', () => {
const b = open('count-base', 'bridge');
assert.equal(b.provenance(), 'generated');
assert.equal(mojoProvenance(b, 'bridge'), 'generated');
assert.equal(open('count-base', 'dev').provenance(), null, 'an exercise\'s own Mojo is nobody\'s translation');
assert.equal(open('count-base', 'hw').provenance(), null);
assert.equal(mojoProvenance({ area: 'dev', state: { mojoOrigin: 'generated-edited' } }), 'generated-edited');
});
test('copying sets generated, editing sets generated-edited, and only the button clears it', async () => {
const s = open('count-base');
assert.ok(s.adoptGeneratedMojo());
assert.equal(s.state.mojoOrigin, 'generated');
const dev = new Session(s.state, { area: 'dev', storageId: null });
assert.equal(dev.provenance(), 'generated');
dev.setSource('mojo', dev.source('mojo') + '\n# my note');
assert.equal(dev.provenance(), 'generated-edited');
forgetCompiles();
await dev.compile(answer());
assert.equal(dev.provenance(), 'generated-edited', 'a successful compile does not clear it');
assert.ok(dev.clearMojoOrigin());
assert.equal(dev.provenance(), null);
assert.equal(dev.state.mojoOrigin, undefined);
assert.ok(!dev.source('mojo').includes(GENERATED_HEADER), 'the header comment goes with it');
assert.ok(dev.source('mojo').includes('# my note'), 'the student\'s own lines stay');
assert.equal(dev.clearMojoOrigin(), false, 'nothing left to clear');
});
test('the Bridge cannot clear provenance, and a translation always starts with the header', () => {
const s = open('count-base');
assert.equal(s.clearMojoOrigin(), false);
let n = 0;
for (const p of PROBLEMS) {
const pseudo = stateForProblem(p).sources.pseudo;
if (!pseudo) continue;
const m = mojoFromPseudo(pseudo, null).source;
if (m) { n++; assert.ok(m.startsWith(GENERATED_HEADER), p.id); }
}
assert.ok(n > 5);
});
test('the godbolt badge: shown for the compiled source, not for one more character, level or input', async () => {
forgetCompiles();
const s = open('count-base', 'dev');
assert.equal(s.compileBadge(), null, 'never compiled: no badge');
await s.compile(answer());
const b = s.compileBadge();
assert.equal(b.kind, 'compiled');
assert.equal(b.status.outcome, 'ran');
assert.equal(b.status.exit, 0);
const original = s.source('mojo');
s.setSource('mojo', original + ' ');
assert.equal(s.compileBadge().kind, 'edited', 'a one-character edit removes the badge');
s.setSource('mojo', original);
assert.equal(s.compileBadge().kind, 'compiled', 'the same text again is the same compile');
const back = open('count-base', 'dev');
assert.equal(back.compileBadge().kind, 'compiled', 'the same text again is the same compile');
back.setBuildLevel('default');
assert.equal(back.compileBadge(), null, 'another build level was never asked');
back.setBuildLevel('O0');
back.setInput('3\n');
assert.equal(back.compileBadge(), null, 'another input was never asked');
});
test('outcomes: did not compile, exit N, timed out', async () => {
forgetCompiles();
const s = open('count-base', 'dev');
await s.compile(answer({ compiled: false, executed: false, exitCode: null }));
assert.equal(s.compileBadge().status.outcome, 'did-not-compile');
forgetCompiles();
await s.compile(answer({ exitCode: 3 }));
assert.deepEqual([s.compileBadge().status.outcome, s.compileBadge().status.exit], ['ran', 3]);
forgetCompiles();
await s.compile(answer({ timedOut: true }));
assert.equal(s.compileBadge().status.outcome, 'timed-out');
});
test('a failed request (no answer) leaves no badge', async () => {
forgetCompiles();
const s = open('count-base', 'dev');
await s.compile(async () => ({ answer: null, error: 'offline', seconds: 1 }));
assert.equal(s.compileBadge(), null);
});
test('the badge survives a new Session and the full answers being dropped, but is not in the state, a link or a file', async () => {
forgetCompiles();
const s = open('count-base', 'dev');
await s.compile(answer());
const src = s.source('mojo');
assert.equal(compiledStatus(src, '', 'O0').outcome, 'ran');
assert.equal(compiledStatus(src, '', 'O0').level, 'O0');
assert.ok(store.has('pplus.compiled'));
store.delete('pplus.compiles'); // the full answers are gone; the summary index stays
assert.equal(open('count-base', 'dev').compileBadge().kind, 'compiled');
const text = JSON.stringify(s.state);
assert.ok(!/compiled|godbolt|pplus\./i.test(text.replace(/"compiler"/g, '')), 'nothing about a compile in the state');
forgetCompiles(); // someone else's browser: the same link, no compile
const theirs = new Session(normalize(JSON.parse(text)), { area: 'dev', storageId: null });
assert.equal(theirs.compileBadge(), null);
});
test('both badges can show together, and the hash covers source, input and level', async () => {
forgetCompiles();
const s = open('count-base', 'bridge');
await s.compile(answer());
assert.equal(s.provenance(), 'generated');
assert.equal(s.compileBadge().kind, 'compiled');
assert.notEqual(compileHash('a', '', 'O0'), compileHash('a', '', 'default'));
assert.notEqual(compileHash('a', '', 'O0'), compileHash('a', 'x', 'O0'));
assert.notEqual(compileHash('a', '', 'O0'), compileHash('a ', '', 'O0'));
});