@@ -0,0 +1,370 @@
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, resolve } from "node:path";
5+import type { GitState } from "./git.ts";
6+import {
7+ buildSystemPrompt,
8+ discoverAgentsMd,
9+ formatGitLine,
10+} from "./prompt.ts";
11+import type { Skill } from "./skills.ts";
12+
13+// ---------------------------------------------------------------------------
14+// Helpers
15+// ---------------------------------------------------------------------------
16+
17+let tmp: string;
18+
19+beforeEach(() => {
20+ tmp = mkdtempSync(join(tmpdir(), "mc-prompt-"));
21+});
22+
23+afterEach(() => {
24+ rmSync(tmp, { recursive: true, force: true });
25+});
26+
27+// ---------------------------------------------------------------------------
28+// discoverAgentsMd
29+// ---------------------------------------------------------------------------
30+
31+describe("discoverAgentsMd", () => {
32+ test("finds AGENTS.md walking from cwd to scan root", () => {
33+ writeFileSync(join(tmp, "AGENTS.md"), "Root instructions");
34+ mkdirSync(join(tmp, "src"), { recursive: true });
35+ writeFileSync(join(tmp, "src", "AGENTS.md"), "Src instructions");
36+
37+ const cwd = join(tmp, "src");
38+ const root = resolve(tmp);
39+ const files = discoverAgentsMd(cwd, root);
40+
41+ // Should be ordered root-to-leaf
42+ expect(files).toHaveLength(2);
43+ expect(files[0]!.path).toBe(join(tmp, "AGENTS.md"));
44+ expect(files[0]!.content).toBe("Root instructions");
45+ expect(files[1]!.path).toBe(join(tmp, "src", "AGENTS.md"));
46+ expect(files[1]!.content).toBe("Src instructions");
47+ });
48+
49+ test("returns empty array when no files found", () => {
50+ const files = discoverAgentsMd(tmp, resolve(tmp));
51+ expect(files).toEqual([]);
52+ });
53+
54+ test("checks globalAgentsDir for global agent instructions", () => {
55+ const globalDir = join(tmp, "global-agents");
56+ mkdirSync(globalDir, { recursive: true });
57+ writeFileSync(join(globalDir, "AGENTS.md"), "Global instructions");
58+
59+ writeFileSync(join(tmp, "AGENTS.md"), "Project instructions");
60+
61+ const files = discoverAgentsMd(tmp, resolve(tmp), globalDir);
62+
63+ // Global instructions come first (general → specific)
64+ expect(files).toHaveLength(2);
65+ expect(files[0]!.content).toBe("Global instructions");
66+ expect(files[1]!.content).toBe("Project instructions");
67+ });
68+
69+ test("does not walk above the scan root", () => {
70+ const parent = join(tmp, "parent");
71+ const child = join(parent, "child");
72+ mkdirSync(child, { recursive: true });
73+
74+ // Put AGENTS.md above scan root — should NOT be found
75+ writeFileSync(join(tmp, "AGENTS.md"), "Above root");
76+ // Put AGENTS.md at scan root — should be found
77+ writeFileSync(join(parent, "AGENTS.md"), "At root");
78+
79+ const files = discoverAgentsMd(child, resolve(parent));
80+ expect(files).toHaveLength(1);
81+ expect(files[0]!.content).toBe("At root");
82+ });
83+
84+ test("orders files root-to-leaf (general → specific)", () => {
85+ const deep = join(tmp, "a", "b", "c");
86+ mkdirSync(deep, { recursive: true });
87+
88+ writeFileSync(join(tmp, "AGENTS.md"), "level-0");
89+ writeFileSync(join(tmp, "a", "AGENTS.md"), "level-1");
90+ writeFileSync(join(tmp, "a", "b", "AGENTS.md"), "level-2");
91+
92+ const files = discoverAgentsMd(join(tmp, "a", "b"), resolve(tmp));
93+ expect(files).toHaveLength(3);
94+ expect(files[0]!.content).toBe("level-0");
95+ expect(files[1]!.content).toBe("level-1");
96+ expect(files[2]!.content).toBe("level-2");
97+ });
98+});
99+
100+// ---------------------------------------------------------------------------
101+// formatGitLine
102+// ---------------------------------------------------------------------------
103+
104+describe("formatGitLine", () => {
105+ test("formats full git state", () => {
106+ const state: GitState = {
107+ root: "/repo",
108+ branch: "main",
109+ staged: 3,
110+ modified: 1,
111+ untracked: 2,
112+ ahead: 5,
113+ behind: 2,
114+ };
115+
116+ const line = formatGitLine(state);
117+ expect(line).toBe(
118+ "Git: branch main | 3 staged, 1 modified, 2 untracked | +5 −2 vs origin/main",
119+ );
120+ });
121+
122+ test("omits staged/modified/untracked when all zero", () => {
123+ const state: GitState = {
124+ root: "/repo",
125+ branch: "main",
126+ staged: 0,
127+ modified: 0,
128+ untracked: 0,
129+ ahead: 0,
130+ behind: 0,
131+ };
132+
133+ const line = formatGitLine(state);
134+ expect(line).toBe("Git: branch main");
135+ });
136+
137+ test("omits fields that are zero", () => {
138+ const state: GitState = {
139+ root: "/repo",
140+ branch: "feature",
141+ staged: 0,
142+ modified: 2,
143+ untracked: 0,
144+ ahead: 0,
145+ behind: 0,
146+ };
147+
148+ const line = formatGitLine(state);
149+ expect(line).toBe("Git: branch feature | 2 modified");
150+ });
151+
152+ test("omits ahead/behind when both zero", () => {
153+ const state: GitState = {
154+ root: "/repo",
155+ branch: "dev",
156+ staged: 1,
157+ modified: 0,
158+ untracked: 0,
159+ ahead: 0,
160+ behind: 0,
161+ };
162+
163+ const line = formatGitLine(state);
164+ expect(line).toBe("Git: branch dev | 1 staged");
165+ });
166+
167+ test("shows only ahead when behind is zero", () => {
168+ const state: GitState = {
169+ root: "/repo",
170+ branch: "main",
171+ staged: 0,
172+ modified: 0,
173+ untracked: 0,
174+ ahead: 3,
175+ behind: 0,
176+ };
177+
178+ const line = formatGitLine(state);
179+ expect(line).toBe("Git: branch main | +3 vs origin/main");
180+ });
181+
182+ test("shows only behind when ahead is zero", () => {
183+ const state: GitState = {
184+ root: "/repo",
185+ branch: "main",
186+ staged: 0,
187+ modified: 0,
188+ untracked: 0,
189+ ahead: 0,
190+ behind: 4,
191+ };
192+
193+ const line = formatGitLine(state);
194+ expect(line).toBe("Git: branch main | −4 vs origin/main");
195+ });
196+});
197+
198+// ---------------------------------------------------------------------------
199+// buildSystemPrompt
200+// ---------------------------------------------------------------------------
201+
202+describe("buildSystemPrompt", () => {
203+ test("includes base instructions", () => {
204+ const prompt = buildSystemPrompt({
205+ cwd: "/home/user/project",
206+ date: "2026-04-05",
207+ });
208+
209+ expect(prompt).toContain("You are mini-coder");
210+ expect(prompt).toContain("# Role");
211+ expect(prompt).toContain("# Tools");
212+ expect(prompt).toContain("# Code quality");
213+ expect(prompt).toContain("# Editing discipline");
214+ expect(prompt).toContain("# Communication");
215+ expect(prompt).toContain("# Persistence");
216+ });
217+
218+ test("includes session footer with date and cwd", () => {
219+ const prompt = buildSystemPrompt({
220+ cwd: "/home/user/project",
221+ date: "2026-04-05",
222+ });
223+
224+ expect(prompt).toContain("Current date: 2026-04-05");
225+ expect(prompt).toContain("Current working directory: /home/user/project");
226+ });
227+
228+ test("includes git line when git state provided", () => {
229+ const prompt = buildSystemPrompt({
230+ cwd: "/home/user/project",
231+ date: "2026-04-05",
232+ git: {
233+ root: "/home/user/project",
234+ branch: "main",
235+ staged: 3,
236+ modified: 1,
237+ untracked: 2,
238+ ahead: 5,
239+ behind: 2,
240+ },
241+ });
242+
243+ expect(prompt).toContain("Git: branch main");
244+ });
245+
246+ test("omits git line when git state is null", () => {
247+ const prompt = buildSystemPrompt({
248+ cwd: "/tmp/no-repo",
249+ date: "2026-04-05",
250+ git: null,
251+ });
252+
253+ expect(prompt).not.toContain("Git:");
254+ });
255+
256+ test("includes AGENTS.md content when provided", () => {
257+ const prompt = buildSystemPrompt({
258+ cwd: "/project",
259+ date: "2026-04-05",
260+ agentsMd: [
261+ {
262+ path: "/project/AGENTS.md",
263+ content: "Always use TypeScript strict mode.",
264+ },
265+ ],
266+ });
267+
268+ expect(prompt).toContain("# Project Context");
269+ expect(prompt).toContain("## /project/AGENTS.md");
270+ expect(prompt).toContain("Always use TypeScript strict mode.");
271+ });
272+
273+ test("includes multiple AGENTS.md files in order", () => {
274+ const prompt = buildSystemPrompt({
275+ cwd: "/project/src",
276+ date: "2026-04-05",
277+ agentsMd: [
278+ { path: "/project/AGENTS.md", content: "Root rules" },
279+ { path: "/project/src/AGENTS.md", content: "Src rules" },
280+ ],
281+ });
282+
283+ const rootIdx = prompt.indexOf("Root rules");
284+ const srcIdx = prompt.indexOf("Src rules");
285+ expect(rootIdx).toBeGreaterThan(-1);
286+ expect(srcIdx).toBeGreaterThan(rootIdx);
287+ });
288+
289+ test("includes skill catalog when skills provided", () => {
290+ const skills: Skill[] = [
291+ {
292+ name: "deploy",
293+ description: "Deploy the app.",
294+ path: "/project/.agents/skills/deploy/SKILL.md",
295+ },
296+ ];
297+
298+ const prompt = buildSystemPrompt({
299+ cwd: "/project",
300+ date: "2026-04-05",
301+ skills,
302+ });
303+
304+ expect(prompt).toContain("<available_skills>");
305+ expect(prompt).toContain("<name>deploy</name>");
306+ });
307+
308+ test("includes plugin suffixes when provided", () => {
309+ const prompt = buildSystemPrompt({
310+ cwd: "/project",
311+ date: "2026-04-05",
312+ pluginSuffixes: [
313+ "MCP server connected: github",
314+ "Custom tool: jira-query",
315+ ],
316+ });
317+
318+ expect(prompt).toContain("MCP server connected: github");
319+ expect(prompt).toContain("Custom tool: jira-query");
320+ });
321+
322+ test("assembly order: base → agents.md → skills → plugins → footer", () => {
323+ const skills: Skill[] = [
324+ {
325+ name: "test",
326+ description: "Test skill.",
327+ path: "/path/SKILL.md",
328+ },
329+ ];
330+
331+ const prompt = buildSystemPrompt({
332+ cwd: "/project",
333+ date: "2026-04-05",
334+ agentsMd: [{ path: "/project/AGENTS.md", content: "Project rules" }],
335+ skills,
336+ pluginSuffixes: ["Plugin context here"],
337+ git: {
338+ root: "/project",
339+ branch: "main",
340+ staged: 0,
341+ modified: 0,
342+ untracked: 0,
343+ ahead: 0,
344+ behind: 0,
345+ },
346+ });
347+
348+ // Verify ordering
349+ const baseIdx = prompt.indexOf("You are mini-coder");
350+ const agentsIdx = prompt.indexOf("Project rules");
351+ const skillsIdx = prompt.indexOf("<available_skills>");
352+ const pluginIdx = prompt.indexOf("Plugin context here");
353+ const footerIdx = prompt.indexOf("Current date:");
354+
355+ expect(baseIdx).toBeGreaterThan(-1);
356+ expect(agentsIdx).toBeGreaterThan(baseIdx);
357+ expect(skillsIdx).toBeGreaterThan(agentsIdx);
358+ expect(pluginIdx).toBeGreaterThan(skillsIdx);
359+ expect(footerIdx).toBeGreaterThan(pluginIdx);
360+ });
361+
362+ test("does not mention readImage in base instructions", () => {
363+ const prompt = buildSystemPrompt({
364+ cwd: "/project",
365+ date: "2026-04-05",
366+ });
367+
368+ expect(prompt).not.toContain("readImage");
369+ });
370+});