| 1 | import assert from "node:assert/strict"; |
| 2 | import fs from "node:fs/promises"; |
| 3 | import { chooseInline, createWorkbench } from "./workbench-fixture.mjs"; |
| 4 | import { startDisplay } from "./rfb-wire-fixture.mjs"; |
| 5 | |
| 6 | const display = await startDisplay(process.env.CAGE_UI_URL || "http://127.0.0.1:5198", { frameInterval: 1000 }); |
| 7 | const { browser, page, state, errors } = await createWorkbench({ |
| 8 | mockWebSockets: false, url: display.url, viewport: { width: 1100, height: 800 }, |
| 9 | }); |
| 10 | page.setDefaultTimeout(10000); |
| 11 | const output = process.env.CAGE_DISPLAY_REPORT || "/tmp/cage-display-resolution"; |
| 12 | const requests = [], actions = []; |
| 13 | let failResize = false; |
| 14 | let runningDensity; |
| 15 | state.settings.balanced_display = false; |
| 16 | const desktop = { installed: true, running: true, managed: true, version: "fixture" }; |
| 17 | await page.route("**/api/claude-desktop**", async (route) => { |
| 18 | const path = new URL(route.request().url()).pathname; |
| 19 | if (path.endsWith("/vnc-credentials")) return route.fulfill({ json: { password: "fixture", display_scale: runningDensity } }); |
| 20 | if (path.endsWith("/resize")) { |
| 21 | const size = route.request().postDataJSON(); |
| 22 | requests.push(size); |
| 23 | if (failResize) return route.fulfill({ status: 502, body: "fixture: guest busy" }); |
| 24 | display.resize(size); |
| 25 | return route.fulfill({ json: { ok: true } }); |
| 26 | } |
| 27 | if (route.request().method() === "GET") return route.fulfill({ json: desktop }); |
| 28 | actions.push(path); |
| 29 | return route.fulfill({ json: { ok: true } }); |
| 30 | }); |
| 31 | async function matched(zoom = 1, density = runningDensity ?? 2) { |
| 32 | await page.waitForFunction(({ zoom, density }) => { |
| 33 | const box = document.querySelector(".claude-desktop-view .remote-canvas"), canvas = box?.querySelector("canvas"); |
| 34 | if (!canvas) return false; |
| 35 | const bounds = box.getBoundingClientRect(), shown = canvas.getBoundingClientRect(); |
| 36 | return canvas.width === Math.floor(bounds.width * density / zoom) && |
| 37 | canvas.height === Math.floor(bounds.height * density / zoom) && |
| 38 | Math.abs(shown.width - bounds.width) < 2 && Math.abs(shown.height - bounds.height) < 2; |
| 39 | }, { zoom, density }, { timeout: 10000 }); |
| 40 | } |
| 41 | try { |
| 42 | state.status.vm_running = state.status.ready = true; |
| 43 | await page.reload(); |
| 44 | await page.getByRole("button", { name: "Claude Desktop", exact: true }).click(); |
| 45 | await matched(); |
| 46 | await page.setViewportSize({ width: 1000, height: 740 }); |
| 47 | await matched(); |
| 48 | await page.getByRole("button", { name: "收起侧栏", exact: true }).click(); |
| 49 | await matched(); |
| 50 | const connections = display.stats.connections; |
| 51 | const selector = page.getByLabel("Desktop 显示大小", { exact: true }); |
| 52 | const screen = page.locator(".desktop-screen"); |
| 53 | const toolbar = page.locator(".claude-desktop-toolbar"); |
| 54 | const toolbarBefore = await toolbar.boundingBox(); |
| 55 | const before = await screen.boundingBox(); |
| 56 | await selector.click(); |
| 57 | await fs.mkdir(output, { recursive: true }); |
| 58 | await page.screenshot({ path: output + "/display-size-open.png" }); |
| 59 | const options = page.getByRole("listbox"); |
| 60 | const bounds = await options.boundingBox(); |
| 61 | const trigger = await selector.boundingBox(); |
| 62 | assert.equal(bounds.x, trigger.x); |
| 63 | assert.ok(bounds.y >= trigger.y + trigger.height && bounds.y + bounds.height > before.y); |
| 64 | assert.deepEqual(await toolbar.boundingBox(), toolbarBefore); |
| 65 | assert.deepEqual(await screen.boundingBox(), before); |
| 66 | await selector.press("Escape"); |
| 67 | await options.waitFor({ state: "hidden" }); |
| 68 | assert.deepEqual(await screen.boundingBox(), before); |
| 69 | await selector.press("Enter"); |
| 70 | await selector.press("ArrowDown"); |
| 71 | await selector.press("ArrowDown"); |
| 72 | await selector.press("Enter"); |
| 73 | await matched(1.25); |
| 74 | await chooseInline(selector, "0.75"); |
| 75 | await matched(0.75); |
| 76 | assert.equal(display.stats.connections, connections, "zoom must preserve the active RFB connection"); |
| 77 | failResize = true; |
| 78 | await chooseInline(selector, "1.1"); |
| 79 | await page.getByRole("alert").filter({ hasText: "画面尺寸未能同步" }).waitFor(); |
| 80 | failResize = false; |
| 81 | await matched(1.1); |
| 82 | await page.getByRole("alert").filter({ hasText: "画面尺寸未能同步" }).waitFor({ state: "hidden" }); |
| 83 | await page.getByRole("button", { name: "展开侧栏", exact: true }).click(); |
| 84 | await matched(1.1); |
| 85 | await page.reload(); |
| 86 | await page.getByRole("button", { name: "Claude Desktop", exact: true }).click(); |
| 87 | await matched(1.1); |
| 88 | assert.equal(await selector.getAttribute("value"), "1.1", "zoom preference must survive reload"); |
| 89 | await chooseInline(selector, "1"); |
| 90 | await matched(); |
| 91 | await page.setViewportSize({ width: 820, height: 558 }); |
| 92 | await matched(); |
| 93 | await page.getByRole("button", { name: "设置", exact: true }).click(); |
| 94 | await page.getByRole("switch", { name: /流畅画质/ }).check(); |
| 95 | await page.getByRole("button", { name: "保存设置", exact: true }).click(); |
| 96 | await page.getByRole("region", { name: "设置", exact: true }).waitFor({ state: "hidden" }); |
| 97 | assert.equal(state.settings.balanced_display, true); |
| 98 | await matched(1, 2); |
| 99 | runningDensity = 1.5; |
| 100 | await page.reload(); |
| 101 | await page.getByRole("button", { name: "Claude Desktop", exact: true }).click(); |
| 102 | await matched(); |
| 103 | await chooseInline(selector, "1.25"); |
| 104 | await matched(1.25); |
| 105 | await chooseInline(selector, "1"); |
| 106 | await matched(); |
| 107 | assert.deepEqual(actions, [], "display changes must not restart the application"); |
| 108 | assert.deepEqual(errors, []); |
| 109 | await page.screenshot({ path: `${output}/small-window.png` }); |
| 110 | await fs.writeFile(`${output}/result.json`, JSON.stringify({ requests, rfb: display.stats, actions, errors }, null, 2)); |
| 111 | console.log("PASS: real RFB framebuffer follows viewport, sidebar and zoom; failure recovery, persistence, small window, no application restart"); |
| 112 | } catch (error) { |
| 113 | console.error(JSON.stringify({ requests, rfb: display.stats, errors, body: await page.locator("body").innerText(), canvas: await page.locator(".remote-canvas").evaluateAll((boxes) => boxes.map((box) => { |
| 114 | const canvas = box.querySelector("canvas"); |
| 115 | return { box: box.getBoundingClientRect().toJSON(), width: canvas?.width, height: canvas?.height, shown: canvas?.getBoundingClientRect().toJSON() }; |
| 116 | })) })); |
| 117 | throw error; |
| 118 | } finally { |
| 119 | await browser.close(); |
| 120 | await display.close(); |
| 121 | } |