Files
marqdo-site/scripts/verify-example.mjs
T

51 lines
2.6 KiB
JavaScript

import { readFileSync, mkdirSync } from 'node:fs';
import { spawnSync } from 'node:child_process';
import { createHash } from 'node:crypto';
import { resolve } from 'node:path';
const [marqdo, view] = process.argv.slice(2).map(path => resolve(path));
if (!marqdo || !view) throw new Error('Pass verified CLI and View executable paths');
const source = 'examples/geometry.mq.md';
const output = resolve('public/examples/geometry');
const hash = bytes => createHash('sha256').update(bytes).digest('hex');
function run(binary, argv) {
const result = spawnSync(binary, argv, { encoding: 'utf8' });
if (result.error) throw result.error;
if (result.status !== 0) throw new Error(`${argv.join(' ')} exited ${result.status}: ${result.stderr}`);
return result.stdout;
}
const cliVersion = run(marqdo, ['--version']).trim();
const viewVersion = run(view, ['--version']).trim();
if (cliVersion !== 'marqdo 3.0.0-rc.6' || viewVersion !== 'marqdo-view 0.3.0') throw new Error('Unexpected CLI or View version');
const commands = [['check', source]];
const check = run(marqdo, commands[0]).trim();
const cases = [];
for (const radius of [0, 1, 3]) {
const command = ['call', source, 'Geometry.circle_area', '--arg', `r=${radius}.0`, '--mode', 'pure', '--output', 'json'];
commands.push(command);
const observation = JSON.parse(run(marqdo, command));
const started = observation.events.find(event => event.kind === 'run.started');
const value = observation.events.at(-1)?.payload.value?.value;
if (started?.payload.entry !== 'root:default::geometry.mq.md::Geometry.circle_area'
|| observation.run.status !== 'ok' || !Number.isFinite(value)
|| Math.abs(value - Math.PI * radius ** 2) > 1e-12) {
throw new Error(`Unexpected CLI result for radius ${radius}`);
}
cases.push({ radius, value });
}
mkdirSync(output, { recursive: true });
const exportLog = run(view, ['export', '--marqdo-bin', marqdo, '--out', output, source]).trim();
const manifest = readFileSync(resolve(output, 'manifest.json'));
const sourceSha256 = hash(readFileSync(source));
const parsed = JSON.parse(manifest);
if (parsed.entry.relative_path !== 'geometry.mq.md' || !parsed.pages.some(page => page.source_sha256 === sourceSha256)
|| parsed.core_version !== '3.0.0-rc.6' || !exportLog.includes('executed: false')) {
throw new Error('View export does not match the source or run mode');
}
console.log(JSON.stringify({
cliVersion, cliSha256: hash(readFileSync(marqdo)), viewVersion, viewSha256: hash(readFileSync(view)),
source, sourceSha256, commands, check, cases, viewManifestSha256: hash(manifest), exportLog,
}, null, 2));