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

99 lines
3.5 KiB
TypeScript

// GitService tests (§5.8 + §11.1): git init in a temp directory → status/diff/discard, untracked → trash/discarded
import { describe, it, expect, beforeAll, afterAll } from "vitest";
import { mkdtempSync, rmSync, writeFileSync, existsSync, readFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { execFileSync } from "node:child_process";
import { GitService } from "../../daemon/src/git/service.js";
let dir = "";
let dataDir = "";
const git = new GitService();
beforeAll(() => {
dir = mkdtempSync(join(tmpdir(), "ac-git-"));
dataDir = join(dir, "data");
const g = (args: string[]) => execFileSync("git", args, { cwd: dir, encoding: "utf8", windowsHide: true });
g(["init"]);
g(["config", "user.email", "t@t"]);
g(["config", "user.name", "t"]);
writeFileSync(join(dir, "a.txt"), "line1\nline2\n");
g(["add", "a.txt"]);
g(["commit", "-m", "init"]);
});
afterAll(() => {
rmSync(dir, { recursive: true, force: true });
});
describe("git.status", () => {
it("not a repository → isRepo:false", async () => {
const d = mkdtempSync(join(tmpdir(), "ac-nogit-"));
try {
const r = await git.status(d);
expect(r.isRepo).toBe(false);
} finally {
rmSync(d, { recursive: true, force: true });
}
});
it("inside a repo: clean → empty files; changes + untracked → classified correctly", async () => {
const clean = await git.status(dir);
expect(clean.isRepo).toBe(true);
expect(clean.branch).toBeTruthy();
expect(clean.files ?? []).toHaveLength(0);
writeFileSync(join(dir, "a.txt"), "line1\nline2\nline3\n");
writeFileSync(join(dir, "new.txt"), "brand new\n");
const r = await git.status(dir);
expect(r.files?.some((f) => f.path === "a.txt" && f.kind === "modified")).toBe(true);
expect(r.files?.some((f) => f.path === "new.txt" && f.kind === "untracked")).toBe(true);
});
});
describe("git.diff", () => {
it("tracked change → unified diff text with +/-", async () => {
const r = await git.diff(dir, "a.txt");
expect(r.diff).toBeTruthy();
expect(r.diff).toContain("+line3");
});
it("untracked file → untracked:true + all-added diff", async () => {
const r = await git.diff(dir, "new.txt");
expect(r.untracked).toBe(true);
expect(r.diff).toContain("+brand new");
});
it("path outside the project → error", async () => {
const r = await git.diff(dir, "../outside.txt");
expect(r.error).toBeTruthy();
expect(r.diff).toBeUndefined();
});
});
describe("git.discard", () => {
it("tracked → git restore brings back the original content", async () => {
const r = await git.discard(dir, "a.txt", dataDir);
expect(r.ok).toBe(true);
expect(readFileSync(join(dir, "a.txt"), "utf8").replace(/\r\n/g, "\n")).toBe("line1\nline2\n");
const st = await git.status(dir);
expect(st.files?.some((f) => f.path === "a.txt")).toBe(false);
});
it("untracked → moved to trash/discarded/<ts>/ and gone from the project", async () => {
const r = await git.discard(dir, "new.txt", dataDir);
expect(r.ok).toBe(true);
expect(r.trashedTo).toContain("trash");
expect(r.trashedTo).toContain("discarded");
expect(existsSync(join(dir, "new.txt"))).toBe(false);
expect(existsSync(r.trashedTo!)).toBe(true);
expect(readFileSync(r.trashedTo!, "utf8")).toBe("brand new\n");
});
it("nonexistent file → error", async () => {
const r = await git.discard(dir, "ghost.txt", dataDir);
expect(r.ok).toBe(false);
expect(r.error).toBeTruthy();
});
});