#!/usr/bin/env node /* CLOSEOUT TEETH — inject, prove the suite CATCHES it, restore byte-identically. Every tooth asserts its own injection landed first: a green teeth run means the test is missing, not that the risk is absent. */ const fs = require('fs'); const path = require('path'); const cp = require('child_process'); const F = (p) => path.join(__dirname, p); const C = 'tests/unit/lineageCloseout.test.js'; const P = 'tests/unit/lineageProductization.test.js'; const TEETH = [ { n: 1, name: 'the automatic monitor is removed from the tick', file: 'src/snapshotScheduler.js', from: " await coverageTick();", to: "", suite: C }, { n: 2, name: 'the monitor takes its denominator from the writer', file: 'src/services/lineageCoverage.js', from: " if (!r || r.published !== true) continue;", to: " if (!r || r.published !== true || r.lineage_action == null) continue;", suite: `${C} ${P}` }, { n: 3, name: 'a multi-date cohort is audited as one date slice', file: 'src/services/lineageCoverage.js', from: " .eq('snapshot_id', h.snapshot_id)\n .gte('game_date', lo)\n .lte('game_date', hi)", to: " .eq('snapshot_id', h.snapshot_id)\n .eq('game_date', h.game_date)", suite: C }, { n: 4, name: 'an audit timeout reports HEALTHY', file: 'src/services/lineageCoverage.js', from: " if (summary.audit_available === false) return HEALTH.AUDIT_UNAVAILABLE;", to: " if (summary.audit_available === false) return HEALTH.HEALTHY;", suite: `${C} ${P}` }, { n: 5, name: 'missing coverage passes the monitor silently', file: 'src/services/lineageCoverage.js', from: " if (health === HEALTH.HEALTHY || health === HEALTH.NO_ELIGIBLE_CLAIMS) {", to: " if (health === HEALTH.HEALTHY || health === HEALTH.NO_ELIGIBLE_CLAIMS || health === HEALTH.MISSING_COVERAGE) {", suite: C }, { n: 6, name: 'the ancestry contract elects a partial row', file: 'src/services/read/readAncestry.js', from: "const isValidAction = (r) => !!r && VALID_ACTION_FIELDS.every((f) => r[f] !== null && r[f] !== undefined);", to: "const isValidAction = (r) => !!r && r.read_id !== null && r.read_id !== undefined;", suite: `${C} ${P}` }, { n: 7, name: 'an incomplete chronology reports itself complete', file: 'src/services/read/readAncestry.js', from: " const unrecorded = attempted.filter((r) => !isValidAction(r)).length;", to: " const unrecorded = 0;", suite: `${C} ${P}` }, { n: 8, name: 'a legacy row is given synthetic lineage', file: 'src/services/read/readAncestry.js', from: " state: ANCESTRY_STATE.LEGACY_UNVERIFIED,\n reason: 'published before lineage recording began',", to: " state: ANCESTRY_STATE.LINEAGE_AVAILABLE,\n reason: 'published before lineage recording began',", suite: `${C} ${P}` }, { n: 9, name: 'lineage is wired into the grade badge router', file: 'src/routes/ledger.js', from: "const ROW_COLUMNS = 'id, player_key", to: "const LINEAGE_ANCESTRY = require('../services/read/readAncestry');\nconst ROW_COLUMNS = 'id, player_key", suite: `${C} ${P}` }, { n: 10, name: 'the monitor is allowed to break the scheduler', file: 'src/snapshotScheduler.js', from: " } catch { /* the monitor must never break the scheduler */ }", to: " } finally { /* unguarded */ }", suite: C, expectLoadError: true }, { n: 11, name: 'persistent shadow is made subject to the canary lease', file: 'src/services/lineageWriteMode.js', from: " return Object.prototype.hasOwnProperty.call(evaluate(now).modes, sp);", to: " return canary.isEnabled(sp, now);", suite: P }, { n: 12, name: 'participant identity convergence reverted', file: 'src/services/model/participantIdentity.js', from: " const canonical = p.mlb_person_id != null ? p.canonical_player_name : null;", to: " const canonical = null;", suite: `${P} tests/unit/participantIdentityConvergence.test.js` }, { n: 13, name: 'the retention collision gate is weakened', file: 'src/services/retentionService.js', from: " if (seen.has(id)) collisions += 1;", to: " if (false) collisions += 1;", suite: `${P} tests/unit/canonicalParticipant.test.js` }, { n: 14, name: 'model belief moved', file: 'src/services/intelligence/probabilityEstimator.js', from: "function estimate", to: "function estimate_TEETH_BROKEN", suite: 'tests/unit/probabilityEstimator.test.js' }, ]; let caught = 0; const missed = []; for (const t of TEETH) { const p = F(t.file); const orig = fs.readFileSync(p, 'utf8'); if (!orig.includes(t.from)) { missed.push(`${t.n} ANCHOR NOT FOUND: ${t.name}`); continue; } const broken = orig.replace(t.from, t.to); if (broken === orig) { missed.push(`${t.n} INJECTION NO-OP: ${t.name}`); continue; } fs.writeFileSync(p, broken); const present = t.to === '' ? !fs.readFileSync(p, 'utf8').includes(t.from) : fs.readFileSync(p, 'utf8').includes(t.to); let failed = false; try { cp.execSync(`npx jest ${t.suite} --testPathIgnorePatterns "/node_modules/" --silent --forceExit`, { cwd: __dirname, stdio: 'pipe' }); } catch { failed = true; } fs.writeFileSync(p, orig); if (fs.readFileSync(p, 'utf8') !== orig) { console.error('RESTORE FAILED', t.file); process.exit(2); } if (!present) { missed.push(`${t.n} INJECTION NOT PRESENT: ${t.name}`); continue; } if (failed) { caught += 1; console.log(` tooth ${String(t.n).padStart(2)} CAUGHT ${t.name}`); } else { missed.push(`${t.n} NOT CAUGHT: ${t.name}`); console.log(` tooth ${String(t.n).padStart(2)} MISSED ${t.name}`); } } console.log(`\n ${caught}/${TEETH.length} teeth landed`); if (missed.length) { console.log(' PROBLEMS:'); missed.forEach((m) => console.log(' ', m)); process.exit(1); }