import type { Locator, Page } from "@playwright/test"; import type { CardAction, DecorationSet } from "../src/bindings"; import { expect, ipcCalls, stubDesktop, test } from "./fixtures"; /** * API 1.21 hover-card actions: a plugin's `decorations.set-actionable` item draws a right-aligned * row of real buttons above its Markdown card. Clicking one runs the plugin's declared command the * way Find Action does (`plugin_run_command` over IPC) and closes the card; an item with no actions * renders the card exactly as before. * * The decoration pipeline is stubbed over `stubDesktop` and published through `setPluginDecorations` * (the entry point the `plugin_decoration_sets` load path feeds). The declared command is registered * through the workbench's real `commands` UI-contribution projection, so `plugin..continue` * resolves exactly as it does in the app. */ const PLUGIN = "card-actions"; const PATH = "/dev/acme-api/CardActions.ts"; const CONTENT = "const answer = 42;\n"; // "answer" occupies cols 6..12 of line 0. const RANGE = { start: { line: 0, col: 6 }, end: { line: 0, col: 12 } }; const CONTINUE: CardAction = { icon: null, label: "Continue", tooltip: "Continue the conversation", command: "continue", argument: "token-0", }; const RUN: CardAction = { icon: "run", label: "Run", tooltip: null, command: "run", argument: null }; function decorationSet(actions: CardAction[]): DecorationSet { return { root: "/dev/acme-api", path: PATH, setId: "card-actions", tree: false, items: [ { range: RANGE, style: "highlight", hover: null, after: " + model", markdownHover: "**model** conversation", actions, }, ], file: null, }; } /** Open a known file from the in-memory backend, then switch the page to the stubbed desktop. */ async function openCardFile(page: Page) { await page.evaluate( async ({ path, content }) => { const backend = "/src/editor/core/backend.ts"; (await import(/* @vite-ignore */ backend)).docBackend().files.set(path, content); const service = "/src/workbench/contributions.ts"; await (await import(/* @vite-ignore */ service)).editorService().openFile(path); }, { path: PATH, content: CONTENT }, ); await expect(page.locator(".cm-content")).toContainText("answer"); await stubDesktop(page); await page.evaluate(() => { window.__ipc.handlers.lsp_hover = () => null; }); } /** * Publish the decoration set and, when `registerCommands` is set, register the plugin's declared * commands through the real projection so the card's actions resolve to executable actions. */ async function publish(page: Page, set: DecorationSet, registerCommands: boolean) { await page.evaluate( async ({ set, plugin, registerCommands }) => { window.__ipc.handlers.plugin_decoration_sets = () => ({ [plugin]: [set] }); if (registerCommands) { window.__ipc.handlers.plugin_run_command = () => null; const backendModule = "/src/features/plugins/backend.ts"; const { createMemoryPluginsBackend, setPluginsBackend } = await import(/* @vite-ignore */ backendModule); const storeModule = "/src/features/plugins/store.ts"; const { usePlugins } = await import(/* @vite-ignore */ storeModule); const base = (await createMemoryPluginsBackend().list()).plugins[0]!; const info = { ...base, id: plugin, name: "Card Actions", enabled: true, hasCode: true, status: "active", activation: [], capabilities: [], }; const ui = [ { pluginId: plugin, point: "commands", key: "continue", decl: { id: "continue", title: "Continue" } }, { pluginId: plugin, point: "commands", key: "run", decl: { id: "run", title: "Run" } }, ]; window.__ipc.handlers.plugin_list = () => ({ plugins: [info], ui, activeTheme: null }); // Re-resolve the backend now that the page is desktop-mode, so runCommand goes over IPC. setPluginsBackend(null); usePlugins.setState({ plugins: [info], ui }); } const decorationsModule = "/src/features/plugins/decorations.ts"; const { setPluginDecorations } = await import(/* @vite-ignore */ decorationsModule); setPluginDecorations(plugin, [set]); }, { set, plugin: PLUGIN, registerCommands }, ); } const card = (page: Page) => page.locator(".cm-tooltip .cm-lsp-hover"); const token = (page: Page) => page.locator(".cm-content").getByText("answer", { exact: true }).first(); /** PR evidence: a full-page shot at the fixed viewport, plus a card close-up when one is open. */ async function evidence(page: Page, name: string, openCard?: Locator) { const dir = process.env.CARD_ACTIONS_EVIDENCE; if (!dir) return; await page.screenshot({ path: `${dir}/${name}.png`, animations: "disabled" }); if (openCard) await openCard.screenshot({ path: `${dir}/${name}-card.png`, animations: "disabled" }); } test("card actions render, run the declared plugin command and close the card", async ({ workbench: page }) => { await openCardFile(page); await publish(page, decorationSet([CONTINUE, RUN]), true); await token(page).hover(); const hover = card(page); await expect(hover).toBeVisible(); // (a) A row of real buttons above the Markdown, with accessible names. await expect(hover.locator("button.cm-plugin-card-action")).toHaveCount(2); await expect(hover.getByRole("button", { name: "Continue", exact: true })).toBeVisible(); await expect(hover.getByRole("button", { name: "Run", exact: true })).toBeVisible(); const shape = await hover.evaluate((el) => { const region = el.querySelector('[role="region"]')!; const open = el.querySelector("button.cm-plugin-card-action")!; const run = el.querySelectorAll("button.cm-plugin-card-action")[1]!; const row = open.parentElement!; return { rowFirst: region.firstElementChild === row, markdownAfterRow: row.nextElementSibling?.classList.contains("cm-lsp-markdown") ?? false, openType: open.type, openTab: open.tabIndex, openTitle: open.title, openAria: open.getAttribute("aria-label"), openText: open.textContent, runHasSvg: run.querySelector("svg") !== null, runAria: run.getAttribute("aria-label"), }; }); expect(shape).toEqual({ rowFirst: true, markdownAfterRow: true, openType: "button", openTab: 0, openTitle: "Continue the conversation", openAria: "Continue", openText: "Continue", runHasSvg: true, runAria: "Run", }); await evidence(page, "after-light", hover); // (b) Clicking runs the declared command over IPC with its argument, and closes the card. await hover.getByRole("button", { name: "Continue", exact: true }).click(); await expect(hover).toHaveCount(0); await expect.poll(async () => (await ipcCalls(page, "plugin_run_command")).length).toBe(1); const calls = await ipcCalls(page, "plugin_run_command"); expect(calls[0]).toMatchObject({ pluginId: PLUGIN, commandId: "continue", arg: "token-0", editor: { path: PATH }, paths: [], settingsScope: null, commandChain: null, request: null, }); await evidence(page, "after-click"); // The same card in Islands Dark. await page.evaluate(async () => { const themeModule = "/src/workbench/theme.ts"; const { setTheme } = await import(/* @vite-ignore */ themeModule); await setTheme("dark"); }); await token(page).hover(); const dark = card(page); await expect(dark).toBeVisible(); await expect(dark.locator("button.cm-plugin-card-action")).toHaveCount(2); await evidence(page, "after-dark", dark); }); test("a card without actions renders no button row", async ({ workbench: page }) => { await openCardFile(page); await publish(page, decorationSet([]), false); await token(page).hover(); const hover = card(page); await expect(hover).toBeVisible(); // (c) The before-state: Markdown only, no action buttons inside the card region. await expect(hover.locator("button.cm-plugin-card-action")).toHaveCount(0); const shape = await hover.evaluate((el) => { const region = el.querySelector('[role="region"]')!; return { noButton: region.querySelector("button") === null, markdownFirst: region.firstElementChild?.classList.contains("cm-lsp-markdown") ?? false, }; }); expect(shape).toEqual({ noButton: true, markdownFirst: true }); await expect(hover).toContainText("model"); await evidence(page, "before-light", hover); });