import { test } from "node:test"; import assert from "node:assert/strict"; import { emptySnapshot, emptyStoredState, loadStoredState, parseSnapshot, saveStoredState, snapshotsEqual, } from "./exportFilterStorage"; // The "Search in" row's two new keys (release 16): `notr` (Transcripts // unticked) and `lc` (Live chat ticked), stored beside `nop` only when off the // default, in the working snapshot and in every profile. test("notr and lc survive a JSON round trip through the parser", () => { const snap = { ...emptySnapshot(), notr: true, nop: true, lc: true }; const back = parseSnapshot(JSON.parse(JSON.stringify(snap))); assert.ok(back); assert.equal(back.notr, true); assert.equal(back.nop, true); assert.equal(back.lc, true); assert.ok(snapshotsEqual(snap, back)); }); test("absent reads as the default, and the default is not written", () => { const back = parseSnapshot({ channels: { included: [], excluded: [] } }); assert.ok(back); assert.equal("notr" in back, false); assert.equal("lc" in back, false); // A profile saved before the row existed equals one that spells the // defaults out. assert.ok(snapshotsEqual(back, { ...emptySnapshot(), notr: false, lc: false })); }); test("a value that is not a boolean is dropped, not coerced", () => { const back = parseSnapshot({ channels: { included: [], excluded: [] }, notr: "yes", lc: 1, }); assert.ok(back); assert.equal(back.notr, undefined); assert.equal(back.lc, undefined); }); test("snapshotsEqual tells each of the row's keys apart", () => { const base = emptySnapshot(); assert.ok(!snapshotsEqual(base, { ...base, notr: true })); assert.ok(!snapshotsEqual(base, { ...base, lc: true })); assert.ok(!snapshotsEqual({ ...base, notr: true }, { ...base, lc: true })); }); test("the working snapshot and a profile keep the row through storage", () => { const store = new Map(); const g = globalThis as { window?: unknown }; const before = g.window; g.window = { localStorage: { getItem: (k: string) => store.get(k) ?? null, setItem: (k: string, v: string) => void store.set(k, v), removeItem: (k: string) => void store.delete(k), }, }; try { const state = emptyStoredState(); state.working = { ...emptySnapshot(), lc: true }; state.profiles = { chatOnly: { ...emptySnapshot(), notr: true, nop: true, lc: true } }; state.activeProfileName = "chatOnly"; saveStoredState(state); const loaded = loadStoredState(); assert.ok(loaded); assert.equal(loaded.working.lc, true); assert.equal(loaded.working.notr, undefined); assert.deepEqual( [loaded.profiles.chatOnly.notr, loaded.profiles.chatOnly.nop, loaded.profiles.chatOnly.lc], [true, true, true], ); assert.equal(loaded.activeProfileName, "chatOnly"); } finally { g.window = before; } });