|
| 1 | +import { mkdir, rm } from "node:fs/promises"; |
| 2 | +import path from "node:path"; |
| 3 | +import { chromium, type Browser } 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 artifactDir = path.resolve(process.cwd(), ".artifacts/control-ui-e2e/chat-panel-reflow"); |
| 18 | +const chatSessionKey = "agent:main:main"; |
| 19 | + |
| 20 | +const longReply = |
| 21 | + "Additional runtime work may make an earlier benchmark misleading. " + |
| 22 | + "The current implementation must preserve the visible conversation while panels resize the transcript. "; |
| 23 | + |
| 24 | +const historyMessageCount = 4; |
| 25 | +const historyMessages = Array.from({ length: historyMessageCount }, (_, index) => ({ |
| 26 | + id: `message-${index}`, |
| 27 | + role: index % 2 === 0 ? "user" : "assistant", |
| 28 | + content: [ |
| 29 | + { |
| 30 | + type: "text", |
| 31 | + text: `${index + 1}. ${longReply.repeat(3)}\n\n${longReply.repeat(2)}`, |
| 32 | + }, |
| 33 | + ], |
| 34 | + timestamp: Date.now() - (historyMessageCount - index) * 1_000, |
| 35 | +})); |
| 36 | + |
| 37 | +async function expectMessagesNotToOverlap(page: import("playwright").Page): Promise<void> { |
| 38 | + await expect |
| 39 | + .poll(() => |
| 40 | + page.locator(".chat-group").evaluateAll((groups) => { |
| 41 | + const visible = groups |
| 42 | + .map((group, index) => { |
| 43 | + const rect = group.getBoundingClientRect(); |
| 44 | + return { bottom: rect.bottom, index, top: rect.top }; |
| 45 | + }) |
| 46 | + .filter((rect) => rect.bottom > 0 && rect.top < window.innerHeight) |
| 47 | + .toSorted((a, b) => a.top - b.top); |
| 48 | + return visible.slice(1).flatMap((current, index) => { |
| 49 | + const previous = visible[index]; |
| 50 | + if (!previous || current.top >= previous.bottom - 1) { |
| 51 | + return []; |
| 52 | + } |
| 53 | + return [ |
| 54 | + { |
| 55 | + current: current.index, |
| 56 | + overlap: previous.bottom - current.top, |
| 57 | + previous: previous.index, |
| 58 | + }, |
| 59 | + ]; |
| 60 | + }); |
| 61 | + }), |
| 62 | + ) |
| 63 | + .toEqual([]); |
| 64 | +} |
| 65 | + |
| 66 | +let server: ControlUiE2eServer; |
| 67 | +let browser: Browser; |
| 68 | + |
| 69 | +describeControlUiE2e("chat transcript panel reflow", () => { |
| 70 | + beforeAll(async () => { |
| 71 | + if (!chromiumAvailable) { |
| 72 | + throw new Error(`Playwright Chromium is unavailable at ${chromiumExecutablePath}`); |
| 73 | + } |
| 74 | + server = await startControlUiE2eServer(); |
| 75 | + browser = await chromium.launch({ executablePath: chromiumExecutablePath }); |
| 76 | + }); |
| 77 | + |
| 78 | + afterAll(async () => { |
| 79 | + await browser?.close(); |
| 80 | + await server?.close(); |
| 81 | + }); |
| 82 | + |
| 83 | + it("keeps transcript rows separate across background-task, file, and diff panel toggles", async () => { |
| 84 | + await rm(artifactDir, { force: true, recursive: true }); |
| 85 | + await mkdir(artifactDir, { recursive: true }); |
| 86 | + const context = await browser.newContext({ |
| 87 | + locale: "en-US", |
| 88 | + serviceWorkers: "block", |
| 89 | + viewport: { height: 900, width: 1280 }, |
| 90 | + }); |
| 91 | + const page = await context.newPage(); |
| 92 | + try { |
| 93 | + await installMockGateway(page, { |
| 94 | + featureMethods: ["chat.metadata", "chat.startup", "sessions.diff"], |
| 95 | + historyMessages, |
| 96 | + methodResponses: { |
| 97 | + "artifacts.list": { artifacts: [] }, |
| 98 | + "sessions.diff": { |
| 99 | + additions: 1, |
| 100 | + baseRef: "main", |
| 101 | + branch: "feature/reflow", |
| 102 | + deletions: 0, |
| 103 | + files: [ |
| 104 | + { |
| 105 | + additions: 1, |
| 106 | + deletions: 0, |
| 107 | + patch: [ |
| 108 | + "diff --git a/src/app.ts b/src/app.ts", |
| 109 | + "--- a/src/app.ts", |
| 110 | + "+++ b/src/app.ts", |
| 111 | + "@@ -1 +1,2 @@", |
| 112 | + " current line", |
| 113 | + "+new line", |
| 114 | + "", |
| 115 | + ].join("\n"), |
| 116 | + path: "src/app.ts", |
| 117 | + status: "modified", |
| 118 | + }, |
| 119 | + ], |
| 120 | + root: "/workspace", |
| 121 | + sessionKey: "main", |
| 122 | + }, |
| 123 | + "sessions.files.list": { |
| 124 | + browser: { entries: [], path: "" }, |
| 125 | + files: [ |
| 126 | + { |
| 127 | + kind: "modified", |
| 128 | + missing: false, |
| 129 | + name: "AGENTS.md", |
| 130 | + path: "/workspace/AGENTS.md", |
| 131 | + size: 2048, |
| 132 | + }, |
| 133 | + ], |
| 134 | + root: "/workspace", |
| 135 | + sessionKey: "main", |
| 136 | + }, |
| 137 | + "tasks.list": { |
| 138 | + tasks: [ |
| 139 | + { |
| 140 | + agentId: "main", |
| 141 | + createdAt: Date.now() - 5_000, |
| 142 | + id: "task-reflow", |
| 143 | + kind: "subagent", |
| 144 | + ownerKey: chatSessionKey, |
| 145 | + progressSummary: "Checking transcript layout", |
| 146 | + runtime: "subagent", |
| 147 | + startedAt: Date.now() - 4_000, |
| 148 | + status: "running", |
| 149 | + taskId: "task-reflow", |
| 150 | + title: "Reflow proof", |
| 151 | + updatedAt: Date.now(), |
| 152 | + }, |
| 153 | + ], |
| 154 | + }, |
| 155 | + }, |
| 156 | + }); |
| 157 | + |
| 158 | + const response = await page.goto(`${server.baseUrl}chat`); |
| 159 | + expect(response?.status()).toBe(200); |
| 160 | + await expect.poll(() => page.locator(".chat-group").count()).toBe(historyMessageCount); |
| 161 | + await page.locator(".chat-thread").evaluate((element) => { |
| 162 | + element.scrollTop = Math.max(0, element.scrollHeight / 2); |
| 163 | + }); |
| 164 | + await expectMessagesNotToOverlap(page); |
| 165 | + await page.screenshot({ path: path.join(artifactDir, "00-closed.png") }); |
| 166 | + |
| 167 | + await page.getByRole("button", { name: "Show background tasks" }).click(); |
| 168 | + await page.locator(".chat-tasks-rail").waitFor({ state: "visible" }); |
| 169 | + await expectMessagesNotToOverlap(page); |
| 170 | + await page.screenshot({ path: path.join(artifactDir, "01-background-tasks.png") }); |
| 171 | + |
| 172 | + await page.getByRole("button", { name: "Show thread files", exact: true }).click(); |
| 173 | + await page.locator(".chat-workspace-rail").waitFor({ state: "visible" }); |
| 174 | + await expectMessagesNotToOverlap(page); |
| 175 | + await page.screenshot({ path: path.join(artifactDir, "02-thread-files.png") }); |
| 176 | + |
| 177 | + await page.locator(".chat-session-diff-toggle").first().click(); |
| 178 | + await page.locator(".session-diff").waitFor({ state: "visible" }); |
| 179 | + await expectMessagesNotToOverlap(page); |
| 180 | + await page.screenshot({ path: path.join(artifactDir, "03-thread-changes.png") }); |
| 181 | + } finally { |
| 182 | + await context.close(); |
| 183 | + } |
| 184 | + }); |
| 185 | +}); |
0 commit comments