import { createIdentity } from '@rosea/identity'; import type { CareEvent } 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 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)); const file = await s.exportRose(OLD); expect(file).toStrictEqual({ format: ROSE_FILE_FORMAT, version: 1, keys: [OLD], events: [act(OLD, 1)] }); 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)); 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 }); expect(await fresh.loadPastKeys()).toEqual([OLD]); expect(await fresh.loadCare()).toEqual([act(OLD, 1), act(OLD, 2)]); }); it('twice is the same as once, and the care given here meanwhile stays', async () => { const old = open(); await old.appendCare(act(OLD, 1)); 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.loadCare()).toEqual([act(NEW, 5), act(OLD, 1)]); 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 }); 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: 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([]); }); });