跳到主要内容

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

浏览文件

remote-visibility-smoke.mjs

102 行 · 5.7 KB历史下载
1// Exercise the shipped noVNC client through its real RFB handshake and raw frames.
2// The fixture owns all requests and never reaches a user's VM.
3import assert from "node:assert/strict";
4import fs from "node:fs/promises";
5import { createWorkbench } from "./workbench-fixture.mjs";
6
7const { startDisplay } = await import("./rfb-wire-fixture.mjs");
8const display = await startDisplay(process.env.CAGE_UI_URL || "http://127.0.0.1:5199");
9const fixture = await createWorkbench({ mockWebSockets: false, url: display.url });
10const { page, state, browser, errors } = fixture;
11const report = display.stats;
12report.checks = [];
13const browserErrors = [];
14page.on("console", (message) => {
15 if (message.type() === "error") browserErrors.push(message.text());
16});
17try {
18 await page.getByRole("button", { name: "启动 Cage 环境", exact: true }).click();
19 await page.getByRole("heading", { name: "Cage 环境已就绪", exact: true }).waitFor();
20 await page.getByRole("button", { name: "打开浏览器", exact: true }).click();
21 await page.waitForFunction(() => Boolean(document.querySelector(".remote-canvas canvas")));
22 await page.getByText("正在连接画面", { exact: true }).waitFor({ state: "hidden" });
23 assert.ok(report.connections > 0);
24 const deadline = Date.now() + 3000;
25 while (report.frames < 5 && Date.now() < deadline)
26 await new Promise((resolve) => setTimeout(resolve, 20));
27 assert.ok(report.frames >= 5, "display did not deliver its first frames");
28 const originalTabs = structuredClone(state.tabs);
29 const before = report.frames;
30 await page.waitForFunction(() => document.querySelector(".remote-canvas canvas")?.width > 0);
31 await page.getByRole("button", { name: "工作台", exact: true }).click();
32 // Bounded observation proves frames stop, rather than only inspecting CSS.
33 await new Promise((resolve) => setTimeout(resolve, 500));
34 const hiddenStart = report.frames;
35 await new Promise((resolve) => setTimeout(resolve, 1000));
36 report.hiddenFrames = report.frames - hiddenStart;
37 report.visibleFrames = before;
38 if (!process.env.CAGE_PERF_BASELINE) {
39 assert.equal(report.hiddenFrames, 0, "hidden workbench continued receiving frames");
40 assert.ok(report.closed > 0, "display connection was not closed");
41 }
42 assert.deepEqual(state.tabs, originalTabs, "hiding the display changed guest tabs");
43 await page.getByRole("button", { name: "打开浏览器", exact: true }).click();
44 await page.getByText("正在连接画面", { exact: true }).waitFor({ state: "hidden" });
45 await page.waitForFunction(() => document.querySelector(".remote-canvas canvas")?.width > 0);
46 const resumed = report.frames;
47 await new Promise((resolve) => setTimeout(resolve, 250));
48 assert.ok(report.frames > resumed, "display frames did not resume");
49 if (!process.env.CAGE_PERF_BASELINE) {
50 await page.evaluate(() => {
51 window.cageWindowVisible = false;
52 window.dispatchEvent(new Event("cage-window-visibility"));
53 });
54 await new Promise((resolve) => setTimeout(resolve, 300));
55 const paused = report.frames;
56 await new Promise((resolve) => setTimeout(resolve, 300));
57 assert.equal(report.frames, paused, "occluded native window continued receiving frames");
58 await page.evaluate(() => {
59 window.cageWindowVisible = true;
60 window.dispatchEvent(new Event("cage-window-visibility"));
61 });
62 await page.getByText("正在连接画面", { exact: true }).waitFor({ state: "hidden" });
63 await new Promise((resolve) => setTimeout(resolve, 250));
64 assert.ok(report.frames > paused, "native window visibility did not resume frames");
65 }
66 assert.deepEqual(errors, []);
67 report.checks.push("real RFB frames", "tabs preserved", "frames resumed");
68 if (!process.env.CAGE_PERF_BASELINE) report.checks.push("hidden frames stopped", "native occlusion paused");
69 const soakSeconds = Number(process.env.CAGE_PERF_SOAK_SECONDS || 0);
70 if (soakSeconds > 0) {
71 const session = await page.context().newCDPSession(page);
72 await session.send("Performance.enable");
73 report.soak = [];
74 const started = Date.now();
75 while ((Date.now() - started) / 1000 < soakSeconds) {
76 const beforeResume = report.frames;
77 await page.evaluate(() => { window.cageWindowVisible = true; window.dispatchEvent(new Event("cage-window-visibility")); });
78 await page.getByText("正在连接画面", { exact: true }).waitFor({ state: "hidden" });
79 await new Promise((resolve) => setTimeout(resolve, 1000));
80 assert.ok(report.frames > beforeResume, "soak: visible display did not resume frames");
81 await page.evaluate(() => { window.cageWindowVisible = false; window.dispatchEvent(new Event("cage-window-visibility")); });
82 await new Promise((resolve) => setTimeout(resolve, 500));
83 const hidden = report.frames;
84 await new Promise((resolve) => setTimeout(resolve, 8500));
85 assert.equal(report.frames, hidden, "soak: frames continued in hidden window");
86 const { metrics } = await session.send("Performance.getMetrics");
87 report.soak.push({ seconds: (Date.now() - started) / 1000, ...Object.fromEntries(metrics.filter((x) => ["JSHeapUsedSize", "JSHeapTotalSize", "TaskDuration", "Documents", "Nodes"].includes(x.name)).map((x) => [x.name, x.value])) });
88 if (process.env.CAGE_PERF_REPORT) await fs.writeFile(process.env.CAGE_PERF_REPORT, JSON.stringify(report, null, 2));
89 }
90 report.checks.push(`${soakSeconds}s repeated display reconnect soak`);
91 await session.detach();
92 }
93 const output = process.env.CAGE_PERF_REPORT;
94 if (output) await fs.writeFile(output, JSON.stringify(report, null, 2));
95 console.log(JSON.stringify(report));
96} catch (error) {
97 console.error(JSON.stringify({ report, errors, browserErrors, visible: await page.locator("body").innerText() }));
98 throw error;
99} finally {
100 await browser.close();
101 await display.close();
102}