跳到主要内容

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

浏览文件

api.test.mjs

157 行 · 5.2 KB历史下载
1import test from "node:test";
2import assert from "node:assert/strict";
3import { normalizeURL } from "./api.js";
4test("address input retains loopback inside guest and rejects active schemes", () => {
5 assert.equal(normalizeURL("localhost:3000"), "http://localhost:3000/");
6 assert.equal(
7 normalizeURL("127.0.0.1:18881/test"),
8 "http://127.0.0.1:18881/test",
9 );
10 assert.equal(normalizeURL("example.com"), "https://example.com/");
11 assert.equal(
12 normalizeURL("hello cage"),
13 "https://www.google.com/search?q=hello%20cage",
14 );
15 assert.throws(() => normalizeURL("javascript:alert(1)"));
16 assert.throws(() => normalizeURL("file:///etc/passwd"));
17});
18
19import { api, native, nativeAvailable } from "./api.js";
20function nativeRuntime(t, fetcher, bridge) {
21 const saved = {
22 fetch: globalThis.fetch,
23 window: globalThis.window,
24 location: globalThis.location,
25 };
26 globalThis.fetch = fetcher;
27 globalThis.window = {
28 webkit: { messageHandlers: { cage: { postMessage: bridge } } },
29 };
30 globalThis.location = new URL("http://localhost:17777/");
31 t.after(() => {
32 for (const [key, value] of Object.entries(saved)) {
33 if (value === undefined) delete globalThis[key];
34 else globalThis[key] = value;
35 }
36 });
37}
38const json = (data, status = 200) =>
39 new Response(JSON.stringify(data), { status });
40test("aborted session opens preserve their cancellation reason and do not replay", async t => {
41 const controller = new AbortController();
42 const reason = new Error("打开会话超时");
43 let calls = 0;
44 nativeRuntime(t, async (_path, options) => {
45 calls++;
46 assert.equal(options.signal, controller.signal);
47 return new Promise((_, reject) => options.signal.addEventListener("abort", () => reject(options.signal.reason), {once:true}));
48 }, () => { throw new Error("unexpected renewal"); });
49 const pending = api("/cli/sessions/test/open", {}, undefined, {}, {signal:controller.signal});
50 controller.abort(reason);
51 await assert.rejects(pending, error => error === reason);
52 assert.equal(calls,1);
53});
54test("Linux native calls survive WebKit's transient messageHandlers objects", async (t) => {
55 const calls = [];
56 nativeRuntime(t, () => { throw Error("unexpected request"); }, () => {});
57 Object.defineProperty(window, "webkit", {
58 get: () => ({ messageHandlers: {} }),
59 });
60 window.cageNativeBridge = {
61 postMessage: async (request) => { calls.push(request); return "clipboard text"; },
62 };
63 assert.equal(nativeAvailable(), true);
64 assert.equal(await native("clipboardRead"), "clipboard text");
65 assert.equal(calls.length, 1);
66 assert.equal(calls[0].action, "clipboardRead");
67});
68test("expired concurrent requests renew once and accept the rejected stop exactly once", async (t) => {
69 let authorized = false,
70 renewals = 0,
71 stops = 0,
72 exchanges = 0,
73 release;
74 const entry = new Promise((resolve) => {
75 release = resolve;
76 });
77 nativeRuntime(
78 t,
79 async (path, options) => {
80 if (path === "/api/session") {
81 assert.equal(JSON.parse(options.body).token, "one-use-test-ticket");
82 authorized = true;
83 exchanges++;
84 return json({});
85 }
86 if (!authorized) return new Response("expired", { status: 401 });
87 if (path === "/api/stop") {
88 stops++;
89 return json({ id: "requested-stop" });
90 }
91 return json({ vm_running: true });
92 },
93 async ({ action }) => {
94 assert.equal(action, "refreshSession");
95 renewals++;
96 return entry;
97 },
98 );
99 const requests = Promise.all([api("/stop", {}), api("/status")]);
100 await new Promise((resolve) => setImmediate(resolve));
101 assert.equal(renewals, 1);
102 release({ url: "http://localhost:17777/#one-use-test-ticket" });
103 const [stop] = await requests;
104 assert.equal(stop.id, "requested-stop");
105 assert.equal(stops, 1);
106 assert.equal(exchanges, 1);
107});
108test("non-authentication failures never replay a mutation", async (t) => {
109 let calls = 0;
110 nativeRuntime(
111 t,
112 async () => {
113 calls++;
114 return new Response("operation failed", { status: 500 });
115 },
116 () => {
117 throw Error("unexpected renewal");
118 },
119 );
120 await assert.rejects(api("/stop", {}), { status: 500 });
121 assert.equal(calls, 1);
122});
123test("a rejected renewed session has a bounded retry", async (t) => {
124 let attempts = 0,
125 renewals = 0;
126 nativeRuntime(
127 t,
128 async (path) => {
129 if (path === "/api/session") return json({});
130 attempts++;
131 return new Response("expired", { status: 401 });
132 },
133 async () => {
134 renewals++;
135 return { url: "http://localhost:17777/#test-ticket" };
136 },
137 );
138 await assert.rejects(api("/stop", {}), { status: 401 });
139 assert.equal(attempts, 2);
140 assert.equal(renewals, 1);
141});
142test("a new control address reconnects without sending a ticket to the old origin", async (t) => {
143 const actions = [];
144 nativeRuntime(
145 t,
146 async (path) => {
147 assert.equal(path, "/api/stop");
148 return new Response("expired", { status: 401 });
149 },
150 async ({ action }) => {
151 actions.push(action);
152 return { url: "http://localhost:17778/#test-ticket" };
153 },
154 );
155 await assert.rejects(api("/stop", {}), /地址已更新/);
156 assert.deepEqual(actions, ["refreshSession", "reconnect"]);
157});