-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathconfig.test.ts
More file actions
390 lines (333 loc) · 11.8 KB
/
Copy pathconfig.test.ts
File metadata and controls
390 lines (333 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
import { expect, test, describe, beforeEach, afterEach } from "bun:test";
import {
loadGlobalConfig,
getCommandDefaults,
applyInteractiveMode,
clearConfigCache,
findGitRoot,
loadProjectConfig,
loadFullConfig,
clearProjectConfigCache,
} from "./config";
import type { AgentFrontmatter } from "./types";
import { mkdirSync, writeFileSync, rmSync, existsSync } from "fs";
import { join } from "path";
import { tmpdir } from "os";
describe("config", () => {
beforeEach(() => {
clearConfigCache();
});
test("loadGlobalConfig returns built-in defaults", async () => {
const config = await loadGlobalConfig();
expect(config.commands).toBeDefined();
expect(config.commands?.copilot).toBeDefined();
expect(config.commands!.copilot!.$1).toBe("prompt"); // Print mode by default
expect(config.commands?.claude?.print).toBe(true); // Print mode by default
expect(config.commands?.codex?._subcommand).toBe("exec"); // Exec subcommand by default
});
test("getCommandDefaults returns defaults for copilot", async () => {
const defaults = await getCommandDefaults("copilot");
expect(defaults).toBeDefined();
expect(defaults?.$1).toBe("prompt"); // Print mode by default
});
test("getCommandDefaults returns undefined for unknown command", async () => {
const defaults = await getCommandDefaults("unknown-command");
expect(defaults).toBeUndefined();
});
// NOTE: Pure merge-logic tests (identity, right-bias, associativity) have been
// replaced by property-based tests in config-monoid.test.ts. See docs/mutation-proof.md
// for the equivalence proof showing property tests catch a strict superset of bugs.
});
describe("findGitRoot", () => {
test("finds git root from current directory", () => {
// The test is running inside the agents repo
const gitRoot = findGitRoot(process.cwd());
expect(gitRoot).not.toBeNull();
expect(existsSync(join(gitRoot!, ".git"))).toBe(true);
});
test("finds git root from subdirectory", () => {
const gitRoot = findGitRoot(join(process.cwd(), "src"));
expect(gitRoot).not.toBeNull();
expect(existsSync(join(gitRoot!, ".git"))).toBe(true);
});
test("returns null for non-git directory", () => {
const gitRoot = findGitRoot(tmpdir());
// tmpdir might be in a git repo on some systems, so we just check it doesn't error
expect(gitRoot === null || typeof gitRoot === "string").toBe(true);
});
});
describe("loadProjectConfig", () => {
const testDir = join(tmpdir(), `md-test-${Date.now()}`);
const subDir = join(testDir, "subdir");
beforeEach(() => {
clearProjectConfigCache();
mkdirSync(subDir, { recursive: true });
});
afterEach(() => {
rmSync(testDir, { recursive: true, force: true });
});
test("returns empty config when no project config exists", async () => {
const config = await loadProjectConfig(testDir);
expect(config).toEqual({});
});
test("loads mdflow.config.yaml from CWD", async () => {
writeFileSync(
join(testDir, "mdflow.config.yaml"),
`commands:
claude:
model: opus
`
);
const config = await loadProjectConfig(testDir);
expect(config.commands?.claude?.model).toBe("opus");
});
test("loads .mdflow.yaml from CWD", async () => {
writeFileSync(
join(testDir, ".mdflow.yaml"),
`commands:
claude:
model: sonnet
`
);
const config = await loadProjectConfig(testDir);
expect(config.commands?.claude?.model).toBe("sonnet");
});
test("loads .mdflow.json from CWD", async () => {
writeFileSync(
join(testDir, ".mdflow.json"),
JSON.stringify({
commands: {
claude: {
model: "haiku",
},
},
})
);
const config = await loadProjectConfig(testDir);
expect(config.commands?.claude?.model).toBe("haiku");
});
test("prefers mdflow.config.yaml over .mdflow.yaml", async () => {
writeFileSync(
join(testDir, "mdflow.config.yaml"),
`commands:
claude:
model: opus
`
);
writeFileSync(
join(testDir, ".mdflow.yaml"),
`commands:
claude:
model: sonnet
`
);
const config = await loadProjectConfig(testDir);
expect(config.commands?.claude?.model).toBe("opus");
});
test("handles invalid YAML gracefully", async () => {
writeFileSync(join(testDir, "mdflow.config.yaml"), "invalid: yaml: content:");
const config = await loadProjectConfig(testDir);
// Should return empty config on parse error
expect(config).toEqual({});
});
test("handles invalid JSON gracefully", async () => {
writeFileSync(join(testDir, ".mdflow.json"), "{ invalid json }");
const config = await loadProjectConfig(testDir);
expect(config).toEqual({});
});
});
describe("loadFullConfig", () => {
const testDir = join(tmpdir(), `md-full-test-${Date.now()}`);
beforeEach(() => {
clearConfigCache();
mkdirSync(testDir, { recursive: true });
});
afterEach(() => {
rmSync(testDir, { recursive: true, force: true });
});
test("includes built-in defaults when no project config", async () => {
const config = await loadFullConfig(testDir);
expect(config.commands?.copilot?.$1).toBe("prompt"); // Print mode by default
});
test("project config overrides global config", async () => {
writeFileSync(
join(testDir, "mdflow.config.yaml"),
`commands:
copilot:
$1: custom-prompt
`
);
const config = await loadFullConfig(testDir);
expect(config.commands?.copilot?.$1).toBe("custom-prompt");
});
test("project config adds new commands", async () => {
writeFileSync(
join(testDir, "mdflow.config.yaml"),
`commands:
my-tool:
$1: body
verbose: true
`
);
const config = await loadFullConfig(testDir);
// Built-in defaults preserved
expect(config.commands?.copilot?.$1).toBe("prompt"); // Print mode by default
// New command added
expect(config.commands?.["my-tool"]?.$1).toBe("body");
expect(config.commands?.["my-tool"]?.verbose).toBe(true);
});
test("project config merges with existing command", async () => {
writeFileSync(
join(testDir, "mdflow.config.yaml"),
`commands:
copilot:
verbose: true
`
);
const config = await loadFullConfig(testDir);
// Built-in default preserved
expect(config.commands?.copilot?.$1).toBe("prompt"); // Print mode by default
// New setting added
expect(config.commands?.copilot?.verbose).toBe(true);
});
});
describe("config cascade", () => {
let testDir: string;
let gitRoot: string;
let subDir: string;
beforeEach(() => {
clearConfigCache();
// Use unique directory per test to avoid cache issues
testDir = join(tmpdir(), `md-cascade-test-${Date.now()}-${Math.random().toString(36).slice(2)}`);
gitRoot = join(testDir, "repo");
subDir = join(gitRoot, "packages", "app");
// Create a fake git repo structure
mkdirSync(join(gitRoot, ".git"), { recursive: true });
mkdirSync(subDir, { recursive: true });
});
afterEach(() => {
rmSync(testDir, { recursive: true, force: true });
});
test("CWD config overrides git root config", async () => {
// Git root config
writeFileSync(
join(gitRoot, "mdflow.config.yaml"),
`commands:
claude:
model: sonnet
verbose: true
`
);
// CWD config (subdirectory)
writeFileSync(
join(subDir, "mdflow.config.yaml"),
`commands:
claude:
model: opus
`
);
const config = await loadProjectConfig(subDir);
// CWD wins for model
expect(config.commands?.claude?.model).toBe("opus");
// Git root setting preserved
expect(config.commands?.claude?.verbose).toBe(true);
});
test("git root config used when CWD has no config", async () => {
writeFileSync(
join(gitRoot, "mdflow.config.yaml"),
`commands:
claude:
model: sonnet
`
);
const config = await loadProjectConfig(subDir);
expect(config.commands?.claude?.model).toBe("sonnet");
});
test("only CWD config used when at git root", async () => {
writeFileSync(
join(gitRoot, "mdflow.config.yaml"),
`commands:
claude:
model: opus
`
);
const config = await loadProjectConfig(gitRoot);
expect(config.commands?.claude?.model).toBe("opus");
});
});
describe("applyInteractiveMode", () => {
test("removes print flag for claude with _interactive: true", () => {
const frontmatter = { print: true, model: "opus", _interactive: true } as AgentFrontmatter;
const result = applyInteractiveMode(frontmatter, "claude");
expect(result.print).toBeUndefined();
expect(result._interactive).toBeUndefined();
expect(result.model).toBe("opus");
});
test("removes print flag for claude with _i: true", () => {
const frontmatter = { print: true, model: "opus", _i: true } as AgentFrontmatter;
const result = applyInteractiveMode(frontmatter, "claude");
expect(result.print).toBeUndefined();
expect(result._i).toBeUndefined();
expect(result.model).toBe("opus");
});
test("handles _interactive with null value (YAML empty key)", () => {
const frontmatter = { print: true, _interactive: null } as AgentFrontmatter;
const result = applyInteractiveMode(frontmatter, "claude");
expect(result.print).toBeUndefined();
expect(result._interactive).toBeUndefined();
});
test("handles _i with null value (YAML empty key)", () => {
const frontmatter = { print: true, _i: null } as AgentFrontmatter;
const result = applyInteractiveMode(frontmatter, "claude");
expect(result.print).toBeUndefined();
expect(result._i).toBeUndefined();
});
test("handles _interactive with empty string value", () => {
const frontmatter = { print: true, _interactive: "" };
const result = applyInteractiveMode(frontmatter, "claude");
expect(result.print).toBeUndefined();
});
test("handles _i with empty string value", () => {
const frontmatter = { print: true, _i: "" };
const result = applyInteractiveMode(frontmatter, "claude");
expect(result.print).toBeUndefined();
});
test("does not trigger interactive mode with _interactive: false", () => {
const frontmatter = { print: true, _interactive: false } as AgentFrontmatter;
const result = applyInteractiveMode(frontmatter, "claude");
expect(result.print).toBe(true);
});
test("does not trigger interactive mode when _interactive not present", () => {
const frontmatter = { print: true, model: "opus" };
const result = applyInteractiveMode(frontmatter, "claude");
expect(result.print).toBe(true);
});
test("triggers interactive mode via external flag (interactiveFromExternal)", () => {
const frontmatter = { print: true, model: "opus" };
const result = applyInteractiveMode(frontmatter, "claude", true);
expect(result.print).toBeUndefined();
expect(result.model).toBe("opus");
});
test("changes copilot $1 from prompt to interactive", () => {
const frontmatter = { $1: "prompt", silent: true, _interactive: true } as AgentFrontmatter;
const result = applyInteractiveMode(frontmatter, "copilot");
expect(result.$1).toBe("interactive");
expect(result.silent).toBe(true);
});
test("removes _subcommand for codex", () => {
const frontmatter = { _subcommand: "exec", _interactive: true } as AgentFrontmatter;
const result = applyInteractiveMode(frontmatter, "codex");
expect(result._subcommand).toBeUndefined();
});
test("adds prompt-interactive for gemini", () => {
const frontmatter = { model: "pro", _interactive: true } as AgentFrontmatter;
const result = applyInteractiveMode(frontmatter, "gemini");
expect(result.$1).toBe("prompt-interactive");
});
test("unknown command just removes _interactive", () => {
const frontmatter = { custom: "value", _interactive: true } as AgentFrontmatter;
const result = applyInteractiveMode(frontmatter, "my-custom-cli");
expect(result._interactive).toBeUndefined();
expect(result.custom).toBe("value");
});
});