|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { readFile } from "node:fs/promises"; |
| 3 | +import { chromium } from "playwright"; |
| 4 | + |
| 5 | +const [url, settingsFile] = process.argv.slice(2); |
| 6 | + |
| 7 | +if (!url || !settingsFile) { |
| 8 | + throw new Error( |
| 9 | + "Usage: node scripts/test-copilot-settings-ui.mjs <url> <settings-file>", |
| 10 | + ); |
| 11 | +} |
| 12 | + |
| 13 | +async function fetchJson(page, path) { |
| 14 | + return await page.evaluate(async (targetPath) => { |
| 15 | + const res = await fetch(targetPath); |
| 16 | + return await res.json(); |
| 17 | + }, path); |
| 18 | +} |
| 19 | + |
| 20 | +let browser; |
| 21 | + |
| 22 | +try { |
| 23 | + browser = await chromium.launch(); |
| 24 | +} catch (error) { |
| 25 | + const message = error instanceof Error |
| 26 | + ? error.message |
| 27 | + : String(error); |
| 28 | + throw new Error( |
| 29 | + `${message}\nInstall Chromium with: bunx playwright install chromium`, |
| 30 | + ); |
| 31 | +} |
| 32 | + |
| 33 | +try { |
| 34 | + const page = await browser.newPage(); |
| 35 | + await page.goto(url, { waitUntil: "domcontentloaded" }); |
| 36 | + |
| 37 | + await page.getByTitle("Settings").waitFor(); |
| 38 | + await page.getByTitle("Settings").click(); |
| 39 | + await page.getByRole("tab", { name: "Agents" }).click(); |
| 40 | + await page.getByRole("button", { name: "Scan" }).click(); |
| 41 | + |
| 42 | + const scanResults = page |
| 43 | + .getByText("Scan Results") |
| 44 | + .locator("xpath=ancestor::div[contains(@class,'rounded-xl')][1]"); |
| 45 | + await scanResults.waitFor(); |
| 46 | + |
| 47 | + const copilotRow = scanResults |
| 48 | + .getByText("copilot") |
| 49 | + .locator( |
| 50 | + "xpath=ancestor::div[contains(@class,'rounded-lg')][1]", |
| 51 | + ); |
| 52 | + await copilotRow.waitFor(); |
| 53 | + await copilotRow.getByText("Claude Sonnet 4.5").waitFor(); |
| 54 | + await copilotRow.getByRole("button", { name: "Add" }).click(); |
| 55 | + await copilotRow.getByText("registered").waitFor(); |
| 56 | + |
| 57 | + const agentsPayload = await fetchJson( |
| 58 | + page, |
| 59 | + "/api/settings/agents", |
| 60 | + ); |
| 61 | + assert.equal(agentsPayload.ok, true); |
| 62 | + const agentEntry = Object.entries( |
| 63 | + agentsPayload.data ?? {}, |
| 64 | + ).find(([id]) => id.startsWith("copilot")); |
| 65 | + assert.ok(agentEntry, "expected a registered Copilot agent"); |
| 66 | + |
| 67 | + const [agentId, agent] = agentEntry; |
| 68 | + assert.equal(agent.model, "claude-sonnet-4.5"); |
| 69 | + |
| 70 | + let actionsPayload = await fetchJson( |
| 71 | + page, |
| 72 | + "/api/settings/actions", |
| 73 | + ); |
| 74 | + for (let attempt = 0; attempt < 20; attempt += 1) { |
| 75 | + if (actionsPayload.data?.take === agentId) break; |
| 76 | + await page.waitForTimeout(250); |
| 77 | + actionsPayload = await fetchJson( |
| 78 | + page, |
| 79 | + "/api/settings/actions", |
| 80 | + ); |
| 81 | + } |
| 82 | + assert.equal(actionsPayload.ok, true); |
| 83 | + assert.deepEqual(actionsPayload.data, { |
| 84 | + take: agentId, |
| 85 | + scene: agentId, |
| 86 | + breakdown: agentId, |
| 87 | + scopeRefinement: agentId, |
| 88 | + }); |
| 89 | + |
| 90 | + const settingsRaw = await readFile(settingsFile, "utf8"); |
| 91 | + assert.match(settingsRaw, new RegExp(`\\[agents\\.${agentId}\\]`)); |
| 92 | + assert.match(settingsRaw, /model = "claude-sonnet-4\.5"/); |
| 93 | +} finally { |
| 94 | + await browser?.close(); |
| 95 | +} |
0 commit comments