// 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, render } from '../scripts/mojo/listings.mjs'; import { forSimulator, simulatorSetup } from '../dist/mojo/att.js'; import { listingFunctions } from '../dist/services/godbolt.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 }; 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); assert.ok(findGolden(GOLDEN, probe, h.mode, h.args ?? ''), 'the committed answer for this probe is found'); assert.equal(findGolden(GOLDEN, probe + ' ', h.mode, h.args ?? ''), null, 'one extra character and the answer no longer applies'); assert.equal(findGolden(GOLDEN, probe, 'run', h.args ?? ''), null, 'a run-mode question is 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'); });