跳到主要内容

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

浏览文件

mobile-cli-live-smoke.mjs

78 行 · 4.3 KB历史下载
1// Started by TestMobileCLIThroughRelayAndSSH. The private one-time launch URL
2// is read from stdin; browser automation uses the real production endpoints.
3import process from "node:process";
4import { mkdir, readFile } from "node:fs/promises";
5import path from "node:path";
6import assert from "node:assert/strict";
7const chunks = [];
8for await (const chunk of process.stdin) chunks.push(chunk);
9const entry = Buffer.concat(chunks).toString("utf8").trim();
10const engine = process.env.CAGE_TEST_MOBILE_BROWSER;
11if (!["chromium", "webkit"].includes(engine)) throw Error("Choose chromium or webkit");
12const playwright = await import(process.env.CAGE_PLAYWRIGHT_MODULE || "playwright");
13const browser = await playwright[engine].launch({headless:true});
14try {
15 const page = await browser.newPage({viewport:{width:390,height:844},deviceScaleFactor:1,isMobile:true,hasTouch:true});
16 page.setDefaultTimeout(18000);
17 // Exercise compatibility with the original independent-session capability.
18 // The shared-terminal user flow has its own real CLI/browser integration test.
19 await page.route("**/api/mobile/cli/projects", async route => {
20 const response = await route.fetch();
21 const value = await response.json();
22 delete value.shared_sessions;
23 await route.fulfill({response, json:value});
24 });
25 await page.route("**/api/mobile/cli/sessions", async route => {
26 if (route.request().method() !== "POST") return route.continue();
27 // Existing headless history is seeded by Go; new production sessions use
28 // the shared path covered by shared-cli-live-smoke.mjs.
29 await route.fulfill({status:200,contentType:"application/json",body:process.env.CAGE_TEST_MOBILE_LEGACY_SESSION});
30 });
31 await page.goto(entry);
32 await page.getByRole("dialog", {name:"工作区侧栏"}).getByRole("button", {name:/ 新建会话$/}).first().click();
33 await page.getByRole("dialog", {name:"工作区侧栏"}).waitFor({state:"hidden"});
34 const message = page.getByRole("textbox", {name:"消息"});
35 await message.fill("initial mobile prompt");
36 await page.getByRole("button", {name:"发送",exact:true}).click();
37 const approve = page.getByRole("button",{name:"允许一次"});
38 await approve.waitFor();
39 assert.match(await page.getByLabel("完整工具参数").innerText(), /mobile-approved\.txt/);
40 await assert.rejects(readFile(process.env.CAGE_TEST_MOBILE_MARKER), {code:"ENOENT"});
41 const out = process.env.CAGE_TEST_MOBILE_CAPTURE;
42 if(out) {
43 await mkdir(out,{recursive:true});
44 await page.screenshot({path:path.join(out,engine+"-cli-approval.png")});
45 }
46 // The pending tool request belongs to the host, so a page reload must
47 // recover it and its original arguments.
48 await page.reload();
49 // Losing both the decision and its immediate status check must not leave
50 // the approval disabled after the next successful read.
51 let offline = false;
52 const sessionRequests = "**/api/mobile/cli/sessions/**";
53 await page.route(sessionRequests, async route => {
54 if (route.request().method() === "POST" && route.request().url().endsWith("/approval")) offline = true;
55 if (offline) await route.abort("internetdisconnected");
56 else await route.continue();
57 });
58 await approve.click();
59 await page.getByRole("alert").getByRole("button", {name:"重新读取"}).waitFor();
60 await page.unroute(sessionRequests);
61 await page.getByText("主机仍在等待这次审批。请核对参数后再次选择。", {exact:true}).waitFor();
62 await assert.rejects(readFile(process.env.CAGE_TEST_MOBILE_MARKER), {code:"ENOENT"});
63 await approve.click();
64 await page.getByText("isolated tool completed", {exact:true}).waitFor();
65 const tools = page.locator(".tool-history");
66 await tools.locator("summary").click();
67 await tools.getByText("Bash", {exact:true}).waitFor();
68 await tools.getByText("已完成", {exact:true}).waitFor();
69 assert.equal(await readFile(process.env.CAGE_TEST_MOBILE_MARKER,"utf8"),"approved");
70 await message.fill("follow-up");
71 await page.getByRole("button", {name:"发送",exact:true}).click();
72 await page.getByText("isolated continuation",{exact:true}).waitFor();
73 if(out) await page.screenshot({path:path.join(out,engine+"-cli-result.png")});
74} catch(error) {
75 throw Error(String(error).replace(/(link-bootstrap)#[A-Za-z0-9_-]+/g,"$1#[redacted]"));
76} finally {
77 await browser.close();
78}