跳到主要内容

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

浏览文件

audio-installed-smoke.mjs

66 行 · 3.8 KB历史下载
1// Explicitly target an unlocked local test installation and its existing VM.
2// This emits a short test tone; it does not read browser content or credentials.
3import assert from "node:assert/strict";
4import { createRequire } from "node:module";
5import { spawn, execFileSync } from "node:child_process";
6const require = createRequire(import.meta.url);
7const { chromium } = require(process.env.CAGE_PLAYWRIGHT_MODULE || "playwright");
8const bundle = process.argv[2];
9for (const key of ["CAGE_CONFIG", "CAGE_SSH_CONFIG", "CAGE_SSH_TARGET"]) assert.ok(process.env[key], `set ${key}`);
10assert.ok(bundle?.startsWith("/") && bundle.endsWith(".app"));
11const ssh = ["-q", "-F", process.env.CAGE_SSH_CONFIG, "-o", "BatchMode=yes", "-o", "ClearAllForwardings=yes", "-o", "ControlPath=none", "-o", "ConnectTimeout=3", process.env.CAGE_SSH_TARGET, "--"];
12const capturePIDs = () => execFileSync("ssh", [...ssh, "pgrep -x parec || true"], { encoding: "utf8" }).trim().split(/\s+/).filter(Boolean);
13const before = new Set(capturePIDs());
14const entry = JSON.parse(execFileSync(bundle + "/Contents/Resources/cage", ["desktop", "--json", "--existing"], { encoding: "utf8", env: { ...process.env, CLAUDE_CAGE_CONFIG: process.env.CAGE_CONFIG } }));
15const browser = await chromium.launch({ headless: true });
16const page = await browser.newPage({ viewport: { width: 1280, height: 850 } });
17let play;
18const errors = [];
19page.on("pageerror", e => errors.push(e.message));
20await page.addInitScript(() => {
21 const Original = window.AudioContext;
22 window.AudioContext = class extends Original {
23 constructor(...args) { super(...args); window.testAudioContext = this; }
24 createGain() { const gain = super.createGain(); window.testAudioGain = gain; return gain; }
25 };
26});
27try {
28 await page.goto(entry.url);
29 const volume = page.getByRole("slider", { name: "环境音量" });
30 await page.getByRole("button", { name: "打开音量控制" }).click();
31 await volume.press("End");
32 await page.waitForFunction(() => window.testAudioGain && window.testAudioContext.state === "running");
33 await page.evaluate(() => {
34 window.testAudioAnalyser = window.testAudioContext.createAnalyser();
35 window.testAudioAnalyser.fftSize = 2048;
36 window.testAudioGain.connect(window.testAudioAnalyser);
37 });
38 const packet = Buffer.alloc(48000 * 4 * 3);
39 for (let i = 0; i < 48000 * 3; i++) {
40 const value = Math.round(Math.sin(i * 2 * Math.PI * 880 / 48000) * 8000);
41 packet.writeInt16LE(value, i * 4); packet.writeInt16LE(value, i * 4 + 2);
42 }
43 play = spawn("ssh", [...ssh, "sudo -u cage-browser /usr/bin/pacat --server=unix:/run/cage-audio/native --playback --raw --format=s16le --rate=48000 --channels=2 --latency-msec=20"]);
44 const completed = new Promise((resolve, reject) => { play.on("error", reject); play.on("close", code => code === 0 ? resolve() : reject(new Error(`guest playback exited ${code}`))); });
45 play.stdin.end(packet);
46 const measured = await page.waitForFunction(() => {
47 const samples = new Float32Array(2048);
48 window.testAudioAnalyser.getFloatTimeDomainData(samples);
49 const rms = Math.sqrt(samples.reduce((sum, v) => sum + v * v, 0) / samples.length);
50 return rms > .14 && rms < .20 ? rms : false;
51 }, null, { timeout: 10000 });
52 console.log(JSON.stringify({ installedAudio: "PASS", RMS: await measured.jsonValue(), chain: "guest application → PulseAudio → SSH → authenticated WebSocket → production AudioWorklet" }));
53 await completed;
54 assert.deepEqual(errors, []);
55} finally {
56 play?.kill();
57 await browser.close();
58}
59let remaining = [];
60for (let i = 0; i < 40; i++) {
61 remaining = capturePIDs().filter(pid => !before.has(pid));
62 if (!remaining.length) break;
63 await new Promise(r => setTimeout(r, 100));
64}
65assert.deepEqual(remaining, [], "closing the player left a capture process");
66console.log("PASS: closing the production viewer stops guest capture");