跳到主要内容

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

浏览文件

macos-window-input.test.mjs

153 行 · 6.1 KB历史下载
1import test from "node:test";
2import assert from "node:assert/strict";
3import { createMacWindowInput } from "./macos-window-input.mjs";
4
5function fixture() {
6 let current = { pid: 23, window: 70, geometry: "geometry-1" }, visible = true;
7 const calls = [], errors = [], captured = new Set(), eventWindow = new EventTarget();
8 const element = Object.assign(new EventTarget(), {
9 getBoundingClientRect: () => ({ left: 20, top: 30, width: 400, height: 300 }),
10 setPointerCapture: id => captured.add(id),
11 hasPointerCapture: id => captured.has(id),
12 releasePointerCapture(id) {
13 captured.delete(id);
14 element.dispatchEvent(new Event("lostpointercapture"));
15 },
16 });
17 const input = createMacWindowInput({
18 element, frame: () => current, active: () => visible,
19 request: async (action, fields) => { calls.push({ action, ...fields }); return { ok: true }; },
20 onError: error => errors.push(error), eventWindow,
21 });
22 input.setKeyboardTarget({ focus() {}, classList: { add() {} } });
23 const pointer = (type, fields = {}) => {
24 const event = new Event(type, { cancelable: true });
25 Object.assign(event, {
26 pointerId: 1, button: 0, buttons: type === "pointerup" ? 0 : 1,
27 clientX: 120, clientY: 105, detail: 1,
28 shiftKey: false, ctrlKey: false, altKey: false, metaKey: false, ...fields,
29 });
30 element.dispatchEvent(event);
31 return event;
32 };
33 return { input, calls, errors, captured, eventWindow, element, pointer,
34 changeTarget: () => { current = { ...current, geometry: "geometry-2" }; },
35 hide: () => { visible = false; },
36 };
37}
38
39test("native window drag preserves down/move/up and coalesces only adjacent moves", async () => {
40 const f = fixture();
41 let release;
42 const gate = f.input.run(() => new Promise(resolve => { release = resolve; }));
43 await Promise.resolve();
44 try {
45 assert.equal(f.pointer("pointerdown").defaultPrevented, true);
46 f.pointer("pointermove", { clientX: 180 });
47 f.pointer("pointermove", { clientX: 300 });
48 f.pointer("pointerdown", { pointerId: 2 });
49 f.pointer("pointerup", { clientX: 900, clientY: -40 });
50 release();
51 await gate;
52 await f.input.run(() => {});
53 assert.deepEqual(f.calls.map(call => call.phase), ["down", "move", "up"]);
54 assert.deepEqual(f.calls.map(call => call.x), [0.25, 0.7, 1 - Number.EPSILON]);
55 assert.equal(f.calls[2].y, 0);
56 assert(f.calls.every(call => call.pid === 23 && call.window === 70 && call.geometry === "geometry-1"));
57 assert.equal(f.captured.size, 0);
58 assert.deepEqual(f.errors, []);
59 } finally { f.input.dispose(); }
60});
61
62test("native window forwards right button, double click and horizontal scroll", async () => {
63 const f = fixture();
64 try {
65 f.pointer("pointerdown", { button: 2, buttons: 2 });
66 f.pointer("pointerup", { button: 2 });
67 f.pointer("pointerdown", { detail: 2 });
68 f.pointer("pointerup", { detail: 2 });
69 f.pointer("wheel", { deltaX: 54, deltaY: -2000 });
70 await f.input.run(() => {});
71 assert.deepEqual(f.calls.slice(0, 4).map(call => [call.phase, call.button, call.clicks]), [
72 ["down", 2, 1], ["up", 2, 1], ["down", 0, 2], ["up", 0, 2],
73 ]);
74 assert.equal(f.calls[4].deltaX, 54);
75 assert.equal(f.calls[4].delta, -1200);
76 assert.equal(f.pointer("contextmenu").defaultPrevented, true);
77 } finally { f.input.dispose(); }
78});
79
80test("cancelled pointer capture resets held input before new keys", async () => {
81 const f = fixture();
82 try {
83 f.input.sendKey(0xffeb, "MetaLeft", true);
84 f.pointer("pointerdown");
85 f.pointer("pointercancel");
86 f.input.sendKey(97, "KeyA", true);
87 await f.input.run(() => {});
88 assert.equal(f.captured.size, 0);
89 assert.deepEqual(f.calls.map(call => call.action), ["key", "pointer", "input-reset", "key"]);
90 assert.equal(f.calls.at(-1).command, false);
91 } finally { f.input.dispose(); }
92});
93
94test("queued native paste sends its chord before a following Enter", async () => {
95 const f = fixture();
96 let release;
97 const clipboard = new Promise(resolve => { release = resolve; });
98 try {
99 const paste = f.input.runKeyAction(async sendKey => {
100 await clipboard;
101 for (const key of [[0xffeb, "MetaLeft", true], [118, "KeyV", true], [118, "KeyV", false], [0xffeb, "MetaLeft", false]])
102 await sendKey(...key);
103 });
104 f.input.sendKey(0xff0d, "Enter", true);
105 f.input.sendKey(0xff0d, "Enter", false);
106 await Promise.resolve();
107 assert.deepEqual(f.calls, []);
108 release();
109 await paste;
110 await f.input.run(() => {});
111 assert.deepEqual(f.calls.map(call => [call.code, call.down]), [
112 ["MetaLeft", true], ["KeyV", true], ["KeyV", false], ["MetaLeft", false], ["Enter", true], ["Enter", false],
113 ]);
114 } finally { f.input.dispose(); }
115});
116
117test("a failed paste cancels already queued Enter and recovers for new input", async () => {
118 const f = fixture();
119 let fail;
120 const clipboard = new Promise((_, reject) => { fail = reject; });
121 try {
122 const paste = f.input.runKeyAction(async () => { await clipboard; });
123 f.input.sendKey(0xff0d, "Enter", true);
124 const drained = f.input.run(() => {});
125 fail(new Error("fixture clipboard failed"));
126 await assert.rejects(paste, /fixture clipboard failed/);
127 await assert.rejects(drained, /fixture clipboard failed/);
128 assert.deepEqual(f.calls.map(call => call.action), ["input-reset"]);
129 f.input.sendKey(97, "KeyA", true);
130 await f.input.run(() => {});
131 assert.equal(f.calls.at(-1).code, "KeyA");
132 } finally { f.input.dispose(); }
133});
134
135test("a frame change rejects queued input and disposal releases only its connection", async () => {
136 const f = fixture();
137 let release;
138 const gate = f.input.run(() => new Promise(resolve => { release = resolve; }));
139 await Promise.resolve();
140 f.input.sendKey(97, "KeyA", true);
141 const drained = f.input.run(() => {});
142 f.changeTarget();
143 release();
144 await gate;
145 await assert.rejects(drained, /输入窗口已变化/);
146 assert.deepEqual(f.calls.map(call => call.action), ["input-reset"]);
147 f.input.dispose();
148 const count = f.calls.length;
149 f.pointer("pointerdown");
150 f.eventWindow.dispatchEvent(new Event("blur"));
151 await assert.rejects(f.input.run(() => {}), /连接已关闭/);
152 assert.equal(f.calls.length, count);
153});