-
-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathloader.test.ts
More file actions
553 lines (477 loc) · 17.4 KB
/
Copy pathloader.test.ts
File metadata and controls
553 lines (477 loc) · 17.4 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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
import { describe, it, expect, beforeEach, afterAll, vi } from 'vitest';
import { join } from 'node:path';
import { homedir } from 'node:os';
import { writeFileSync, unlinkSync, mkdtempSync, mkdirSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import {
clearSkillsCache,
discoverAllAgents,
loadSkillFromFile,
loadSkillFromMarkdown,
loadSkillsFromDirectory,
resolveAgentAsync,
resolveSkillAsync,
resolveSkillPath,
SkillLoaderError,
SKILL_DIRECTORIES,
AGENT_DIRECTORIES,
AGENT_MARKER_FILE,
} from './loader.js';
vi.mock('./remote.js', () => ({
resolveRemoteSkill: vi.fn(),
resolveRemoteAgent: vi.fn(),
}));
import { resolveRemoteSkill, resolveRemoteAgent } from './remote.js';
describe('loadSkillFromFile', () => {
it('rejects unsupported file types', async () => {
await expect(loadSkillFromFile('/path/to/skill.json')).rejects.toThrow(SkillLoaderError);
await expect(loadSkillFromFile('/path/to/skill.json')).rejects.toThrow('Unsupported skill file');
});
it('throws for missing files', async () => {
await expect(loadSkillFromFile('/nonexistent/skill.md')).rejects.toThrow(SkillLoaderError);
});
});
describe('resolveSkillAsync', () => {
it('resolves skills from conventional directories', async () => {
const repoRoot = new URL('../..', import.meta.url).pathname;
const skill = await resolveSkillAsync('testing-guidelines', repoRoot);
expect(skill.name).toBe('testing-guidelines');
expect(skill.description).toBeDefined();
});
it('throws for unknown skills', async () => {
await expect(resolveSkillAsync('nonexistent-skill')).rejects.toThrow(SkillLoaderError);
await expect(resolveSkillAsync('nonexistent-skill')).rejects.toThrow('Skill not found');
});
it('forwards githubToken and offline to remote skill resolution', async () => {
vi.mocked(resolveRemoteSkill).mockResolvedValue({
name: 'remote-skill',
description: 'from remote',
prompt: 'prompt',
});
await resolveSkillAsync('remote-skill', '/tmp/repo', {
remote: 'owner/repo',
offline: true,
githubToken: 'test-token',
});
expect(resolveRemoteSkill).toHaveBeenCalledWith('owner/repo', 'remote-skill', {
offline: true,
githubToken: 'test-token',
});
});
});
describe('resolveAgentAsync', () => {
it('forwards githubToken and offline to remote agent resolution', async () => {
vi.mocked(resolveRemoteAgent).mockResolvedValue({
name: 'remote-agent',
description: 'from remote',
prompt: 'prompt',
});
await resolveAgentAsync('remote-agent', '/tmp/repo', {
remote: 'owner/repo',
offline: true,
githubToken: 'test-token',
});
expect(resolveRemoteAgent).toHaveBeenCalledWith('owner/repo', 'remote-agent', {
offline: true,
githubToken: 'test-token',
});
});
});
describe('skills caching', () => {
const skillsDir = new URL('../../.claude/skills', import.meta.url).pathname;
beforeEach(() => {
clearSkillsCache();
});
it('caches directory loads', async () => {
const skills1 = await loadSkillsFromDirectory(skillsDir);
expect(skills1.size).toBeGreaterThan(0);
// Second load should return cached result (same reference)
const skills2 = await loadSkillsFromDirectory(skillsDir);
expect(skills2).toBe(skills1);
});
it('clearSkillsCache clears the cache', async () => {
const skills1 = await loadSkillsFromDirectory(skillsDir);
clearSkillsCache();
const skills2 = await loadSkillsFromDirectory(skillsDir);
// After clearing, should be a new Map instance
expect(skills2).not.toBe(skills1);
});
});
describe('rootDir tracking', () => {
const skillsDir = new URL('../../.claude/skills', import.meta.url).pathname;
it('sets rootDir when loading from markdown', async () => {
const skillPath = join(skillsDir, 'testing-guidelines', 'SKILL.md');
const skill = await loadSkillFromMarkdown(skillPath);
expect(skill.rootDir).toBe(join(skillsDir, 'testing-guidelines'));
});
it('sets rootDir for skills from conventional directories', async () => {
const repoRoot = new URL('../..', import.meta.url).pathname;
const skill = await resolveSkillAsync('testing-guidelines', repoRoot);
expect(skill).toBeDefined();
expect(skill.rootDir).toContain('skills');
expect(skill.rootDir).toContain('testing-guidelines');
});
});
describe('direct path resolution', () => {
const skillsDir = new URL('../../.claude/skills', import.meta.url).pathname;
it('resolves skill from directory path with SKILL.md', async () => {
const skillDir = join(skillsDir, 'testing-guidelines');
const skill = await resolveSkillAsync(skillDir);
expect(skill.name).toBe('testing-guidelines');
expect(skill.rootDir).toBe(skillDir);
});
it('resolves skill from file path', async () => {
const skillPath = join(skillsDir, 'testing-guidelines', 'SKILL.md');
const skill = await resolveSkillAsync(skillPath);
expect(skill.name).toBe('testing-guidelines');
});
it('resolves relative path with repoRoot', async () => {
const repoRoot = new URL('../..', import.meta.url).pathname;
const skill = await resolveSkillAsync('./.claude/skills/testing-guidelines', repoRoot);
expect(skill.name).toBe('testing-guidelines');
});
it('throws for nonexistent path', async () => {
await expect(resolveSkillAsync('./nonexistent/skill')).rejects.toThrow(SkillLoaderError);
await expect(resolveSkillAsync('./nonexistent/skill')).rejects.toThrow('Skill not found at path');
});
});
describe('SKILL_DIRECTORIES', () => {
it('contains expected directories in order', () => {
expect(SKILL_DIRECTORIES).toEqual([
'.agents/skills',
'.claude/skills',
'.warden/skills',
]);
});
});
describe('AGENT_DIRECTORIES', () => {
it('contains expected directories in order', () => {
expect(AGENT_DIRECTORIES).toEqual([
'.agents/agents',
'.claude/agents',
'.warden/agents',
]);
});
it('AGENT_MARKER_FILE is AGENT.md', () => {
expect(AGENT_MARKER_FILE).toBe('AGENT.md');
});
});
describe('resolveSkillPath', () => {
it('expands ~ to home directory', () => {
const result = resolveSkillPath('~/code/skills/my-skill');
expect(result).toBe(join(homedir(), 'code/skills/my-skill'));
});
it('expands lone ~ to home directory', () => {
const result = resolveSkillPath('~');
expect(result).toBe(homedir());
});
it('preserves absolute paths', () => {
const absolutePath = '/Users/test/code/skills/my-skill';
const result = resolveSkillPath(absolutePath, '/some/repo');
expect(result).toBe(absolutePath);
});
it('joins relative paths with repoRoot', () => {
const result = resolveSkillPath('./skills/my-skill', '/repo/root');
expect(result).toBe('/repo/root/skills/my-skill');
});
it('returns relative path as-is when no repoRoot', () => {
const result = resolveSkillPath('./skills/my-skill');
expect(result).toBe('./skills/my-skill');
});
});
describe('resolveSkillAsync with absolute and tilde paths', () => {
const skillsDir = new URL('../../.claude/skills', import.meta.url).pathname;
it('resolves absolute path to skill directory', async () => {
const absolutePath = join(skillsDir, 'testing-guidelines');
const skill = await resolveSkillAsync(absolutePath, '/different/repo');
expect(skill.name).toBe('testing-guidelines');
});
it('resolves absolute path to skill file', async () => {
const absolutePath = join(skillsDir, 'testing-guidelines', 'SKILL.md');
const skill = await resolveSkillAsync(absolutePath, '/different/repo');
expect(skill.name).toBe('testing-guidelines');
});
it('resolves tilde path to skill directory', async () => {
// Create a path using ~ that points to the skills dir
const homeRelativePath = skillsDir.replace(homedir(), '~');
// Only run this test if the skills dir is under home
if (homeRelativePath.startsWith('~/')) {
const skill = await resolveSkillAsync(`${homeRelativePath}/testing-guidelines`, '/different/repo');
expect(skill.name).toBe('testing-guidelines');
}
});
});
describe('flat markdown skill files', () => {
const tempDir = mkdtempSync(join(tmpdir(), 'warden-test-'));
const tempSkillPath = join(tempDir, 'my-custom-skill.md');
// Create a flat .md skill file with non-SKILL.md filename
writeFileSync(
tempSkillPath,
`---
name: my-custom-skill
description: A test skill with custom filename
---
This is the prompt content.
`
);
afterAll(() => {
try {
unlinkSync(tempSkillPath);
} catch {
// ignore cleanup errors
}
});
it('loads flat .md files with any filename (not just SKILL.md)', async () => {
const skill = await loadSkillFromFile(tempSkillPath);
expect(skill.name).toBe('my-custom-skill');
expect(skill.description).toBe('A test skill with custom filename');
expect(skill.prompt).toBe('This is the prompt content.');
});
it('loadSkillFromFile accepts .md extension', async () => {
// A flat .md file should be loaded using loadSkillFromMarkdown
// (same as SKILL.md format with frontmatter)
const skillsDir = new URL('../../.claude/skills', import.meta.url).pathname;
const skillMdPath = join(skillsDir, 'testing-guidelines', 'SKILL.md');
const skill = await loadSkillFromFile(skillMdPath);
expect(skill.name).toBe('testing-guidelines');
});
it('loadSkillsFromDirectory returns entry paths for tracking', async () => {
const skillsDir = new URL('../../.claude/skills', import.meta.url).pathname;
clearSkillsCache();
const skills = await loadSkillsFromDirectory(skillsDir);
// Each loaded skill should have an entry field matching the directory name
const skillWriter = skills.get('testing-guidelines');
expect(skillWriter).toBeDefined();
expect(skillWriter!.skill.name).toBe('testing-guidelines');
expect(skillWriter!.entry).toBe('testing-guidelines');
});
it('loadSkillsFromDirectory calls onWarning for malformed skills', async () => {
const warnings: string[] = [];
const onWarning = (message: string) => warnings.push(message);
// Create a temp directory with a malformed skill
const tempDir = join(import.meta.dirname, '.test-malformed-skills');
try {
mkdirSync(tempDir, { recursive: true });
// Create a .md file with frontmatter but missing required name field
writeFileSync(
join(tempDir, 'bad-skill.md'),
`---
description: Missing name field
---
Content here
`
);
clearSkillsCache();
await loadSkillsFromDirectory(tempDir, { onWarning });
expect(warnings.length).toBe(1);
expect(warnings[0]).toContain('bad-skill.md');
expect(warnings[0]).toContain("missing 'name'");
} finally {
rmSync(tempDir, { recursive: true, force: true });
}
});
it('warns when invalid tool names are filtered from allowed-tools', async () => {
const warnings: string[] = [];
const onWarning = (message: string) => warnings.push(message);
// Create a temp directory with a skill containing invalid tool names
const tempDir2 = join(import.meta.dirname, '.test-invalid-tools');
try {
mkdirSync(tempDir2, { recursive: true });
// Create a skill with a mix of valid and invalid tool names
writeFileSync(
join(tempDir2, 'test-skill.md'),
`---
name: test-skill
description: A test skill with invalid tools
allowed-tools: Read InvalidTool Grep FakeTool
---
Test prompt content.
`
);
clearSkillsCache();
const skills = await loadSkillsFromDirectory(tempDir2, { onWarning });
// Skill should still load with only valid tools
const skill = skills.get('test-skill');
expect(skill).toBeDefined();
expect(skill!.skill.tools?.allowed).toEqual(['Read', 'Grep']);
// Should have warnings for each invalid tool
expect(warnings.length).toBe(2);
expect(warnings[0]).toContain("Invalid tool name 'InvalidTool'");
expect(warnings[0]).toContain('ignored');
expect(warnings[0]).toContain('Valid tools:');
expect(warnings[1]).toContain("Invalid tool name 'FakeTool'");
} finally {
rmSync(tempDir2, { recursive: true, force: true });
}
});
});
describe('loadSkillsFromDirectory with markerFile', () => {
it('uses AGENT.md as marker file when specified', async () => {
const agentDir = mkdtempSync(join(tmpdir(), 'warden-agent-test-'));
try {
// Create a directory-format agent with AGENT.md
mkdirSync(join(agentDir, 'my-agent'), { recursive: true });
writeFileSync(
join(agentDir, 'my-agent', 'AGENT.md'),
`---
name: my-agent
description: A test agent
---
Agent prompt content.
`
);
clearSkillsCache();
const agents = await loadSkillsFromDirectory(agentDir, { markerFile: 'AGENT.md' });
expect(agents.size).toBe(1);
const agent = agents.get('my-agent');
expect(agent).toBeDefined();
expect(agent!.skill.name).toBe('my-agent');
expect(agent!.skill.prompt).toBe('Agent prompt content.');
} finally {
rmSync(agentDir, { recursive: true, force: true });
}
});
it('ignores SKILL.md when markerFile is AGENT.md', async () => {
const mixedDir = mkdtempSync(join(tmpdir(), 'warden-mixed-test-'));
try {
// Create a directory with SKILL.md (should be ignored)
mkdirSync(join(mixedDir, 'my-skill'), { recursive: true });
writeFileSync(
join(mixedDir, 'my-skill', 'SKILL.md'),
`---
name: my-skill
description: A skill not an agent
---
Skill prompt.
`
);
// Create a directory with AGENT.md (should be found)
mkdirSync(join(mixedDir, 'my-agent'), { recursive: true });
writeFileSync(
join(mixedDir, 'my-agent', 'AGENT.md'),
`---
name: my-agent
description: An agent
---
Agent prompt.
`
);
clearSkillsCache();
const agents = await loadSkillsFromDirectory(mixedDir, { markerFile: 'AGENT.md' });
expect(agents.size).toBe(1);
expect(agents.has('my-agent')).toBe(true);
expect(agents.has('my-skill')).toBe(false);
} finally {
rmSync(mixedDir, { recursive: true, force: true });
}
});
it('caches separately for different markerFiles', async () => {
const cacheDir = mkdtempSync(join(tmpdir(), 'warden-cache-test-'));
try {
mkdirSync(join(cacheDir, 'entry'), { recursive: true });
writeFileSync(
join(cacheDir, 'entry', 'SKILL.md'),
`---
name: a-skill
description: Skill
---
Prompt.
`
);
writeFileSync(
join(cacheDir, 'entry', 'AGENT.md'),
`---
name: an-agent
description: Agent
---
Prompt.
`
);
clearSkillsCache();
const skills = await loadSkillsFromDirectory(cacheDir);
const agents = await loadSkillsFromDirectory(cacheDir, { markerFile: 'AGENT.md' });
expect(skills.has('a-skill')).toBe(true);
expect(agents.has('an-agent')).toBe(true);
// They should be different Map instances (different cache keys)
expect(skills).not.toBe(agents);
} finally {
rmSync(cacheDir, { recursive: true, force: true });
}
});
});
describe('discoverAllAgents', () => {
it('discovers agents from .agents/agents directory', async () => {
const repoRoot = mkdtempSync(join(tmpdir(), 'warden-discover-agents-'));
try {
mkdirSync(join(repoRoot, '.agents', 'agents', 'my-agent'), { recursive: true });
writeFileSync(
join(repoRoot, '.agents', 'agents', 'my-agent', 'AGENT.md'),
`---
name: my-agent
description: Test agent
---
Agent prompt.
`
);
clearSkillsCache();
const agents = await discoverAllAgents(repoRoot);
expect(agents.size).toBe(1);
const agent = agents.get('my-agent');
expect(agent).toBeDefined();
expect(agent!.skill.name).toBe('my-agent');
expect(agent!.directory).toBe('./.agents/agents');
} finally {
rmSync(repoRoot, { recursive: true, force: true });
}
});
it('returns empty map when no repoRoot', async () => {
const agents = await discoverAllAgents();
expect(agents.size).toBe(0);
});
});
describe('resolveAgentAsync', () => {
it('resolves agent by name from conventional directories', async () => {
const repoRoot = mkdtempSync(join(tmpdir(), 'warden-resolve-agent-'));
try {
mkdirSync(join(repoRoot, '.agents', 'agents', 'test-agent'), { recursive: true });
writeFileSync(
join(repoRoot, '.agents', 'agents', 'test-agent', 'AGENT.md'),
`---
name: test-agent
description: A test agent
---
Agent instructions.
`
);
clearSkillsCache();
const agent = await resolveAgentAsync('test-agent', repoRoot);
expect(agent.name).toBe('test-agent');
expect(agent.description).toBe('A test agent');
expect(agent.prompt).toBe('Agent instructions.');
} finally {
rmSync(repoRoot, { recursive: true, force: true });
}
});
it('throws for unknown agents', async () => {
await expect(resolveAgentAsync('nonexistent-agent')).rejects.toThrow(SkillLoaderError);
await expect(resolveAgentAsync('nonexistent-agent')).rejects.toThrow('Agent not found');
});
it('resolves agent from direct path', async () => {
const agentDir = mkdtempSync(join(tmpdir(), 'warden-agent-path-'));
try {
writeFileSync(
join(agentDir, 'AGENT.md'),
`---
name: path-agent
description: Agent from path
---
Path agent prompt.
`
);
const agent = await resolveAgentAsync(agentDir);
expect(agent.name).toBe('path-agent');
} finally {
rmSync(agentDir, { recursive: true, force: true });
}
});
});