跳到主要内容

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

浏览文件

browser-navigation-smoke.mjs

68 行 · 2.9 KB历史下载
1import assert from "node:assert/strict";
2import { createWorkbench } from "./workbench-fixture.mjs";
3
4const { browser, page, state, errors } = await createWorkbench();
5await page.addInitScript(() => {
6 window.EventSource = class extends EventTarget {
7 constructor() {
8 super();
9 window.navigationEvents = this;
10 queueMicrotask(() => this.onopen?.());
11 }
12 close() {}
13 };
14});
15const back = page.getByRole("button", { name: "后退", exact: true });
16const forward = page.getByRole("button", { name: "前进", exact: true });
17const send = (snapshot) => page.evaluate(snapshot => {
18 window.navigationEvents.onmessage({ data: JSON.stringify(snapshot) });
19}, { tabs: state.tabs, active: state.active, ...snapshot });
20async function navigate(button, path, id) {
21 const request = page.waitForRequest(`**/api/browser/${path}`);
22 await button.click();
23 assert.deepEqual((await request).postDataJSON(), { id });
24}
25try {
26 state.status.ready = state.status.vm_running = true;
27 state.tabs = [
28 { id: "first", title: "First tab", kind: "web", url: "https://example.test/first" },
29 { id: "second", title: "Second tab", kind: "web", url: "https://example.test/second" },
30 ];
31 state.active = "first";
32 await page.reload();
33 await page.getByRole("button", { name: "打开浏览器", exact: true }).click();
34 await send({ back: false, forward: false });
35 assert.equal(await back.isDisabled(), true);
36 assert.equal(await forward.isDisabled(), true);
37 const reads = () => state.requests.filter(item => item.path === "/browser/tabs").length;
38 const before = reads();
39
40 await send({ back: true, forward: false });
41 await navigate(back, "back", "first");
42 await send({ back: false, forward: true });
43 await navigate(forward, "forward", "first");
44
45 // While a new tab is activating, the previous tab's history cannot enable its buttons.
46 await send({ back: true, forward: true });
47 await page.route("**/api/browser/activate", async route => {
48 await new Promise(resolve => setTimeout(resolve, 150));
49 state.active = route.request().postDataJSON().id;
50 await route.fulfill({ json: { ok: true } });
51 });
52 await page.getByRole("tab", { name: "Second tab", exact: true }).click();
53 assert.equal(await back.isDisabled(), true);
54 assert.equal(await forward.isDisabled(), true);
55 await send({ active: "second", back: false, forward: false });
56 assert.equal(await back.isDisabled(), true);
57 await send({ active: "second", back: true, forward: false });
58 await Promise.all([
59 page.waitForResponse("**/api/status"),
60 page.waitForResponse("**/api/terminals"),
61 ]);
62 await navigate(back, "back", "second");
63 assert.equal(reads(), before, "live history updates must work without REST polling");
64 assert.deepEqual(errors, []);
65 console.log("PASS: back/forward state via events, correct action targets, no stale history on tab switch and no REST polling");
66} finally {
67 await browser.close();
68}