|
| 1 | +import { mkdir } from "node:fs/promises"; |
| 2 | +import path from "node:path"; |
| 3 | +import { chromium, type Browser, type Page } from "playwright"; |
| 4 | +import { afterAll, beforeAll, describe, expect, it } from "vitest"; |
| 5 | +import { |
| 6 | + canRunPlaywrightChromium, |
| 7 | + installMockGateway, |
| 8 | + resolvePlaywrightChromiumExecutablePath, |
| 9 | + startControlUiE2eServer, |
| 10 | + type ControlUiE2eServer, |
| 11 | +} from "../test-helpers/control-ui-e2e.ts"; |
| 12 | + |
| 13 | +const chromiumExecutablePath = resolvePlaywrightChromiumExecutablePath(chromium.executablePath()); |
| 14 | +const chromiumAvailable = canRunPlaywrightChromium(chromiumExecutablePath); |
| 15 | +const allowMissingChromium = process.env.OPENCLAW_UI_E2E_ALLOW_MISSING_CHROMIUM === "1"; |
| 16 | +const describeControlUiE2e = chromiumAvailable || !allowMissingChromium ? describe : describe.skip; |
| 17 | +const captureProof = process.env.OPENCLAW_CAPTURE_UI_PROOF === "1"; |
| 18 | +const proofStage = process.env.OPENCLAW_JSON_CODE_PROOF_STAGE ?? "after"; |
| 19 | +const artifactDir = path.resolve(process.cwd(), ".artifacts/control-ui-e2e/chat-json-code-block"); |
| 20 | + |
| 21 | +function fencedJson(lineCount: number): string { |
| 22 | + const values = Array.from({ length: lineCount - 2 }, (_, index) => ` ${index},`); |
| 23 | + values[values.length - 1] = values.at(-1)?.slice(0, -1) ?? ""; |
| 24 | + return `\`\`\`json\n[\n${values.join("\n")}\n]\n\`\`\``; |
| 25 | +} |
| 26 | + |
| 27 | +const shortJson = `\`\`\`json |
| 28 | +{ |
| 29 | + "status": "ok", |
| 30 | + "items": [ |
| 31 | + "alpha" |
| 32 | + ] |
| 33 | +} |
| 34 | +\`\`\``; |
| 35 | + |
| 36 | +async function setThemeMode(page: Page, mode: "dark" | "light"): Promise<void> { |
| 37 | + await page.emulateMedia({ colorScheme: mode }); |
| 38 | + await page.evaluate((nextMode) => { |
| 39 | + const root = document.documentElement; |
| 40 | + root.dataset.themeMode = nextMode; |
| 41 | + root.dataset.themeResolved = nextMode; |
| 42 | + root.classList.toggle("wa-light", nextMode === "light"); |
| 43 | + root.classList.toggle("wa-dark", nextMode === "dark"); |
| 44 | + root.style.colorScheme = nextMode; |
| 45 | + }, mode); |
| 46 | +} |
| 47 | + |
| 48 | +let browser: Browser; |
| 49 | +let server: ControlUiE2eServer; |
| 50 | + |
| 51 | +describeControlUiE2e("Control UI JSON code blocks", () => { |
| 52 | + beforeAll(async () => { |
| 53 | + if (!chromiumAvailable) { |
| 54 | + throw new Error(`Playwright Chromium is unavailable at ${chromiumExecutablePath}`); |
| 55 | + } |
| 56 | + if (captureProof) { |
| 57 | + await mkdir(artifactDir, { recursive: true }); |
| 58 | + } |
| 59 | + server = await startControlUiE2eServer(undefined, { source: true }); |
| 60 | + browser = await chromium.launch({ executablePath: chromiumExecutablePath }); |
| 61 | + }); |
| 62 | + |
| 63 | + afterAll(async () => { |
| 64 | + await browser?.close(); |
| 65 | + await server?.close(); |
| 66 | + }); |
| 67 | + |
| 68 | + it.each(["dark", "light"] as const)( |
| 69 | + "keeps short JSON visible and reserves one disclosure header for long JSON in %s mode", |
| 70 | + async (theme) => { |
| 71 | + const context = await browser.newContext({ |
| 72 | + colorScheme: theme, |
| 73 | + locale: "en-US", |
| 74 | + serviceWorkers: "block", |
| 75 | + viewport: { height: 900, width: 1440 }, |
| 76 | + }); |
| 77 | + await context.grantPermissions(["clipboard-read", "clipboard-write"], { |
| 78 | + origin: new URL(server.baseUrl).origin, |
| 79 | + }); |
| 80 | + const page = await context.newPage(); |
| 81 | + await installMockGateway(page, { |
| 82 | + historyMessages: [ |
| 83 | + { |
| 84 | + role: "user", |
| 85 | + content: [{ type: "text", text: "Show the full diagnostic payload." }], |
| 86 | + timestamp: Date.now(), |
| 87 | + __openclaw: { id: "user-json-long", seq: 1 }, |
| 88 | + }, |
| 89 | + { |
| 90 | + role: "assistant", |
| 91 | + content: [{ type: "text", text: fencedJson(41) }], |
| 92 | + timestamp: Date.now() + 1, |
| 93 | + __openclaw: { id: "assistant-json-long", seq: 2 }, |
| 94 | + }, |
| 95 | + { |
| 96 | + role: "user", |
| 97 | + content: [{ type: "text", text: "Return the deployment receipt as JSON." }], |
| 98 | + timestamp: Date.now() + 2, |
| 99 | + __openclaw: { id: "user-json-short", seq: 3 }, |
| 100 | + }, |
| 101 | + { |
| 102 | + role: "assistant", |
| 103 | + content: [{ type: "text", text: shortJson }], |
| 104 | + timestamp: Date.now() + 3, |
| 105 | + __openclaw: { id: "assistant-json-short", seq: 4 }, |
| 106 | + }, |
| 107 | + ], |
| 108 | + }); |
| 109 | + |
| 110 | + try { |
| 111 | + await page.goto(`${server.baseUrl}chat`); |
| 112 | + await setThemeMode(page, theme); |
| 113 | + const shortBubble = page.locator('[data-entry-id="assistant-json-short"]'); |
| 114 | + const longBubble = page.locator('[data-entry-id="assistant-json-long"]'); |
| 115 | + await shortBubble.waitFor({ state: "visible" }); |
| 116 | + if (captureProof) { |
| 117 | + await page.screenshot({ |
| 118 | + animations: "disabled", |
| 119 | + path: path.join(artifactDir, `${proofStage}-${theme}.png`), |
| 120 | + }); |
| 121 | + } |
| 122 | + |
| 123 | + expect(await shortBubble.locator("details.json-collapse").count()).toBe(0); |
| 124 | + expect(await shortBubble.locator("pre code").isVisible()).toBe(true); |
| 125 | + |
| 126 | + const details = longBubble.locator("details.json-collapse"); |
| 127 | + const summary = details.locator("summary"); |
| 128 | + expect(await summary.textContent()).toContain("JSON · 41 lines"); |
| 129 | + expect(await summary.locator(".code-block-copy").count()).toBe(1); |
| 130 | + expect(await details.locator(".code-block-header").count()).toBe(1); |
| 131 | + await summary.click(); |
| 132 | + await expect.poll(() => details.getAttribute("open")).toBe(""); |
| 133 | + await summary.locator(".code-block-copy").click(); |
| 134 | + await expect.poll(() => details.getAttribute("open")).toBe(""); |
| 135 | + } finally { |
| 136 | + await context.close(); |
| 137 | + } |
| 138 | + }, |
| 139 | + ); |
| 140 | +}); |
0 commit comments