import { GARDENER_FIELDS, GARDENER_OPTIONS, type Gardener } from '@rosea/schema'; import { validateGardener } from '@rosea/schema/validate'; import { IDBFactory, IDBKeyRange } from 'fake-indexeddb'; import { afterEach, describe, expect, it, vi } from 'vitest'; import { asGardener, openStorage, type Change, type Storage } from '../src/index.js'; const look: Gardener = { presentation: 'girl', skin: 'skin-3', hair: 'braids', hairColour: 'auburn', clothes: 'sage', hat: 'straw-hat' }; const KEY = 'K'.repeat(87); const opened: Storage[] = []; const open = (indexedDB: IDBFactory = new IDBFactory(), channel: string | null = null): Storage => { const s = openStorage({ indexedDB, IDBKeyRange }, { channel }); opened.push(s); return s; }; afterEach(() => { for (const s of opened.splice(0)) s.close(); vi.restoreAllMocks(); }); describe('the gardener (planet-garden.md G6)', () => { it('is null before the player chose, then kept across a reopen', async () => { const factory = new IDBFactory(); const s = open(factory); expect(await s.loadGardener()).toBeNull(); await s.saveGardener(new Proxy(look, {})); s.close(); expect(await open(factory).loadGardener()).toEqual(look); }); it('refuses a value off its list, a missing field, an extra field, or free text', async () => { const s = open(); for (const bad of [ { ...look, hat: 'crown' }, { ...look, clothes: 'green suit' }, { ...look, extra: 'a scarf' }, (({ hat: _, ...rest }) => rest)(look), { ...look, presentation: 'Marek' } ]) { await expect(s.saveGardener(bad as unknown as Gardener)).rejects.toThrow(/unknown shape/); } expect(await s.loadGardener()).toBeNull(); }); it('the hand check, the schema and the lists agree', () => { expect([...GARDENER_FIELDS].sort()).toEqual(Object.keys(GARDENER_OPTIONS).sort()); for (const k of GARDENER_FIELDS) { for (const v of GARDENER_OPTIONS[k]) expect(validateGardener({ ...look, [k]: v }).ok).toBe(true); expect(asGardener({ ...look, [k]: 'other' })).toBeNull(); expect(validateGardener({ ...look, [k]: 'other' }).ok).toBe(false); } expect(asGardener(look)).toEqual(look); }); it('never offers what would recreate the book\'s boy', () => { const everything = JSON.stringify(GARDENER_OPTIONS).toLowerCase(); for (const word of ['scarf', 'coat', 'cape', 'suit', 'crown', 'sword', 'golden']) expect(everything).not.toContain(word); }); it('is announced to the other tabs, and never travels in the rose file', async () => { const factory = new IDBFactory(); const a = open(factory, 'rosea-test-gardener'); const b = open(factory, 'rosea-test-gardener'); const heard: Change[] = []; b.onChange((w) => heard.push(w)); await a.saveGardener(look); await new Promise((r) => setTimeout(r, 100)); expect(heard).toEqual(['gardener']); expect(JSON.stringify(await a.exportRose(KEY))).not.toMatch(/braids|auburn|skin-3|gardener/); }); });