跳到主要内容

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

浏览文件

windows-native-bridge.test.mjs

65 行 · 2.7 KB历史下载
1import test from 'node:test';
2import assert from 'node:assert/strict';
3import { readFile } from 'node:fs/promises';
4import { randomUUID } from 'node:crypto';
5import vm from 'node:vm';
6
7const source = await readFile(new URL('../desktop/windows/MainWindow.cs', import.meta.url), 'utf8');
8const script = source.match(/private const string BridgeScript = """\s*([\s\S]*?)\s*""";/)?.[1];
9assert.ok(script);
10
11function bridge() {
12 let reply, sent;
13 const timers = new Map();
14 const window = {};
15 window.top = window;
16 vm.runInNewContext(script, {
17 window, location: { protocol: 'http:', hostname: 'localhost' }, crypto: { randomUUID },
18 chrome: { webview: {
19 addEventListener: (_, callback) => { reply = callback; },
20 postMessage: value => { sent = value; },
21 } },
22 setTimeout: (callback, delay) => { const id = randomUUID(); timers.set(id, { callback, delay }); return id; },
23 clearTimeout: id => timers.delete(id),
24 });
25 return {
26 call: request => window.webkit.messageHandlers.cage.postMessage(request),
27 reply: value => reply({ data: { id: sent.id, ...value } }),
28 timers,
29 };
30}
31
32test('native file replies remain pending beyond four minutes and through the backend deadline', async () => {
33 for (const action of ['uploadHostFiles', 'chooseUploadFiles', 'clipboardSyncRead', 'clipboardSyncWrite',
34 'saveBrowserDownload', 'browserDownloadAction', 'desktopFileAction', 'exportDiagnostics', 'importTheme', 'exportTheme',
35 'shareLocalProject', 'chooseFolder', 'chooseGuestMedia']) {
36 const fixture = bridge();
37 const pending = fixture.call({ action });
38 const timer = [...fixture.timers.values()][0];
39 assert.ok(timer.delay > 30 * 60 * 1000, action);
40 fixture.reply({ result: { saved: true } });
41 assert.equal((await pending).saved, true, action);
42 assert.equal(fixture.timers.size, 0);
43 }
44});
45
46test('file failure reaches the caller and the following request can succeed', async () => {
47 const fixture = bridge();
48 const first = fixture.call({ action: 'saveBrowserDownload' });
49 fixture.reply({ error: '磁盘空间不足' });
50 await assert.rejects(first, /磁盘空间不足/);
51 const second = fixture.call({ action: 'clipboardSyncRead' });
52 fixture.reply({ result: { text: 'next copy' } });
53 assert.equal((await second).text, 'next copy');
54 assert.equal(fixture.timers.size, 0);
55});
56
57test('ordinary native commands retain their bounded timeout', async () => {
58 const fixture = bridge();
59 const pending = fixture.call({ action: 'accountDevice' });
60 const timer = [...fixture.timers.values()][0];
61 assert.equal(timer.delay, 240_000);
62 timer.callback();
63 await assert.rejects(pending, /原生操作超时/);
64 fixture.reply({ result: true });
65});