commands.test.ts

1.6 KB · raw

1import { beforeEach, expect, it, vi } from "vitest";23const { createSummonCapability } = vi.hoisted(() => ({4  createSummonCapability: vi.fn(() => "summon-token"),5}));67vi.mock("./messages", () => ({ createSummonCapability }));89type CommandListener = (command: string) => void | Promise<void>;1011let listener: CommandListener;12let sendMessage: ReturnType<typeof vi.fn>;1314beforeEach(async () => {15  vi.resetModules();16  createSummonCapability.mockClear();17  sendMessage = vi.fn(18    (19      _tabId: number,20      message: { type?: string },21      optionsOrCallback?: { frameId?: number } | ((response: unknown) => void),22    ) => {23      if (message.type === "PING" && typeof optionsOrCallback === "function") {24        optionsOrCallback({ ok: true });25      }26      return Promise.resolve({ ok: true });27    },28  );29  (globalThis as { chrome?: unknown }).chrome = {30    runtime: { lastError: undefined },31    commands: { onCommand: { addListener: vi.fn((next: CommandListener) => { listener = next; }) } },32    tabs: {33      query: vi.fn(async () => [{ id: 42, url: "https://shop.example/checkout" }]),34      sendMessage,35    },36    action: { openPopup: vi.fn(async () => {}) },37  };38  const { setupCommands } = await import("./commands");39  setupCommands();40});4142it("PB-26-201: summon command binds and delivers its capability to the top frame only", async () => {43  await listener("summon-inline");4445  expect(createSummonCapability).toHaveBeenCalledWith(42, "https://shop.example", 0);46  expect(sendMessage).toHaveBeenCalledWith(47    42,48    { type: "SUMMON_AUTOFILL", source: "shortcut", capability: "summon-token" },49    { frameId: 0 },50  );51});