[storage] DaySentence: Opened petals in the log; the rose file carries them as version 2
This commit is contained in:
@@ -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' }] }
|
||||
|
||||
Reference in New Issue
Block a user