123import 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 88 89 90 expect(view.container.textContent).toContain("Pair with your other device");91 expect(view.container.textContent).toContain("Scan this code on the device that holds your vault");92 93 expect(view.container.textContent).not.toContain("Enter invitation manually");94 });95});