| 1 | |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { spawnSync } from "node:child_process"; |
| 4 | import { mkdtemp, readFile, rm, stat } from "node:fs/promises"; |
| 5 | import { tmpdir } from "node:os"; |
| 6 | import { isAbsolute, join } from "node:path"; |
| 7 | import { setTimeout } from "node:timers/promises"; |
| 8 | const binary = process.argv[2]; |
| 9 | assert.ok(binary && isAbsolute(binary), "Pass an absolute cage-client binary."); |
| 10 | const root = await mkdtemp(join(tmpdir(), "cage-agent-client-")); |
| 11 | const config = join(root, "config.json"); |
| 12 | let pid; |
| 13 | const invoke = (args) => { |
| 14 | const result = spawnSync(binary, args, { |
| 15 | encoding: "utf8", |
| 16 | timeout: 30000, |
| 17 | env: { ...process.env, CLAUDE_CAGE_CONFIG: config }, |
| 18 | }); |
| 19 | assert.ifError(result.error); |
| 20 | assert.equal( |
| 21 | result.status, |
| 22 | 0, |
| 23 | `Client operation failed: ${result.stdout} ${result.stderr}`, |
| 24 | ); |
| 25 | return JSON.parse(result.stdout); |
| 26 | }; |
| 27 | try { |
| 28 | const capabilities = invoke(["agent", "capabilities"]); |
| 29 | assert.ok( |
| 30 | capabilities.data.operations.some((x) => x.name === "client.hosts"), |
| 31 | ); |
| 32 | invoke(["desktop", "--json"]); |
| 33 | pid = JSON.parse(await readFile(join(root, "control.json"), "utf8")).pid; |
| 34 | const context = invoke(["agent", "call", "client.context"]); |
| 35 | assert.equal(context.data.client_only, true); |
| 36 | const doctor = invoke(["agent", "doctor"]); |
| 37 | assert.equal(doctor.data.scope, "client"); |
| 38 | assert.equal(doctor.data.snapshot.client_only, true); |
| 39 | assert.equal(doctor.data.snapshot.connection_id, context.data.connection_id); |
| 40 | const routes = invoke(["agent", "routes"]); |
| 41 | assert.ok(routes.data.includes("POST /api/agent/native")); |
| 42 | const hosts = invoke(["agent", "call", "client.hosts"]); |
| 43 | assert.deepEqual(hosts.data.hosts, []); |
| 44 | console.log( |
| 45 | "PASS: lightweight client exposes the shared Agent protocol, native queue route, exact client context, scope-aware doctor and saved host collection. No remote host or VM was started.", |
| 46 | ); |
| 47 | } finally { |
| 48 | if (pid) { |
| 49 | try { |
| 50 | process.kill(pid, "SIGTERM"); |
| 51 | } catch (error) { |
| 52 | if (error.code !== "ESRCH") throw error; |
| 53 | } |
| 54 | let retired = false; |
| 55 | for (let i = 0; i < 100; i++) { |
| 56 | try { |
| 57 | await stat(join(root, "control.json")); |
| 58 | } catch (error) { |
| 59 | if (error.code === "ENOENT") { |
| 60 | retired = true; |
| 61 | break; |
| 62 | } |
| 63 | throw error; |
| 64 | } |
| 65 | await setTimeout(100); |
| 66 | } |
| 67 | assert.ok( |
| 68 | retired, |
| 69 | "Client controller is still running; preserve its test profile.", |
| 70 | ); |
| 71 | } |
| 72 | await rm(root, { recursive: true, force: true }); |
| 73 | } |