|
| 1 | +import { afterEach, describe, expect, it } from "vitest"; |
| 2 | +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { buildSkillsAddArgs, resolveAgentTargets } from "./skillsTargets.js"; |
| 6 | + |
| 7 | +const tmpDirs: string[] = []; |
| 8 | + |
| 9 | +function tempDir(prefix: string): string { |
| 10 | + const dir = mkdtempSync(join(tmpdir(), prefix)); |
| 11 | + tmpDirs.push(dir); |
| 12 | + return dir; |
| 13 | +} |
| 14 | + |
| 15 | +/** A project root containing the given `<host>/skills` folders. */ |
| 16 | +function projectWith(...hostDirs: string[]): string { |
| 17 | + const root = tempDir("hf-targets-proj-"); |
| 18 | + for (const host of hostDirs) mkdirSync(join(root, host, "skills"), { recursive: true }); |
| 19 | + return root; |
| 20 | +} |
| 21 | + |
| 22 | +/** A PATH-style string pointing at a dir that contains the given fake executables. */ |
| 23 | +function pathWith(...bins: string[]): string { |
| 24 | + const dir = tempDir("hf-targets-bin-"); |
| 25 | + for (const bin of bins) writeFileSync(join(dir, bin), ""); |
| 26 | + return dir; |
| 27 | +} |
| 28 | + |
| 29 | +afterEach(() => { |
| 30 | + for (const dir of tmpDirs.splice(0)) rmSync(dir, { recursive: true, force: true }); |
| 31 | +}); |
| 32 | + |
| 33 | +describe("resolveAgentTargets", () => { |
| 34 | + const blank = { env: {}, pathStr: "", platform: "linux" as const }; |
| 35 | + |
| 36 | + // ── 1. Existing project folders win, mapped dir → upstream key ────────────── |
| 37 | + |
| 38 | + it("honours an existing `.hermes/skills` folder, nothing else", () => { |
| 39 | + const result = resolveAgentTargets({ ...blank, cwd: projectWith(".hermes") }); |
| 40 | + expect(result.agents).toEqual(["hermes-agent"]); |
| 41 | + }); |
| 42 | + |
| 43 | + it("maps `.factory` → droid and `.kiro` → kiro-cli (dir names differ from keys)", () => { |
| 44 | + const result = resolveAgentTargets({ ...blank, cwd: projectWith(".factory", ".kiro") }); |
| 45 | + expect(result.agents).toEqual(["droid", "kiro-cli"]); |
| 46 | + }); |
| 47 | + |
| 48 | + it("maps the shared `.agents` dir to the single `universal` key", () => { |
| 49 | + const result = resolveAgentTargets({ ...blank, cwd: projectWith(".agents") }); |
| 50 | + expect(result.agents).toEqual(["universal"]); |
| 51 | + }); |
| 52 | + |
| 53 | + it("returns claude-code first across multiple existing folders", () => { |
| 54 | + const result = resolveAgentTargets({ ...blank, cwd: projectWith(".agents", ".claude") }); |
| 55 | + expect(result.agents).toEqual(["claude-code", "universal"]); |
| 56 | + }); |
| 57 | + |
| 58 | + it("existing folders take precedence over CLAUDECODE and PATH", () => { |
| 59 | + const result = resolveAgentTargets({ |
| 60 | + cwd: projectWith(".hermes"), |
| 61 | + env: { CLAUDECODE: "1" }, |
| 62 | + pathStr: pathWith("claude", "cursor"), |
| 63 | + platform: "linux", |
| 64 | + }); |
| 65 | + expect(result.agents).toEqual(["hermes-agent"]); |
| 66 | + }); |
| 67 | + |
| 68 | + // ── 2a. Claude Code env on a blank project ────────────────────────────────── |
| 69 | + |
| 70 | + it("targets just claude-code when running under Claude Code", () => { |
| 71 | + const result = resolveAgentTargets({ |
| 72 | + ...blank, |
| 73 | + cwd: projectWith(), |
| 74 | + env: { CLAUDECODE: "1" }, |
| 75 | + }); |
| 76 | + expect(result.agents).toEqual(["claude-code"]); |
| 77 | + }); |
| 78 | + |
| 79 | + // ── 2b. gstack route: installed agent CLIs on PATH ────────────────────────── |
| 80 | + |
| 81 | + it("detects installed agent CLIs on PATH (blank project, no CLAUDECODE)", () => { |
| 82 | + const result = resolveAgentTargets({ |
| 83 | + cwd: projectWith(), |
| 84 | + env: {}, |
| 85 | + pathStr: pathWith("claude", "hermes"), |
| 86 | + platform: "linux", |
| 87 | + }); |
| 88 | + expect(result.agents).toEqual(["claude-code", "hermes-agent"]); |
| 89 | + }); |
| 90 | + |
| 91 | + it("collapses universal-bucket CLIs (cursor/codex/…) to a single `universal`", () => { |
| 92 | + const result = resolveAgentTargets({ |
| 93 | + cwd: projectWith(), |
| 94 | + env: {}, |
| 95 | + pathStr: pathWith("cursor", "codex", "gemini"), |
| 96 | + platform: "linux", |
| 97 | + }); |
| 98 | + expect(result.agents).toEqual(["universal"]); |
| 99 | + }); |
| 100 | + |
| 101 | + // ── 2c. Floor ─────────────────────────────────────────────────────────────── |
| 102 | + |
| 103 | + it("falls back to claude-code + universal (.claude + .agents) when nothing is found", () => { |
| 104 | + const result = resolveAgentTargets({ ...blank, cwd: projectWith() }); |
| 105 | + expect(result.agents).toEqual(["claude-code", "universal"]); |
| 106 | + }); |
| 107 | + |
| 108 | + // ── Invariant: never the `--all` spray ────────────────────────────────────── |
| 109 | + |
| 110 | + it("never returns the `'*'` wildcard agent", () => { |
| 111 | + for (const cwd of [projectWith(), projectWith(".hermes"), projectWith(".claude")]) { |
| 112 | + const result = resolveAgentTargets({ ...blank, cwd }); |
| 113 | + expect(result.agents).not.toContain("*"); |
| 114 | + expect(result.agents.length).toBeGreaterThan(0); |
| 115 | + } |
| 116 | + }); |
| 117 | +}); |
| 118 | + |
| 119 | +describe("buildSkillsAddArgs", () => { |
| 120 | + it("installs every skill to the given agents, non-interactive — not `--all`", () => { |
| 121 | + expect(buildSkillsAddArgs(["claude-code", "universal"])).toEqual([ |
| 122 | + "--skill", |
| 123 | + "*", |
| 124 | + "--agent", |
| 125 | + "claude-code", |
| 126 | + "universal", |
| 127 | + "--yes", |
| 128 | + ]); |
| 129 | + }); |
| 130 | +}); |
0 commit comments