/** * @file Tests for the browser-preferences backup: only allowlisted keys leave * (never the access token), an import writes only allowlisted keys, and a file * that is not one of ours is refused. * @author Nguyễn Ngọc Trí Vĩ */ import { beforeEach, describe, expect, it } from "vitest"; import { exportPrefs, importPrefs, PREFS_FORMAT } from "../prefsBackup"; beforeEach(() => localStorage.clear()); describe("prefsBackup", () => { it("exports only preference keys, never the access token", () => { localStorage.setItem("theme", "dark"); localStorage.setItem("agenthub.pet.size", "2"); localStorage.setItem("agent-monitor-notifications", "{}"); localStorage.setItem("dashboard_token", "secret"); localStorage.setItem("sidebar-connection-stats", "{}"); const out = exportPrefs(); expect(out.format).toBe(PREFS_FORMAT); expect(Object.keys(out.prefs).sort()).toEqual([ "agent-monitor-notifications", "agenthub.pet.size", "theme", ]); }); it("imports allowlisted keys only and refuses a foreign file", () => { const n = importPrefs( JSON.stringify({ format: PREFS_FORMAT, version: 1, prefs: { theme: "light", dashboard_token: "evil", "agenthub.x": 5 }, }) ); expect(n).toBe(1); expect(localStorage.getItem("theme")).toBe("light"); expect(localStorage.getItem("dashboard_token")).toBeNull(); expect(() => importPrefs(JSON.stringify({ format: "other", prefs: {} }))).toThrow(); expect(() => importPrefs("not json")).toThrow(); }); });