Files
Rosea/packages/content/test/candidates.test.ts
T

88 lines
5.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, expect, it } from 'vitest';
import { asVerified, validateCandidate, validateCandidates, verifyCandidates } from '../src/candidates.js';
import { validateEntry } from '../src/validator.js';
const candidate = (id: string, over: Record<string, unknown> = {}): Record<string, unknown> => ({
id,
text: { en: 'Fixture text for a candidate, not a real sentence.' },
source: { work: 'Fixture 1:1', author: null, tradition: 'biblical', translation: 'KJV' },
use: ['sunset'],
tone: 'gentle',
promisesOutcome: false,
check: { url: 'https://example.org/fixture', where: 'chapter 1, verse 1', publicDomain: 'KJV, 1611/1769' },
...over
});
describe('a candidate (day-sentence.md P8)', () => {
it('becomes a valid library entry once a person has checked it, with no check field left', () => {
const entry = asVerified(candidate('fx-1'), 'Mark', '2026-11-02') as Record<string, unknown>;
expect(validateEntry(entry)).toEqual([]);
expect(entry).not.toHaveProperty('check');
expect(entry['source']).toMatchObject({ publicDomain: true, verifiedBy: 'Mark', verifiedAt: '2026-11-02' });
});
it('passes when it would pass the library', () => {
expect(validateCandidate(candidate('fx-1'))).toEqual([]);
});
it('is held to every library rule: length, forbidden words, promises, translations', () => {
expect(validateCandidate(candidate('fx-long', { text: { en: 'x'.repeat(241) } })).map((p) => p.rule)).toContain('too-long');
expect(validateCandidate(candidate('fx-word', { text: { en: 'A lonely road.' } })).map((p) => p.rule)).toContain('forbidden-word');
expect(validateCandidate(candidate('fx-promise', { text: { en: 'Everything will be well.' } })).map((p) => p.rule)).toContain('promises-outcome');
expect(
validateCandidate(candidate('fx-niv', { source: { work: 'Fixture', author: null, tradition: 'biblical', translation: 'NIV' } })).map((p) => p.rule)
).toContain('translation-under-copyright');
});
it('needs a check that says where to read it', () => {
const { check: _, ...noCheck } = candidate('fx-1');
expect(validateCandidate(noCheck)[0]?.message).toMatch(/check\.url/);
expect(validateCandidate(candidate('fx-1', { check: { url: 'http://example.org', where: 'x', publicDomain: 'x' } }))[0]?.message).toMatch(/https/);
});
it('may not say it was verified: only the person who verifies adds that', () => {
for (const field of ['publicDomain', 'verifiedBy', 'verifiedAt']) {
const c = candidate('fx-1', { source: { work: 'F', author: null, tradition: 'biblical', translation: 'KJV', [field]: field === 'publicDomain' ? true : 'x' } });
expect(validateCandidate(c).map((p) => p.message).join(' '), field).toMatch(new RegExp(`source\\.${field}`));
}
});
it('may not reuse an id the library or another candidate has', () => {
const library = [asVerified(candidate('fx-1'), 'Mark', '2026-11-01')];
const problems = validateCandidates([candidate('fx-1'), candidate('fx-2'), candidate('fx-2')], library);
expect(problems.map((p) => `${p.id} ${p.rule}`)).toEqual(['fx-1 duplicate-id', 'fx-2 duplicate-id']);
});
});
describe('library:verify — the author’s command', () => {
const files = { candidates: { 'biblical.json': [candidate('fx-1'), candidate('fx-2')], 'poetry.json': [candidate('fx-3', { source: { work: 'P', author: 'A Poet', tradition: 'poetry', translation: null } })] }, library: {} };
it('moves the named candidates into the library file of the same name, stamped with the name and the day', () => {
const result = verifyCandidates({ ...files, ids: ['fx-1', 'fx-3'], by: 'Mark', day: '2026-11-02' });
expect(result.ok).toBe(true);
if (!result.ok) return;
expect(result.moved).toEqual(['fx-1', 'fx-3']);
expect(Object.keys(result.library).sort()).toEqual(['biblical.json', 'poetry.json']);
expect(result.library['biblical.json']).toEqual([asVerified(candidate('fx-1'), 'Mark', '2026-11-02')]);
expect(result.candidates['biblical.json']).toEqual([candidate('fx-2')]);
expect(result.candidates['poetry.json']).toEqual([]);
});
it('adds to what the library already holds, never replaces it', () => {
const kept = asVerified(candidate('old-1'), 'Mark', '2026-11-01');
const result = verifyCandidates({ ...files, library: { 'biblical.json': [kept] }, ids: ['fx-2'], by: 'Mark', day: '2026-11-02' });
expect(result.ok && result.library['biblical.json']).toEqual([kept, asVerified(candidate('fx-2'), 'Mark', '2026-11-02')]);
});
it('refuses, moving nothing, without a person’s name, with an unknown id, or with a line the library would refuse', () => {
expect(verifyCandidates({ ...files, ids: ['fx-1'], by: ' ', day: '2026-11-02' })).toMatchObject({ ok: false });
expect(verifyCandidates({ ...files, ids: ['fx-1'], by: 'candidate', day: '2026-11-02' })).toMatchObject({ ok: false });
expect(verifyCandidates({ ...files, ids: [], by: 'Mark', day: '2026-11-02' })).toMatchObject({ ok: false });
expect(verifyCandidates({ ...files, ids: ['fx-1', 'nope'], by: 'Mark', day: '2026-11-02' })).toEqual({ ok: false, problems: ["no candidate 'nope'"] });
const bad = { candidates: { 'biblical.json': [candidate('fx-bad', { text: { en: 'A lonely road.' } })] }, library: {} };
const result = verifyCandidates({ ...bad, ids: ['fx-bad'], by: 'Mark', day: '2026-11-02' });
expect(result.ok).toBe(false);
expect(!result.ok && result.problems.join(' ')).toMatch(/forbidden-word/);
});
});