// SPDX-FileCopyrightText: 2026 Scientific Computing Studio, LLC // SPDX-License-Identifier: Apache-2.0 // The library (library/, pplus-library): the registry core builds from it is current, every routine checks and // runs as its record says, and USE behaves as docs/library.md describes: names resolve, runs step over the // routines, the prover uses their contracts, and the Mojo translator writes each routine once. import test from 'node:test'; import assert from 'node:assert/strict'; import fs from 'node:fs'; import { spawnSync } from 'node:child_process'; import { libraryModules, libraryRoutine } from '../dist/library/registry.js'; import { studyOf, runPseudo, provePseudo } from '../dist/services/pseudo.js'; import { pseudoToMojo } from '../dist/sync/pseudo-to-mojo.js'; import { checkMojo } from '../dist/mojo/check.js'; import { runMojo } from '../dist/mojo/interp.js'; import { NO_CLOCK } from './helpers/pace.mjs'; test('the registry is generated from the library checkout and is up to date', () => { assert.ok(fs.existsSync('library/modules'), 'library/ is not checked out: git submodule update --init'); const r = spawnSync(process.execPath, ['scripts/library.mjs', 'emit', '--check'], { encoding: 'utf8' }); assert.equal(r.status, 0, r.stderr || r.stdout); }); test('the task that asks godbolt about the Mojo references finds the pin, and would ask the pinned compiler', () => { // library-verify-mojo read the pin from where the corpus used to live and stopped before asking anything const sources = spawnSync(process.execPath, ['scripts/library.mjs', 'mojo-sources'], { encoding: 'utf8', maxBuffer: 1 << 26 }); assert.equal(sources.status, 0, sources.stderr); const py = spawnSync('python3', ['--version'], { encoding: 'utf8' }); // no python3 here: the pipelines' own tests are skipped the same way, except under CI, where a skip hides a gap if (py.status !== 0) { assert.ok(!process.env.CI, 'python3 is not installed, and CI must run this'); return; } const pin = JSON.parse(fs.readFileSync('library/mojo/pin.json', 'utf8')); const r = spawnSync('python3', ['scripts/library_godbolt.py', '--dry-run', '--force'], { input: sources.stdout, encoding: 'utf8' }); assert.equal(r.status, 0, r.stderr); const asks = r.stdout.split('\n').filter((l) => l.includes('would ask')); assert.ok(asks.length >= 16, `${asks.length} references would be asked`); for (const l of asks) assert.ok(l.includes(`(${pin.godbolt.compiler})`), l); }); test('every routine: one routine per file, its cases run, and its record is current and has no failing check', () => { const mods = libraryModules(); assert.ok(mods.length >= 6, `only ${mods.length} modules`); let routines = 0; for (const m of mods) { for (const r of m.routines) { routines++; const st = studyOf(r.source); assert.ok(st.ok, `${r.id}: ${st.diagnostics.filter((d) => d.severity === 'error').map((d) => d.message).join('; ')}`); const subs = [...st.program.program.subs.values()]; assert.deepEqual(subs.map((s) => s.name), [r.name], `${r.id} defines ${subs.map((s) => s.name)}`); assert.ok(r.cases.length >= 2, `${r.id} has fewer than two cases`); for (const c of r.cases) { const tr = runPseudo(st, { call: c.call, maxSteps: 2000000 }); assert.equal(tr.error, null, `${r.id}: ${c.call}: ${tr.error?.message}`); assert.ok(c.result !== undefined, `${r.id}: ${c.call} has no recorded result: run pixi run library-check`); } const all = [...Object.entries(r.checks), ...r.references.flatMap((x) => Object.entries(x.checks).map(([k, v]) => [`${x.lang}/${x.level} ${k}`, v])), ...(r.proof ? [['proof', r.proof.check]] : [])]; for (const [k, c] of all) assert.notEqual(c.status, 'fail', `${r.id} ${k}: ${c.detail}`); for (const k of ['structure', 'pseudo', 'prover', 'fuzz']) assert.ok(r.checks[k], `${r.id} has no ${k} check recorded: run pixi run library-check`); } } assert.ok(routines >= 16, `only ${routines} routines`); }); test('USE: a module or one routine; unknown names are errors at the USE line', () => { assert.ok(libraryRoutine('md.LennardJones')); assert.ok(libraryRoutine('MD', 'lennardjones'), 'names ignore case'); const ids = (src) => studyOf(src).diagnostics.filter((d) => d.severity === 'error').map((d) => `${d.id}@${d.line}`); assert.deepEqual(ids('USE nosuchmodule\n'), ['unknown-library@1']); assert.deepEqual(ids('USE md.NoSuchRoutine\n'), ['unknown-routine@1']); assert.deepEqual(ids('USE md.MinimumImage\nFUNCTION MinimumImage(X : FLOAT) RETURNS FLOAT\n RETURN X\nENDFUNCTION\n'), ['library-clash@2']); assert.deepEqual(ids('USE align.MatchScore\nOUTPUT LinearGapPenalty(2, -1)\n'), ['unknown-function@2'], 'USE of one routine brings in only that one'); assert.deepEqual(ids('IF TRUE THEN\n USE md\nENDIF\n'), ['use-place@2']); }); test('a run steps over a library routine, and a broken REQUIRE is reported at the call', () => { const st = studyOf('USE md\nOUTPUT MinimumImage(2.3, 1.0)\nOUTPUT MinimumImage(1.0, 0.0)\n'); const tr = runPseudo(st, {}); assert.equal(tr.output[0], '0.2999999999999998'); assert.ok(tr.steps.every((s) => s.line === null || s.line <= 3), 'a step carries a line of the library file'); assert.ok(tr.steps.some((s) => s.kind === 'call' && /from the library \(md\.MinimumImage\)/.test(s.text))); assert.equal(tr.error?.kind, 'require'); assert.equal(tr.error.line, 3); assert.match(tr.error.message, /In the library routine md\.MinimumImage/); }); test('the prover uses a routine\'s REQUIRE at the call and its ENSURE afterwards, without proving it again', () => { const r = provePseudo(studyOf('USE seq.HammingDistance\nFUNCTION F(S : STRING) RETURNS INTEGER\n ENSURE RESULT <= LENGTH(S)\n RETURN HammingDistance(S, S)\nENDFUNCTION\n'), NO_CLOCK); assert.deepEqual(r.units.map((u) => u.name), ['F'], 'library routines are not units of the program'); const obs = r.units[0].obligations; assert.equal(obs.find((o) => o.kind === 'require').status, 'proved'); assert.equal(obs.find((o) => o.kind === 'ensure').status, 'proved', 'the ENSURE of HammingDistance is a fact after the call'); }); test('the Mojo made from a program that uses the library holds each routine once, dependencies first, and agrees', () => { const src = 'USE align\nOUTPUT NeedlemanWunschScore("GATTACA", "GCATGCU", 1, -1, -1), " ", MatchScore(\'A\', \'A\', 2, -1)\n'; const mj = pseudoToMojo(src); const defs = [...mj.source.matchAll(/^def (\w+)/gm)].map((m) => m[1]); assert.deepEqual(defs, ['match_score', 'linear_gap_penalty', 'needleman_wunsch_score', 'main']); assert.match(mj.source, /# From the Programming\+ library: align\.MatchScore/); const ck = checkMojo(mj.source); assert.deepEqual(ck.diagnostics.filter((d) => d.severity === 'error'), []); const run = runMojo(ck.module, mj.source, { maxSteps: 4000000 }); assert.equal(run.stdout.trim(), runPseudo(studyOf(src), {}).output.join('\n')); });