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
95 lines
6.1 KiB
JavaScript
95 lines
6.1 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import fs from 'node:fs';
|
|
import { normalize } from '../dist/core/state.js';
|
|
|
|
import { Session } from '../dist/core/session.js';
|
|
|
|
// godbolt's stored answers, in pplus-core
|
|
const goldens = fs.readdirSync(new URL('../core/mojo/golden/', import.meta.url)).filter((f) => /^[0-9a-f]{64}\.json$/.test(f))
|
|
.map((f) => JSON.parse(fs.readFileSync(new URL(`../core/mojo/golden/${f}`, import.meta.url), 'utf8')));
|
|
|
|
test('the session keeps each answer with the source it answered, and says when it is stale', async () => {
|
|
const s = new Session(normalize({ v: 3, lang: 'mojo', sources: { mojo: 'fn main():\n print(1)\n' } }));
|
|
assert.equal(s.diagnostics('mojo')[0].id, 'removed-syntax');
|
|
const stderr = goldens.find((g) => g.firstItem === 'probes/dialect-fn').result.buildStderr;
|
|
let asked = null;
|
|
const fake = async (source, opts) => { asked = { source, ...opts }; return { answer: { compiled: false, buildStderr: stderr.replace('<source>:3:', '<source>:1:'), executed: false, exitCode: null, stdout: '', stderr: '', timedOut: false, asm: [] }, error: null, seconds: 0.1 }; };
|
|
const rec = await s.compile(fake);
|
|
assert.equal(asked.level, 'O0');
|
|
assert.equal(rec.diagnostics[0].origin, 'the Mojo 1.1.0 compiler');
|
|
assert.equal(s.diagnostics('mojo')[0].id, 'compiler', 'the compiler\'s answer replaces Programming+\'s own finding');
|
|
assert.equal(s.lastCompile().fresh, true);
|
|
s.setSource('mojo', 'def main():\n print(1)\n');
|
|
assert.equal(s.lastCompile().fresh, false);
|
|
assert.deepEqual(s.diagnostics('mojo'), [], 'an answer for older source is not shown on the new lines');
|
|
const failed = await s.compile(async () => ({ answer: null, error: 'Could not reach godbolt.org (offline).', seconds: 0 }));
|
|
assert.equal(failed.outcome.error, 'Could not reach godbolt.org (offline).');
|
|
});
|
|
|
|
test('Compile & run goes through the compile backend, and the record names the service that answered', async () => {
|
|
const { compileBackend, setCompileBackend, explorerBackend, GODBOLT } = await import('../core/dist/services/compile-backend.js');
|
|
assert.equal(compileBackend(), GODBOLT);
|
|
assert.equal(GODBOLT.host, 'godbolt.org');
|
|
assert.equal(GODBOLT.compiler, 'mojo_1_1_0');
|
|
const asked = [];
|
|
const fake = { id: 'fake', host: 'checker.example', compiler: 'mojo_1_1_0', compile: async (source, opts) => { asked.push({ source, ...opts }); return { answer: null, error: 'checker.example did not answer in time.', seconds: 0 }; } };
|
|
const prev = setCompileBackend(fake);
|
|
try {
|
|
const s = new Session(normalize({ v: 3, lang: 'mojo', sources: { mojo: 'def main():\n print(7)\n' } }));
|
|
const rec = await s.compile();
|
|
assert.equal(asked.length, 1);
|
|
assert.equal(asked[0].source, 'def main():\n print(7)\n');
|
|
assert.equal(asked[0].level, 'O0');
|
|
assert.equal(rec.host, 'checker.example');
|
|
} finally {
|
|
setCompileBackend(prev);
|
|
}
|
|
assert.equal(compileBackend(), GODBOLT);
|
|
// a Compiler Explorer at another address speaks the same API and names itself in its messages
|
|
const elsewhere = explorerBackend('local', { base: 'http://127.0.0.1:9', compiler: 'mojo_1_1_0', host: 'the local checker' });
|
|
const out = await elsewhere.compile('def main():\n pass\n', { timeoutMs: 2000 });
|
|
assert.equal(out.answer, null);
|
|
assert.match(out.error, /the local checker/);
|
|
});
|
|
|
|
test('godbolt\'s answer outlives the session: a new one for the same source finds it without asking again', async () => {
|
|
const { forgetCompiles } = await import('../dist/core/compiles.js');
|
|
forgetCompiles();
|
|
const src = 'def main():\n print(42)\n';
|
|
const answer = { compiled: true, buildStderr: '', executed: true, exitCode: 0, stdout: '42\n', stderr: '', timedOut: false, asm: [{ text: '"example::main()":' }, { text: ' retq' }] };
|
|
let asks = 0;
|
|
const first = new Session(normalize({ v: 3, lang: 'mojo', sources: { mojo: src } }));
|
|
await first.compile(async () => { asks++; return { answer, error: null, seconds: 0.1 }; });
|
|
// a change of problem, a reload: a new session for the same program
|
|
const again = new Session(normalize({ v: 3, lang: 'mojo', sources: { mojo: src } }));
|
|
const last = again.lastCompile();
|
|
assert.ok(last, 'the answer is there before anyone presses Compile');
|
|
assert.equal(last.fresh, true);
|
|
assert.equal(last.record.outcome.answer.stdout, '42\n');
|
|
assert.equal(last.record.functions.length, 1, 'the listing is split into functions again');
|
|
assert.equal(asks, 1);
|
|
// a different program does not see it, and comes back to it once the source is the same again
|
|
const other = new Session(normalize({ v: 3, lang: 'mojo', sources: { mojo: 'def main():\n print(7)\n' } }));
|
|
assert.equal(other.lastCompile(), null);
|
|
other.setSource('mojo', src);
|
|
assert.equal(other.lastCompile()?.fresh, true, 'editing back to a compiled source brings its answer back');
|
|
// a failure to reach godbolt is not kept
|
|
forgetCompiles();
|
|
const offline = new Session(normalize({ v: 3, lang: 'mojo', sources: { mojo: src } }));
|
|
await offline.compile(async () => ({ answer: null, error: 'offline', seconds: 0 }));
|
|
assert.equal(new Session(normalize({ v: 3, lang: 'mojo', sources: { mojo: src } })).lastCompile(), null);
|
|
forgetCompiles();
|
|
});
|
|
|
|
test('Stage 6b: comparing builds does not replace the build the student chose', async () => {
|
|
const { Session } = await import('../dist/core/session.js');
|
|
const { normalize } = await import('../dist/core/state.js');
|
|
const s = new Session(normalize({ v: 3, sources: { mojo: 'def main():\n print(1)\n' }, lang: 'mojo' }));
|
|
const fake = (level) => async () => ({ ok: true, answer: { compiled: true, executed: true, exitCode: 0, stdout: level, stderr: '', buildStderr: '', asm: [] } });
|
|
await s.compile(fake('O0'));
|
|
await s.compile(fake('default'), 'default');
|
|
assert.equal(s.lastCompile().record.level, 'O0', 'the view keeps the chosen level');
|
|
assert.ok(s.compileAt(s.source('mojo'), 'default'), 'the other build is there to compare with');
|
|
});
|