pending-adoption-holder.test.ts

2.2 KB · raw

1import { beforeEach, describe, expect, it, vi } from "vitest";2import {3  listPendingAdoptionHolders,4  persistPendingAdoptionIdentity,5  persistPendingAdoptionHolder,6  readPendingAdoptionIdentity,7  readPendingAdoptionHolder,8  removePendingAdoptionHolder,9} from "./pending-adoption-holder";1011const data: Record<string, unknown> = {};12vi.stubGlobal("chrome", { storage: { local: {13  get: vi.fn(async (key: string) => ({ [key]: data[key] })),14  set: vi.fn(async (value: Record<string, unknown>) => Object.assign(data, value)),15  remove: vi.fn(async (key: string) => { delete data[key]; }),16} } });1718const request = { type: "ADOPT_REQUEST", device_id: "phone", device_name: "Phone", device_type: "android" } as const;1920describe("pending adoption holder", () => {21  beforeEach(() => {22    delete data.peach_pending_adoption_holder_v1;23    delete data.peach_pending_adoption_identity_v2;24  });2526  it("persists and lists a typed candidate across reads", async () => {27    await persistPendingAdoptionHolder(request);28    const holder = await readPendingAdoptionHolder();29    expect(holder?.request.device_id).toBe("phone");30    expect((await listPendingAdoptionHolders())).toHaveLength(1);31  });3233  it("removes an explicitly revoked candidate", async () => {34    await persistPendingAdoptionHolder(request);35    await removePendingAdoptionHolder();36    expect(await listPendingAdoptionHolders()).toEqual([]);37  });3839  it("lists and explicitly revokes a v2 pending identity", async () => {40    await persistPendingAdoptionIdentity({41      deviceId: "phone-v2",42      deviceName: "Phone v2",43      deviceType: "android",44      longTermPublicKeyHex: "ab".repeat(32),45      trustSource: "direct",46      pairedAt: 123,47    }, 123);4849    expect((await readPendingAdoptionIdentity())?.device.deviceId).toBe("phone-v2");50    expect(await listPendingAdoptionHolders()).toHaveLength(1);51    await removePendingAdoptionHolder();52    expect(await listPendingAdoptionHolders()).toEqual([]);53  });5455  it("does not silently delete malformed persisted data", async () => {56    data.peach_pending_adoption_holder_v1 = "not-json";57    expect(await readPendingAdoptionHolder()).toBeNull();58    expect(data.peach_pending_adoption_holder_v1).toBe("not-json");59  });60});