[storage] DaySentence: Opened petals in the log; the rose file carries them as version 2
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
import type { CareEvent, SentenceOpened } from '@rosea/schema';
|
||||
import { validateSentenceOpened } from '@rosea/schema/validate';
|
||||
import Dexie from 'dexie';
|
||||
import { IDBFactory, IDBKeyRange } from 'fake-indexeddb';
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import { asSentenceOpened, openStorage, type Storage } from '../src/index.js';
|
||||
|
||||
const KEY = 'K'.repeat(87);
|
||||
const petal: SentenceOpened = { kind: 'sentence-opened', ts: 1774944000000, localDay: '2026-03-31', from: KEY, messageId: 'ps-118-24', clientVersion: '0.1.0' };
|
||||
const weed: CareEvent = { kind: 'care', ts: 1774944060000, localDay: '2026-03-31', from: KEY, to: KEY, act: 'weed', trace: 'petal:rose', messageId: null, clientVersion: '0.1.0' };
|
||||
|
||||
const opened: Storage[] = [];
|
||||
const open = (indexedDB: IDBFactory = new IDBFactory()): Storage => {
|
||||
const s = openStorage({ indexedDB, IDBKeyRange });
|
||||
opened.push(s);
|
||||
return s;
|
||||
};
|
||||
afterEach(() => {
|
||||
for (const s of opened.splice(0)) s.close();
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
describe('opened petals (day-sentence.md P6)', () => {
|
||||
it('are appended to the same log and come back in order, across a reopen', async () => {
|
||||
const factory = new IDBFactory();
|
||||
const s = open(factory);
|
||||
await s.appendOpened(petal);
|
||||
await s.appendCare(weed);
|
||||
await s.appendOpened(new Proxy({ ...petal, ts: petal.ts + 86_400_000, localDay: '2026-04-01' }, {}));
|
||||
s.close();
|
||||
const again = open(factory);
|
||||
expect(await again.loadOpened()).toEqual([petal, { ...petal, ts: petal.ts + 86_400_000, localDay: '2026-04-01' }]);
|
||||
expect(await again.loadCare()).toEqual([weed]);
|
||||
});
|
||||
|
||||
it('loading care passes over them quietly: they are not broken entries', async () => {
|
||||
const s = open();
|
||||
await s.appendOpened(petal);
|
||||
const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined);
|
||||
expect(await s.loadCare()).toEqual([]);
|
||||
expect(warn).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('refuses text in place of an id, or anything else the contract refuses', async () => {
|
||||
const s = open();
|
||||
for (const bad of [
|
||||
{ ...petal, messageId: 'The LORD is my shepherd' },
|
||||
{ ...petal, note: 'hello' },
|
||||
{ ...petal, kind: 'care' },
|
||||
{ ...petal, from: 'short' }
|
||||
]) {
|
||||
await expect(s.appendOpened(bad as SentenceOpened)).rejects.toThrow(/unknown shape/);
|
||||
}
|
||||
expect(await s.loadOpened()).toEqual([]);
|
||||
});
|
||||
|
||||
it('a broken one already in the log is skipped and said, never removed', async () => {
|
||||
const factory = new IDBFactory();
|
||||
const s = open(factory);
|
||||
await s.appendOpened(petal);
|
||||
s.close();
|
||||
const raw = new Dexie('rosea', { indexedDB: factory, IDBKeyRange });
|
||||
raw.version(2).stores({ profile: 'key', events: '++id', keys: 'id' });
|
||||
await raw.table('events').add({ event: { kind: 'sentence-opened', messageId: 'Free words' } });
|
||||
raw.close();
|
||||
const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined);
|
||||
expect(await open(factory).loadOpened()).toEqual([petal]);
|
||||
expect(warn).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it('the hand check agrees with the schema on every case above', () => {
|
||||
for (const value of [petal, { ...petal, messageId: 'A sentence' }, { ...petal, note: 'x' }, { ...petal, ts: -1 }, { ...petal, ts: 1.5 }, { ...petal, localDay: '31.3.2026' }]) {
|
||||
expect(asSentenceOpened(value) !== null, JSON.stringify(value)).toBe(validateSentenceOpened(value).ok);
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user