Files
Rosea/scripts/validate-library.ts

105 lines
4.2 KiB
TypeScript

/**
* `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.
*
* It also holds the candidates in content/candidates/ to every rule but
* verification (day-sentence.md P8), and the app's build runs it first, so what
* ships is what passed (P5). ROSEA_LIBRARY_DIR swaps the library folder, for the
* e2e build only; the app's Vite config reads the same variable.
*/
import { existsSync, readFileSync, readdirSync } from 'node:fs';
import { join, relative, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { validateCandidates } from '@rosea/content/candidates';
import { validateEntry, validateLibrary, type Rule } from '@rosea/content/validate';
const root = fileURLToPath(new URL('..', import.meta.url));
const libraryDir = resolve(root, process.env['ROSEA_LIBRARY_DIR'] ?? join('content', 'library'));
const candidatesDir = join(root, 'content', 'candidates');
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(`${relative(root, libraryDir)}/${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 (${relative(root, libraryDir).replaceAll('\\', '/')}): ${entries.length} entr${entries.length === 1 ? 'y' : 'ies'} in ${jsonIn(libraryDir).length} files, ${sunset} sunset (release needs 365)`
);
// ── the candidates: proposed, not verified ─────────────────────────────────
const candidates: unknown[] = [];
const candidateFiles = existsSync(candidatesDir) ? jsonIn(candidatesDir) : [];
for (const file of candidateFiles) {
const data: unknown = JSON.parse(readFileSync(join(candidatesDir, file), 'utf8'));
if (!Array.isArray(data)) {
console.error(`content/candidates/${file}: must be an array of candidates`);
failed = true;
continue;
}
candidates.push(...data);
}
for (const p of validateCandidates(candidates, entries)) {
console.error(`candidate ${p.id} [${p.rule}] ${p.message}`);
failed = true;
}
console.log(`candidates: ${candidates.length} in ${candidateFiles.length} files, waiting for a person to verify them`);
// ── 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);