// SPDX-FileCopyrightText: 2026 Scientific Computing Studio, LLC // SPDX-License-Identifier: Apache-2.0 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'; import { MOJO_VERSION, GODBOLT_COMPILER } from '../core/dist/mojo/generated/pin.js'; // godbolt's stored answers, in pplus-core const goldens = fs.readdirSync(new URL('../core/library/mojo/golden/', import.meta.url)).filter((f) => /^[0-9a-f]{64}\.json$/.test(f)) .map((f) => JSON.parse(fs.readFileSync(new URL(`../core/library/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(':3:', ':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 ${MOJO_VERSION} 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, GODBOLT_COMPILER); const asked = []; const fake = { id: 'fake', host: 'checker.example', compiler: GODBOLT_COMPILER, 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: GODBOLT_COMPILER, 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'); }); test('a compile says how it was built: the arguments the request sent, from core\'s pin', async () => { const { MOJO_BUILD_ARGS } = await import('../core/dist/mojo/generated/pin.js'); const { BUILD_ARGS } = await import('../core/dist/services/godbolt.js'); assert.match(MOJO_BUILD_ARGS, /--target-cpu \S+ --fp-mode contract=off/); for (const level of ['O0', 'default']) assert.ok(BUILD_ARGS[level].startsWith(MOJO_BUILD_ARGS), `${level} is asked for with the pin's arguments first`); // the summary line of a compile and the badge's tooltip show them, and neither names arguments of its own const fs = await import('node:fs'); for (const f of ['src/ui/views/compiler.ts', 'src/ui/provenance.ts']) { const text = fs.readFileSync(f, 'utf8'); assert.ok(text.includes('MOJO_BUILD_ARGS'), `${f} shows the pin's arguments`); assert.doesNotMatch(text, /--target-cpu|--fp-mode/, `${f} writes build arguments of its own`); } // the two places the site's own tools run a Mojo file (a Quarto page's run, and the page checker) build it the same way for (const f of ['quarto/_extensions/pplus/render.mjs', 'scripts/check-pages.mjs']) { const text = fs.readFileSync(f, 'utf8'); assert.ok(text.includes('mojoBuildArgs('), `${f} takes the pin's arguments`); assert.doesNotMatch(text, /\['run', file\]/, `${f} runs mojo with no build arguments`); assert.doesNotMatch(text, /--target-cpu|--fp-mode/, `${f} writes build arguments of its own`); } });