import { readFileSync, readdirSync } from 'node:fs'; import { join } from 'node:path'; import { fileURLToPath } from 'node:url'; import { describe, expect, it } from 'vitest'; import { LENGTH_CAP, validateEntry, validateLibrary, type Rule } from '../src/validator.js'; const fixturesDir = fileURLToPath(new URL('../../../content/fixtures', import.meta.url)); interface Fixture { readonly expect: Rule; readonly why: string; readonly entry?: unknown; readonly entries?: readonly unknown[]; } const fixtures = readdirSync(fixturesDir) .filter((f) => f.endsWith('.json')) .sort() .map((file) => ({ file, fixture: JSON.parse(readFileSync(join(fixturesDir, file), 'utf8')) as Fixture })); const valid = { id: 'valid-entry', text: { en: 'Fixture text, not a real sentence.', cs: 'Testovací text.' }, source: { work: 'Fixture', author: null, tradition: 'biblical', translation: 'KJV', publicDomain: true, verifiedBy: 'Fixture', verifiedAt: '2026-11-01' }, use: ['sunset', 'comfort'], tone: 'gentle', promisesOutcome: false }; describe('library validator — negative fixtures', () => { it('finds the fixtures', () => { // A glob matching nothing would pass every case below. expect(fixtures.length).toBeGreaterThanOrEqual(13); }); it.each(fixtures)('$file fails with its named rule', ({ fixture }) => { const problems = fixture.entries !== undefined ? validateLibrary(fixture.entries) : validateEntry(fixture.entry); expect(problems.map((p) => p.rule), fixture.why).toContain(fixture.expect); for (const p of problems) expect(p.message.length, 'each problem says what is wrong').toBeGreaterThan(10); }); it('every rule the validator has is proved by at least one fixture', () => { const proved = new Set(fixtures.map(({ fixture }) => fixture.expect)); const RULES: readonly Rule[] = [ 'schema', 'no-source', 'not-public-domain', 'translation-under-copyright', 'too-long', 'promises-outcome', 'forbidden-word', 'no-tradition', 'duplicate-id' ]; expect(RULES.filter((r) => !proved.has(r))).toEqual([]); }); }); describe('library validator — what passes', () => { it('accepts a sourced, public-domain, short, gentle entry', () => { expect(validateEntry(valid)).toEqual([]); }); it('accepts a line the author wrote for Rosea, though it is not public domain', () => { const own = { ...valid, source: { ...valid.source, tradition: 'rosea', translation: null, publicDomain: false } }; expect(validateEntry(own)).toEqual([]); }); it('accepts text exactly at the length cap, counting characters and not UTF-16 units', () => { const atCap = { ...valid, text: { en: 'ř'.repeat(LENGTH_CAP) } }; expect(validateEntry(atCap)).toEqual([]); }); it('accepts an empty library, and says nothing about it', () => { expect(validateLibrary([])).toEqual([]); }); });