跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

browser-resize-smoke.mjs

59 行 · 3.2 KB历史下载
1import assert from "node:assert/strict";
2import { createWorkbench } from "./workbench-fixture.mjs";
3import { startDisplay } from "./rfb-wire-fixture.mjs";
4
5const display = await startDisplay(process.env.CAGE_UI_URL || "http://127.0.0.1:5198", { frameInterval: 200 });
6const { browser, page, state, errors } = await createWorkbench({
7 url: display.url, mockWebSockets: false, viewport: { width: 1000, height: 740 },
8});
9page.setDefaultTimeout(10000);
10await page.route("**/api/browser/resize", route => {
11 return route.fulfill({ json: { ok: true } });
12});
13try {
14 state.status.ready = state.status.vm_running = true;
15 state.tabs = [{ id: "page", kind: "web", title: "Resize", url: "https://example.test" }];
16 state.active = "page";
17 await page.reload();
18 await page.getByRole("button", { name: "打开浏览器", exact: true }).click();
19 const canvas = page.locator(".browser-lane .remote-canvas canvas");
20 const pixel = () => canvas.evaluate(c => [...c.getContext("2d").getImageData(c.width - 1, c.height - 1, 1, 1).data]);
21 await page.waitForFunction(() => {
22 const c = document.querySelector(".browser-lane canvas");
23 return c?.width && c.getContext("2d").getImageData(0, 0, 1, 1).data[0] > 0;
24 });
25 display.pauseFrames(true);
26 const oldPixel = await pixel();
27 const connections = display.stats.connections;
28 const oldBox = await canvas.boundingBox();
29 const resized = page.waitForRequest(req => req.url().endsWith("/api/browser/resize") && req.postDataJSON().width > oldBox.width * 2);
30 await page.setViewportSize({ width: 1240, height: 760 });
31 const box = await page.locator(".browser-lane .remote-canvas").boundingBox();
32 const shown = await canvas.boundingBox();
33 assert.ok(Math.abs(shown.width - box.width) < 1 && Math.abs(shown.height - box.height) < 1,
34 "the previous frame must fill the browser pane before the guest responds");
35 const x = Math.floor(box.width * .75), y = Math.floor(box.height * .25);
36 await canvas.click({ position: { x, y } });
37 assert.ok(Math.abs(display.stats.pointer.x - x / box.width * 640) <= 1);
38 assert.ok(Math.abs(display.stats.pointer.y - y / box.height * 480) <= 1);
39 const desired = (await resized).postDataJSON();
40 assert.equal(desired.width, Math.floor(box.width * 2));
41 display.resize(desired); // Send dimensions now, withhold all pixel updates.
42 await page.waitForFunction(w => document.querySelector(".browser-lane canvas")?.width === w, desired.width);
43 assert.deepEqual(await pixel(), oldPixel, "enlarging must preserve the visible corner until new pixels arrive");
44 display.pauseFrames(false);
45 await page.waitForFunction(value => {
46 const c = document.querySelector(".browser-lane canvas");
47 const p = c.getContext("2d").getImageData(c.width - 1, c.height - 1, 1, 1).data;
48 return p[3] === 255 && p[0] !== value;
49 }, oldPixel[0]);
50 assert.equal(display.stats.connections, connections);
51 assert.deepEqual(errors, []);
52 console.log("PASS: browser resize fills the pane, preserves the last frame and pointer mapping, then repaints over the same RFB connection");
53} catch (error) {
54 console.error({ errors, stats: display.stats, body: await page.locator("body").innerText() });
55 throw error;
56} finally {
57 await browser.close();
58 await display.close();
59}