pending-vouched-devices.test.ts

1.9 KB · raw

1import { beforeEach, describe, expect, it } from "vitest";2import {3  addPendingVouchedDevice,4  listPendingVouchedDevices,5  removePendingVouchedDevice,6} from "./pending-vouched-devices";78function makeStorageStub() {9  const local = new Map<string, unknown>();10  (globalThis as unknown as { chrome: unknown }).chrome = {11    storage: {12      local: {13        async get(key?: string | string[] | null) {14          if (key == null) {15            const all: Record<string, unknown> = {};16            for (const [k, v] of local) all[k] = v;17            return all;18          }19          const keys = Array.isArray(key) ? key : [key];20          const out: Record<string, unknown> = {};21          for (const k of keys) if (local.has(k)) out[k] = local.get(k);22          return out;23        },24        async set(values: Record<string, unknown>) {25          for (const [k, v] of Object.entries(values)) local.set(k, v);26        },27        async remove(key: string | string[]) {28          for (const k of Array.isArray(key) ? key : [key]) local.delete(k);29        },30      },31    },32  };33}3435const SAMPLE = {36  deviceId: "VVV-1",37  deviceName: "Vouched Pixel",38  deviceType: "android",39  longTermPublicKeyHex: "a".repeat(64),40  signingPublicKeyHex: "b".repeat(64),41  pairedAt: 1,42  receivedAt: 2,43  vouchedByDeviceId: "issuer-1",44};4546describe("pending vouched devices", () => {47  beforeEach(() => makeStorageStub());4849  it("add + list + remove", async () => {50    await addPendingVouchedDevice(SAMPLE);51    expect(await listPendingVouchedDevices()).toEqual([SAMPLE]);52    await removePendingVouchedDevice(SAMPLE.deviceId);53    expect(await listPendingVouchedDevices()).toEqual([]);54  });5556  it("caps oversized peer device names", async () => {57    await addPendingVouchedDevice({ ...SAMPLE, deviceName: "V".repeat(100) });58    const [d] = await listPendingVouchedDevices();59    expect(d.deviceName).toBe("V".repeat(64));60  });61});