AdoptVaultView.test.tsx

7.9 KB · raw

1// @vitest-environment jsdom23import type { JSX } from "solid-js";4import { afterEach, describe, expect, it, vi } from "vitest";5import { Onboarding } from "./Onboarding";6import {7  AdoptVaultView,8  type AdoptVaultViewProps,9} from "./AdoptVaultView";10import { flushView, inputText, renderView } from "./view-test-utils";11import { createPairingInvitation } from "../../shared/pairing-invitation";1213const INVITATION = createPairingInvitation(14  "wss://relay.peachpasswords.com",15  "AAECAwQFBgcICQoLDA0ODw",16);1718vi.mock("@zxcvbn-ts/core", () => ({19  zxcvbn: (value: string) => ({ score: value.length >= 16 ? 4 : 1 }),20  zxcvbnOptions: { setOptions: vi.fn() },21}));22vi.mock("@zxcvbn-ts/language-common", () => ({23  adjacencyGraphs: {},24  dictionary: {},25}));26vi.mock("@zxcvbn-ts/language-en", () => ({27  translations: {},28  dictionary: {},29}));3031function button(container: HTMLElement, text: string): HTMLButtonElement {32  const found = Array.from(container.querySelectorAll("button")).find((b) =>33    (b.textContent ?? "").includes(text),34  );35  if (!found) throw new Error(`button not found: ${text}`);36  return found as HTMLButtonElement;37}3839async function enterInvitation(container: HTMLElement) {40  button(container, "Enter invitation manually").click();41  await flushView();42  inputText(43    container.querySelector("#adopt-relay-invitation") as HTMLTextAreaElement,44    INVITATION,45  );46  await flushView();47}4849async function enterStrongPassword(container: HTMLElement) {50  inputText(container.querySelector("#adopt-password") as HTMLInputElement, "9xQ!vZ2pLm74Tr#Ke8Wd5Bn");51  inputText(container.querySelector("#adopt-confirm-password") as HTMLInputElement, "9xQ!vZ2pLm74Tr#Ke8Wd5Bn");52  await flushView();53}5455describe("vault adoption onboarding", () => {56  const cleanups: Array<() => void> = [];5758  afterEach(() => {59    vi.useRealTimers();60    while (cleanups.length) cleanups.pop()!();61  });6263  function mounted(view: () => JSX.Element) {64    const rendered = renderView(view);65    cleanups.push(rendered.dispose);66    return rendered;67  }6869  it("T-A21: Onboarding offers Create vault, Restore from backup, and I already have Peach", () => {70    vi.useFakeTimers();71    const onComplete = vi.fn();72    const onRestore = vi.fn();73    const onAdopt = vi.fn();74    const view = mounted(() => <Onboarding onComplete={onComplete} onRestore={onRestore} onAdopt={onAdopt} />);7576    button(view.container, "next").click();77    vi.advanceTimersByTime(220);78    button(view.container, "next").click();79    vi.advanceTimersByTime(220);8081    expect(view.container.textContent).toContain("Create vault");82    expect(view.container.textContent).toContain("Restore from backup");83    expect(view.container.textContent).toContain("I already have Peach");84    button(view.container, "I already have Peach").click();85    expect(onAdopt).toHaveBeenCalledTimes(1);86  });8788  it("T-A22: password screen uses the same minimum-length gate as create-vault", () => {89    const view = mounted(() => <AdoptVaultView onCancel={() => {}} onDone={() => {}} startAdoption={vi.fn()} />);90    const continueButton = button(view.container, "Continue");91    expect(continueButton.disabled).toBe(true);92    inputText(view.container.querySelector("#adopt-password") as HTMLInputElement, "short");93    inputText(view.container.querySelector("#adopt-confirm-password") as HTMLInputElement, "short");94    expect(view.container.textContent).toContain("need 7 more characters");95    expect(continueButton.disabled).toBe(true);96  });9798  it("T-A26: relay adoption cannot start until a valid invitation is entered", async () => {99    const startAdoption = vi.fn(100      async (_args: Parameters<AdoptVaultViewProps["startAdoption"]>[0]) => {},101    );102    const view = mounted(() => <AdoptVaultView onCancel={() => {}} onDone={() => {}} startAdoption={startAdoption} />);103    await enterStrongPassword(view.container);104    button(view.container, "Continue").click();105    await flushView();106107    // No relay selector here: the code carries the relay, so this screen states108    // it rather than offering a choice it would overwrite109    // (docs/sync-relay-surfacing.md).110    expect(view.container.textContent).toContain("relay carried in the code you scan");111    expect(view.container.querySelector(".peach-sync-mode")).toBeNull();112    expect(view.container.querySelector(".peach-sync-mode-summary")).toBeNull();113    button(view.container, "Enter invitation manually").click();114    await flushView();115    expect(button(view.container, "Connect securely").disabled).toBe(true);116    inputText(117      view.container.querySelector("#adopt-relay-invitation") as HTMLTextAreaElement,118      "peach-pair-v1:not-valid",119    );120    await flushView();121    expect(button(view.container, "Connect securely").disabled).toBe(true);122123    inputText(124      view.container.querySelector("#adopt-relay-invitation") as HTMLTextAreaElement,125      INVITATION,126    );127    await flushView();128    expect(button(view.container, "Connect securely").disabled).toBe(false);129    expect(startAdoption).not.toHaveBeenCalled();130  });131132  it("T-A27: the relay invitation is handed to the adoption transport", async () => {133    const startAdoption = vi.fn(134      async (_args: Parameters<AdoptVaultViewProps["startAdoption"]>[0]) => {},135    );136    const view = mounted(() => <AdoptVaultView onCancel={() => {}} onDone={() => {}} startAdoption={startAdoption} />);137    await enterStrongPassword(view.container);138    button(view.container, "Continue").click();139    await enterInvitation(view.container);140    button(view.container, "Connect securely").click();141    await flushView();142143    expect(startAdoption).toHaveBeenCalledTimes(1);144    expect(startAdoption.mock.calls[0][0]).toMatchObject({145      connection: { invitation: INVITATION },146    });147  });148149  it("T-A23: adoption flow reaches the SAS screen", async () => {150    const startAdoption = vi.fn(async ({ confirmSAS }: { confirmSAS(sas: string): Promise<boolean> }) => {151      await confirmSAS("895245");152    });153    const view = mounted(() => <AdoptVaultView onCancel={() => {}} onDone={() => {}} startAdoption={startAdoption} />);154    await enterStrongPassword(view.container);155    button(view.container, "Continue").click();156    await enterInvitation(view.container);157    button(view.container, "Connect securely").click();158    await flushView();159160    expect(view.container.textContent).toContain("Confirm this code matches");161    expect(view.container.textContent).toContain("8 9 5 2 4 5");162  });163164  it("T-A24: success leaves a vault and opens Peach", async () => {165    const onDone = vi.fn();166    const view = mounted(167      () => (168        <AdoptVaultView169          onCancel={() => {}}170          onDone={onDone}171          startAdoption={vi.fn(async () => {})}172        />173      ),174    );175    await enterStrongPassword(view.container);176    button(view.container, "Continue").click();177    await enterInvitation(view.container);178    button(view.container, "Connect securely").click();179    await flushView();180181    expect(view.container.textContent).toContain("Joined your vault.");182    button(view.container, "Open Peach").click();183    expect(onDone).toHaveBeenCalledTimes(1);184  });185186  it("T-A25: SAS cancel keeps receiver vault-less and returns to the password step", async () => {187    const createVault = vi.fn();188    const view = mounted(189      () => (190        <AdoptVaultView191          onCancel={() => {}}192          onDone={() => {}}193          startAdoption={vi.fn(async ({ confirmSAS }) => {194            const ok = await confirmSAS("895245");195            if (ok) await createVault();196            else throw new Error("sas_rejected");197          })}198        />199      ),200    );201    await enterStrongPassword(view.container);202    button(view.container, "Continue").click();203    await enterInvitation(view.container);204    button(view.container, "Connect securely").click();205    await flushView();206    button(view.container, "Cancel").click();207    await flushView();208209    expect(createVault).not.toHaveBeenCalled();210    expect(view.container.textContent).toContain("Set a password for this device.");211  });212});