[storage] CareLog: Append-only care events and the kept identity, checked against the contract
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
import { createIdentity } from '@rosea/identity';
|
||||
import type { CareEvent } from '@rosea/schema';
|
||||
import { validateCareEvent } from '@rosea/schema/validate';
|
||||
import Dexie from 'dexie';
|
||||
import { IDBFactory, IDBKeyRange } from 'fake-indexeddb';
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import { asCareEvent, openStorage, type Storage } from '../src/index.js';
|
||||
|
||||
const KEY = 'K'.repeat(87);
|
||||
const water: CareEvent = {
|
||||
kind: 'care', ts: 1774944000000, localDay: '2026-03-31', from: KEY, to: KEY, act: 'water',
|
||||
trace: 'petal:pale-pink', messageId: null, clientVersion: '0.1.0', amount: 0.5
|
||||
};
|
||||
const weed: CareEvent = { kind: 'care', ts: 1774944060000, localDay: '2026-03-31', from: KEY, to: KEY, act: 'weed', trace: 'petal:pale-pink', 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('the care log', () => {
|
||||
it('appends and comes back in the order written, across a reopen', async () => {
|
||||
const factory = new IDBFactory();
|
||||
const s = open(factory);
|
||||
await s.appendCare(water);
|
||||
await s.appendCare(new Proxy(weed, {})); // as Svelte 5 state would hand it over
|
||||
s.close();
|
||||
expect(await open(factory).loadCare()).toEqual([water, weed]);
|
||||
});
|
||||
|
||||
it('refuses to append anything the contract refuses', async () => {
|
||||
const s = open();
|
||||
for (const bad of [
|
||||
{ ...water, note: 'hello' },
|
||||
{ ...weed, amount: 0.5 },
|
||||
{ ...water, amount: 2 },
|
||||
{ ...weed, messageId: 'meet me at the station' }
|
||||
]) {
|
||||
await expect(s.appendCare(bad as CareEvent)).rejects.toThrow(/unknown shape/);
|
||||
}
|
||||
expect(await s.loadCare()).toEqual([]);
|
||||
});
|
||||
|
||||
it('a broken entry already in the log is skipped and said, never removed', async () => {
|
||||
const factory = new IDBFactory();
|
||||
const s = open(factory);
|
||||
await s.appendCare(water);
|
||||
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: 'care', act: 'dance' } });
|
||||
raw.close();
|
||||
const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined);
|
||||
expect(await open(factory).loadCare()).toEqual([water]);
|
||||
expect(warn).toHaveBeenCalledWith(expect.stringMatching(/skipped, not removed/), expect.anything());
|
||||
});
|
||||
});
|
||||
|
||||
describe('asCareEvent agrees with the schema\'s own validator', () => {
|
||||
const variants: Record<string, unknown>[] = [
|
||||
water, weed,
|
||||
{ ...water, amount: 0 }, { ...water, amount: 1 }, { ...water, amount: 1.5 }, { ...water, amount: -1 },
|
||||
{ ...water, amount: undefined }, { ...weed, amount: 0.2 }, { ...weed, act: 'water' },
|
||||
{ ...weed, act: 'wind' }, { ...weed, ts: -1 }, { ...weed, ts: 1.5 }, { ...weed, localDay: '2026-3-31' },
|
||||
{ ...weed, from: 'short' }, { ...weed, trace: 'petal:Pink' }, { ...weed, messageId: 'ps-23-4' },
|
||||
{ ...weed, messageId: 'x'.repeat(65) }, { ...weed, clientVersion: '1.0' }, { ...weed, kind: 'sunset' },
|
||||
{ ...weed, extra: true }
|
||||
].map((v) => Object.fromEntries(Object.entries(v).filter(([, x]) => x !== undefined)));
|
||||
|
||||
it.each(variants.map((v, i) => [i, v] as const))('variant %i', (_i, v) => {
|
||||
expect(asCareEvent(v) !== null).toBe(validateCareEvent(v).ok);
|
||||
});
|
||||
});
|
||||
|
||||
describe('the identity', () => {
|
||||
it('is kept once; a second, racing one is dropped and the first returned', async () => {
|
||||
const factory = new IDBFactory();
|
||||
const s = open(factory);
|
||||
const [first, second] = [await createIdentity(), await createIdentity()];
|
||||
const [a, b] = await Promise.all([s.keepIdentity(first), open(factory).keepIdentity(second)]);
|
||||
expect(a.publicKey).toBe(b.publicKey);
|
||||
expect((await open(factory).loadIdentity())?.publicKey).toBe(a.publicKey);
|
||||
});
|
||||
|
||||
it('the private key comes back still unexportable', async () => {
|
||||
const factory = new IDBFactory();
|
||||
await open(factory).keepIdentity(await createIdentity());
|
||||
const back = await open(factory).loadIdentity();
|
||||
expect(back?.privateKey.extractable).toBe(false);
|
||||
});
|
||||
|
||||
it('nothing kept reads null', async () => {
|
||||
expect(await open().loadIdentity()).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('the database version', () => {
|
||||
it('upgrading from version 1 keeps the sunset place', async () => {
|
||||
const factory = new IDBFactory();
|
||||
const v1 = new Dexie('rosea', { indexedDB: factory, IDBKeyRange });
|
||||
v1.version(1).stores({ profile: 'key' });
|
||||
await v1.table('profile').put({ key: 'sunsetPlace', value: { kind: 'device-zone' } });
|
||||
v1.close();
|
||||
const s = open(factory);
|
||||
expect(await s.loadSunsetPlace()).toEqual({ kind: 'device-zone' });
|
||||
expect(await s.loadCare()).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -1,16 +1,16 @@
|
||||
import { IDBFactory, IDBKeyRange } from 'fake-indexeddb';
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import { asSunsetPlace, openProfile, type Profile, type SunsetPlace } from '../src/index.js';
|
||||
import { asSunsetPlace, openStorage, type Storage, type SunsetPlace } from '../src/index.js';
|
||||
|
||||
const brno: SunsetPlace = {
|
||||
kind: 'town',
|
||||
town: { name: 'Brno', region: '', country: 'Czechia', lat: 49.2, lon: 16.61, timeZone: 'Europe/Prague' }
|
||||
};
|
||||
|
||||
const opened: Profile[] = [];
|
||||
const opened: Storage[] = [];
|
||||
/** A fresh, empty fake database per call unless one is passed: tests never share state. */
|
||||
function open(indexedDB: IDBFactory = new IDBFactory()): Profile {
|
||||
const p = openProfile({ indexedDB, IDBKeyRange });
|
||||
function open(indexedDB: IDBFactory = new IDBFactory()): Storage {
|
||||
const p = openStorage({ indexedDB, IDBKeyRange });
|
||||
opened.push(p);
|
||||
return p;
|
||||
}
|
||||
@@ -83,7 +83,7 @@ describe('the profile store — the sunset place', () => {
|
||||
it('a database that cannot open rejects, so the app can fall back to the session', async () => {
|
||||
const broken = new IDBFactory();
|
||||
broken.open = () => { throw new DOMException('blocked by the browser', 'SecurityError'); };
|
||||
await expect(openProfile({ indexedDB: broken, IDBKeyRange }).loadSunsetPlace()).rejects.toThrow();
|
||||
await expect(openStorage({ indexedDB: broken, IDBKeyRange }).loadSunsetPlace()).rejects.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user