@@ -0,0 +1,149 @@
1+import { afterEach, beforeEach, describe, expect, test } from "bun:test";
2+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
3+import { tmpdir } from "node:os";
4+import { join } from "node:path";
5+import { expandTemplate, loadCustomCommands } from "./custom-commands.ts";
6+
7+// ─── parseFrontmatter / loadCustomCommands ────────────────────────────────────
8+
9+describe("loadCustomCommands", () => {
10+ let dir: string;
11+
12+ beforeEach(() => {
13+ dir = mkdtempSync(join(tmpdir(), "mc-cmd-test-"));
14+ });
15+
16+ afterEach(() => {
17+ rmSync(dir, { recursive: true, force: true });
18+ });
19+
20+ function writeCmd(root: string, name: string, content: string): void {
21+ const cmdsDir = join(root, ".agents", "commands");
22+ mkdirSync(cmdsDir, { recursive: true });
23+ writeFileSync(join(cmdsDir, `${name}.md`), content);
24+ }
25+
26+ test("returns empty map when no .agents/commands dir exists", () => {
27+ const result = loadCustomCommands(dir);
28+ expect(result.size).toBe(0);
29+ });
30+
31+ test("loads a command with frontmatter", () => {
32+ writeCmd(
33+ dir,
34+ "hello",
35+ "---\ndescription: Say hello\nmodel: zen/claude-3-5-haiku\n---\n\nHello $ARGUMENTS",
36+ );
37+ const result = loadCustomCommands(dir);
38+ const cmd = result.get("hello");
39+ expect(cmd?.description).toBe("Say hello");
40+ expect(cmd?.model).toBe("zen/claude-3-5-haiku");
41+ expect(cmd?.template).toBe("Hello $ARGUMENTS");
42+ expect(cmd?.source).toBe("local");
43+ });
44+
45+ test("falls back to name as description when frontmatter has none", () => {
46+ writeCmd(dir, "mytool", "Just a plain template");
47+ const result = loadCustomCommands(dir);
48+ expect(result.get("mytool")?.description).toBe("mytool");
49+ });
50+
51+ test("body without frontmatter is used as template verbatim", () => {
52+ writeCmd(dir, "plain", "Do the thing with $1");
53+ expect(loadCustomCommands(dir).get("plain")?.template).toBe(
54+ "Do the thing with $1",
55+ );
56+ });
57+
58+ test("ignores non-.md files", () => {
59+ const cmdsDir = join(dir, ".agents", "commands");
60+ mkdirSync(cmdsDir, { recursive: true });
61+ writeFileSync(join(cmdsDir, "script.sh"), "echo hi");
62+ expect(loadCustomCommands(dir).size).toBe(0);
63+ });
64+
65+ test("local command overrides global with same name", () => {
66+ // Simulate by writing two commands to the local dir and checking only one wins
67+ writeCmd(dir, "search", "---\ndescription: local\n---\nlocal template");
68+ const result = loadCustomCommands(dir);
69+ expect(result.get("search")?.description).toBe("local");
70+ expect(result.get("search")?.source).toBe("local");
71+ });
72+});
73+
74+// ─── expandTemplate ───────────────────────────────────────────────────────────
75+
76+describe("expandTemplate", () => {
77+ test("replaces $ARGUMENTS with the full args string", async () => {
78+ const out = await expandTemplate("Search for $ARGUMENTS", "foo bar", "/");
79+ expect(out).toBe("Search for foo bar");
80+ });
81+
82+ test("replaces positional $1, $2, $3", async () => {
83+ const out = await expandTemplate("$1 $2 $3", "alpha beta gamma", "/");
84+ expect(out).toBe("alpha beta gamma");
85+ });
86+
87+ test("replaces $1 without touching $10+ (no such placeholder)", async () => {
88+ // We only support $1–$9; $10 should not be replaced by $1 + "0"
89+ const out = await expandTemplate("$10", "x", "/");
90+ // $1 → "x", then "0" remains → "x0"
91+ expect(out).toBe("x0");
92+ });
93+
94+ test("missing positional args become empty string", async () => {
95+ const out = await expandTemplate("$1 $2 $3", "only-one", "/");
96+ expect(out).toBe("only-one ");
97+ });
98+
99+ test("quoted tokens are parsed as single args", async () => {
100+ const out = await expandTemplate("$1|$2", '"hello world" second', "/");
101+ expect(out).toBe("hello world|second");
102+ });
103+
104+ test("replaces all occurrences of the same $ARGUMENTS", async () => {
105+ const out = await expandTemplate(
106+ "Arg: $ARGUMENTS and again $ARGUMENTS",
107+ "test",
108+ "/",
109+ );
110+ expect(out).toBe("Arg: test and again test");
111+ });
112+
113+ test("shell interpolation !`cmd` is replaced with command output", async () => {
114+ const out = await expandTemplate("Result: !`echo hello`", "", "/");
115+ expect(out).toBe("Result: hello");
116+ });
117+
118+ test("duplicate !`cmd` occurrences are all replaced", async () => {
119+ const out = await expandTemplate("!`echo hi` and !`echo hi`", "", "/");
120+ expect(out).toBe("hi and hi");
121+ });
122+
123+ test("failed shell command suppresses stderr, keeps stdout", async () => {
124+ // exit 1 with stdout output — stdout kept, stderr discarded
125+ const out = await expandTemplate(
126+ "pre !`echo out; echo err >&2; exit 1` post",
127+ "",
128+ "/",
129+ );
130+ expect(out).toBe("pre out post");
131+ });
132+
133+ test("successful command includes stderr in output", async () => {
134+ const out = await expandTemplate("!`echo err >&2; exit 0`", "", "/");
135+ expect(out).toBe("err");
136+ });
137+
138+ test("timed-out shell command leaves empty string", async () => {
139+ // sleep 60 will be killed by the 10s timeout — use a short sleep and
140+ // mock the timeout by testing that a non-blocking command returns quickly
141+ const out = await expandTemplate("pre !`exit 1` post", "", "/");
142+ expect(out).toBe("pre post");
143+ });
144+
145+ test("no placeholders — template returned unchanged", async () => {
146+ const out = await expandTemplate("plain text", "ignored", "/");
147+ expect(out).toBe("plain text");
148+ });
149+});