Files
Rosea/packages/storage/test/profile.test.ts
T

106 lines
4.3 KiB
TypeScript

import { IDBFactory, IDBKeyRange } from 'fake-indexeddb';
import { afterEach, describe, expect, it, vi } from 'vitest';
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: Storage[] = [];
/** A fresh, empty fake database per call unless one is passed: tests never share state. */
function open(indexedDB: IDBFactory = new IDBFactory()): Storage {
const p = openStorage({ indexedDB, IDBKeyRange });
opened.push(p);
return p;
}
afterEach(() => {
for (const p of opened.splice(0)) p.close();
vi.restoreAllMocks();
});
describe('the profile store — the sunset place', () => {
it('reads null before the player has answered', async () => {
expect(await open().loadSunsetPlace()).toBeNull();
});
it('keeps a chosen town, and keeps it across a reopen', async () => {
const factory = new IDBFactory();
const first = open(factory);
await first.saveSunsetPlace(brno);
first.close();
expect(await open(factory).loadSunsetPlace()).toEqual(brno);
});
it('keeps "not now" as the device zone, not as a town', async () => {
const p = open();
await p.saveSunsetPlace({ kind: 'device-zone' });
expect(await p.loadSunsetPlace()).toEqual({ kind: 'device-zone' });
});
it('saves a place handed over inside a Proxy, as Svelte 5 state is, by writing a plain copy', async () => {
// Found by the e2e suite: IndexedDB cannot clone a Proxy, and the UI's towns live in $state.
const proxied = new Proxy(brno, {}) as SunsetPlace;
expect(() => structuredClone(proxied)).toThrow();
const p = open();
await p.saveSunsetPlace(proxied);
expect(await p.loadSunsetPlace()).toEqual(brno);
});
it('refuses to write a place of the wrong shape', async () => {
await expect(open().saveSunsetPlace({ kind: 'town' } as unknown as SunsetPlace)).rejects.toThrow(/unknown shape/);
});
it('a new choice replaces the old one', async () => {
const p = open();
await p.saveSunsetPlace({ kind: 'device-zone' });
await p.saveSunsetPlace(brno);
expect(await p.loadSunsetPlace()).toEqual(brno);
});
it('a record of the wrong shape reads as no answer, and says so in the console', async () => {
const factory = new IDBFactory();
// Write a broken record straight into the store, as an old version or a torn write might.
const p = open(factory);
await p.saveSunsetPlace(brno);
p.close();
await new Promise<void>((resolve, reject) => {
const req = factory.open('rosea');
req.onsuccess = () => {
const tx = req.result.transaction('profile', 'readwrite');
tx.objectStore('profile').put({ key: 'sunsetPlace', value: { kind: 'town', town: { name: 'Brno', lat: 'north' } } });
tx.oncomplete = () => { req.result.close(); resolve(); };
tx.onerror = () => reject(tx.error);
};
req.onerror = () => reject(req.error);
});
const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined);
expect(await open(factory).loadSunsetPlace()).toBeNull();
expect(warn).toHaveBeenCalledWith(expect.stringMatching(/unknown shape/), expect.anything());
});
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(openStorage({ indexedDB: broken, IDBKeyRange }).loadSunsetPlace()).rejects.toThrow();
});
});
describe('asSunsetPlace — the shape check', () => {
it.each([
[null], [42], ['town'], [{ kind: 'gps' }], [{ kind: 'town' }],
[{ kind: 'town', town: { ...(brno as { town: object }).town, lat: 91 } }],
[{ kind: 'town', town: { ...(brno as { town: object }).town, lon: Number.NaN } }],
[{ kind: 'town', town: { ...(brno as { town: object }).town, name: '' } }],
[{ kind: 'town', town: { ...(brno as { town: object }).town, timeZone: 7 } }]
])('refuses %j', (value) => {
expect(asSunsetPlace(value)).toBeNull();
});
it('keeps only the fields it knows', () => {
const extra = { kind: 'town', town: { ...(brno as { town: object }).town, gps: [49.2, 16.6] } };
expect(asSunsetPlace(extra)).toEqual(brno);
});
});