123456789101112import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";131415const { mockSendMessage } = vi.hoisted(() => ({ mockSendMessage: vi.fn() }));16vi.mock("../../shared/bridge", () => ({ sendMessage: mockSendMessage }));1718const SOURCE_INVITATION =19 "peach-pair-v2:eyJyZWxheSI6IndzczovL3JlbGF5LnBlYWNocGFzc3dvcmRzLmNvbSIsInRva2VuIjoiQUFFQ0F3UUZCZ2NJQ1FvTERBME9EdyJ9";2021import { PairDeviceView } from "./PairDevice";22import { renderView } from "./view-test-utils";232425type RuntimeListener = (msg: { type?: string; payload?: unknown }) => void;26let listeners: RuntimeListener[] = [];2728function pushFromBackground(type: string, payload: unknown): void {29 for (const l of [...listeners]) l({ type, payload });30}3132beforeEach(() => {33 listeners = [];34 mockSendMessage.mockReset();35 mockSendMessage.mockImplementation((msg: { type?: string }) => {36 if (msg?.type === "START_ADOPTION_SOURCE") {37 return Promise.resolve({38 ok: true,39 data: { invitation: SOURCE_INVITATION },40 });41 }42 if (msg?.type === "GET_SYNC_SETTINGS" || msg?.type === "SET_SYNC_SETTINGS") {43 return Promise.resolve({44 ok: true,45 data: { mode: "public", relayUrl: "wss://relay.peachpasswords.com" },46 });47 }48 return Promise.resolve({ ok: true });49 });50 (globalThis as unknown as { chrome: unknown }).chrome = {51 runtime: {52 onMessage: {53 addListener: (fn: RuntimeListener) => { listeners.push(fn); },54 removeListener: (fn: RuntimeListener) => { listeners = listeners.filter((l) => l !== fn); },55 },56 },57 };58});5960describe("PairDevice — relay adoption source", () => {61 const cleanups: Array<() => void> = [];62 afterEach(() => {63 while (cleanups.length) cleanups.pop()!();64 });6566 function mount(onLearnPro: () => void = () => {}) {67 const rendered = renderView(68 <PairDeviceView69 isPro={true}70 onBack={() => {}}71 onLearnPro={onLearnPro}72 />,73 );74 cleanups.push(rendered.dispose);75 return rendered;76 }7778 it("opens the relay room before rendering one source QR", async () => {79 const { container } = mount();80 await vi.waitFor(() => {81 expect(container.textContent).toContain("This extension has the vault");82 });8384 expect(mockSendMessage).toHaveBeenCalledWith({85 type: "START_ADOPTION_SOURCE",86 });87 expect(container.querySelector("svg.peach-pair-qr")).toBeTruthy();88 expect(container.querySelector("#pairing-invitation-manual")).toBeNull();89 expect(container.textContent).not.toContain("Show a code instead");90 expect(container.textContent).not.toContain("peach-pair-v2:");91 });9293 94 it("names the relay in force without making it a decision", async () => {95 const { container } = mount();96 await vi.waitFor(() => {97 expect(container.textContent).toContain("This extension has the vault");98 });99100 const summary = container.querySelector(".peach-sync-mode-summary");101 expect(summary?.textContent).toContain("Using Public relay");102 103 expect(container.querySelector(".peach-sync-mode")).toBeNull();104105 container.querySelector<HTMLButtonElement>(".peach-sync-mode-change")!.click();106 await vi.waitFor(() => {107 expect(container.querySelectorAll(".peach-sync-mode")).toHaveLength(3);108 });109 110 expect(container.querySelector("svg.peach-pair-qr")).toBeNull();111 });112113 it("rebuilds the invitation when the relay changes, since the code carries it", async () => {114 const { container } = mount();115 await vi.waitFor(() => {116 expect(container.textContent).toContain("This extension has the vault");117 });118 const opened = () =>119 mockSendMessage.mock.calls.filter(120 ([m]) => (m as { type?: string })?.type === "START_ADOPTION_SOURCE",121 ).length;122 expect(opened()).toBe(1);123124 container.querySelector<HTMLButtonElement>(".peach-sync-mode-change")!.click();125 await vi.waitFor(() => {126 expect(container.querySelectorAll(".peach-sync-mode")).toHaveLength(3);127 });128 const noSync = [...container.querySelectorAll<HTMLButtonElement>("button.peach-sync-mode")].find(129 (b) => b.textContent?.includes("No sync"),130 )!;131 noSync.click();132133 await vi.waitFor(() => {134 expect(mockSendMessage).toHaveBeenCalledWith({ type: "CANCEL_ADOPTION_SOURCE" });135 expect(opened()).toBe(2);136 });137 });138139 it("shows the SAS and waits for the popup's single confirmation", async () => {140 const { container } = mount();141 await vi.waitFor(() => {142 expect(container.textContent).toContain("This extension has the vault");143 });144145 pushFromBackground("ADOPTION_SOURCE_SAS", {146 id: "test-sas-id",147 sas: "48219371",148 receiverDeviceName: "June's phone",149 });150 expect(container.textContent).toContain("June's phone");151 expect(container.textContent).toContain("48219371");152 expect(container.textContent).toContain("Compare the 8-digit safety code");153 expect(container.textContent).toContain("complete copy of this vault");154 expect(container.textContent).not.toContain("Session ID");155 expect(container.querySelector("#adoption-consent-password")).toBeNull();156 157 expect(mockSendMessage).not.toHaveBeenCalledWith(158 expect.objectContaining({ type: "SOURCE_SAS_DECISION" }),159 );160 const matchButton = [...container.querySelectorAll<HTMLButtonElement>("button")]161 .find((b) => b.textContent?.trim() === "They match");162 expect(matchButton).toBeTruthy();163 const rejectButton = [...container.querySelectorAll<HTMLButtonElement>("button")]164 .find((b) => b.textContent?.trim() === "They don't match");165 expect(rejectButton).toBeTruthy();166167 matchButton!.click();168 await vi.waitFor(() => {169 expect(mockSendMessage).toHaveBeenCalledWith({170 type: "SOURCE_SAS_DECISION",171 payload: { id: "test-sas-id", ok: true },172 });173 });174175 176 177 pushFromBackground("ADOPTION_SOURCE_RESULT", { status: "complete" });178 expect(container.textContent).toContain("Device added");179 expect(container.querySelectorAll(".peach-pair-bird-pose-happy")).toHaveLength(2);180 });181182 it("shows the Peach Pro upgrade path when neither pairing peer is Pro", async () => {183 const onLearnPro = vi.fn();184 const { container } = mount(onLearnPro);185 await vi.waitFor(() => {186 expect(container.textContent).toContain("This extension has the vault");187 });188189 pushFromBackground("ADOPTION_SOURCE_RESULT", {190 status: "error",191 error: "ADOPT_PRO_REQUIRED",192 });193194 expect(container.textContent).toContain("Pair freely");195 expect(container.textContent).toContain("at least one of these two devices");196 expect(container.textContent).toContain("$39 once");197 const openPro = [...container.querySelectorAll<HTMLButtonElement>("button")]198 .find((button) => button.textContent?.trim() === "Open Peach Pro");199 expect(openPro).toBeTruthy();200 openPro!.click();201 expect(onLearnPro).toHaveBeenCalledOnce();202 });203});