跳到主要内容

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

浏览文件

browser-vertical-tabs-smoke.mjs

150 行 · 9.6 KB历史下载
1import assert from "node:assert/strict";
2import fs from "node:fs/promises";
3import { fileURLToPath } from "node:url";
4import { createWorkbench } from "./workbench-fixture.mjs";
5import { startDisplay } from "./rfb-wire-fixture.mjs";
6
7const engine = process.env.CAGE_TEST_BROWSER || "chromium";
8const output = new URL(`../../docs/test-reports/2026-11-07-browser-vertical-tabs/${engine}/`, import.meta.url);
9await fs.mkdir(output, { recursive: true });
10const display = await startDisplay(process.env.CAGE_UI_URL || "http://127.0.0.1:5198", { frameInterval: 200 });
11const { browser, page, state, errors } = await createWorkbench({ url: display.url, mockWebSockets: engine === "webkit" });
12page.setDefaultTimeout(10000);
13const checks = [];
14const tabs = page.getByRole("tablist", { name: "浏览器标签页", exact: true });
15const settings = page.getByRole("region", { name: "浏览器设置", exact: true });
16const rail = page.getByRole("complementary", { name: "竖排标签栏", exact: true });
17const shot = name => page.screenshot({ path: fileURLToPath(new URL(name + ".png", output)) });
18async function openSettings() {
19 await page.getByRole("button", { name: "浏览器菜单", exact: true }).last().click();
20 await page.getByRole("menuitem", { name: "设置", exact: true }).click();
21 await settings.getByRole("navigation", { name: "浏览器设置分类" }).getByRole("button", { name: "外观", exact: true }).click();
22}
23async function closeSettings() {
24 await page.getByRole("button", { name: "关闭 设置", exact: true }).click();
25 await settings.waitFor({ state: "hidden" });
26}
27async function drag(from, to) {
28 const a = await from.boundingBox(), b = await to.boundingBox();
29 await page.mouse.move(a.x + a.width / 2, a.y + a.height / 2);
30 await page.mouse.down();
31 await page.mouse.move(b.x + b.width / 2, b.y + b.height / 2, { steps: 12 });
32 await page.mouse.up();
33}
34try {
35 state.status.ready = state.status.vm_running = true;
36 state.tabs = Array.from({ length: 5 }, (_, i) => ({
37 id: `tab-${i}`, kind: i === 4 ? "editor" : "web", title: `测试网页 ${i}`,
38 workspace: i === 4 ? state.settings.workspace : undefined, url: `https://example.test/${i}`,
39 }));
40 state.active = "tab-0";
41 await page.reload();
42 await page.getByRole("button", { name: "打开浏览器", exact: true }).click();
43 if (engine !== "webkit") await page.locator(".remote-canvas canvas").waitFor();
44 assert.equal(await tabs.getAttribute("aria-orientation"), "horizontal");
45 const surface = await page.locator(".remote-view").elementHandle();
46 await openSettings();
47 const picker = page.getByRole("combobox", { name: "标签栏布局", exact: true });
48 const help = settings.getByText("浏览器设置页跟随 Cage 当前的字体与配色。", { exact: true });
49 await settings.locator('.browser-settings-section').evaluate(element => Promise.all(element.getAnimations().map(animation => animation.finished)));
50 const before = await help.boundingBox();
51 await picker.click();
52 const expanded = await help.boundingBox();
53 assert.ok(Math.abs(expanded.y - before.y) < 1, "floating options must not move the following content");
54 await page.keyboard.press("Escape");
55 assert.ok(Math.abs((await help.boundingBox()).y - before.y) < 1, "closing options restores the layout");
56 await picker.click();
57 await page.getByRole("option", { name: "竖排(左侧)", exact: true }).click();
58 await shot("browser-settings");
59 assert.equal(await picker.getAttribute("aria-expanded"), "false");
60 await closeSettings();
61 await rail.waitFor();
62 assert.equal(await tabs.getAttribute("aria-orientation"), "vertical");
63 assert.ok(await surface.evaluate(node => node === document.querySelector(".remote-view")), "changing layout must retain the remote-view component");
64 const first = tabs.getByRole("tab", { name: "测试网页 0", exact: true });
65 const second = tabs.getByRole("tab", { name: "测试网页 1", exact: true });
66 assert.ok((await second.boundingBox()).y > (await first.boundingBox()).y);
67 const railBox = await rail.boundingBox(), bodyBox = await page.locator(".browser-lane .pane-body").boundingBox();
68 assert.ok(railBox.x + railBox.width <= bodyBox.x + 1);
69 await shot("vertical-desktop");
70 await first.focus();
71 await page.keyboard.press("ArrowDown");
72 await page.waitForFunction(() => document.querySelector('[data-tab-id="tab-1"] [role="tab"]').getAttribute("aria-selected") === "true");
73 await page.keyboard.press("End");
74 await page.waitForFunction(() => document.querySelector('[data-tab-id="tab-4"] [role="tab"]').getAttribute("aria-selected") === "true");
75 await page.keyboard.press("Home");
76 await drag(first, tabs.getByRole("tab", { name: "测试网页 3", exact: true }));
77 await page.waitForFunction(() => [...document.querySelectorAll('.browser-tabs [role="tab"]')].findIndex(t => t.textContent === "测试网页 0") === 2);
78 await first.click({ button: "right" });
79 await page.getByRole("menuitem", { name: "置顶标签", exact: true }).click();
80 await page.waitForFunction(() => document.querySelector(".browser-tabs .browser-tab").dataset.tabId === "tab-0");
81 const order = await tabs.getByRole("tab").allTextContents();
82 await first.click({ button: "right" });
83 await page.getByRole("menuitem", { name: "重命名标签", exact: true }).click();
84 const rename = page.getByRole("dialog", { name: "重命名标签", exact: true });
85 await rename.getByRole("textbox").fill("命名后的网页");
86 await rename.getByRole("button", { name: "保存名称", exact: true }).click();
87 order[0] = "命名后的网页";
88 await page.reload();
89 await page.getByRole("button", { name: "打开浏览器", exact: true }).click();
90 await rail.waitFor();
91 assert.deepEqual(await tabs.getByRole("tab").allTextContents(), order);
92 await page.getByRole("button", { name: "新建网页", exact: true }).click();
93 await page.waitForFunction(() => document.querySelectorAll('.browser-tabs [role="tab"]').length === 6);
94 await tabs.getByRole("tab", { name: "测试网页 2", exact: true }).click();
95 await page.getByRole("button", { name: "关闭 测试网页 2", exact: true }).click();
96 await tabs.getByRole("tab", { name: "测试网页 2", exact: true }).waitFor({ state: "hidden" });
97 checks.push("横排默认值;设置下拉内嵌展开与收起;竖排切换保留画面组件;上下键和 Home/End;拖动排序、置顶、刷新保留、新建和关闭");
98
99 // Drag from the rail into the page to exercise native dual-browser splitting.
100 await drag(tabs.getByRole("tab", { name: "测试网页 3", exact: true }), page.locator(".browser-lane .pane-body"));
101 await page.waitForFunction(() => document.querySelectorAll(".browser-header-cell").length === 2);
102 assert.equal(await rail.count(), 1);
103 assert.equal(await tabs.count(), 1);
104 assert.ok(state.split?.left && state.split?.right);
105 await page.locator(".browser-header-cell").first().getByRole("button", { name: "退出分屏", exact: true }).click();
106 await page.waitForFunction(() => document.querySelectorAll(".browser-header-cell").length === 1);
107 checks.push("竖排标签拖入网页进入双网页分屏;共享一列标签;退出分屏");
108
109 for (let i = 5; i < 35; i++) state.tabs.push({ id: `tab-${i}`, kind: "web", title: `更多网页 ${i}`, url: `https://example.test/${i}` });
110 await page.waitForFunction(() => document.querySelectorAll('.browser-tabs [role="tab"]').length === 35);
111 await page.getByRole("button", { name: "收起侧栏", exact: true }).click();
112 for (const width of [1280, 820, 375]) {
113 await page.setViewportSize({ width, height: 850 });
114 await tabs.getByRole("tab").first().focus();
115 await page.keyboard.press("End");
116 await page.waitForFunction(() => {
117 const list = document.querySelector(".browser-tabs"), selected = list.querySelector('[aria-selected="true"]');
118 const a = list.getBoundingClientRect(), b = selected.getBoundingClientRect();
119 return list.scrollHeight > list.clientHeight && list.scrollWidth <= list.clientWidth + 1 && b.y >= a.y && b.bottom <= a.bottom + 1;
120 });
121 assert.ok((await page.locator(".browser-lane .pane-body").boundingBox()).width > 100);
122 await shot("many-tabs-" + width);
123 }
124 checks.push("35 个标签在 1280/820/375 宽度下纵向滚动,键盘选中末项自动滚入视野,网页仍有可用空间");
125 await page.setViewportSize({ width: 1280, height: 850 });
126 await page.getByRole("button", { name: "展开侧栏", exact: true }).click();
127 await openSettings();
128 await picker.focus();
129 await page.keyboard.press("Enter");
130 await page.keyboard.press("Home");
131 await page.keyboard.press("Enter");
132 assert.equal(await picker.getAttribute("aria-expanded"), "false");
133 await closeSettings();
134 await rail.waitFor({ state: "hidden" });
135 assert.equal(await tabs.getAttribute("aria-orientation"), "horizontal");
136 await page.reload();
137 await page.getByRole("button", { name: "打开浏览器", exact: true }).click();
138 assert.equal(await tabs.getAttribute("aria-orientation"), "horizontal");
139 assert.equal(state.saves.length, 0, "layout preferences must not save or restart the guest environment");
140 assert.deepEqual(errors, []);
141 checks.push("键盘切回横排并刷新保留;未写入环境设置或重启环境;无页面异常");
142 await fs.writeFile(new URL("result.json", output), JSON.stringify({ passed: true, checks, scope: `真实 React DOM 与存储;${engine === "webkit" ? "显示连接使用替身" : "WebSocket/RFB 使用隔离显示服务"};客机网页与控制 API 使用隔离替身,未实测三平台原生应用。` }, null, 2));
143 console.log(JSON.stringify({ passed: true, checks }, null, 2));
144} catch (error) {
145 await shot("failure");
146 throw error;
147} finally {
148 await browser.close();
149 await display.close();
150}