/** * `pnpm validate:library` — every hope sentence in content/library/, and every * negative fixture in content/fixtures/ (CLAUDE.md §5.5). * * Passes when the library has no problems AND every fixture fails with the rule * it names. A fixture that passes means a rule has stopped firing: that fails * the run, the same as a bad sentence. */ import { readFileSync, readdirSync } from 'node:fs'; import { join } from 'node:path'; import { fileURLToPath } from 'node:url'; import { validateEntry, validateLibrary, type Rule } from '@rosea/content/validate'; const root = fileURLToPath(new URL('..', import.meta.url)); const libraryDir = join(root, 'content', 'library'); const fixturesDir = join(root, 'content', 'fixtures'); const jsonIn = (dir: string): string[] => readdirSync(dir) .filter((f) => f.endsWith('.json')) .sort(); let failed = false; // ── the library ──────────────────────────────────────────────────────────── const entries: unknown[] = []; for (const file of jsonIn(libraryDir)) { const data: unknown = JSON.parse(readFileSync(join(libraryDir, file), 'utf8')); if (!Array.isArray(data)) { console.error(`content/library/${file}: must be an array of entries`); failed = true; continue; } entries.push(...data); } for (const p of validateLibrary(entries)) { console.error(`library ${p.id} [${p.rule}] ${p.message}`); failed = true; } const sunset = entries.filter( (e) => typeof e === 'object' && e !== null && Array.isArray((e as { use?: unknown }).use) && (e as { use: unknown[] }).use.includes('sunset') ).length; console.log( `library: ${entries.length} entr${entries.length === 1 ? 'y' : 'ies'} in ${jsonIn(libraryDir).length} files, ${sunset} sunset (release needs 365)` ); // ── the fixtures: each must FAIL, with its named rule ─────────────────────── interface Fixture { expect: Rule; entry?: unknown; entries?: unknown[]; } const fixtureFiles = jsonIn(fixturesDir); let fixtureOk = 0; for (const file of fixtureFiles) { const fixture = JSON.parse(readFileSync(join(fixturesDir, file), 'utf8')) as Fixture; const problems = fixture.entries !== undefined ? validateLibrary(fixture.entries) : validateEntry(fixture.entry); if (problems.some((p) => p.rule === fixture.expect)) { fixtureOk += 1; } else { console.error( `fixture ${file} expected [${fixture.expect}], got ${problems.length === 0 ? 'a PASS' : problems.map((p) => `[${p.rule}]`).join(' ')}` ); failed = true; } } console.log(`fixtures: ${fixtureOk} of ${fixtureFiles.length} rejected as expected`); if (fixtureFiles.length === 0) { console.error('fixtures: none found. The negative fixtures ARE the validator (CLAUDE.md §5.5).'); failed = true; } process.exit(failed ? 1 : 0);