122 lines
5.5 KiB
TypeScript
122 lines
5.5 KiB
TypeScript
import { createIdentity } from '@rosea/identity';
|
|
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';
|
|
|
|
const OLD = 'O'.repeat(87);
|
|
const NEW = 'N'.repeat(87);
|
|
const act = (to: string, ts: number): CareEvent => ({
|
|
kind: 'care', ts, localDay: '2026-03-01', from: to, to, act: 'water',
|
|
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 });
|
|
opened.push(s);
|
|
return s;
|
|
};
|
|
afterEach(() => {
|
|
for (const s of opened.splice(0)) s.close();
|
|
});
|
|
|
|
describe('saving the rose to a file', () => {
|
|
it('holds her keys and her care, and nothing else: no private key, no town', async () => {
|
|
const s = open();
|
|
// A real identity is kept, so a leak of anything from it would have something to leak.
|
|
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: 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);
|
|
});
|
|
});
|
|
|
|
describe('bringing her back', () => {
|
|
it('on a new device: her old key joins the past keys and all her care comes back', async () => {
|
|
const old = open();
|
|
await old.appendCare(act(OLD, 1));
|
|
await old.appendCare(act(OLD, 2));
|
|
const file = JSON.parse(JSON.stringify(await old.exportRose(OLD)));
|
|
|
|
const fresh = open();
|
|
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, 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));
|
|
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([]);
|
|
});
|
|
|
|
it('refuses anything that is not a rose, and changes nothing', async () => {
|
|
const s = open();
|
|
await s.appendCare(act(NEW, 1));
|
|
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' }] }
|
|
]) {
|
|
await expect(s.importRose(bad, NEW)).rejects.toThrow(/does not hold a rose/);
|
|
}
|
|
expect(await s.loadCare()).toEqual([act(NEW, 1)]);
|
|
expect(await s.loadPastKeys()).toEqual([]);
|
|
});
|
|
});
|