#!/usr/bin/env node /** Self-test for agent-rules.mjs. No network. */ import { BEGIN, END, CLAUDE_POINTER, ruleBlock, blockIn, withBlock, isPointer, findings } from './agent-rules.mjs'; import { readFileSync } from 'node:fs'; import { dirname, join } from 'node:path'; import { fileURLToPath } from 'node:url'; let pass = 0, fail = 0; const ok = m => { console.log(` ✓ ${m}`); pass++; }; const no = (m, got) => { console.log(` ✗ ${m}${got !== undefined ? ` — got ${JSON.stringify(got)}` : ''}`); fail++; }; const eq = (a, b, m) => (JSON.stringify(a) === JSON.stringify(b) ? ok(m) : no(m, a)); const block = ruleBlock('Rule one.\n'); console.log('inserting the block'); eq(withBlock('', block), `# Agents\n\n${block}`, 'an empty file becomes a heading and the block'); const doc = '# Agents\n\nOnly this repo.\n'; const once = withBlock(doc, block); eq(once, `# Agents\n\n${block}\nOnly this repo.\n`, 'the block goes under the first heading, repo notes keep their place'); eq(withBlock(once, block), once, 'applying it twice changes nothing'); eq(withBlock('no heading here\n', block), `${block}\nno heading here\n`, 'a file without a heading gets the block on top'); const foreign = '\n# This is NOT the Next.js you know\n\nBody.\n\n'; eq(withBlock(foreign, block), `${block}\n${foreign}`, 'a heading inside another tool\'s marked region is not the title: the block goes on top, never inside it'); eq(withBlock(`${foreign}\n# Title\n\nNotes.\n`, block), `${foreign}\n# Title\n\n${block}\nNotes.\n`, '…and the first heading outside such a region still takes it'); const newer = ruleBlock('Rule two.\n'); eq(blockIn(withBlock(once, newer)), newer, 'a changed block replaces the old one in place'); eq(withBlock(once, newer).includes('Rule one.'), false, '…and nothing of the old one is left'); eq(withBlock(once, newer).includes('Only this repo.'), true, '…and the repo notes survive'); ok(BEGIN.startsWith('