messages.create-vault-from-root-key.test.ts

3.0 KB · raw

1import { beforeEach, describe, expect, it, vi } from "vitest";2import { computeVaultId } from "../shared/sync-identity";34async function dispatch(message: { type: string; payload?: unknown }) {5  vi.resetModules();6  const messages = await import("./messages");7  return messages.handleMessage(8    message as never,9    { url: chrome.runtime.getURL("popup.html") } as chrome.runtime.MessageSender,10  );11}1213describe("CREATE_VAULT_FROM_ROOT_KEY message", () => {14  beforeEach(() => {15    vi.stubGlobal("__PEACH_E2E_PASSKEYS__", false);16    vi.stubGlobal("chrome", {17      runtime: {18        id: "ext",19        getURL: (path = "") => `chrome-extension://ext/${path}`,20      },21      tabs: {22        query: vi.fn((_query: unknown, callback?: (tabs: Array<{ id?: number }>) => void) => {23          if (typeof callback === "function") callback([]);24          return Promise.resolve([]);25        }),26        sendMessage: vi.fn(async () => ({ ok: true })),27      },28      storage: {29        local: {30          get: vi.fn(async () => ({})),31          set: vi.fn(),32          remove: vi.fn(),33        },34        session: { get: vi.fn(async () => ({})), set: vi.fn(), remove: vi.fn() },35      },36      alarms: { clear: vi.fn(), create: vi.fn(), onAlarm: { addListener: vi.fn() } },37      idle: { setDetectionInterval: vi.fn(), onStateChanged: { addListener: vi.fn() } },38    });39  });4041  it("T-A06: succeeds with a valid receiver password and root key", async () => {42    const res = await dispatch({43      type: "CREATE_VAULT_FROM_ROOT_KEY",44      payload: { password: "receiver-local-passphrase-456", rootKeyHex: "44".repeat(32) },45    }) as { ok: boolean; vaultId?: string };4647    expect(res.ok).toBe(true);48    expect(res.vaultId).toMatch(/^[0-9a-f]{64}$/);49  });5051  it("T-A07: fails gracefully for invalid rootKeyHex", async () => {52    const res = await dispatch({53      type: "CREATE_VAULT_FROM_ROOT_KEY",54      payload: { password: "receiver-local-passphrase-456", rootKeyHex: "nope" },55    }) as { ok: boolean; error?: string };5657    expect(res.ok).toBe(false);58    expect(res.error).toContain("ADOPT_INVALID_ROOT_KEY");59  });6061  it("T-A08: is vault-less allowed", async () => {62    const hasBefore = await dispatch({ type: "HAS_VAULT" }) as { ok: boolean; data?: boolean };63    expect(hasBefore.data).toBe(false);6465    const create = await dispatch({66      type: "CREATE_VAULT_FROM_ROOT_KEY",67      payload: { password: "receiver-local-passphrase-456", rootKeyHex: "55".repeat(32) },68    }) as { ok: boolean };6970    expect(create.ok).toBe(true);71  });7273  it("T-A09: returns the vault id computed from the adopted root key", async () => {74    const rootKeyHex = "66".repeat(32);75    const expected = await computeVaultId(new Uint8Array(Buffer.from(rootKeyHex, "hex")));76    const res = await dispatch({77      type: "CREATE_VAULT_FROM_ROOT_KEY",78      payload: { password: "receiver-local-passphrase-456", rootKeyHex },79    }) as { ok: boolean; vaultId?: string };8081    expect(res).toEqual({ ok: true, vaultId: expected });82  });83});