Files
plugins/packages/agent-console/test/v1/terminal-fallback.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

81 lines
2.9 KiB
TypeScript

// node-pty is a native module. magpie installs plugins with `bun add` and never runs install scripts, so on a
// platform its tarball has no prebuild for (e.g. Linux musl, 32-bit ARM) there is no binary at all, and a broken
// or ABI-mismatched binary throws on load. Either way only the terminal panel may be affected: it falls back to a
// plain shell over pipes (`simple: true`, which the panel shows as "Simple terminal"), and the daemon keeps running.
import { afterEach, describe, expect, it, vi } from "vitest";
import { mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
type Sent = { index: number; text: string };
function fakeGateway() {
const sent: Sent[] = [];
const events: unknown[] = [];
const gw = {
onTerminalInput: undefined as undefined | ((conn: { subs: Set<string> }, index: number, op: number, payload: Buffer) => void),
publishTerminal(index: number, payload: Buffer) {
sent.push({ index, text: payload.toString("utf8") });
},
publish(_ch: string, data: unknown) {
events.push(data);
},
};
return { gw, sent, events };
}
async function waitFor(check: () => boolean, ms = 8000) {
const end = Date.now() + ms;
while (!check()) {
if (Date.now() > end) throw new Error("timed out");
await new Promise((r) => setTimeout(r, 50));
}
}
async function runFallback() {
const { TerminalService } = await import("../../daemon/src/terminal/service.js");
const { gw, sent } = fakeGateway();
const svc = new TerminalService(gw as any);
const cwd = mkdtempSync(join(tmpdir(), "ac-term-"));
try {
const made = await svc.create("p1", cwd, 80, 24);
expect("error" in made).toBe(false);
if ("error" in made) return;
expect(made.simple).toBe(true);
expect(svc.list("p1")[0]).toMatchObject({ simple: true, alive: true });
expect(svc.attach(made.id)).toMatchObject({ simple: true });
// The plain shell still runs commands
const conn = { subs: new Set([`terminal:${made.index}`]) };
gw.onTerminalInput?.(conn, made.index, 2, Buffer.from("echo fallback-$((40+2))\n"));
await waitFor(() => sent.some((s) => s.index === made.index && s.text.includes("fallback-42")));
svc.killAll();
} finally {
rmSync(cwd, { recursive: true, force: true });
}
}
describe.skipIf(process.platform === "win32")("terminal without node-pty", () => {
afterEach(() => {
vi.doUnmock("node-pty");
vi.resetModules();
});
it("falls back to a plain shell when node-pty cannot be loaded", async () => {
vi.resetModules();
vi.doMock("node-pty", () => {
throw new Error("simulated: no prebuilt pty.node for this platform");
});
await runFallback();
});
it("falls back to a plain shell when node-pty loads but cannot spawn", async () => {
vi.resetModules();
vi.doMock("node-pty", () => ({
spawn() {
throw new Error("simulated: posix_spawnp failed");
},
}));
await runFallback();
});
});