// #36: the evidence half of the M3 group map. `GROUPS` (tools/groups.mjs) names, per acceptance // group, the exact test that proves it plus the assertions/calls its body must keep. This module // turns that claim into something checkable in two places: // // * `findTestBody` (static, used by tools/groups.test.mjs): the declaration exists exactly once, // its body still holds at least `minAsserts` assertions and every required snippet; // * this file's CLI (run by tools/test.sh AFTER the Node suite): the reporter output shows every // registered test actually ran and passed, and none of them was skipped. A title substring in a // comment, an emptied test body, or a registration that never runs cannot satisfy it. import fs from 'node:fs' import path from 'node:path' import { fileURLToPath } from 'node:url' import { GROUPS } from './groups.mjs' const escapeRe = s => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') // A test title as written in the source, with `${...}` standing for the case it is built for. export function titleToRegExp(title) { const parts = String(title).split(/\$\{[^}]*\}/g).map(escapeRe) return new RegExp(`^${parts.join('.*?')}$`) } // The source text of one test declaration: located by its exact title literal, bounded by the // matching close of the `test(` call at the declaration's own indentation. export function findTestBody(fileText, title) { const needles = ['`', "'", '"'].map(q => `test(${q}${title}${q}`) const hits = needles.map(n => fileText.indexOf(n)).filter(i => i >= 0) if (hits.length !== 1) return null // absent, or declared under more than one quoting/title const start = fileText.lastIndexOf('\n', hits[0]) + 1 const indent = fileText.slice(start, hits[0]) if (/\S/.test(indent)) return null const closer = new RegExp(`^${escapeRe(indent)}\\}\\)\\s*$`, 'm') const rest = fileText.slice(start) const m = closer.exec(rest) if (!m) return null return { start, end: start + m.index, body: rest.slice(0, m.index) } } const GLYPH = /^\s*(✔|✖|﹣)\s+(.+?)\s+\((?:<1|\d+(?:\.\d+)?)ms\)(?:\s+#\s+(SKIP|TODO))?\s*$/ // The spec reporter's per-test lines, as `devbox run test` records them in results/node-test.log. export function parseReporter(text) { const out = [] for (const line of text.split('\n')) { const m = GLYPH.exec(line) if (!m) continue const [, glyph, name, skipped] = m const status = glyph === '✖' ? 'fail' : (skipped || glyph === '﹣') ? 'skipped' : 'pass' out.push({ name, status }) } return out } export function checkLog(logText, groups = GROUPS) { const ran = parseReporter(logText) const errors = [] let checked = 0 for (const [group, spec] of Object.entries(groups)) { for (const ev of spec.evidence) { checked += 1 const rx = titleToRegExp(ev.test) const hits = ran.filter(t => rx.test(t.name)) const passed = hits.filter(t => t.status === 'pass').length const failed = hits.filter(t => t.status === 'fail').length const skipped = hits.filter(t => t.status === 'skipped').length const want = ev.minTests ?? 1 const where = `${group}: ${ev.file} "${ev.test}"` if (passed < want) { errors.push(`${where}: ${passed} passing run(s), expected at least ${want}` + (hits.length ? ` (saw ${JSON.stringify(hits.slice(0, 4))})` : ' (never ran)')) } if (failed > 0) errors.push(`${where}: ${failed} failing run(s)`) if (skipped > 0) errors.push(`${where}: ${skipped} skipped run(s)`) } } return { checked, errors } } function main(argv) { const root = path.dirname(fileURLToPath(import.meta.url)) const logPath = argv[2] ?? path.join(root, '..', 'results', 'node-test.log') let text try { text = fs.readFileSync(logPath, 'utf8') } catch (e) { console.error(`evidence: cannot read ${logPath}: ${e.message}`) return 1 } const { checked, errors } = checkLog(text) if (errors.length) { console.error(`evidence: ${errors.length} group evidence problem(s) in ${logPath}:`) for (const e of errors) console.error(` - ${e}`) return 1 } console.log(`evidence: ${checked} registered group evidence test(s) ran and passed (${logPath})`) return 0 } if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { process.exitCode = main(process.argv) }