/** * @file Tests for lane-agents.js: agent-template installation into a lane's * .claude/agents/, including path resolution, git-exclude idempotence, and * missing-repo rejection. * @author Nguyễn Ngọc Trí Vĩ */ const { execFileSync } = require("node:child_process"); const os = require("node:os"); const path = require("node:path"); const fs = require("node:fs"); const SUITE_ROOT = fs.mkdtempSync(path.join(os.tmpdir(), "ccam-lane-agents-")); process.env.DASHBOARD_DB_PATH = path.join(SUITE_ROOT, "dashboard.db"); process.env.LANES_ROOT = path.join(SUITE_ROOT, "lanes"); const { describe, it, after } = require("node:test"); const assert = require("node:assert/strict"); const lanesLib = require("../lib/lanes"); const { installAgents } = require("../lib/lane-agents"); after(() => fs.rmSync(SUITE_ROOT, { recursive: true, force: true })); let laneSeq = 0; /** Create a real git repo dir (used as a lane's cwd). */ function makeRepoDir() { const cwd = path.join(SUITE_ROOT, `repo-${++laneSeq}`); fs.mkdirSync(cwd, { recursive: true }); execFileSync("git", ["init", "--initial-branch=main"], { cwd, stdio: "pipe" }); return cwd; } /** Create a lane record backed by an existing directory. */ function makeLane(cwd) { return lanesLib.createLane({ title: "agents-test", cwd, kind: "managed" }); } describe("installAgents", () => { it("copies agent templates from agents/ dir into lane's .claude/agents/", async () => { const cwd = makeRepoDir(); const lane = makeLane(cwd); const result = await installAgents(lane); assert.deepEqual([...result.installed].sort(), ["qc-local.md", "senior-gate-reviewer.md"]); assert.ok(fs.existsSync(path.join(cwd, ".claude", "agents", "qc-local.md"))); assert.ok(fs.existsSync(path.join(cwd, ".claude", "agents", "senior-gate-reviewer.md"))); // Verify the source path is new (agents/) not old (data/agent-templates/) const oldPath = path.join(__dirname, "..", "data", "agent-templates", "ship-feature-lane"); assert.equal(fs.existsSync(oldPath), false); }); it("throws ENOTGITREPO when lane cwd has no .git", async () => { const cwd = path.join(SUITE_ROOT, "no-git"); fs.mkdirSync(cwd, { recursive: true }); const lane = makeLane(cwd); await assert.rejects( () => installAgents(lane), (err) => err.code === "ENOTGITREPO" ); }); it("adds .claude/agents/ line to git info/exclude idempotently", async () => { const cwd = makeRepoDir(); const lane = makeLane(cwd); await installAgents(lane); // first await installAgents(lane); // reinstall — should not duplicate const excludePath = path.join(cwd, ".git", "info", "exclude"); const content = fs.readFileSync(excludePath, "utf8"); const matches = content.split("\n").filter((l) => l === ".claude/agents/"); assert.equal(matches.length, 1); }); it("overwrites existing agents on reinstall", async () => { const cwd = makeRepoDir(); const lane = makeLane(cwd); await installAgents(lane); // Tamper with the installed file const agentPath = path.join(cwd, ".claude", "agents", "qc-local.md"); fs.writeFileSync(agentPath, "tampered"); // Reinstall await installAgents(lane); const content = fs.readFileSync(agentPath, "utf8"); assert.ok(content.includes("#"), "reinstalled file should look like markdown, not 'tampered'"); assert.notEqual(content, "tampered"); }); it("creates .claude/agents/ dir if it does not exist", async () => { const cwd = makeRepoDir(); const lane = makeLane(cwd); assert.equal(fs.existsSync(path.join(cwd, ".claude", "agents")), false); await installAgents(lane); assert.ok(fs.existsSync(path.join(cwd, ".claude", "agents"))); }); });