import type { Page } from "@playwright/test"; import type { AgentProvider, CustomAgentInput } from "../src/bindings"; import { expect, ipcCalls, stubDesktop, test } from "./fixtures"; async function openAgents(page: Page) { const settings = "/src/features/settings/model/staging.ts"; await page.evaluate(async (path) => (await import(/* @vite-ignore */ path)).openSettings("agents.list"), settings); return page.getByRole("dialog", { name: "Settings", exact: true }); } for (const theme of ["light", "dark"] as const) { test(`duplicates a built-in agent from the keyboard in ${theme}, also in a narrow window`, async ({ workbench: page, }) => { await stubDesktop(page); await page.evaluate(async (theme) => { const themePath = "/src/workbench/theme.ts"; (await import(/* @vite-ignore */ themePath)).previewTheme(theme); const claude: AgentProvider = { id: "claude", name: "Claude Code", command: "claude", program: "claude", args: [], defaultProgram: "claude", defaultArgs: [], loginHint: null, commandOverridden: false, commandEditable: true, commandReadOnlyReason: null, installed: true, canSignIn: true, canCheckCliUpdate: false, canDuplicate: true, env: {}, config: [], thoughtLevels: {}, commands: [], source: { kind: "builtin" }, }; let providers: AgentProvider[] = [claude]; Object.assign(window.__ipc.handlers, { settings_get_global: () => ({}), agent_registry_list: () => ({ agents: [], warning: null }), agent_providers: () => providers, agent_custom_save: ({ input }: { input: CustomAgentInput }) => { providers = [ ...providers, { ...claude, id: input.id, name: input.name, base: input.base, defaultProgram: null, defaultArgs: [], canDuplicate: false, commandEditable: false, commandReadOnlyReason: "customAgent", env: input.env ?? {}, source: { kind: "custom" }, }, ]; return null; }, }); }, theme); const settings = await openAgents(page); const claudePage = settings.getByRole("region", { name: "Claude Code" }); const duplicate = claudePage.getByRole("button", { name: "Duplicate…" }); await duplicate.focus(); await page.keyboard.press("Enter"); let dialog = page.getByRole("dialog", { name: "Duplicate Claude Code" }); await expect(dialog.getByRole("textbox", { name: "Name" })).toBeFocused(); await expect(dialog.getByRole("textbox", { name: "Name" })).toHaveValue("Claude Code 2"); await expect(dialog.getByRole("textbox", { name: "ID" })).toHaveValue("claude-code-2"); await expect(dialog.getByText(/CLAUDE_CONFIG_DIR/)).toBeVisible(); // Escape cancels and gives the focus back to the button that opened the dialog. await page.keyboard.press("Escape"); await expect(dialog).toBeHidden(); await expect(duplicate).toBeFocused(); await page.keyboard.press("Enter"); dialog = page.getByRole("dialog", { name: "Duplicate Claude Code" }); await page.setViewportSize({ width: 520, height: 760 }); const box = (await dialog.boundingBox())!; expect(box.x).toBeGreaterThanOrEqual(0); expect(box.x + box.width).toBeLessThanOrEqual(520); expect(await dialog.evaluate((el) => el.scrollWidth <= el.clientWidth)).toBe(true); await expect(dialog.getByText(/~ is not expanded/)).toBeVisible(); if (process.env.OPENIDEA_AGENT_DUPLICATE_SHOTS) await dialog.screenshot({ path: `${process.env.OPENIDEA_AGENT_DUPLICATE_SHOTS}/dialog-${theme}-520.png` }); await page.keyboard.press("ControlOrMeta+a"); await page.keyboard.type("Claude Work"); await page.keyboard.press("Tab"); await expect(dialog.getByRole("textbox", { name: "ID" })).toBeFocused(); await expect(dialog.getByRole("textbox", { name: "ID" })).toHaveValue("claude-work"); await page.keyboard.press("Tab"); await expect(dialog.getByRole("textbox", { name: "Environment variables" })).toBeFocused(); await page.keyboard.type("CLAUDE_CONFIG_DIR=/Users/me/.claude-work"); await page.keyboard.press("Enter"); await expect(dialog).toBeHidden(); expect(await ipcCalls(page, "agent_custom_save")).toEqual([ { input: { id: "claude-work", name: "Claude Work", command: "", base: "claude", env: { CLAUDE_CONFIG_DIR: "/Users/me/.claude-work" }, }, }, ]); // The copy is selected and the list keeps the keyboard. const copyPage = settings.getByRole("region", { name: "Claude Work" }); await expect(copyPage.getByText("Based on Claude Code · Agent file")).toBeVisible(); await expect(copyPage.getByRole("region", { name: "Launch" }).locator("code")).toHaveText("claude"); await expect(settings.getByRole("listbox", { name: "Agents" })).toBeFocused(); await expect(settings.getByRole("option", { name: /^Claude Work/ })).toHaveAttribute("aria-selected", "true"); await expect(copyPage.getByRole("textbox", { name: "Environment variables" })).toHaveValue( "CLAUDE_CONFIG_DIR=/Users/me/.claude-work", ); const details = (await copyPage.boundingBox())!; expect(details.width).toBeGreaterThan(180); if (process.env.OPENIDEA_AGENT_DUPLICATE_SHOTS) await settings.screenshot({ path: `${process.env.OPENIDEA_AGENT_DUPLICATE_SHOTS}/copy-${theme}-520.png` }); }); }