跳到主要内容

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

浏览文件

browser-visibility.test.mjs

73 行 · 3.3 KB历史下载
1import assert from "node:assert/strict";
2import { test } from "node:test";
3import { BrowserVisibility } from "./browser-visibility.mjs";
4
5function fixture() {
6 const windows = new Map([[1, { windowState: "fullscreen", left: 0, top: 0, width: 1200, height: 800 }]]);
7 const targets = new Map([["one", 1], ["two", 1]]);
8 const calls = [];
9 let legacyViewer = false;
10 const cdp = async (method, input) => {
11 calls.push({ method, ...input });
12 if (method === "Browser.getWindowForTarget") {
13 const windowId = targets.get(input.targetId);
14 return { windowId, bounds: { ...windows.get(windowId) } };
15 }
16 if (method === "Browser.getWindowBounds") return { bounds: { ...windows.get(input.windowId) } };
17 const current = windows.get(input.windowId);
18 assert.ok(current);
19 if (input.bounds.windowState === "minimized") assert.equal(current.windowState, "normal");
20 Object.assign(current, input.bounds);
21 };
22 const visibility = new BrowserVisibility(cdp, () => [...targets.keys()], async () => !legacyViewer);
23 return { visibility, windows, targets, calls, setLegacyViewer: value => { legacyViewer = value; } };
24}
25
26test("browser visibility restores fullscreen and processes shared windows once", async () => {
27 const f = fixture();
28 await f.visibility.set(false);
29 assert.equal(f.windows.get(1).windowState, "minimized");
30 assert.equal(f.calls.filter(call => call.method === "Browser.setWindowBounds").length, 2);
31 await f.visibility.set(false);
32 assert.equal(f.calls.filter(call => call.method === "Browser.setWindowBounds").length, 2);
33 await f.visibility.set(true);
34 assert.equal(f.windows.get(1).windowState, "fullscreen");
35 assert.equal(f.visibility.saved.size, 0);
36});
37
38test("browser visibility preserves geometry, closed windows and legacy viewers", async () => {
39 const f = fixture();
40 const bounds = { windowState: "normal", left: 40, top: 80, width: 900, height: 600 };
41 f.windows.set(2, { ...bounds }); f.targets.set("popup", 2);
42 f.setLegacyViewer(true);
43 await f.visibility.set(false);
44 assert.equal(f.calls.filter(call => call.method === "Browser.setWindowBounds").length, 0);
45 f.setLegacyViewer(false);
46 await f.visibility.set(false);
47 f.targets.delete("one"); f.targets.delete("two"); f.windows.delete(1);
48 await f.visibility.set(true);
49 assert.deepEqual(f.windows.get(2), bounds);
50 assert.equal(f.visibility.saved.size, 0);
51});
52
53test("a newer browser viewer restores a hide that is already running", async () => {
54 let release, entered;
55 const blocked = new Promise(resolve => { entered = resolve; });
56 const ready = new Promise(resolve => { release = resolve; });
57 let state = "fullscreen";
58 const visibility = new BrowserVisibility(async (method, input) => {
59 if (method === "Target.activateTarget") return {};
60 if (method === "Browser.getWindowForTarget") return { windowId: 1, bounds: { windowState: state } };
61 if (method === "Browser.getWindowBounds") return { bounds: { windowState: state } };
62 if (input.bounds.windowState === "minimized") { entered(); await ready; }
63 state = input.bounds.windowState;
64 }, () => ["one"]);
65 const hidden = visibility.set(false);
66 await blocked;
67 const activated = visibility.activate("one");
68 const shown = visibility.set(true);
69 release();
70 await Promise.all([hidden, activated, shown]);
71 assert.equal(state, "fullscreen");
72 assert.equal(visibility.saved.size, 0);
73});