import { beforeEach, describe, expect, it } from "vitest"; import { addPendingVouchedDevice, listPendingVouchedDevices, removePendingVouchedDevice, } from "./pending-vouched-devices"; 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); }, }, }, }; } const SAMPLE = { deviceId: "VVV-1", deviceName: "Vouched Pixel", deviceType: "android", longTermPublicKeyHex: "a".repeat(64), signingPublicKeyHex: "b".repeat(64), pairedAt: 1, receivedAt: 2, vouchedByDeviceId: "issuer-1", }; describe("pending vouched devices", () => { beforeEach(() => makeStorageStub()); it("add + list + remove", async () => { await addPendingVouchedDevice(SAMPLE); expect(await listPendingVouchedDevices()).toEqual([SAMPLE]); await removePendingVouchedDevice(SAMPLE.deviceId); expect(await listPendingVouchedDevices()).toEqual([]); }); it("caps oversized peer device names", async () => { await addPendingVouchedDevice({ ...SAMPLE, deviceName: "V".repeat(100) }); const [d] = await listPendingVouchedDevices(); expect(d.deviceName).toBe("V".repeat(64)); }); });