跳到主要内容

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

浏览文件

browser-menu-smoke.mjs

99 行 · 6.2 KB历史下载
1import assert from "node:assert/strict";
2import fs from "node:fs/promises";
3import { createWorkbench } from "./workbench-fixture.mjs";
4
5// Isolated UI data; no running environment, credentials or personal files.
6const { browser, page, state, errors } = await createWorkbench();
7page.setDefaultTimeout(10000);
8const output = process.env.CAGE_MENU_CAPTURE || `/tmp/cage-browser-menu-${browser.browserType().name()}`;
9await fs.mkdir(output, { recursive: true });
10await page.route("**/api/vault/browser", route => route.fulfill({ json: {
11 available: true, entries: [], candidates: [], origin: "https://example.test",
12} }));
13try {
14 state.status.ready = state.status.vm_running = true;
15 state.tabs = [{ id: "menu-web", kind: "web", title: "测试网页", url: "https://example.test" }];
16 state.active = "menu-web";
17 await page.reload();
18 await page.getByRole("button", { name: "打开浏览器", exact: true }).click();
19 await page.getByRole("button", { name: "收起侧栏", exact: true }).click();
20 const trigger = page.getByRole("button", { name: "浏览器菜单", exact: true });
21 const menu = page.getByRole("menu", { name: "浏览器菜单", exact: true });
22 const header = page.locator(".browser-chrome");
23 const star = header.getByRole("button", { name: "收藏此页", exact: true });
24 const folder = header.getByRole("button", { name: "收藏夹", exact: true });
25 for (const width of [1280, 375]) {
26 await page.setViewportSize({ width, height: 850 });
27 const address = await page.locator(".address-bar").boundingBox();
28 const starBox = await star.boundingBox(), folderBox = await folder.boundingBox(), triggerBox = await trigger.boundingBox();
29 assert.ok(address.x + address.width <= starBox.x && starBox.x + starBox.width <= folderBox.x && folderBox.x + folderBox.width <= triggerBox.x, "favorites stay beside the address bar without overlapping");
30 assert.ok(triggerBox.x + triggerBox.width <= width, "toolbar actions stay within the viewport");
31 await page.screenshot({ path: `${output}/toolbar-${width}.png` });
32 const closed = await header.boundingBox();
33 await trigger.focus();
34 await trigger.press("ArrowDown");
35 await menu.waitFor();
36 const opened = await header.boundingBox(), box = await menu.boundingBox();
37 assert.ok(Math.abs(opened.height - closed.height) < 1, "menu does not change the header height");
38 const body = await page.locator(".browser-lane .pane-body").boundingBox();
39 assert.ok(box.y >= body.y - 1 && box.y < body.y + 10, "menu overlays the browser content below the toolbar");
40 assert.ok(box.width <= 240 && box.x >= 0 && box.x + box.width <= width, "menu stays compact and within the viewport");
41 const items = menu.getByRole("menuitem");
42 const required = ["下载", "密码"];
43 assert.deepEqual((await items.allTextContents()).filter(text => required.includes(text)), required);
44 assert.equal(await menu.getByRole("menuitem", { name: /收藏/ }).count(), 0);
45 for (let i = 1; i < await items.count(); i++) assert.ok((await items.nth(i).boundingBox()).y > (await items.nth(i - 1).boundingBox()).y);
46 assert.equal(await items.first().evaluate(el => el === document.activeElement), true);
47 await page.keyboard.press("ArrowDown");
48 assert.equal(await items.nth(1).evaluate(el => el === document.activeElement), true);
49 await page.keyboard.press("End");
50 assert.equal(await menu.getByRole("menuitem", { name: "设置", exact: true }).evaluate(el => el === document.activeElement), true);
51 await menu.getByRole("menuitem", { name: "密码", exact: true }).focus();
52 await page.keyboard.press("Enter");
53 const picker = page.getByRole("region", { name: "当前网站的密码" });
54 await picker.waitFor();
55 await picker.getByText("此网站尚未保存账号密码。", { exact: true }).waitFor();
56 await page.keyboard.press("Escape");
57 await picker.waitFor({ state: "hidden" });
58 await page.screenshot({ path: `${output}/menu-${width}.png` });
59 await page.keyboard.press("Escape");
60 await menu.waitFor({ state: "hidden" });
61 assert.equal(await trigger.evaluate(el => el === document.activeElement), true);
62 assert.ok(Math.abs((await header.boundingBox()).height - closed.height) < 1, "closing restores the original layout");
63 await trigger.click();
64 await page.getByRole("textbox", { name: "网页地址或搜索" }).click();
65 await menu.waitFor({ state: "hidden" });
66 }
67 await star.click();
68 const editor = page.getByRole("dialog", { name: "添加收藏", exact: true });
69 await editor.waitFor();
70 await menu.waitFor({ state: "hidden" });
71 await editor.getByRole("button", { name: "取消", exact: true }).click();
72 await editor.waitFor({ state: "hidden" });
73 assert.equal(await star.evaluate(el => el === document.activeElement), true);
74 await folder.focus();
75 await folder.press("Enter");
76 const favorites = page.getByRole("dialog", { name: "收藏夹", exact: true });
77 await favorites.waitFor();
78 await menu.waitFor({ state: "hidden" });
79 await favorites.getByRole("button", { name: "关闭收藏夹", exact: true }).click();
80 assert.equal(await folder.evaluate(el => el === document.activeElement), true);
81 await trigger.click();
82 await menu.getByRole("menuitem", { name: "浏览器下载", exact: true }).click();
83 const downloads = page.getByRole("dialog", { name: "下载文件", exact: true });
84 await downloads.waitFor();
85 await menu.waitFor({ state: "hidden" });
86 await downloads.getByRole("button", { name: "关闭面板", exact: true }).click();
87 await downloads.waitFor({ state: "hidden" });
88 assert.equal(await trigger.evaluate(el => el === document.activeElement), true);
89 await page.keyboard.press("Meta+d");
90 await editor.waitFor();
91 await editor.getByRole("button", { name: "取消", exact: true }).click();
92 await page.keyboard.press("Meta+Shift+o");
93 await favorites.waitFor();
94 await page.keyboard.press("Escape");
95 await favorites.waitFor({ state: "hidden" });
96 assert.equal(await folder.evaluate(el => el === document.activeElement), true);
97 assert.deepEqual(errors, []);
98 console.log(`PASS: favorites toolbar at 1280/375, direct and keyboard access, focus restoration, browser menu, downloads, passwords and favorites shortcuts. Screenshots: ${output}`);
99} finally { await browser.close(); }