import { beforeEach, describe, expect, it } from "vitest"; import * as v from "valibot"; import { createPersistentSignal } from "./persist"; /** * The load-time guard, tested at the seam itself: everything above it (the * connection, the login draft, repeat mode) inherits this behaviour, so these are * the tests that say what a `schema` is for — and what it refuses to do. */ const KEY = "hackermusic.test.shape.v1"; interface Cfg { host: string; retries: number; } const cfgSchema = v.object({ host: v.string(), retries: v.number() }); const seed = (raw: string) => localStorage.setItem(KEY, raw); beforeEach(() => localStorage.clear()); describe("createPersistentSignal — a stored value that fits", () => { it("is loaded, reactively and synchronously", () => { seed(JSON.stringify({ host: "https://nav.example.com", retries: 2 })); const store = createPersistentSignal( KEY, { host: "", retries: 0 }, { schema: cfgSchema, }, ); expect(store.peek()).toEqual({ host: "https://nav.example.com", retries: 2 }); expect(store.get()).toEqual({ host: "https://nav.example.com", retries: 2 }); }); it("writes through without consulting the schema", () => { // The write path is volume/scrub/seek: validating bytes we just constructed // would tax every frame to guard against a mistake we did not make. This asserts // the documented promise rather than the convenient one — a check that fires on // load but not on write. let checks = 0; const counting = v.object({ host: v.pipe( v.string(), v.check(() => { checks += 1; return true; }), ), retries: v.number(), }); seed(JSON.stringify({ host: "https://a.example.com", retries: 1 })); const store = createPersistentSignal(KEY, { host: "", retries: 0 }, { schema: counting }); expect(checks).toBe(1); // read: checked store.set({ host: "https://b.example.com", retries: 3 }); expect(checks).toBe(1); // write: not checked expect(JSON.parse(localStorage.getItem(KEY)!)).toEqual({ host: "https://b.example.com", retries: 3, }); store.set((c) => ({ ...c, retries: c.retries + 1 })); expect(checks).toBe(1); }); }); describe("createPersistentSignal — a stored value that does not fit", () => { it("falls back on JSON that parses but is the wrong shape — the case JSON.parse could not see", () => { // Syntax errors were always caught. `{host: 3}` never was: it went to the // caller as a Cfg, and surfaced wherever a string was expected of it. seed(JSON.stringify({ host: 3, retries: 1 })); const recovered: string[] = []; const store = createPersistentSignal( KEY, { host: "", retries: 0 }, { schema: cfgSchema, onRecover: () => recovered.push("recovered") }, ); expect(store.peek()).toEqual({ host: "", retries: 0 }); expect(recovered).toEqual(["recovered"]); // once, at load — not on every read }); it("falls back when a required field is simply absent", () => { seed(JSON.stringify({ host: "https://nav.example.com" })); const store = createPersistentSignal( KEY, { host: "", retries: 0 }, { schema: cfgSchema, }, ); expect(store.peek()).toEqual({ host: "", retries: 0 }); }); it("falls back on a value that is not an object at all", () => { // `null` is the case that used to crash a reader doing `peek().host`, three // frames away from a screen with no way out of it. for (const raw of ["null", '"a string"', "42", "[]"]) { localStorage.clear(); seed(raw); const store = createPersistentSignal( KEY, { host: "", retries: 0 }, { schema: cfgSchema, }, ); expect(store.peek(), raw).toEqual({ host: "", retries: 0 }); } }); it("keeps a value with everything the type needs, and drops the keys it has no field for", () => { // An unknown key is not a reason to throw away a usable value; it is a reason // not to hand it on, so the reader only ever sees what Cfg says exists. seed('{"host":"a","retries":1,"playCount":9}'); const store = createPersistentSignal( KEY, { host: "", retries: 0 }, { schema: cfgSchema, }, ); expect(store.peek()).toEqual({ host: "a", retries: 1 }); }); it("leaves the junk in place for the next write to overwrite", () => { // Deleting on a failed load would look like the user's settings vanished, and // buys nothing: the value is unread, and the first write replaces it. seed(JSON.stringify({ host: 3 })); createPersistentSignal(KEY, { host: "", retries: 0 }, { schema: cfgSchema }); expect(localStorage.getItem(KEY)).toContain('"host":3'); }); it("trusts anything when no schema is given — the escape hatch stays open", () => { // Keys whose shape is genuinely open (a number, a record by server id) must not // be forced to invent a schema to keep working. seed('{"host":123}'); const store = createPersistentSignal(KEY, { host: "", retries: 0 }); expect(store.peek()).toEqual({ host: 123 } as unknown as Cfg); }); });