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
84 lines
4.2 KiB
JavaScript
84 lines
4.2 KiB
JavaScript
// Stage 13b: the best-practices switch. Off by default, one per area, remembered in the browser and nowhere else;
|
|
// while it is off the rules are never called (a spy proves it), and nothing about them reaches a diagnostic.
|
|
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { Session } from '../dist/core/session.js';
|
|
import { stateForProblem, problemById } from '../dist/core/catalog.js';
|
|
import { practicesOn, setPracticesOn, forgetPractices, onPracticesChange } from '../dist/core/prefs.js';
|
|
import { findPractices, PRACTICES_ORIGIN } from '../core/dist/mojo/practices.js';
|
|
import { encodeState } from '../dist/core/state.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 spy = () => { const f = (...a) => { f.calls++; return findPractices(...a); }; f.calls = 0; return f; };
|
|
const open = (id, area, practices) => new Session(stateForProblem(problemById(id)), { area, storageId: null, practices });
|
|
const noted = (s) => s.allDiagnostics().filter((d) => d.origin === PRACTICES_ORIGIN);
|
|
|
|
test('off by default in every area, and the rules are never called while off', () => {
|
|
forgetPractices();
|
|
for (const area of ['bridge', 'dev', 'hw', null]) assert.equal(practicesOn(area), false, String(area));
|
|
for (const area of ['bridge', 'dev', 'hw']) {
|
|
const f = spy();
|
|
const s = open('count-base', area, f);
|
|
s.setLang('mojo');
|
|
s.allDiagnostics(); s.diagnostics('mojo'); s.practices(); s.trace();
|
|
assert.equal(f.calls, 0, `${area}: no rule ran`);
|
|
assert.deepEqual(s.practices(), []);
|
|
assert.deepEqual(noted(s), []);
|
|
}
|
|
});
|
|
|
|
test('the switch is per area, remembered under its own key, and a fresh read finds it', () => {
|
|
forgetPractices();
|
|
setPracticesOn('dev', true);
|
|
assert.equal(store.get('pplus.practices.dev'), '1');
|
|
assert.equal(practicesOn('dev'), true);
|
|
assert.equal(practicesOn('bridge'), false, 'the other area is unaffected');
|
|
assert.equal(practicesOn('hw'), false, 'Hardware has no switch');
|
|
setPracticesOn('hw', true);
|
|
assert.equal(practicesOn('hw'), false);
|
|
let told = 0; const off = onPracticesChange(() => told++);
|
|
setPracticesOn('bridge', true); setPracticesOn('bridge', false);
|
|
off(); setPracticesOn('bridge', true);
|
|
assert.equal(told, 2);
|
|
setPracticesOn('bridge', false); setPracticesOn('dev', false);
|
|
assert.ok(!store.has('pplus.practices.dev') && !store.has('pplus.practices.bridge'), 'off is stored as absent');
|
|
});
|
|
|
|
test('on in the Developer area: findings are notes on the student\'s own lines; the Bridge, still off, stays quiet', () => {
|
|
forgetPractices();
|
|
setPracticesOn('dev', true);
|
|
const f = spy();
|
|
const dev = open('count-base', 'dev', f);
|
|
dev.setSource('mojo', 'def main():\n var n: Int\n n = 5\n n = n + 1\n print(n)\n');
|
|
const notes = noted(dev);
|
|
assert.ok(f.calls >= 1);
|
|
assert.deepEqual(notes.map((d) => d.id).sort(), ['practice-augmented-assign', 'practice-declare-then-assign']);
|
|
assert.ok(notes.every((d) => d.severity === 'note' && d.fix && d.lang === 'mojo'));
|
|
const g = spy();
|
|
const bridge = open('count-base', 'bridge', g);
|
|
bridge.allDiagnostics(); bridge.practices();
|
|
assert.equal(g.calls, 0);
|
|
forgetPractices();
|
|
});
|
|
|
|
test('on in the Bridge: the generated Mojo draws findings in the helper\'s list, not in Problems', () => {
|
|
forgetPractices();
|
|
setPracticesOn('bridge', true);
|
|
const s = open('count-base', 'bridge');
|
|
const rules = new Set(s.practices().map((x) => x.rule));
|
|
for (const id of ['declare-then-assign', 'augmented-assign', 'codepoint-loop']) assert.ok(rules.has(id), id);
|
|
assert.deepEqual(noted(s), [], 'Problems stays about the pseudo and the translation');
|
|
assert.equal(s.source('mojo'), s.derivedMojo().source, 'the Mojo is still the literal translation');
|
|
forgetPractices();
|
|
});
|
|
|
|
test('the switch is a browser preference: it is not in the state, so a link cannot turn it on', async () => {
|
|
forgetPractices();
|
|
setPracticesOn('dev', true);
|
|
const st = stateForProblem(problemById('count-base'));
|
|
assert.ok(!/practices/i.test(JSON.stringify(st)));
|
|
assert.ok(!/practices/i.test(await encodeState(st)));
|
|
forgetPractices();
|
|
});
|