@@ -1,197 +0,0 @@
1-import { afterEach, beforeEach, describe, expect, it } 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 { loadLocalContextFile } from "./context-files.ts";
6-import { buildSystemPrompt } from "./system-prompt.ts";
7-
8-let tmpDir: string;
9-let fakeHome: string;
10-
11-beforeEach(() => {
12- tmpDir = mkdtempSync(join(tmpdir(), "mc-test-"));
13- fakeHome = mkdtempSync(join(tmpdir(), "mc-home-"));
14-});
15-
16-afterEach(() => {
17- rmSync(tmpDir, { recursive: true, force: true });
18- rmSync(fakeHome, { recursive: true, force: true });
19-});
20-
21-describe("loadLocalContextFile", () => {
22- it("returns null when no context files exist", () => {
23- expect(loadLocalContextFile(tmpDir)).toBeNull();
24- });
25-
26- it("reads all context files when multiple exist", () => {
27- mkdirSync(join(tmpDir, ".agents"), { recursive: true });
28- writeFileSync(join(tmpDir, ".agents", "AGENTS.md"), "agents content");
29- writeFileSync(join(tmpDir, "AGENTS.md"), "root content");
30- writeFileSync(join(tmpDir, "CLAUDE.md"), "claude content");
31- const result = loadLocalContextFile(tmpDir) as string;
32- expect(result).toContain("agents content");
33- expect(result).toContain("claude content");
34- expect(result).toContain("root content");
35- });
36-
37- it("reads both CLAUDE.md and AGENTS.md when .agents dir absent", () => {
38- writeFileSync(join(tmpDir, "CLAUDE.md"), "claude content");
39- writeFileSync(join(tmpDir, "AGENTS.md"), "root content");
40- const result = loadLocalContextFile(tmpDir) as string;
41- expect(result).toContain("claude content");
42- expect(result).toContain("root content");
43- });
44-
45- it("reads AGENTS.md at root when others absent", () => {
46- writeFileSync(join(tmpDir, "AGENTS.md"), "root content");
47- expect(loadLocalContextFile(tmpDir)).toBe("root content");
48- });
49-
50- it("reads .claude/CLAUDE.md when .agents files absent", () => {
51- mkdirSync(join(tmpDir, ".claude"), { recursive: true });
52- writeFileSync(join(tmpDir, ".claude", "CLAUDE.md"), "claude dir content");
53- expect(loadLocalContextFile(tmpDir)).toBe("claude dir content");
54- });
55-
56- it("reads .agents/CLAUDE.md when .agents/AGENTS.md absent", () => {
57- mkdirSync(join(tmpDir, ".agents"), { recursive: true });
58- writeFileSync(
59- join(tmpDir, ".agents", "CLAUDE.md"),
60- "agents claude content",
61- );
62- expect(loadLocalContextFile(tmpDir)).toBe("agents claude content");
63- });
64-
65- it("walks ancestor directories up to git root", () => {
66- mkdirSync(join(tmpDir, ".git"), { recursive: true });
67- writeFileSync(join(tmpDir, "AGENTS.md"), "root context");
68- const nested = join(tmpDir, "packages", "lib", "src");
69- mkdirSync(nested, { recursive: true });
70- expect(loadLocalContextFile(nested)).toBe("root context");
71- });
72-
73- it("stops walking at git root and does not go beyond", () => {
74- const gitRoot = join(tmpDir, "repo");
75- mkdirSync(join(gitRoot, ".git"), { recursive: true });
76- writeFileSync(join(tmpDir, "AGENTS.md"), "outside git");
77- const nested = join(gitRoot, "src");
78- mkdirSync(nested, { recursive: true });
79- expect(loadLocalContextFile(nested)).toBeNull();
80- });
81-
82- it("nearest context file wins over ancestor", () => {
83- mkdirSync(join(tmpDir, ".git"), { recursive: true });
84- writeFileSync(join(tmpDir, "AGENTS.md"), "root context");
85- const nested = join(tmpDir, "packages", "lib");
86- mkdirSync(nested, { recursive: true });
87- writeFileSync(join(nested, "AGENTS.md"), "nested context");
88- expect(loadLocalContextFile(nested)).toBe("nested context");
89- });
90-});
91-
92-describe("buildSystemPrompt", () => {
93- it("includes base guidelines without context files", () => {
94- const prompt = buildSystemPrompt("mock-time-anchor", tmpDir, fakeHome);
95- expect(prompt).toContain("You are mini-coder");
96- expect(prompt).toContain("Guidelines:");
97- expect(prompt).not.toContain("# Project context");
98- expect(prompt).toContain("# Safety");
99- expect(prompt).toContain("# Communication");
100- expect(prompt).toContain("# Error handling");
101- expect(prompt).toContain(
102- "Inspect code and files primarily through shell commands",
103- );
104- expect(prompt).toContain("invoke `mc-edit` via shell");
105- expect(prompt).toContain("never guess unknown state");
106- });
107-
108- it("includes local context under # Project context", () => {
109- writeFileSync(join(tmpDir, "AGENTS.md"), "local project info");
110- const prompt = buildSystemPrompt("mock-time-anchor", tmpDir, fakeHome);
111- expect(prompt).toContain("# Project context");
112- expect(prompt).toContain("local project info");
113- });
114-
115- it("has exactly one # Project context section when local context present", () => {
116- writeFileSync(join(tmpDir, "AGENTS.md"), "local info");
117- const prompt = buildSystemPrompt("mock-time-anchor", tmpDir, fakeHome);
118- const occurrences = prompt.split("# Project context").length - 1;
119- expect(occurrences).toBe(1);
120- });
121-
122- it("includes cwd and current time in prompt", () => {
123- const prompt = buildSystemPrompt("mock", tmpDir, fakeHome);
124- expect(prompt).toContain("Current working directory:");
125- expect(prompt).toContain("Current date/time:");
126- });
127-
128- it("includes global context when ~/.agents/AGENTS.md present", () => {
129- mkdirSync(join(fakeHome, ".agents"), { recursive: true });
130- writeFileSync(join(fakeHome, ".agents", "AGENTS.md"), "global info");
131- const prompt = buildSystemPrompt("mock", tmpDir, fakeHome);
132- expect(prompt).toContain("# Project context");
133- expect(prompt).toContain("global info");
134- });
135-
136- it("includes global context from ~/.claude/CLAUDE.md", () => {
137- mkdirSync(join(fakeHome, ".claude"), { recursive: true });
138- writeFileSync(join(fakeHome, ".claude", "CLAUDE.md"), "claude global info");
139- const prompt = buildSystemPrompt("mock", tmpDir, fakeHome);
140- expect(prompt).toContain("# Project context");
141- expect(prompt).toContain("claude global info");
142- });
143-
144- it("includes both global and local context in order (global before local)", () => {
145- mkdirSync(join(fakeHome, ".agents"), { recursive: true });
146- writeFileSync(join(fakeHome, ".agents", "AGENTS.md"), "global info");
147- writeFileSync(join(tmpDir, "AGENTS.md"), "local info");
148- const prompt = buildSystemPrompt("mock", tmpDir, fakeHome);
149- expect(prompt).toContain("global info");
150- expect(prompt).toContain("local info");
151- expect(prompt.indexOf("global info")).toBeLessThan(
152- prompt.indexOf("local info"),
153- );
154- });
155-
156- it("does not include # Project context when neither global nor local context present", () => {
157- const prompt = buildSystemPrompt("mock", tmpDir, fakeHome);
158- expect(prompt).not.toContain("# Project context");
159- });
160-
161- it("includes skill metadata guidance when skills are discoverable", () => {
162- const skillDir = join(tmpDir, ".agents", "skills", "deploy");
163- mkdirSync(skillDir, { recursive: true });
164- writeFileSync(
165- join(skillDir, "SKILL.md"),
166- "---\nname: deploy\ndescription: Deploy safely\n---\n\n# Deploy\nDetailed body",
167- );
168-
169- const prompt = buildSystemPrompt("mock", tmpDir, fakeHome);
170- expect(prompt).toContain("# Skills");
171- expect(prompt).toContain(
172- "call `readSkill` with that skill name before doing anything else",
173- );
174- expect(prompt).toContain("Check ALL skills against the current task");
175- expect(prompt).toContain("<available_skills>");
176- expect(prompt).toContain("<name>deploy</name>");
177- expect(prompt).toContain("<description>Deploy safely</description>");
178- expect(prompt).toContain(".agents/skills/deploy/SKILL.md</location>");
179- expect(prompt).toContain("<source>local</source>");
180- expect(prompt).not.toContain("Detailed body");
181- });
182-
183- it("includes globally discovered skill metadata when homeDir is provided", () => {
184- const skillDir = join(fakeHome, ".agents", "skills", "release");
185- mkdirSync(skillDir, { recursive: true });
186- writeFileSync(
187- join(skillDir, "SKILL.md"),
188- "---\nname: release\ndescription: Ship releases\n---\n\nRelease body",
189- );
190-
191- const prompt = buildSystemPrompt("mock", tmpDir, fakeHome);
192- expect(prompt).toContain("<name>release</name>");
193- expect(prompt).toContain("<description>Ship releases</description>");
194- expect(prompt).toContain("<source>global</source>");
195- expect(prompt).not.toContain("Release body");
196- });
197-});