From 8bcc30fad6d8cab4d1ef5cf6cc64d6b69c663fdf Mon Sep 17 00:00:00 2001 From: nntrivi2001 Date: Mon, 24 Aug 2026 09:41:11 +0700 Subject: [PATCH] test(lanes): rework lane-agents tests against real lanes, add skills-install coverage lane-agents.test.js now drives installAgents through real lane records (lanesLib.createLane) instead of ad-hoc git fixtures, and asserts the agent-template source moved to agents/ (not the retired data/agent-templates/ path). New skills-install.test.js covers the ship-feature-lane skill copy resolving from skills/, not .claude/skills/. --- server/__tests__/lane-agents.test.js | 168 ++++++++++++------------ server/__tests__/skills-install.test.js | 83 ++++++++++++ 2 files changed, 164 insertions(+), 87 deletions(-) create mode 100644 server/__tests__/skills-install.test.js diff --git a/server/__tests__/lane-agents.test.js b/server/__tests__/lane-agents.test.js index 3dae1dc9..67f90caa 100644 --- a/server/__tests__/lane-agents.test.js +++ b/server/__tests__/lane-agents.test.js @@ -1,108 +1,102 @@ /** - * @file Tests for server/lib/lane-agents.js: installing the ship-feature-lane - * agent templates into a lane's .claude/agents/ and git-excluding them. Uses - * REAL git fixtures (a plain clone AND a `git worktree add` lane) because the - * exclude-file location depends on git's own git-dir/git-common-dir split — - * the same distinction E2's merge-driver setup had to get right. + * @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 { describe, it, before, after } = require("node:test"); -const assert = require("node:assert/strict"); -const fs = require("node:fs"); +const { execFileSync } = require("node:child_process"); const os = require("node:os"); const path = require("node:path"); -const { execFileSync } = require("node:child_process"); +const fs = require("node:fs"); -const ROOT = fs.mkdtempSync(path.join(os.tmpdir(), "ccam-agents-")); +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 laneAgents = require("../lib/lane-agents"); +const { describe, it, after } = require("node:test"); +const assert = require("node:assert/strict"); -const g = (cwd, ...args) => { - const env = { ...process.env }; - delete env.GIT_DIR; - delete env.GIT_WORK_TREE; - delete env.GIT_INDEX_FILE; - delete env.GIT_COMMON_DIR; - delete env.GIT_OBJECT_DIRECTORY; - delete env.GIT_ALTERNATE_OBJECT_DIRECTORIES; - delete env.GIT_PREFIX; - delete env.GIT_NAMESPACE; - delete env.GIT_CONFIG_PARAMETERS; - env.GIT_TERMINAL_PROMPT = "0"; - return execFileSync("git", args, { cwd, encoding: "utf8", env }); -}; -const gc = (cwd, ...args) => g(cwd, "-c", "user.email=t@h", "-c", "user.name=t", ...args); +const lanesLib = require("../lib/lanes"); +const { installAgents } = require("../lib/lane-agents"); -after(() => fs.rmSync(ROOT, { recursive: true, force: true })); +after(() => fs.rmSync(SUITE_ROOT, { recursive: true, force: true })); -describe("installAgents against a plain clone", () => { - const REPO = path.join(ROOT, "plain-repo"); +let laneSeq = 0; - before(() => { - fs.mkdirSync(REPO, { recursive: true }); - g(REPO, "init", "-q", "-b", "main", REPO); - fs.writeFileSync(path.join(REPO, "README.md"), "hello\n"); - gc(REPO, "add", "-A"); - gc(REPO, "commit", "-qm", "init"); +/** 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("writes both templates and appends the git-exclude line", async () => { - const result = await laneAgents.installAgents({ cwd: REPO }); - assert.deepEqual(result.installed.sort(), ["qc-local.md", "senior-gate-reviewer.md"]); - assert.ok(fs.existsSync(path.join(REPO, ".claude", "agents", "qc-local.md"))); - assert.ok(fs.existsSync(path.join(REPO, ".claude", "agents", "senior-gate-reviewer.md"))); - - const exclude = fs.readFileSync(path.join(REPO, ".git", "info", "exclude"), "utf8"); - assert.match(exclude, /^\.claude\/agents\/$/m); + 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("is idempotent — a second call does not duplicate the exclude line", async () => { - await laneAgents.installAgents({ cwd: REPO }); - const exclude = fs.readFileSync(path.join(REPO, ".git", "info", "exclude"), "utf8"); - const matches = exclude.split("\n").filter((line) => line === ".claude/agents/"); + 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 template files on reinstall (not a merge)", async () => { - const dest = path.join(REPO, ".claude", "agents", "qc-local.md"); - fs.writeFileSync(dest, "stale content from a previous version\n"); - await laneAgents.installAgents({ cwd: REPO }); - const content = fs.readFileSync(dest, "utf8"); - assert.doesNotMatch(content, /stale content/); - }); -}); - -describe("installAgents against a real git-worktree lane", () => { - it("writes the exclude line to the SHARED common dir, not the worktree-private one", async () => { - const wt = require("../lib/worktree"); - const SRC = path.join(ROOT, "wt-src"); - fs.mkdirSync(SRC, { recursive: true }); - g(SRC, "init", "-q", "-b", "main", SRC); - fs.writeFileSync(path.join(SRC, "README.md"), "hello\n"); - gc(SRC, "add", "-A"); - gc(SRC, "commit", "-qm", "init"); - - const wtDir = path.join(ROOT, "wt-lane"); - await wt.addWorktree({ sourceRepo: SRC, dir: wtDir, branch: "feat/agents", base: "main" }); - - const result = await laneAgents.installAgents({ cwd: wtDir }); - assert.deepEqual(result.installed.sort(), ["qc-local.md", "senior-gate-reviewer.md"]); - assert.ok(fs.existsSync(path.join(wtDir, ".claude", "agents", "qc-local.md"))); - - // /.git is a FILE for a worktree lane — info/exclude must NOT be - // written under it. It belongs in the source repo's own .git/info/, - // shared across every worktree. - assert.ok(fs.statSync(path.join(wtDir, ".git")).isFile()); - const exclude = fs.readFileSync(path.join(SRC, ".git", "info", "exclude"), "utf8"); - assert.match(exclude, /^\.claude\/agents\/$/m); - }); -}); - -describe("installAgents against a non-git directory", () => { - it("throws ENOTGITREPO rather than crashing on a missing .git", async () => { - const plain = path.join(ROOT, "not-a-repo"); - fs.mkdirSync(plain, { recursive: true }); - await assert.rejects(() => laneAgents.installAgents({ cwd: plain }), { code: "ENOTGITREPO" }); + 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"))); }); }); diff --git a/server/__tests__/skills-install.test.js b/server/__tests__/skills-install.test.js new file mode 100644 index 00000000..f3653cf8 --- /dev/null +++ b/server/__tests__/skills-install.test.js @@ -0,0 +1,83 @@ +/** + * @file Tests for skills-install.js: skill-copy path resolution (source: + * repoRoot/skills/ not repoRoot/.claude/skills/) and error handling. + * @author Nguyễn Ngọc Trí Vĩ + */ + +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-skills-")); +process.env.DASHBOARD_DB_PATH = path.join(SUITE_ROOT, "dashboard.db"); + +const { describe, it, after } = require("node:test"); +const assert = require("node:assert/strict"); + +const { installShipFeatureLaneSkill } = require("../lib/skills-install"); + +after(() => fs.rmSync(SUITE_ROOT, { recursive: true, force: true })); + +describe("installShipFeatureLaneSkill", () => { + it("resolves source from repoRoot/skills/ (new path), not .claude/skills/", () => { + // Build a fake repoRoot with the skill only under skills/ship-feature-lane/ + const repoRoot = path.join(SUITE_ROOT, "mock-repo"); + const src = path.join(repoRoot, "skills", "ship-feature-lane"); + fs.mkdirSync(src, { recursive: true }); + fs.writeFileSync(path.join(src, "SKILL.md"), "# Fake Ship Feature Lane Skill"); + + // The old path should NOT exist + const oldPath = path.join(repoRoot, ".claude", "skills", "ship-feature-lane", "SKILL.md"); + assert.equal(fs.existsSync(oldPath), false); + + // Override HOME so we don't touch real ~/.claude + const origHome = process.env.HOME; + const fakeHome = path.join(SUITE_ROOT, "fake-home"); + process.env.HOME = fakeHome; + + try { + const result = installShipFeatureLaneSkill({ repoRoot }); + assert.equal(result.installed, true); + assert.ok(result.path.startsWith(fakeHome)); + + const installedFile = path.join(result.path, "SKILL.md"); + assert.ok(fs.existsSync(installedFile)); + assert.equal(fs.readFileSync(installedFile, "utf8"), "# Fake Ship Feature Lane Skill"); + } finally { + process.env.HOME = origHome; + } + }); + + it("throws ENOSKILLSRC when skills/ship-feature-lane/ is missing", () => { + const repoRoot = path.join(SUITE_ROOT, "empty-repo"); + fs.mkdirSync(repoRoot, { recursive: true }); + assert.throws( + () => installShipFeatureLaneSkill({ repoRoot }), + (err) => err.code === "ENOSKILLSRC" + ); + }); + + it("overwrites an existing installed skill (force: true)", () => { + const repoRoot = path.join(SUITE_ROOT, "mock-repo-overwrite"); + const src = path.join(repoRoot, "skills", "ship-feature-lane"); + fs.mkdirSync(src, { recursive: true }); + fs.writeFileSync(path.join(src, "SKILL.md"), "# Updated skill"); + + const origHome = process.env.HOME; + const fakeHome = path.join(SUITE_ROOT, "fake-home-overwrite"); + process.env.HOME = fakeHome; + + try { + // First install + installShipFeatureLaneSkill({ repoRoot }); + // Tamper with installed copy + const existing = path.join(fakeHome, ".claude", "skills", "ship-feature-lane", "SKILL.md"); + fs.writeFileSync(existing, "# Tampered"); + // Reinstall — should overwrite + installShipFeatureLaneSkill({ repoRoot }); + assert.equal(fs.readFileSync(existing, "utf8"), "# Updated skill"); + } finally { + process.env.HOME = origHome; + } + }); +});