跳到主要内容

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

浏览文件

client-context.test.mjs

181 行 · 8.2 KB历史下载
1import test from "node:test";
2import assert from "node:assert/strict";
3import { api, native } from "./api.js";
4import { clientKeyboard, clientStorageKey, runtimeURL, setClientContext } from "./client-context.mjs";
5
6function environment(t) {
7 const saved = { fetch: globalThis.fetch, window: globalThis.window, location: globalThis.location };
8 globalThis.location = new URL("http://localhost:17777/");
9 globalThis.window = { webkit: { messageHandlers: { cage: { postMessage: async () => true } } } };
10 setClientContext({ mode: "remote", connection_id: "first", instance_id: "instance-one" });
11 t.after(() => {
12 setClientContext({ mode: "local", connection_id: "local" });
13 for (const [key, value] of Object.entries(saved)) {
14 if (value === undefined) delete globalThis[key]; else globalThis[key] = value;
15 }
16 });
17}
18
19test("an old API result cannot continue an action in a new remote environment", async (t) => {
20 environment(t);
21 let release;
22 globalThis.fetch = (path, options) => {
23 assert.equal(path, "/api/input/files/create");
24 assert.equal(options.headers["X-Cage-Connection"], "first");
25 return new Promise((resolve) => { release = resolve; });
26 };
27 const pending = api("/input/files/create", { target: "browser" });
28 setClientContext({ mode: "remote", connection_id: "second", instance_id: "instance-two" });
29 release(new Response(JSON.stringify({ id: "old-upload" })));
30 await assert.rejects(pending, (error) => error.code === "connection_changed");
31});
32
33test("a native picker finishing after a host switch cannot yield an upload handle", async (t) => {
34 environment(t);
35 let release;
36 window.webkit.messageHandlers.cage.postMessage = (request) => {
37 assert.equal(request.connection_id, "first");
38 return new Promise((resolve) => { release = resolve; });
39 };
40 const pending = native("chooseUploadFiles", { multiple: true });
41 setClientContext({ mode: "remote", connection_id: "second" });
42 release({ fileSelection: "old-file-handle", count: 1 });
43 await assert.rejects(pending, (error) => error.code === "connection_changed");
44});
45
46test("quitting a remote client never suspends the cloud account", async (t) => {
47 environment(t);
48 globalThis.fetch = () => { throw new Error("quit must not call a runtime API"); };
49 let request;
50 window.webkit.messageHandlers.cage.postMessage = async (value) => { request = value; return true; };
51 await native("quitNow");
52 assert.deepEqual(request, { action: "quitNow", connection_id: "first" });
53});
54
55const localGateway = (state) => ({
56 mode: "local", desktop_gateway: true, connection_id: "local-view",
57 connection: { state, error_code: state === "failed" ? "connection_lost" : "" },
58});
59
60test("quit still reaches the desktop after the selected CLI controller exits", async (t) => {
61 environment(t);
62 setClientContext(localGateway("connected"));
63 const calls = [];
64 let status;
65 globalThis.fetch = async (path) => {
66 calls.push(path);
67 if (path === "/api/client/context") return Response.json(localGateway("failed"));
68 return new Response("connection unavailable", { status });
69 };
70 window.webkit.messageHandlers.cage.postMessage = ({ action }) => calls.push(action);
71 for (status of [502, 409]) {
72 calls.length = 0;
73 await native("quitNow");
74 assert.deepEqual(calls, ["/api/account/suspend", "/api/client/context", "quitNow"]);
75 }
76 calls.length = 0;
77 setClientContext(localGateway("failed"));
78 await native("quitNow");
79 assert.deepEqual(calls, ["quitNow"]);
80});
81
82test("a disconnected view from another generation cannot authorize a pending quit", async (t) => {
83 environment(t);
84 setClientContext(localGateway("connected"));
85 globalThis.fetch = async (path) => path === "/api/client/context"
86 ? Response.json({ ...localGateway("failed"), connection_id: "replacement" })
87 : new Response("view changed", { status: 409 });
88 window.webkit.messageHandlers.cage.postMessage = () => assert.fail("stale quit");
89 await assert.rejects(native("quitNow"), { status: 409 });
90});
91
92test("quit still reaches the desktop when the CLI HTTP listener is gone", async (t) => {
93 environment(t);
94 setClientContext({ mode: "local", connection_id: "local" });
95 globalThis.fetch = async (path) => {
96 if (path === "/api/diagnostics/events") return Response.json({});
97 assert.equal(path, "/api/account/suspend");
98 throw new TypeError("Failed to fetch");
99 };
100 const actions = [];
101 window.webkit.messageHandlers.cage.postMessage = ({ action }) => actions.push(action);
102 await native("quitNow");
103 assert.deepEqual(actions, ["quitNow"]);
104});
105
106test("an offline quit cannot cross a connection change during account suspension", async (t) => {
107 environment(t);
108 setClientContext(localGateway("connected"));
109 let failRequest;
110 globalThis.fetch = (path) => {
111 if (path === "/api/diagnostics/events") return Promise.resolve(Response.json({}));
112 return new Promise((resolve, reject) => { failRequest = reject; });
113 };
114 window.webkit.messageHandlers.cage.postMessage = () => assert.fail("stale quit");
115 const quitting = native("quitNow");
116 setClientContext({ ...localGateway("connected"), connection_id: "replacement" });
117 failRequest(new TypeError("Failed to fetch"));
118 await assert.rejects(quitting, { code: "connection_changed" });
119});
120
121test("a reachable local controller must lock or report already locked before quit", async (t) => {
122 environment(t);
123 setClientContext(localGateway("connected"));
124 window.dispatchEvent = () => {};
125 const calls = [];
126 let status = 500;
127 globalThis.fetch = async (path) => {
128 calls.push(path);
129 if (path === "/api/client/context") return Response.json(localGateway("connected"));
130 return Response.json({}, { status });
131 };
132 window.webkit.messageHandlers.cage.postMessage = ({ action }) => calls.push(action);
133 for (status of [500, 409, 502]) {
134 await assert.rejects(native("quitNow"), { status });
135 assert.ok(!calls.includes("quitNow"));
136 }
137 for (status of [200, 423]) {
138 calls.length = 0;
139 await native("quitNow");
140 assert.deepEqual(calls, ["/api/account/suspend", "quitNow"]);
141 }
142});
143
144test("an account device operation retains its host generation and rejects a late result", async (t) => {
145 environment(t);
146 let release;
147 window.webkit.messageHandlers.cage.postMessage = (request) => {
148 assert.deepEqual(request, { action: "accountDevice", operation: "unlock", kind: "automatic", connection_id: "first" });
149 return new Promise((resolve) => { release = resolve; });
150 };
151 const pending = native("accountDevice", { operation: "unlock", kind: "automatic" });
152 setClientContext({ mode: "remote", connection_id: "second" });
153 release({ unlocked: true });
154 await assert.rejects(pending, (error) => error.code === "connection_changed");
155});
156
157test("stream URLs carry the generation while layout follows the persistent instance", (t) => {
158 environment(t);
159 assert.equal(runtimeURL("/browser/events?after=1"), "/api/browser/events?after=1&connection=first");
160 const key = clientStorageKey("cage-tab-layout");
161 setClientContext({ mode: "remote", connection_id: "reconnected", instance_id: "instance-one" });
162 assert.equal(clientStorageKey("cage-tab-layout"), key);
163 setClientContext({ mode: "remote", connection_id: "other", instance_id: "instance-two" });
164 assert.notEqual(clientStorageKey("cage-tab-layout"), key);
165});
166
167test("remote keyboard defaults follow the client and a saved choice does not alter server settings", (t) => {
168 environment(t);
169 const previous = globalThis.localStorage;
170 const values = new Map();
171 globalThis.localStorage = { getItem: (key) => values.get(key) ?? null };
172 t.after(() => { if (previous === undefined) delete globalThis.localStorage; else globalThis.localStorage = previous; });
173 const mac = { mode: "remote", client_platform: "darwin", instance_id: "linux-server" };
174 const windows = { mode: "remote", client_platform: "windows", instance_id: "linux-server" };
175 assert.equal(clientKeyboard("native", mac), "mac");
176 assert.equal(clientKeyboard("mac", windows), "native");
177 values.set(clientStorageKey("cage-keyboard", mac), "native");
178 assert.equal(clientKeyboard("mac", mac), "native");
179 assert.equal(clientKeyboard("native", { ...mac, instance_id: "other-server" }), "mac");
180 assert.equal(clientKeyboard("native", { mode: "local", client_platform: "darwin" }), "native");
181});