Files
Rosea/packages/schema/test/schemas.test.ts
mnovak 6885a217cd [repo] Workspace: Review Phase 0 and record gaps, lessons and handover
Adversarial review with per-package counts and 13 reintroduced bugs, all
caught. Fixes from the review: the trace geocell is capped at 6 characters
(~1 km; 16 allowed centimetres), one forbidden-word hit per place, and an
e2e guard that meta charset stays within the first 1024 bytes.

CLAUDE.md §6.2 no longer contradicts itself about care acts; §2 and §3
say what Phase 0 actually built.
2026-11-01 00:36:06 +01:00

119 lines
3.6 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { validateCareEvent, validateHopeSentence, validateTrace } from '../src/validate.js';
const KEY_A = 'A'.repeat(43);
const KEY_B = 'b_-'.repeat(15);
const careEvent = {
kind: 'care',
ts: 1761500000000,
localDay: '2026-10-24',
from: KEY_A,
to: KEY_B,
act: 'water',
trace: 'petal:pale-pink',
messageId: 'ps-23-4',
clientVersion: '0.1.0'
};
const sentence = {
id: 'ps-23-4',
text: { en: 'Placeholder text for a schema test.' },
source: {
work: 'Psalms 23:4',
author: null,
tradition: 'biblical',
translation: 'KJV',
publicDomain: true,
verifiedBy: 'Test',
verifiedAt: '2026-11-01'
},
use: ['sunset'],
tone: 'gentle',
promisesOutcome: false
};
const trace = {
traceId: 'r'.repeat(22),
geocell: 'u2fkb',
dayBucket: '2026-10-24',
gesture: 'petal',
sig: 's'.repeat(64)
};
describe('hope sentence', () => {
it('accepts a complete entry', () => {
expect(validateHopeSentence(sentence)).toMatchObject({ ok: true });
});
it('rejects an entry with no source', () => {
const { source: _source, ...rest } = sentence;
const result = validateHopeSentence(rest);
expect(result.ok).toBe(false);
expect(!result.ok && result.errors.join('\n')).toContain("'source'");
});
it('rejects an id with a space, because an id is what travels between players', () => {
expect(validateHopeSentence({ ...sentence, id: 'you are seen' }).ok).toBe(false);
});
it('rejects an unknown use', () => {
expect(validateHopeSentence({ ...sentence, use: ['push-notification'] }).ok).toBe(false);
});
});
describe('care event', () => {
it('accepts every act the rose has', () => {
for (const act of ['water', 'shade', 'weed', 'petals']) {
expect(validateCareEvent({ ...careEvent, act }), act).toMatchObject({ ok: true });
}
});
it('rejects the acts §6.2 used to list by mistake', () => {
for (const act of ['wind', 'light']) {
expect(validateCareEvent({ ...careEvent, act }).ok, act).toBe(false);
}
});
it('accepts a null messageId', () => {
expect(validateCareEvent({ ...careEvent, messageId: null })).toMatchObject({ ok: true });
});
it('rejects free text in messageId (CLAUDE.md §5.6 rule 1)', () => {
expect(validateCareEvent({ ...careEvent, messageId: 'meet me at the station' }).ok).toBe(false);
});
it('rejects a missing localDay', () => {
const { localDay: _day, ...rest } = careEvent;
expect(validateCareEvent(rest).ok).toBe(false);
});
it('rejects any extra field, so nothing rides along unseen', () => {
expect(validateCareEvent({ ...careEvent, note: 'hello' }).ok).toBe(false);
});
});
describe('trace', () => {
it('accepts a day-bucketed trace', () => {
expect(validateTrace(trace)).toMatchObject({ ok: true });
});
it('rejects a day bucket finer than a day', () => {
expect(validateTrace({ ...trace, dayBucket: '2026-10-24T18:42' }).ok).toBe(false);
});
it('rejects a timestamp field', () => {
expect(validateTrace({ ...trace, ts: 1761500000000 }).ok).toBe(false);
});
it('rejects a precise coordinate in place of a cell', () => {
expect(validateTrace({ ...trace, geocell: '50.0755,14.4378' }).ok).toBe(false);
});
it('rejects a cell finer than ~1 km: a long geohash is a house, not a district', () => {
expect(validateTrace({ ...trace, geocell: 'u2fkbn' }).ok, '6 chars, ~1.2 km').toBe(true);
expect(validateTrace({ ...trace, geocell: 'u2fkbnx' }).ok, '7 chars, ~150 m').toBe(false);
expect(validateTrace({ ...trace, geocell: 'u2fkbnxq9vk2' }).ok, '12 chars, ~4 cm').toBe(false);
});
});