AdoptVaultView.test.tsx

6.1 KB · raw

1// @vitest-environment jsdom23import type { JSX } from "solid-js";4import { afterEach, describe, expect, it, vi } from "vitest";5import { Onboarding } from "./Onboarding";6import { AdoptVaultView } from "./AdoptVaultView";7import { flushView, inputText, renderView } from "./view-test-utils";89vi.mock("@zxcvbn-ts/core", () => ({10  zxcvbn: (value: string) => ({ score: value.length >= 16 ? 4 : 1 }),11  zxcvbnOptions: { setOptions: vi.fn() },12}));13vi.mock("@zxcvbn-ts/language-common", () => ({14  adjacencyGraphs: {},15  dictionary: {},16}));17vi.mock("@zxcvbn-ts/language-en", () => ({18  translations: {},19  dictionary: {},20}));2122function button(container: HTMLElement, text: string): HTMLButtonElement {23  const found = Array.from(container.querySelectorAll("button")).find((b) =>24    (b.textContent ?? "").includes(text),25  );26  if (!found) throw new Error(`button not found: ${text}`);27  return found as HTMLButtonElement;28}2930async function enterStrongPassword(container: HTMLElement) {31  inputText(container.querySelector("#adopt-password") as HTMLInputElement, "9xQ!vZ2pLm74Tr#Ke8Wd5Bn");32  inputText(container.querySelector("#adopt-confirm-password") as HTMLInputElement, "9xQ!vZ2pLm74Tr#Ke8Wd5Bn");33  await flushView();34}3536describe("vault adoption onboarding", () => {37  const cleanups: Array<() => void> = [];3839  afterEach(() => {40    vi.useRealTimers();41    while (cleanups.length) cleanups.pop()!();42  });4344  function mounted(view: () => JSX.Element) {45    const rendered = renderView(view);46    cleanups.push(rendered.dispose);47    return rendered;48  }4950  it("T-A21: Onboarding separates pairing from backup restore", () => {51    vi.useFakeTimers();52    const onComplete = vi.fn();53    const onRestore = vi.fn();54    const onAdopt = vi.fn();55    const view = mounted(() => <Onboarding onComplete={onComplete} onRestore={onRestore} onAdopt={onAdopt} />);5657    button(view.container, "next").click();58    vi.advanceTimersByTime(220);59    button(view.container, "next").click();60    vi.advanceTimersByTime(220);6162    expect(view.container.textContent).toContain("Create vault");63    expect(view.container.textContent).toContain("Already using Peach?");64    expect(view.container.textContent).toContain("Pair with another device");65    expect(view.container.textContent).toContain("Scan a QR and confirm one 8-digit code.");66    expect(view.container.textContent).toContain("Restore a backup");67    button(view.container, "Pair with another device").click();68    expect(onAdopt).toHaveBeenCalledTimes(1);69  });7071  it("T-A22: password screen uses the same minimum-length gate as create-vault", () => {72    const view = mounted(() => <AdoptVaultView onCancel={() => {}} onDone={() => {}} />);73    const continueButton = button(view.container, "Continue");74    expect(continueButton.disabled).toBe(true);75    inputText(view.container.querySelector("#adopt-password") as HTMLInputElement, "short");76    inputText(view.container.querySelector("#adopt-confirm-password") as HTMLInputElement, "short");77    expect(view.container.textContent).toContain("need 7 more characters");78    expect(continueButton.disabled).toBe(true);79  });8081  it("T-A28: pair phase shows the pairing-request QR after a strong password", async () => {82    const view = mounted(() => <AdoptVaultView onCancel={() => {}} onDone={() => {}} />);83    await enterStrongPassword(view.container);84    button(view.container, "Continue").click();85    await flushView();8687    // The browser is the source of the QR now — the phone scans it.88    // We assert the heading + copy; the actual QR is rendered by a background89    // call that's out of scope for a jsdom test (chrome.runtime is undefined).90    expect(view.container.textContent).toContain("Pair with your other device");91    expect(view.container.textContent).toContain("Scan on the device that holds your vault");92    // The scan button from the old flow is gone — the phone is the scanner now.93    expect(view.container.textContent).not.toContain("Enter invitation manually");94  });9596  it("shows the receiver SAS without adding a second confirmation button", async () => {97    type RuntimeListener = (msg: { type?: string; payload?: unknown }) => void;98    const listeners: RuntimeListener[] = [];99    Object.defineProperty(globalThis, "chrome", {100      configurable: true,101      value: {102        runtime: {103          lastError: undefined,104          sendMessage(105            message: { type?: string },106            callback: (response: unknown) => void,107          ) {108            if (message.type === "START_ADOPTION_RECEIVER") {109              callback({ ok: true, data: { invitation: "peach-pair-v1:test" } });110            } else {111              callback({ ok: true });112            }113          },114          onMessage: {115            addListener(fn: RuntimeListener) { listeners.push(fn); },116            removeListener(fn: RuntimeListener) {117              const index = listeners.indexOf(fn);118              if (index >= 0) listeners.splice(index, 1);119            },120          },121        },122      },123    });124125    const view = mounted(() => <AdoptVaultView onCancel={() => {}} onDone={() => {}} />);126    await enterStrongPassword(view.container);127    button(view.container, "Continue").click();128    await flushView();129130    for (const listener of listeners) {131      listener({132        type: "ADOPTION_SOURCE_SAS",133        payload: { sas: "48219371", displayOnly: true },134      });135    }136    await flushView();137138    expect(view.container.textContent).toContain("48219371");139    expect(view.container.textContent).toContain("Pairing continues when you confirm there");140    expect(view.container.textContent).not.toContain("They match");141    expect(view.container.textContent).not.toContain("They don't match");142143    for (const listener of listeners) {144      listener({145        type: "ADOPTION_SOURCE_RESULT",146        payload: { status: "error", error: "ADOPT_PRO_REQUIRED" },147      });148    }149    await flushView();150151    expect(view.container.textContent).toContain("Pair freely");152    expect(view.container.textContent).toContain("at least one of these two devices");153    expect(view.container.textContent).toContain("$39 once");154    expect(view.container.querySelector('a[href*="buy.polar.sh"]')).toBeTruthy();155  });156});