// Planted bugs: each mutant must make the test suite fail ("tests that fail without the change").
import { readFileSync, writeFileSync } from 'node:fs';
import { spawnSync } from 'node:child_process';
const F = 'src/build.ts';
const MUTANTS = [
['digest of trimmed text', "update(typeof text === 'string' ? Buffer.from(text, 'utf8') : text)", "update(typeof text === 'string' ? Buffer.from(text.trim(), 'utf8') : text)"],
['schema changed', "string[] cids';", "string[] ipfs';"],
['digests not hex prefixed', "return '0x' + createHash", "return createHash"],
['order from input keys', "return [...known, ...keys", "return [...keys, ...known"],
['revocable ignored', "schemaUid(CHARTER_SCHEMA, resolver, revocable === true)", "schemaUid(CHARTER_SCHEMA, resolver, true)"],
['no cid check', "if (!isCid(c)) throw", "if (false) throw"],
];
const orig = readFileSync(F, 'utf8'); let survived = 0;
try {
for (const [name, from, to] of MUTANTS) {
if (!orig.includes(from)) throw new Error('mutant does not apply: ' + name);
writeFileSync(F, orig.replace(from, to));
const r = spawnSync('node', ['--import', 'tsx', '--test', 'test/build.test.ts'], { encoding: 'utf8' });
const caught = r.status !== 0; if (!caught) survived++;
console.log(caught ? 'caught ' : 'SURVIVED', name);
}
} finally { writeFileSync(F, orig); }
console.log(survived ? `${survived} mutant(s) survived` : `all ${MUTANTS.length} mutants caught`);
process.exit(survived ? 1 : 0);
agentgate-source: code mirror on Cloudflare Pages. All repos