pending-adoption-holder.test.ts

1.5 KB · raw

1import { beforeEach, describe, expect, it, vi } from "vitest";2import {3  listPendingAdoptionHolders,4  persistPendingAdoptionHolder,5  readPendingAdoptionHolder,6  removePendingAdoptionHolder,7} from "./pending-adoption-holder";89const data: Record<string, unknown> = {};10vi.stubGlobal("chrome", { storage: { local: {11  get: vi.fn(async (key: string) => ({ [key]: data[key] })),12  set: vi.fn(async (value: Record<string, unknown>) => Object.assign(data, value)),13  remove: vi.fn(async (key: string) => { delete data[key]; }),14} } });1516const request = { type: "ADOPT_REQUEST", device_id: "phone", device_name: "Phone", device_type: "android" } as const;1718describe("pending adoption holder", () => {19  beforeEach(() => { delete data.peach_pending_adoption_holder_v1; });2021  it("persists and lists a typed candidate across reads", async () => {22    await persistPendingAdoptionHolder(request);23    const holder = await readPendingAdoptionHolder();24    expect(holder?.request.device_id).toBe("phone");25    expect((await listPendingAdoptionHolders())).toHaveLength(1);26  });2728  it("removes an explicitly revoked candidate", async () => {29    await persistPendingAdoptionHolder(request);30    await removePendingAdoptionHolder();31    expect(await listPendingAdoptionHolders()).toEqual([]);32  });3334  it("does not silently delete malformed persisted data", async () => {35    data.peach_pending_adoption_holder_v1 = "not-json";36    expect(await readPendingAdoptionHolder()).toBeNull();37    expect(data.peach_pending_adoption_holder_v1).toBe("not-json");38  });39});