import { beforeEach, describe, expect, it, vi } from "vitest"; import { listPendingAdoptionHolders, persistPendingAdoptionHolder, readPendingAdoptionHolder, removePendingAdoptionHolder, } from "./pending-adoption-holder"; const data: Record = {}; vi.stubGlobal("chrome", { storage: { local: { get: vi.fn(async (key: string) => ({ [key]: data[key] })), set: vi.fn(async (value: Record) => Object.assign(data, value)), remove: vi.fn(async (key: string) => { delete data[key]; }), } } }); const request = { type: "ADOPT_REQUEST", device_id: "phone", device_name: "Phone", device_type: "android" } as const; describe("pending adoption holder", () => { beforeEach(() => { delete data.peach_pending_adoption_holder_v1; }); it("persists and lists a typed candidate across reads", async () => { await persistPendingAdoptionHolder(request); const holder = await readPendingAdoptionHolder(); expect(holder?.request.device_id).toBe("phone"); expect((await listPendingAdoptionHolders())).toHaveLength(1); }); it("removes an explicitly revoked candidate", async () => { await persistPendingAdoptionHolder(request); await removePendingAdoptionHolder(); expect(await listPendingAdoptionHolders()).toEqual([]); }); it("does not silently delete malformed persisted data", async () => { data.peach_pending_adoption_holder_v1 = "not-json"; expect(await readPendingAdoptionHolder()).toBeNull(); expect(data.peach_pending_adoption_holder_v1).toBe("not-json"); }); });