import { beforeEach, expect, it, vi } from "vitest"; const { createSummonCapability } = vi.hoisted(() => ({ createSummonCapability: vi.fn(() => "summon-token"), })); vi.mock("./messages", () => ({ createSummonCapability })); type CommandListener = (command: string) => void | Promise; let listener: CommandListener; let sendMessage: ReturnType; beforeEach(async () => { vi.resetModules(); createSummonCapability.mockClear(); sendMessage = vi.fn( ( _tabId: number, message: { type?: string }, optionsOrCallback?: { frameId?: number } | ((response: unknown) => void), ) => { if (message.type === "PING" && typeof optionsOrCallback === "function") { optionsOrCallback({ ok: true }); } return Promise.resolve({ ok: true }); }, ); (globalThis as { chrome?: unknown }).chrome = { runtime: { lastError: undefined }, commands: { onCommand: { addListener: vi.fn((next: CommandListener) => { listener = next; }) } }, tabs: { query: vi.fn(async () => [{ id: 42, url: "https://shop.example/checkout" }]), sendMessage, }, action: { openPopup: vi.fn(async () => {}) }, }; const { setupCommands } = await import("./commands"); setupCommands(); }); it("PB-26-201: summon command binds and delivers its capability to the top frame only", async () => { await listener("summon-inline"); expect(createSummonCapability).toHaveBeenCalledWith(42, "https://shop.example", 0); expect(sendMessage).toHaveBeenCalledWith( 42, { type: "SUMMON_AUTOFILL", source: "shortcut", capability: "summon-token" }, { frameId: 0 }, ); });