|
| 1 | +import { describe, test, expect } from "bun:test" |
| 2 | +import { Command } from "../../src/command/index" |
| 3 | +import { Instance } from "../../src/project/instance" |
| 4 | +import { tmpdir } from "../fixture/fixture" |
| 5 | + |
| 6 | +// --------------------------------------------------------------------------- |
| 7 | +// Helpers |
| 8 | +// --------------------------------------------------------------------------- |
| 9 | + |
| 10 | +async function withInstance(fn: () => Promise<void>) { |
| 11 | + await using tmp = await tmpdir({ git: true }) |
| 12 | + await Instance.provide({ directory: tmp.path, fn }) |
| 13 | +} |
| 14 | + |
| 15 | +// --------------------------------------------------------------------------- |
| 16 | +// Tests: Default commands are always available and correctly configured |
| 17 | +// --------------------------------------------------------------------------- |
| 18 | + |
| 19 | +describe("Command module", () => { |
| 20 | + describe("default commands", () => { |
| 21 | + test("init, discover, review are always present", async () => { |
| 22 | + await withInstance(async () => { |
| 23 | + const commands = await Command.list() |
| 24 | + const names = commands.map((c) => c.name) |
| 25 | + expect(names).toContain("init") |
| 26 | + expect(names).toContain("discover") |
| 27 | + expect(names).toContain("review") |
| 28 | + }) |
| 29 | + }) |
| 30 | + |
| 31 | + test("discover command has correct metadata", async () => { |
| 32 | + await withInstance(async () => { |
| 33 | + const cmd = await Command.get("discover") |
| 34 | + expect(cmd).toBeDefined() |
| 35 | + expect(cmd.name).toBe("discover") |
| 36 | + expect(cmd.source).toBe("command") |
| 37 | + expect(cmd.description).toBe("scan data stack and set up connections") |
| 38 | + }) |
| 39 | + }) |
| 40 | + |
| 41 | + test("discover template references project_scan tool", async () => { |
| 42 | + await withInstance(async () => { |
| 43 | + const cmd = await Command.get("discover") |
| 44 | + const template = await cmd.template |
| 45 | + expect(template).toContain("project_scan") |
| 46 | + }) |
| 47 | + }) |
| 48 | + |
| 49 | + test("discover template has $ARGUMENTS placeholder", async () => { |
| 50 | + await withInstance(async () => { |
| 51 | + const cmd = await Command.get("discover") |
| 52 | + expect(cmd.hints).toContain("$ARGUMENTS") |
| 53 | + }) |
| 54 | + }) |
| 55 | + |
| 56 | + test("init command has correct metadata", async () => { |
| 57 | + await withInstance(async () => { |
| 58 | + const cmd = await Command.get("init") |
| 59 | + expect(cmd).toBeDefined() |
| 60 | + expect(cmd.name).toBe("init") |
| 61 | + expect(cmd.source).toBe("command") |
| 62 | + expect(cmd.description).toBe("create/update AGENTS.md") |
| 63 | + }) |
| 64 | + }) |
| 65 | + |
| 66 | + test("review command has subtask flag", async () => { |
| 67 | + await withInstance(async () => { |
| 68 | + const cmd = await Command.get("review") |
| 69 | + expect(cmd).toBeDefined() |
| 70 | + expect(cmd.name).toBe("review") |
| 71 | + expect(cmd.subtask).toBe(true) |
| 72 | + expect(cmd.source).toBe("command") |
| 73 | + }) |
| 74 | + }) |
| 75 | + |
| 76 | + test("all default commands have source 'command'", async () => { |
| 77 | + await withInstance(async () => { |
| 78 | + const commands = await Command.list() |
| 79 | + const defaults = commands.filter((c) => ["init", "discover", "review"].includes(c.name)) |
| 80 | + expect(defaults.length).toBe(3) |
| 81 | + for (const cmd of defaults) { |
| 82 | + expect(cmd.source).toBe("command") |
| 83 | + } |
| 84 | + }) |
| 85 | + }) |
| 86 | + |
| 87 | + test("Command.get returns undefined for non-existent commands", async () => { |
| 88 | + await withInstance(async () => { |
| 89 | + const cmd = await Command.get("nonexistent-command-12345") |
| 90 | + expect(cmd).toBeUndefined() |
| 91 | + }) |
| 92 | + }) |
| 93 | + }) |
| 94 | + |
| 95 | + describe("user-defined commands from config", () => { |
| 96 | + test("config commands are loaded alongside defaults", async () => { |
| 97 | + await using tmp = await tmpdir({ |
| 98 | + git: true, |
| 99 | + config: { |
| 100 | + command: { |
| 101 | + "my-custom": { |
| 102 | + description: "Custom command", |
| 103 | + template: "Do something custom with $1", |
| 104 | + }, |
| 105 | + }, |
| 106 | + }, |
| 107 | + }) |
| 108 | + await Instance.provide({ |
| 109 | + directory: tmp.path, |
| 110 | + fn: async () => { |
| 111 | + const commands = await Command.list() |
| 112 | + const names = commands.map((c) => c.name) |
| 113 | + // Defaults still present |
| 114 | + expect(names).toContain("init") |
| 115 | + expect(names).toContain("discover") |
| 116 | + expect(names).toContain("review") |
| 117 | + // Custom command also present |
| 118 | + expect(names).toContain("my-custom") |
| 119 | + const custom = await Command.get("my-custom") |
| 120 | + expect(custom.source).toBe("command") |
| 121 | + expect(custom.description).toBe("Custom command") |
| 122 | + }, |
| 123 | + }) |
| 124 | + }) |
| 125 | + }) |
| 126 | +}) |
| 127 | + |
| 128 | +// --------------------------------------------------------------------------- |
| 129 | +// Tests: Resilient loading pattern (isolated from module mocks) |
| 130 | +// |
| 131 | +// These tests verify the try/catch pattern used in command/index.ts to ensure |
| 132 | +// MCP and Skill failures don't prevent default commands from being served. |
| 133 | +// --------------------------------------------------------------------------- |
| 134 | + |
| 135 | +describe("Command loading resilience pattern", () => { |
| 136 | + // NOTE: These tests duplicate the loading logic from command/index.ts rather than |
| 137 | + // mocking the real MCP/Skill modules. This avoids complex module mocking but means |
| 138 | + // loadCommands() below could drift from the real implementation. If the loading |
| 139 | + // pattern in command/index.ts changes, these tests should be updated to match. |
| 140 | + async function loadCommands(opts: { |
| 141 | + mcpPrompts: () => Promise<Record<string, any>> |
| 142 | + skillAll: () => Promise<Array<{ name: string; description: string; content: string }>> |
| 143 | + }) { |
| 144 | + const result: Record<string, { name: string; source: string; description?: string }> = { |
| 145 | + init: { name: "init", source: "command", description: "create/update AGENTS.md" }, |
| 146 | + discover: { name: "discover", source: "command", description: "scan data stack" }, |
| 147 | + review: { name: "review", source: "command", description: "review changes" }, |
| 148 | + } |
| 149 | + |
| 150 | + // This matches the pattern in command/index.ts |
| 151 | + try { |
| 152 | + for (const [name, prompt] of Object.entries(await opts.mcpPrompts())) { |
| 153 | + result[name] = { name, source: "mcp", description: (prompt as any).description } |
| 154 | + } |
| 155 | + } catch { |
| 156 | + // MCP prompt loading failed — continue with default commands |
| 157 | + } |
| 158 | + |
| 159 | + try { |
| 160 | + for (const skill of await opts.skillAll()) { |
| 161 | + if (result[skill.name]) continue |
| 162 | + result[skill.name] = { name: skill.name, source: "skill", description: skill.description } |
| 163 | + } |
| 164 | + } catch { |
| 165 | + // Skill loading failed — continue with default commands |
| 166 | + } |
| 167 | + |
| 168 | + return Object.values(result) |
| 169 | + } |
| 170 | + |
| 171 | + test("all plugins healthy — defaults + plugins present", async () => { |
| 172 | + const commands = await loadCommands({ |
| 173 | + mcpPrompts: async () => ({ "mcp-cmd": { description: "MCP prompt" } }), |
| 174 | + skillAll: async () => [{ name: "my-skill", description: "A skill", content: "" }], |
| 175 | + }) |
| 176 | + const names = commands.map((c) => c.name) |
| 177 | + expect(names).toContain("init") |
| 178 | + expect(names).toContain("discover") |
| 179 | + expect(names).toContain("review") |
| 180 | + expect(names).toContain("mcp-cmd") |
| 181 | + expect(names).toContain("my-skill") |
| 182 | + }) |
| 183 | + |
| 184 | + test("MCP throws — defaults + skills still present", async () => { |
| 185 | + const commands = await loadCommands({ |
| 186 | + mcpPrompts: async () => { |
| 187 | + throw new Error("MCP server unavailable") |
| 188 | + }, |
| 189 | + skillAll: async () => [{ name: "my-skill", description: "A skill", content: "" }], |
| 190 | + }) |
| 191 | + const names = commands.map((c) => c.name) |
| 192 | + expect(names).toContain("init") |
| 193 | + expect(names).toContain("discover") |
| 194 | + expect(names).toContain("review") |
| 195 | + expect(names).toContain("my-skill") |
| 196 | + expect(commands.filter((c) => c.source === "mcp").length).toBe(0) |
| 197 | + }) |
| 198 | + |
| 199 | + test("Skills throws — defaults + MCP still present", async () => { |
| 200 | + const commands = await loadCommands({ |
| 201 | + mcpPrompts: async () => ({ "mcp-cmd": { description: "MCP prompt" } }), |
| 202 | + skillAll: async () => { |
| 203 | + throw new Error("Skill discovery failed") |
| 204 | + }, |
| 205 | + }) |
| 206 | + const names = commands.map((c) => c.name) |
| 207 | + expect(names).toContain("init") |
| 208 | + expect(names).toContain("discover") |
| 209 | + expect(names).toContain("review") |
| 210 | + expect(names).toContain("mcp-cmd") |
| 211 | + expect(commands.filter((c) => c.source === "skill").length).toBe(0) |
| 212 | + }) |
| 213 | + |
| 214 | + test("BOTH throw — only defaults present", async () => { |
| 215 | + const commands = await loadCommands({ |
| 216 | + mcpPrompts: async () => { |
| 217 | + throw new Error("MCP server unavailable") |
| 218 | + }, |
| 219 | + skillAll: async () => { |
| 220 | + throw new Error("Skill discovery failed") |
| 221 | + }, |
| 222 | + }) |
| 223 | + const names = commands.map((c) => c.name) |
| 224 | + expect(names).toContain("init") |
| 225 | + expect(names).toContain("discover") |
| 226 | + expect(names).toContain("review") |
| 227 | + expect(commands.length).toBe(3) |
| 228 | + }) |
| 229 | + |
| 230 | + test("skill with same name as default is skipped", async () => { |
| 231 | + const commands = await loadCommands({ |
| 232 | + mcpPrompts: async () => ({}), |
| 233 | + skillAll: async () => [{ name: "discover", description: "Rogue skill", content: "" }], |
| 234 | + }) |
| 235 | + const discover = commands.find((c) => c.name === "discover")! |
| 236 | + expect(discover.source).toBe("command") |
| 237 | + expect(discover.description).toBe("scan data stack") |
| 238 | + }) |
| 239 | + |
| 240 | + test("MCP command can overwrite default (same name)", async () => { |
| 241 | + const commands = await loadCommands({ |
| 242 | + mcpPrompts: async () => ({ discover: { description: "MCP discover" } }), |
| 243 | + skillAll: async () => [], |
| 244 | + }) |
| 245 | + const discover = commands.find((c) => c.name === "discover")! |
| 246 | + // MCP overwrites default because it's applied after defaults |
| 247 | + expect(discover.source).toBe("mcp") |
| 248 | + }) |
| 249 | +}) |
0 commit comments