// @vitest-environment jsdom // DRAFT DESTINATION: src/popup/components/AdoptionConsentModal.test.tsx // T-A16..T-A20: source consent UI cannot grant without re-auth. import { describe, expect, it, vi } from "vitest"; import { AdoptionConsentModal } from "./AdoptionConsentModal"; import { inputText, renderView } from "../views/view-test-utils"; function button(container: HTMLElement, text: string): HTMLButtonElement { const found = Array.from(container.querySelectorAll("button")).find((b) => (b.textContent ?? "").includes(text), ); if (!found) throw new Error(`button not found: ${text}`); return found as HTMLButtonElement; } function mount(overrides: Partial[0]> = {}) { const props = { receiverDeviceName: "June's iPhone", sas: "895245", verifyMasterPassword: vi.fn(async () => true), onCancel: vi.fn(), onApprove: vi.fn(async () => {}), ...overrides, }; const rendered = renderView(); return { ...rendered, props }; } describe("AdoptionConsentModal", () => { it("T-A16: renders receiver device name and consequence copy", () => { const { container } = mount(); expect(container.textContent).toContain('"June\'s iPhone" wants to join this vault.'); expect(container.textContent).toContain("copies your entire vault including all passwords and Pro"); expect(container.textContent).toContain("8 9 5 2 4 5"); }); it("T-A17: Share vault button is disabled until a password is entered", () => { const { container } = mount(); const share = button(container, "Share vault")!; expect(share.disabled).toBe(true); inputText(container.querySelector("#adoption-consent-password") as HTMLInputElement, "pw"); expect(share.disabled).toBe(false); }); it("T-A18: wrong password shows inline error and does not approve", async () => { const { container, props } = mount({ verifyMasterPassword: vi.fn(async () => false) }); inputText(container.querySelector("#adoption-consent-password") as HTMLInputElement, "wrong"); button(container, "Share vault").click(); await Promise.resolve(); expect(container.textContent).toContain("Incorrect password"); expect(props.onApprove).not.toHaveBeenCalled(); }); it("T-A19: correct password calls onApprove", async () => { const { container, props } = mount({ verifyMasterPassword: vi.fn(async () => true) }); inputText(container.querySelector("#adoption-consent-password") as HTMLInputElement, "correct"); button(container, "Share vault").click(); await Promise.resolve(); expect(props.verifyMasterPassword).toHaveBeenCalledWith("correct"); expect(props.onApprove).toHaveBeenCalledTimes(1); }); it("T-A20: cancel closes without approval", () => { const { container, props } = mount(); button(container, "Cancel").click(); expect(props.onCancel).toHaveBeenCalledTimes(1); expect(props.onApprove).not.toHaveBeenCalled(); }); });