// @vitest-environment jsdom // // The notification and hints stores read their per-account localStorage when they are imported. In a new // tab (bookmark, typed URL: no pin in sessionStorage) that is before BootGate pins the account, so they // read as nobody's; they must read again on the pin. The notification store used to keep its empty, // nobody's history, and the tab's first notice replaced the account's stored history with it. import { beforeEach, describe, expect, it, vi } from "vitest"; const entry = (id: number, message: string) => ({ id, level: "info", message, ts: 1, read: true, dismissed: true, transient: false, repeat: 1, requiresInteraction: false, }); const storedMessages = (account: number) => ( JSON.parse(localStorage.getItem(`siftlode.notifications.${account}`) ?? "[]") as { message: string; }[] ).map((n) => n.message); beforeEach(() => { localStorage.clear(); sessionStorage.clear(); vi.resetModules(); // both stores load at import: each test needs a fresh one }); describe("stores read at import, in a tab not yet pinned", () => { it("keep the account's notification history and settings once the tab is pinned", async () => { localStorage.setItem("siftlode.notifications.5", JSON.stringify([entry(7, "older")])); localStorage.setItem("siftlode.notifSettings.5", JSON.stringify({ durationMs: 0 })); const notifications = await import("./notifications"); const { setActiveAccount } = await import("./api/core"); notifications.notify({ message: "before the pin" }); // e.g. a failed boot probe setActiveAccount(5); notifications.notify({ message: "after the pin" }); expect(storedMessages(5)).toEqual(["older", "before the pin", "after the pin"]); expect(notifications.getNotifSettings().durationMs).toBe(0); }); it("read the account's hints choice once the tab is pinned", async () => { localStorage.setItem("siftlode.hints.5", "0"); const hints = await import("./hints"); const { setActiveAccount } = await import("./api/core"); expect(hints.hintsEnabled()).toBe(true); // nobody's: the default setActiveAccount(5); expect(hints.hintsEnabled()).toBe(false); }); }); describe("a pinned tab pinned to another account", () => { it("leaves the previous account's notices with that account", async () => { // BootGate moves a tab whose account was signed out elsewhere onto the one the server answers as. sessionStorage.setItem("siftlode.activeAccount", "4"); localStorage.setItem("siftlode.notifications.4", JSON.stringify([entry(7, "four's")])); localStorage.setItem("siftlode.notifications.5", JSON.stringify([entry(8, "five's")])); const notifications = await import("./notifications"); const { setActiveAccount } = await import("./api/core"); notifications.notify({ message: "as four" }); setActiveAccount(5); notifications.notify({ message: "as five" }); expect(storedMessages(5)).toEqual(["five's", "as five"]); expect(storedMessages(4)).toEqual(["four's", "as four"]); }); });