import { beforeEach, describe, expect, it } from "vitest"; import { clearPendingSyncAlerts, dismissPendingSyncAlerts, getPendingSyncAlerts, recordSyncAlerts, } from "./sync-alerts"; function makeStorageStub() { const local = new Map(); (globalThis as unknown as { chrome: unknown }).chrome = { storage: { local: { async get(key?: string | string[] | null) { if (key == null) { const all: Record = {}; for (const [k, v] of local) all[k] = v; return all; } const keys = Array.isArray(key) ? key : [key]; const out: Record = {}; for (const k of keys) if (local.has(k)) out[k] = local.get(k); return out; }, async set(values: Record) { for (const [k, v] of Object.entries(values)) local.set(k, v); }, async remove(key: string | string[]) { for (const k of Array.isArray(key) ? key : [key]) local.delete(k); }, }, }, }; } describe("sync alerts", () => { beforeEach(() => makeStorageStub()); it("records security alerts and caps oversized device names", async () => { await recordSyncAlerts([ { type: "DEVICE_REVOKED", entryId: "", entryTitle: "", deviceName: "R".repeat(100), timestamp: 1 }, ]); const [a] = await getPendingSyncAlerts(); expect(a.deviceName).toBe("R".repeat(64)); }); it("drops non-alert records", async () => { await recordSyncAlerts([ { type: "ROUTINE_SYNC", entryId: "", entryTitle: "", deviceName: "x", timestamp: 1 } as never, ]); expect(await getPendingSyncAlerts()).toEqual([]); }); it("clear empties the queue", async () => { await recordSyncAlerts([ { type: "DEVICE_KEY_ROTATED", entryId: "", entryTitle: "", deviceName: "k", timestamp: 1 }, ]); await clearPendingSyncAlerts(); expect(await getPendingSyncAlerts()).toEqual([]); }); it("dismisses one alert group without losing another", async () => { await recordSyncAlerts([ { type: "DEVICE_KEY_ROTATED", entryId: "", entryTitle: "", deviceName: "k", timestamp: 1 }, { type: "DEVICE_REVOKED", entryId: "", entryTitle: "", deviceName: "r", timestamp: 2 }, ]); await dismissPendingSyncAlerts("DEVICE_KEY_ROTATED"); expect(await getPendingSyncAlerts()).toEqual([ { type: "DEVICE_REVOKED", entryId: "", entryTitle: "", deviceName: "r", timestamp: 2 }, ]); }); });