|
| 1 | +// Control UI E2E proves per-agent config writes use the canonical keyed shape. |
| 2 | +import { mkdir } from "node:fs/promises"; |
| 3 | +import path from "node:path"; |
| 4 | +import { chromium, type Browser } from "playwright"; |
| 5 | +import { afterAll, beforeAll, describe, expect, it } from "vitest"; |
| 6 | +import { |
| 7 | + canRunPlaywrightChromium, |
| 8 | + installMockGateway, |
| 9 | + resolvePlaywrightChromiumExecutablePath, |
| 10 | + startControlUiE2eServer, |
| 11 | + type ControlUiE2eServer, |
| 12 | +} from "../test-helpers/control-ui-e2e.ts"; |
| 13 | + |
| 14 | +const chromiumExecutablePath = resolvePlaywrightChromiumExecutablePath(chromium.executablePath()); |
| 15 | +const chromiumAvailable = canRunPlaywrightChromium(chromiumExecutablePath); |
| 16 | +const allowMissingChromium = process.env.OPENCLAW_UI_E2E_ALLOW_MISSING_CHROMIUM === "1"; |
| 17 | +const describeControlUiE2e = chromiumAvailable || !allowMissingChromium ? describe : describe.skip; |
| 18 | +const captureUiProof = process.env.OPENCLAW_CAPTURE_UI_PROOF === "1"; |
| 19 | +const proofDir = path.join(process.cwd(), ".artifacts", "control-ui-e2e", "agent-config-save"); |
| 20 | + |
| 21 | +let browser: Browser; |
| 22 | +let server: ControlUiE2eServer; |
| 23 | + |
| 24 | +function requireRecord(value: unknown): Record<string, unknown> { |
| 25 | + if (!value || typeof value !== "object" || Array.isArray(value)) { |
| 26 | + throw new Error("Expected object value"); |
| 27 | + } |
| 28 | + return value as Record<string, unknown>; |
| 29 | +} |
| 30 | + |
| 31 | +describeControlUiE2e("Control UI agent config save", () => { |
| 32 | + beforeAll(async () => { |
| 33 | + if (!chromiumAvailable) { |
| 34 | + throw new Error(`Playwright Chromium is unavailable at ${chromiumExecutablePath}`); |
| 35 | + } |
| 36 | + server = await startControlUiE2eServer(); |
| 37 | + browser = await chromium.launch({ executablePath: chromiumExecutablePath }); |
| 38 | + }); |
| 39 | + |
| 40 | + afterAll(async () => { |
| 41 | + await browser?.close(); |
| 42 | + await server?.close(); |
| 43 | + }); |
| 44 | + |
| 45 | + it("submits keyed entries and surfaces Gateway validation failures", async () => { |
| 46 | + const context = await browser.newContext({ |
| 47 | + locale: "en-US", |
| 48 | + serviceWorkers: "block", |
| 49 | + viewport: { height: 900, width: 1280 }, |
| 50 | + }); |
| 51 | + const page = await context.newPage(); |
| 52 | + const config = { |
| 53 | + agents: { |
| 54 | + entries: { |
| 55 | + main: { |
| 56 | + default: true, |
| 57 | + tools: { profile: "full" }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + }; |
| 62 | + const gateway = await installMockGateway(page, { |
| 63 | + assistantName: "Main agent", |
| 64 | + defaultAgentId: "main", |
| 65 | + methodResponses: { |
| 66 | + "agents.list": { |
| 67 | + agents: [{ id: "main", name: "Main agent" }], |
| 68 | + defaultId: "main", |
| 69 | + mainKey: "main", |
| 70 | + scope: "agent", |
| 71 | + }, |
| 72 | + "config.get": { |
| 73 | + config, |
| 74 | + sourceConfig: config, |
| 75 | + runtimeConfig: config, |
| 76 | + hash: "agent-config-hash-1", |
| 77 | + issues: [], |
| 78 | + raw: JSON.stringify(config), |
| 79 | + valid: true, |
| 80 | + }, |
| 81 | + "tools.catalog": { |
| 82 | + agentId: "main", |
| 83 | + profiles: [{ id: "full", label: "Full" }], |
| 84 | + groups: [ |
| 85 | + { |
| 86 | + id: "fs", |
| 87 | + label: "Files", |
| 88 | + source: "core", |
| 89 | + tools: [ |
| 90 | + { |
| 91 | + id: "read", |
| 92 | + label: "read", |
| 93 | + description: "Read files", |
| 94 | + source: "core", |
| 95 | + defaultProfiles: ["full"], |
| 96 | + }, |
| 97 | + ], |
| 98 | + }, |
| 99 | + ], |
| 100 | + }, |
| 101 | + "tools.effective": { |
| 102 | + agentId: "main", |
| 103 | + profile: "full", |
| 104 | + groups: [], |
| 105 | + notices: [], |
| 106 | + }, |
| 107 | + }, |
| 108 | + }); |
| 109 | + |
| 110 | + try { |
| 111 | + const response = await page.goto(`${server.baseUrl}settings/agents/main/tools`); |
| 112 | + expect(response?.status()).toBe(200); |
| 113 | + await gateway.waitForRequest("agents.list"); |
| 114 | + await gateway.waitForRequest("config.get"); |
| 115 | + await gateway.waitForRequest("tools.catalog"); |
| 116 | + |
| 117 | + await page |
| 118 | + .locator(".agent-tools-group") |
| 119 | + .filter({ hasText: "Files" }) |
| 120 | + .locator(".agent-tools-group__summary") |
| 121 | + .click(); |
| 122 | + await gateway.deferNext("config.set"); |
| 123 | + await page.locator("#agent-tool-read wa-switch").click(); |
| 124 | + |
| 125 | + const request = await gateway.waitForRequest("config.set"); |
| 126 | + const params = requireRecord(request.params); |
| 127 | + const raw = requireRecord(JSON.parse(String(params.raw))); |
| 128 | + expect(raw).toEqual({ |
| 129 | + agents: { |
| 130 | + entries: { |
| 131 | + main: { |
| 132 | + default: true, |
| 133 | + tools: { profile: "full", deny: ["read"] }, |
| 134 | + }, |
| 135 | + }, |
| 136 | + }, |
| 137 | + }); |
| 138 | + expect(requireRecord(raw.agents)).not.toHaveProperty("list"); |
| 139 | + expect(params.baseHash).toBe("agent-config-hash-1"); |
| 140 | + |
| 141 | + await gateway.rejectDeferred("config.set", { |
| 142 | + code: "INVALID_REQUEST", |
| 143 | + message: "mock validation failure", |
| 144 | + }); |
| 145 | + await page.getByRole("alert").filter({ hasText: "mock validation failure" }).waitFor(); |
| 146 | + |
| 147 | + if (captureUiProof) { |
| 148 | + await mkdir(proofDir, { recursive: true }); |
| 149 | + await page.screenshot({ |
| 150 | + animations: "disabled", |
| 151 | + fullPage: true, |
| 152 | + path: path.join(proofDir, "01-save-error.png"), |
| 153 | + }); |
| 154 | + } |
| 155 | + } finally { |
| 156 | + await context.close(); |
| 157 | + } |
| 158 | + }); |
| 159 | +}); |
0 commit comments