[storage] DaySentence: Opened petals in the log; the rose file carries them as version 2

This commit is contained in:
2026-11-02 08:55:05 +01:00
parent 636c0bcbc0
commit cdbaf56195
3 changed files with 194 additions and 25 deletions
+76
View File
@@ -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);
}
});
});
+35 -5
View File
@@ -1,5 +1,5 @@
import { createIdentity } from '@rosea/identity';
import type { CareEvent } from '@rosea/schema';
import type { CareEvent, SentenceOpened } from '@rosea/schema';
import { IDBFactory, IDBKeyRange } from 'fake-indexeddb';
import { afterEach, describe, expect, it } from 'vitest';
import { asRoseFile, openStorage, ROSE_FILE_FORMAT, type Storage } from '../src/index.js';
@@ -11,6 +11,10 @@ const act = (to: string, ts: number): CareEvent => ({
trace: 'petal:rose', messageId: null, clientVersion: '0.1.0', amount: 0.5
});
const petal = (from: string, ts: number): SentenceOpened => ({
kind: 'sentence-opened', ts, localDay: '2026-03-01', from, messageId: 'ps-118-24', clientVersion: '0.1.0'
});
const opened: Storage[] = [];
const open = (indexedDB: IDBFactory = new IDBFactory()): Storage => {
const s = openStorage({ indexedDB, IDBKeyRange });
@@ -28,14 +32,16 @@ describe('saving the rose to a file', () => {
await s.keepIdentity(await createIdentity());
await s.saveSunsetPlace({ kind: 'town', town: { name: 'Brno', region: '', country: 'Czechia', lat: 49.2, lon: 16.61, timeZone: 'Europe/Prague' } });
await s.appendCare(act(OLD, 1));
await s.appendOpened(petal(OLD, 2));
const file = await s.exportRose(OLD);
expect(file).toStrictEqual({ format: ROSE_FILE_FORMAT, version: 1, keys: [OLD], events: [act(OLD, 1)] });
expect(file).toStrictEqual({ format: ROSE_FILE_FORMAT, version: 2, keys: [OLD], events: [act(OLD, 1)], opened: [petal(OLD, 2)] });
expect(JSON.stringify(file)).not.toMatch(/Brno|privateKey|CryptoKey/);
});
it('survives a trip through JSON text unchanged', async () => {
const s = open();
await s.appendCare(act(OLD, 1));
await s.appendOpened(petal(OLD, 2));
const file = await s.exportRose(OLD);
expect(asRoseFile(JSON.parse(JSON.stringify(file)))).toEqual(file);
});
@@ -49,27 +55,48 @@ describe('bringing her back', () => {
const file = JSON.parse(JSON.stringify(await old.exportRose(OLD)));
const fresh = open();
expect(await fresh.importRose(file, NEW)).toEqual({ added: 2 });
expect(await fresh.importRose(file, NEW)).toEqual({ added: 2, petals: 0 });
expect(await fresh.loadPastKeys()).toEqual([OLD]);
expect(await fresh.loadCare()).toEqual([act(OLD, 1), act(OLD, 2)]);
});
it('her opened petals come with her, so the next petal is the one she would have held (day-sentence.md P7)', async () => {
const old = open();
await old.appendCare(act(OLD, 1));
await old.appendOpened(petal(OLD, 2));
const file = JSON.parse(JSON.stringify(await old.exportRose(OLD)));
const fresh = open();
await fresh.appendOpened(petal(NEW, 9));
expect(await fresh.importRose(file, NEW)).toEqual({ added: 1, petals: 1 });
expect(await fresh.loadOpened()).toEqual([petal(NEW, 9), petal(OLD, 2)]);
});
it('a version 1 file, saved before petals existed, is still brought back', async () => {
const v1 = { format: ROSE_FILE_FORMAT, version: 1, keys: [OLD], events: [act(OLD, 1)] };
const fresh = open();
expect(await fresh.importRose(v1, NEW)).toEqual({ added: 1, petals: 0 });
expect(await fresh.loadCare()).toEqual([act(OLD, 1)]);
});
it('twice is the same as once, and the care given here meanwhile stays', async () => {
const old = open();
await old.appendCare(act(OLD, 1));
await old.appendOpened(petal(OLD, 2));
const file = await old.exportRose(OLD);
const here = open();
await here.appendCare(act(NEW, 5));
await here.importRose(file, NEW);
expect(await here.importRose(file, NEW)).toEqual({ added: 0 });
expect(await here.importRose(file, NEW)).toEqual({ added: 0, petals: 0 });
expect(await here.loadCare()).toEqual([act(NEW, 5), act(OLD, 1)]);
expect(await here.loadOpened()).toEqual([petal(OLD, 2)]);
expect(await here.loadPastKeys()).toEqual([OLD]);
});
it('a file saved on this very device adds nothing and does not list its own key as past', async () => {
const s = open();
await s.appendCare(act(NEW, 1));
expect(await s.importRose(await s.exportRose(NEW), NEW)).toEqual({ added: 0 });
await s.appendOpened(petal(NEW, 2));
expect(await s.importRose(await s.exportRose(NEW), NEW)).toEqual({ added: 0, petals: 0 });
expect(await s.loadPastKeys()).toEqual([]);
});
@@ -79,6 +106,9 @@ describe('bringing her back', () => {
for (const bad of [
null, 'rose', { format: 'other', version: 1, keys: [OLD], events: [] },
{ format: ROSE_FILE_FORMAT, version: 2, keys: [OLD], events: [] },
{ format: ROSE_FILE_FORMAT, version: 3, keys: [OLD], events: [], opened: [] },
{ format: ROSE_FILE_FORMAT, version: 2, keys: [OLD], events: [], opened: [{ ...petal(OLD, 1), messageId: 'Free words' }] },
{ format: ROSE_FILE_FORMAT, version: 1, keys: [OLD], events: [], opened: [petal(OLD, 1)] },
{ format: ROSE_FILE_FORMAT, version: 1, keys: [], events: [] },
{ format: ROSE_FILE_FORMAT, version: 1, keys: ['short'], events: [] },
{ format: ROSE_FILE_FORMAT, version: 1, keys: [OLD], events: [{ ...act(OLD, 1), note: 'hello' }] }