// SPDX-FileCopyrightText: 2026 Scientific Computing Studio, LLC // SPDX-License-Identifier: Apache-2.0 // scripts/mojo/listings.mjs: godbolt's saved answers become x86-64 panes, and the program <-> exercise links hold. import test from 'node:test'; import assert from 'node:assert/strict'; import fs from 'node:fs'; import { stripHeader, readHeader, findGolden, buildListing, buildAll, render } from '../scripts/mojo/listings.mjs'; import { forSimulator, simulatorSetup } from '../dist/mojo/att.js'; import { listingFunctions, mojoBuildArgs } from '../dist/services/godbolt.js'; import { MOJO_BUILD_ARGS } from '../dist/mojo/generated/pin.js'; import { parseAsm } from '../dist/services/asm.js'; const ROOT = new URL('..', import.meta.url).pathname; const GOLDEN = ROOT + 'library/mojo/golden'; const mods = { forSimulator, simulatorSetup, listingFunctions, mojoBuildArgs }; test('stripHeader drops what emit_ts.py drops, and says how many lines went', () => { const text = '# origin: x\n# probe: y\n# mode: both\n\ndef main():\n pass\n'; const { source, dropped } = stripHeader(text); assert.equal(source, 'def main():\n pass\n'); assert.equal(dropped, 4); // three header lines and the blank one after assert.equal(stripHeader('# a comment, not a header key\ndef f(): pass').dropped, 0); }); test('readHeader reads the keys verify.py knows and ignores other comments', () => { const h = readHeader('# probe: q\n# mode: both\n# args: -O0 --debug-level=line-tables\n# a note\n# expect: pass\ndef main(): pass'); assert.equal(h.mode, 'both'); assert.equal(h.args, '-O0 --debug-level=line-tables'); assert.equal(h.expect, 'pass'); assert.equal(h.note, undefined); }); test('a golden is used only for the exact source, mode and arguments it answered', () => { const probe = fs.readFileSync(ROOT + 'library/mojo/probes/godbolt-line-tables-O0.mojo', 'utf8'); const h = readHeader(probe); const args = mojoBuildArgs(h.args ?? ''); assert.ok(findGolden(GOLDEN, probe, h.mode, args), 'the committed answer for this probe is found'); assert.equal(findGolden(GOLDEN, probe + ' ', h.mode, args), null, 'one extra character and the answer no longer applies'); assert.equal(findGolden(GOLDEN, probe, 'run', args), null, 'a run-mode question is a different question'); assert.equal(findGolden(GOLDEN, probe, h.mode, h.args ?? ''), null, 'its own arguments without the pin\'s are a different question'); }); test('real godbolt answers become x86-64 panes the assembler accepts, with lines that point into the shown source', () => { let made = 0; for (const f of fs.readdirSync(ROOT + 'library/mojo/probes').filter((x) => x.endsWith('.mojo'))) { const text = fs.readFileSync(ROOT + 'library/mojo/probes/' + f, 'utf8'); if (!/^# mode: (asm|both)/m.test(text)) continue; const r = buildListing(text, GOLDEN, mods); if (r.why) continue; // e.g. built without -O0, so the optimizer inlined every function made++; const p = parseAsm(r.asm); assert.ok(p.ok, `${f}: ${p.diagnostics[0]?.message}`); assert.match(r.setup, /^#/, `${f}: the setup starts with the comment saying where the arguments arrive`); const shown = stripHeader(text).source.split('\n').length; for (const m of r.asm.matchAll(/; mojo line (\d+)/g)) assert.ok(+m[1] >= 1 && +m[1] <= shown, `${f}: line ${m[1]} is outside the ${shown}-line program the pane shows`); } assert.ok(made >= 1, 'at least one committed probe has a usable listing; if none does, the corpus lost its assembly'); }); test('a program with no answer, or one that was never asked for a listing, says why instead of inventing one', () => { assert.match(buildListing('# mode: both\n# args: -O0\ndef main(): print(1)\n', GOLDEN, mods).why, /no godbolt answer/); assert.match(buildListing('def main(): print(1)\n', GOLDEN, mods).why, /mode: both/); }); test('render keeps the header and interface, and writes programs in name order', () => { const out = render({ zeta: { entry: 'z(Int)', asm: 'z:\n ret', setup: '# z', build: '' }, alpha: { entry: 'a(Int)', asm: 'a:\n ret', setup: '# a', build: '-O0' } }); assert.match(out, /export interface MojoListing/); assert.ok(out.indexOf('"alpha"') < out.indexOf('"zeta"')); assert.ok(out.endsWith('};\n')); }); test('every exercise that names a Mojo program has that program in library/mojo/local/', () => { const dir = ROOT + 'src/exercises/'; const missing = []; for (const f of fs.readdirSync(dir).filter((x) => x.endsWith('.ts') && !x.startsWith('_'))) { const m = /mojoProgram:\s*['"]([^'"]+)['"]/.exec(fs.readFileSync(dir + f, 'utf8')); if (m && !fs.existsSync(ROOT + `library/mojo/local/${m[1]}.mojo`)) missing.push(`${f} -> library/mojo/local/${m[1]}.mojo`); } assert.deepEqual(missing, [], 'create the file, or remove `mojoProgram` from the exercise; until then `pixi run play` will not regenerate programs.ts, because the catalog would throw'); }); // What an exercise's Hardware pane opens with. Measured on 2026-11-12: the whole of demo's program (every function // the compiler kept at -O0, and the 492 KB location table its bounds checks address, placed byte by byte in the // setup) made listings.ts 2.9 MB, and with it `node --test tests/*.test.mjs` passed a 3 GB memory cap. const LISTING_LINES = 400, LISTINGS_BYTES = 64 * 1024; test('the committed listings.ts is what the tool writes from the stored answers, built as they were asked, and small', () => { const { listings } = buildAll(ROOT + 'library/mojo/local', GOLDEN, mods); const committed = fs.readFileSync(ROOT + 'src/mojo/generated/listings.ts', 'utf8'); assert.ok(committed === render(listings), 'src/mojo/generated/listings.ts is not what scripts/mojo/listings.mjs writes from the stored answers: run `pixi run play --no-verify`, or `node scripts/mojo/listings.mjs` after a build'); assert.ok(Object.keys(listings).length >= 1, 'at least one exercise program has a stored listing'); for (const [name, l] of Object.entries(listings)) { const own = readHeader(fs.readFileSync(ROOT + `library/mojo/local/${name}.mojo`, 'utf8')).args ?? ''; assert.equal(l.build, mojoBuildArgs(own), `${name}: the arguments its answer was asked with, the pin's first`); assert.ok(l.build.startsWith(MOJO_BUILD_ARGS), `${name}: ${l.build}`); assert.ok(parseAsm(l.asm).ok, `${name}: the stored listing assembles`); const lines = l.asm.split('\n').length; assert.ok(lines <= LISTING_LINES, `${name}: ${lines} lines of x86-64; an exercise's pane holds its function and what that calls, at most ${LISTING_LINES} lines`); } assert.ok(committed.length <= LISTINGS_BYTES, `listings.ts is ${committed.length} bytes, over ${LISTINGS_BYTES}`); });