Files
plugins/packages/agent-console/test/v1/gate.test.ts
T
qingnian108 a431798fe1 agent-console 1.0.0: a page plugin that drives the local Agent CLIs (#535)
Adds Agent Console (@magpie-community/page-agent-console 1.0.0), a community page plugin that drives the Agent CLIs on this computer (Claude Code, Codex, ACP agents) through a local daemon on 127.0.0.1, with its source in packages/agent-console and a registry entry. Claude Code is the user's own `claude` CLI driven over stream-json (daemon/src/providers/claude/cli.ts); no Anthropic SDK is installed, bundled or in the branch's history. Cold install ~45 MB (node-pty ~26 MB), stated in the README and registry summary in en and zh.

By qingnian108 (#535).

What was run at 8e8f910 (mote, macOS, throwaway HOME, no real accounts):
- packages/agent-console: `npm ci --ignore-scripts --workspaces=false`; `node scripts/verify-build.mjs`: all five bundles byte-identical (daemon/dist/main.js 08c95aa3…, app/app.js 9a6911f3…); `vitest run`: 45 files, 254 passed, 17 skipped; `npm run check` clean.
- Real Claude Code 2.1.316 (npm @anthropic-ai/claude-code, sandbox HOME/CLAUDE_CONFIG_DIR, local fake Messages API): `AC_REAL_CLAUDE=… vitest run --config vitest.real.config.ts test/real/claude-cli.real.test.ts`: 4 of 4 (initialize lists 51 slash commands, streaming turn, Bash permission approved and denied, interrupt, --resume with set_model).
- Probe: the driver's exact argv with `--permission-mode bypassPermissions --allow-dangerously-skip-permissions`: the tool runs with no can_use_tool request.
- Repo root: `HOME=$(mktemp -d) bun test`: 4006 tests, 0 fail; `bun scripts/check.mjs` exit 0.
- Install as magpie does: `npm pack --ignore-scripts` (552 KB) then `bun add --ignore-scripts`: 45 MB node_modules, no @anthropic-ai/*, index.mjs imports.
- Read every changed line since 41c7a94: argv values limited to SAFE_ARG tokens (no cmd.exe metacharacters reach the shim path), no new network hosts, no install scripts, no eval; no commit in main..8e8f910 contains claude-agent-sdk.

Not tried: magpie's desktop window loading the page; a Windows .cmd/.ps1 claude shim end to end.
2026-10-31 20:11:22 +08:00

98 lines
4.0 KiB
TypeScript

// Plugin gate (lib/gate.mjs): driven by a fake ensureDaemon, no real daemon is started.
import { describe, expect, it } from "vitest";
import { join, resolve } from "node:path";
import { pathToFileURL } from "node:url";
import { freePort, request } from "./helpers-net.js";
const gateUrl = pathToFileURL(join(resolve(__dirname, "..", ".."), "lib", "gate.mjs")).href;
describe("gate (lib/gate.mjs)", () => {
it("checks Host, refuses any Origin, requires the gate key, and serializes opens", async () => {
const { createGate } = await import(/* @vite-ignore */ gateUrl);
const port = await freePort();
const daemonPort = await freePort();
const key = "c".repeat(64);
const token = "a".repeat(64);
let calls = 0;
const gate = createGate({
port,
key,
token,
ensureDaemon: async () => {
calls++;
await new Promise((r) => setTimeout(r, 50));
return { port: daemonPort };
},
});
await gate.listen();
try {
expect((await request(port, `/open?k=${key}`, { host: `evil.example:${port}` })).status).toBe(421);
expect((await request(port, `/open?k=${key}`, { host: `127.0.0.1:${port + 1}` })).status).toBe(421);
expect((await request(port, `/open?k=${key}`, { origin: "null" })).status).toBe(403);
expect((await request(port, `/open?k=${key}`, { origin: "https://evil.example" })).status).toBe(403);
expect((await request(port, "/")).status).toBe(404);
expect((await request(port, "/open")).status).toBe(403);
expect((await request(port, "/open?k=wrong")).status).toBe(403);
// the daemon token is not a gate key, in either parameter
expect((await request(port, `/open?k=${token}`)).status).toBe(403);
expect((await request(port, `/open?t=${token}`)).status).toBe(403);
expect((await request(port, `/open?t=${key}`)).status).toBe(403);
expect(calls).toBe(0);
const results = await Promise.all([
request(port, `/open?k=${key}&theme=dark`),
request(port, `/open?k=${key}&theme=dark`),
]);
expect(calls).toBe(1);
for (const result of results) {
expect(result.status).toBe(302);
expect(result.headers.location).toBe(`http://127.0.0.1:${daemonPort}/?theme=dark#t=${token}`);
expect(result.headers["cache-control"]).toBe("no-store");
expect(result.headers["x-content-type-options"]).toBe("nosniff");
expect(result.headers["referrer-policy"]).toBe("no-referrer");
expect(result.headers["access-control-allow-origin"]).toBeUndefined();
expect(result.headers.location).not.toContain(key);
}
} finally {
await gate.close();
}
});
it("answers 503 when the daemon cannot start, and recovers on the next open", async () => {
const { createGate } = await import(/* @vite-ignore */ gateUrl);
const port = await freePort();
const key = "d".repeat(64);
const token = "b".repeat(64);
let fail = true;
const gate = createGate({
port,
key,
token,
ensureDaemon: async () => {
if (fail) throw new Error("spawn ENOENT");
return { port: 45678 };
},
});
await gate.listen();
try {
const first = await request(port, `/open?k=${key}`);
expect(first.status).toBe(503);
expect(first.body).not.toContain(token);
fail = false;
const second = await request(port, `/open?k=${key}`);
expect(second.status).toBe(302);
} finally {
await gate.close();
}
});
it("refuses to start with a missing gate key or one equal to the daemon token", async () => {
const { createGate } = await import(/* @vite-ignore */ gateUrl);
const ensureDaemon = async () => ({ port: 1 });
const token = "e".repeat(64);
expect(() => createGate({ port: 1, token, ensureDaemon })).toThrow();
expect(() => createGate({ port: 1, key: token, token, ensureDaemon })).toThrow();
expect(() => createGate({ port: 1, key: "f".repeat(64), token, ensureDaemon })).not.toThrow();
});
});