-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
executable file
·725 lines (657 loc) · 32.8 KB
/
test.js
File metadata and controls
executable file
·725 lines (657 loc) · 32.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
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
#!/usr/bin/env node
'use strict';
const fs = require('fs');
const path = require('path');
const os = require('os');
const { execFileSync, execSync } = require('child_process');
const REGISTRY_DIR = path.resolve(__dirname);
const CLI = path.join(REGISTRY_DIR, 'bin', 'cli.js');
let passed = 0;
let failed = 0;
function check(name, condition, detail) {
if (condition) { console.log(` PASS: ${name}`); passed++; }
else { console.log(` FAIL: ${name}${detail ? ' — ' + detail : ''}`); failed++; }
}
function run(args, env) {
try {
const stdout = execFileSync(process.execPath, [CLI, ...args], {
cwd: REGISTRY_DIR,
env: { ...process.env, ...(env || {}) },
encoding: 'utf8',
timeout: 30000,
});
return { status: 0, stdout, stderr: '' };
} catch (e) {
return { status: e.status || 1, stdout: (e.stdout || '').toString(), stderr: (e.stderr || '').toString() };
}
}
function tmpDir() { return fs.mkdtempSync(path.join(os.tmpdir(), 'ar-test-')); }
function rmrf(d) { fs.rmSync(d, { recursive: true, force: true }); }
function writeAgent(reg, name, fm, body) {
const dir = path.join(reg, 'agents', name);
fs.mkdirSync(dir, { recursive: true });
fs.writeFileSync(path.join(dir, 'agent.md'), `---\n${fm}\n---\n\n${body || 'Body.'}\n`);
}
function copyLib(reg) {
execSync(`cp -r "${path.join(REGISTRY_DIR, 'bin')}" "${reg}/"`);
execSync(`cp -r "${path.join(REGISTRY_DIR, 'lib')}" "${reg}/"`);
execSync(`cp "${path.join(REGISTRY_DIR, 'package.json')}" "${reg}/"`);
}
console.log('=== Agent Registry — Test Suite ===\n');
// ── Frontmatter Parser ──────────────────────────────────────
console.log('--- Frontmatter Parser ---');
try {
execFileSync(process.execPath, [path.join(REGISTRY_DIR, 'lib', 'test-frontmatter.js')], {
cwd: REGISTRY_DIR, encoding: 'utf8', timeout: 30000
});
check('frontmatter parser tests all pass', true);
} catch (e) {
check('frontmatter parser tests all pass', false, (e.stdout || '').toString().split('\n').pop());
}
// ── Profile Module ──────────────────────────────────────────
console.log('\n--- Profile Module ---');
try {
execFileSync(process.execPath, [path.join(REGISTRY_DIR, 'lib', 'test-profiles.js')], {
cwd: REGISTRY_DIR, encoding: 'utf8', timeout: 30000
});
check('profile module tests all pass', true);
} catch (e) {
check('profile module tests all pass', false, (e.stdout || '').toString().split('\n').pop());
}
// ── Agent Install ───────────────────────────────────────────
console.log('\n--- Agent Installation ---');
{
const home = tmpDir();
run(['install', '--agent', 'cit-deck-creator'], { HOME: home });
const dst = path.join(home, '.claude', 'commands', 'cit-deck-creator.md');
check('agent file created', fs.existsSync(dst));
if (fs.existsSync(dst)) {
const content = fs.readFileSync(dst, 'utf8');
check('has registry path comment', content.startsWith('<!-- agent-registry-path:'));
check('frontmatter stripped', !content.includes('---\nname:'));
}
// Skill dependency auto-installed
check('skill dependency auto-installed', fs.existsSync(path.join(home, '.claude', 'commands', 'slides')));
rmrf(home);
}
// ── Behavior Injection ─────────────────────────────────────
console.log('\n--- Behavior Injection ---');
{
const reg = tmpDir();
const home = tmpDir();
fs.mkdirSync(path.join(reg, 'behaviors'), { recursive: true });
fs.writeFileSync(path.join(reg, 'behaviors', 'test-rule.md'),
'---\nname: test-rule\ndescription: A test rule\n---\n\n## Test Rule\n\nAlways verify before committing.\n');
writeAgent(reg, 'behave-agent',
'name: behave-agent\ndescription: B\nversion: 1.0.0\nauthor: Me\nbehaviors:\n - test-rule',
'Do your job.');
copyLib(reg);
execFileSync(process.execPath, [path.join(reg, 'bin', 'cli.js'), 'install', '--agent', 'behave-agent'], {
cwd: reg, env: { ...process.env, HOME: home }, encoding: 'utf8', timeout: 30000
});
const dst = path.join(home, '.claude', 'commands', 'behave-agent.md');
check('agent with behaviors installed', fs.existsSync(dst));
if (fs.existsSync(dst)) {
const content = fs.readFileSync(dst, 'utf8');
check('has behaviors start marker', content.includes('<!-- behaviors:start -->'));
check('has behaviors end marker', content.includes('<!-- behaviors:end -->'));
check('behavior content injected', content.includes('## Test Rule'));
check('behavior content includes rule', content.includes('Always verify before committing.'));
check('agent body still present', content.includes('Do your job.'));
check('behaviors appear before body', content.indexOf('## Test Rule') < content.indexOf('Do your job.'));
}
rmrf(reg); rmrf(home);
}
console.log('\n--- Missing Behavior Error ---');
{
const reg = tmpDir();
const home = tmpDir();
writeAgent(reg, 'bad-behave',
'name: bad-behave\ndescription: B\nversion: 1.0.0\nauthor: Me\nbehaviors:\n - nonexistent');
copyLib(reg);
const result = (() => {
try {
execFileSync(process.execPath, [path.join(reg, 'bin', 'cli.js'), 'install', '--agent', 'bad-behave'], {
cwd: reg, env: { ...process.env, HOME: home }, encoding: 'utf8', timeout: 30000
});
return { status: 0 };
} catch (e) {
return { status: 1, stderr: (e.stderr || '').toString(), stdout: (e.stdout || '').toString() };
}
})();
check('missing behavior rejects install', result.status !== 0);
rmrf(reg); rmrf(home);
}
console.log('\n--- Agent Without Behaviors Unchanged ---');
{
const reg = tmpDir();
const home = tmpDir();
writeAgent(reg, 'plain-agent', 'name: plain-agent\ndescription: P\nversion: 1.0.0\nauthor: Me', 'Plain body.');
copyLib(reg);
execFileSync(process.execPath, [path.join(reg, 'bin', 'cli.js'), 'install', '--agent', 'plain-agent'], {
cwd: reg, env: { ...process.env, HOME: home }, encoding: 'utf8', timeout: 30000
});
const dst = path.join(home, '.claude', 'commands', 'plain-agent.md');
if (fs.existsSync(dst)) {
const content = fs.readFileSync(dst, 'utf8');
check('no behaviors markers when none declared', !content.includes('<!-- behaviors:'));
check('plain body present', content.includes('Plain body.'));
}
rmrf(reg); rmrf(home);
}
// ── Skill Install ───────────────────────────────────────────
console.log('\n--- Skill Installation ---');
{
const home = tmpDir();
run(['install', '--skill', 'slides'], { HOME: home });
const link = path.join(home, '.claude', 'commands', 'slides');
const isLink = fs.existsSync(link) && fs.lstatSync(link).isSymbolicLink();
check('skill symlink created', isLink);
if (isLink) {
check('symlink points to registry', fs.readlinkSync(link).includes('skills/slides/commands'));
}
rmrf(home);
}
// ── Orchestrator Subagent Auto-Install ──────────────────────
console.log('\n--- Orchestrator Subagent Auto-Install ---');
{
const reg = tmpDir();
const home = tmpDir();
writeAgent(reg, 'sub-a', 'name: sub-a\ndescription: A\nversion: 1.0.0\nauthor: Me');
writeAgent(reg, 'sub-b', 'name: sub-b\ndescription: B\nversion: 1.0.0\nauthor: Me');
writeAgent(reg, 'my-orch', 'name: my-orch\ndescription: O\nversion: 1.0.0\nauthor: Me\ntype: orchestrator\nsubagents:\n - sub-a\n - sub-b');
copyLib(reg);
execFileSync(process.execPath, [path.join(reg, 'bin', 'cli.js'), 'install', '--agent', 'my-orch'], {
cwd: reg, env: { ...process.env, HOME: home }, encoding: 'utf8', timeout: 30000
});
const cmds = path.join(home, '.claude', 'commands');
check('orchestrator installed', fs.existsSync(path.join(cmds, 'my-orch.md')));
check('subagent sub-a auto-installed', fs.existsSync(path.join(cmds, 'sub-a.md')));
check('subagent sub-b auto-installed', fs.existsSync(path.join(cmds, 'sub-b.md')));
rmrf(reg); rmrf(home);
}
// ── Uninstall ───────────────────────────────────────────────
console.log('\n--- Uninstall ---');
{
const home = tmpDir();
run(['install', '--agent', 'devops'], { HOME: home });
check('agent exists before uninstall', fs.existsSync(path.join(home, '.claude', 'commands', 'devops.md')));
run(['uninstall', '--agent', 'devops'], { HOME: home });
check('agent removed after uninstall', !fs.existsSync(path.join(home, '.claude', 'commands', 'devops.md')));
rmrf(home);
}
// ── Uninstall Subagent Warns ────────────────────────────────
console.log('\n--- Uninstall Subagent Warning ---');
{
const reg = tmpDir();
const home = tmpDir();
writeAgent(reg, 'dep', 'name: dep\ndescription: D\nversion: 1.0.0\nauthor: Me');
writeAgent(reg, 'orch', 'name: orch\ndescription: O\nversion: 1.0.0\nauthor: Me\ntype: orchestrator\nsubagents:\n - dep');
copyLib(reg);
const cli = path.join(reg, 'bin', 'cli.js');
execFileSync(process.execPath, [cli, 'install', '--agent', 'orch'], { cwd: reg, env: { ...process.env, HOME: home }, encoding: 'utf8' });
let out = '';
try { out = execFileSync(process.execPath, [cli, 'uninstall', '--agent', 'dep'], { cwd: reg, env: { ...process.env, HOME: home }, encoding: 'utf8' }); } catch (e) { out = (e.stdout || '').toString(); }
check('warns about orchestrator dependency', /warning/i.test(out) && out.includes('orch'));
rmrf(reg); rmrf(home);
}
// ── Uninstall Orchestrator Keeps Subagents ──────────────────
console.log('\n--- Uninstall Orchestrator Keeps Subagents ---');
{
const reg = tmpDir();
const home = tmpDir();
writeAgent(reg, 'kept', 'name: kept\ndescription: K\nversion: 1.0.0\nauthor: Me');
writeAgent(reg, 'rem', 'name: rem\ndescription: R\nversion: 1.0.0\nauthor: Me\ntype: orchestrator\nsubagents:\n - kept');
copyLib(reg);
const cli = path.join(reg, 'bin', 'cli.js');
execFileSync(process.execPath, [cli, 'install', '--agent', 'rem'], { cwd: reg, env: { ...process.env, HOME: home }, encoding: 'utf8' });
execFileSync(process.execPath, [cli, 'uninstall', '--agent', 'rem'], { cwd: reg, env: { ...process.env, HOME: home }, encoding: 'utf8' });
const cmds = path.join(home, '.claude', 'commands');
check('orchestrator removed', !fs.existsSync(path.join(cmds, 'rem.md')));
check('subagent kept', fs.existsSync(path.join(cmds, 'kept.md')));
rmrf(reg); rmrf(home);
}
// ── Circular Dependency Guard ───────────────────────────────
console.log('\n--- Circular Dependency Guard ---');
{
const reg = tmpDir();
const home = tmpDir();
writeAgent(reg, 'ca', 'name: ca\ndescription: A\nversion: 1.0.0\nauthor: Me\ntype: orchestrator\nsubagents:\n - cb');
writeAgent(reg, 'cb', 'name: cb\ndescription: B\nversion: 1.0.0\nauthor: Me\ntype: orchestrator\nsubagents:\n - ca');
copyLib(reg);
let completed = false;
try {
execFileSync(process.execPath, [path.join(reg, 'bin', 'cli.js'), 'install', '--agent', 'ca'], {
cwd: reg, env: { ...process.env, HOME: home }, encoding: 'utf8', timeout: 10000
});
completed = true;
} catch { completed = true; /* threw but didn't hang */ }
check('circular dependency does not hang', completed);
rmrf(reg); rmrf(home);
}
// ── Name Validation ─────────────────────────────────────────
console.log('\n--- Name Validation ---');
{
const home = tmpDir();
const r1 = run(['install', '--agent', '../traversal'], { HOME: home });
check('rejects path traversal', r1.status !== 0 || /invalid/i.test(r1.stdout + r1.stderr));
const r2 = run(['install', '--agent', '.hidden'], { HOME: home });
check('rejects hidden dir name', r2.status !== 0 || /invalid/i.test(r2.stdout + r2.stderr));
const r3 = run(['install', '--agent', 'a'.repeat(129)], { HOME: home });
check('rejects name > 128 chars', r3.status !== 0 || /invalid/i.test(r3.stdout + r3.stderr));
rmrf(home);
}
// ── List Groups Correctly ───────────────────────────────────
console.log('\n--- List Command ---');
{
const r = run(['list']);
check('list succeeds', r.status === 0);
check('list shows agents', /cit-deck-creator|devops/i.test(r.stdout));
}
// ── Integration: Full Lifecycle ─────────────────────────────
console.log('\n--- Integration: Full Lifecycle ---');
{
const home = tmpDir();
const r1 = run(['install'], { HOME: home });
check('install all succeeds', r1.status === 0);
const r2 = run(['status'], { HOME: home });
check('status shows installed', /installed/i.test(r2.stdout));
const r3 = run(['uninstall', '--all'], { HOME: home });
check('uninstall all succeeds', r3.status === 0);
const r4 = run(['status'], { HOME: home });
check('status shows not installed', /not installed/i.test(r4.stdout));
rmrf(home);
}
// ── Integration: Project Mode ───────────────────────────────
console.log('\n--- Integration: Project Mode ---');
{
const home = tmpDir();
const proj = tmpDir();
run(['project', 'devops', proj], { HOME: home });
const claudeMd = path.join(proj, '.claude', 'CLAUDE.md');
check('CLAUDE.md created', fs.existsSync(claudeMd));
if (fs.existsSync(claudeMd)) {
const content = fs.readFileSync(claudeMd, 'utf8');
check('CLAUDE.md has agent content', /infrastructure/i.test(content));
check('ref paths rewritten', content.includes('.claude/ref/devops/'));
}
check('ref docs copied', fs.existsSync(path.join(proj, '.claude', 'ref', 'devops', 'deployment-runbook.md')));
rmrf(home); rmrf(proj);
}
// ── Update Command ─────────────────────────────────────────
console.log('\n--- Update Command ---');
{
const reg = tmpDir();
const home = tmpDir();
// Create behavior + agent
fs.mkdirSync(path.join(reg, 'behaviors'), { recursive: true });
fs.writeFileSync(path.join(reg, 'behaviors', 'rule-v1.md'),
'---\nname: rule-v1\ndescription: V1\n---\n\n## Rule V1\n\nVersion one.\n');
writeAgent(reg, 'up-agent',
'name: up-agent\ndescription: U\nversion: 1.0.0\nauthor: Me\nbehaviors:\n - rule-v1',
'Agent body.');
copyLib(reg);
const cli = path.join(reg, 'bin', 'cli.js');
// Initial install
execFileSync(process.execPath, [cli, 'install', '--agent', 'up-agent'], {
cwd: reg, env: { ...process.env, HOME: home }, encoding: 'utf8', timeout: 30000
});
const dst = path.join(home, '.claude', 'commands', 'up-agent.md');
check('initial install has v1 content', fs.readFileSync(dst, 'utf8').includes('Version one.'));
// Update the behavior file
fs.writeFileSync(path.join(reg, 'behaviors', 'rule-v1.md'),
'---\nname: rule-v1\ndescription: V1\n---\n\n## Rule V1\n\nVersion two updated.\n');
// Run update
execFileSync(process.execPath, [cli, 'update'], {
cwd: reg, env: { ...process.env, HOME: home }, encoding: 'utf8', timeout: 30000
});
check('after update has v2 content', fs.readFileSync(dst, 'utf8').includes('Version two updated.'));
check('after update v1 content gone', !fs.readFileSync(dst, 'utf8').includes('Version one.'));
rmrf(reg); rmrf(home);
}
// ── List Shows Behaviors ───────────────────────────────────
console.log('\n--- List Shows Behaviors ---');
{
const reg = tmpDir();
fs.mkdirSync(path.join(reg, 'behaviors'), { recursive: true });
fs.writeFileSync(path.join(reg, 'behaviors', 'my-rule.md'),
'---\nname: my-rule\ndescription: A discipline rule\n---\n\nContent.\n');
fs.mkdirSync(path.join(reg, 'agents'), { recursive: true });
copyLib(reg);
let out = '';
try {
out = execFileSync(process.execPath, [path.join(reg, 'bin', 'cli.js'), 'list'], {
cwd: reg, encoding: 'utf8', timeout: 30000
});
} catch (e) { out = (e.stdout || '').toString(); }
check('list shows behaviors section', /behaviors/i.test(out));
check('list shows my-rule', out.includes('my-rule'));
check('list shows description', out.includes('A discipline rule'));
rmrf(reg);
}
// ── Criteria Injection ─────────────────────────────────────
console.log('\n--- Criteria Injection ---');
{
const reg = tmpDir();
const home = tmpDir();
fs.mkdirSync(path.join(reg, 'criteria'), { recursive: true });
fs.writeFileSync(path.join(reg, 'criteria', 'test-criterion.md'),
'---\nname: test-criterion\ndescription: A test criterion\ngate: true\nmetric: test_metric\npass_when: "equals 0"\n---\n\n## Test Criterion\n\nCheck this thing.\n');
writeAgent(reg, 'criteria-agent',
'name: criteria-agent\ndescription: C\nversion: 1.0.0\nauthor: Me\ncriteria:\n - test-criterion',
'Do your job.');
copyLib(reg);
execFileSync(process.execPath, [path.join(reg, 'bin', 'cli.js'), 'install', '--agent', 'criteria-agent'], {
cwd: reg, env: { ...process.env, HOME: home }, encoding: 'utf8', timeout: 30000
});
const dst = path.join(home, '.claude', 'commands', 'criteria-agent.md');
check('agent with criteria installed', fs.existsSync(dst));
if (fs.existsSync(dst)) {
const content = fs.readFileSync(dst, 'utf8');
check('has criteria start marker', content.includes('<!-- criteria:start -->'));
check('has criteria end marker', content.includes('<!-- criteria:end -->'));
check('criteria content injected', content.includes('## Test Criterion'));
check('criteria content includes rule', content.includes('Check this thing.'));
check('agent body still present', content.includes('Do your job.'));
check('criteria appear before body', content.indexOf('## Test Criterion') < content.indexOf('Do your job.'));
}
rmrf(reg); rmrf(home);
}
console.log('\n--- Missing Criteria Error ---');
{
const reg = tmpDir();
const home = tmpDir();
writeAgent(reg, 'bad-criteria',
'name: bad-criteria\ndescription: B\nversion: 1.0.0\nauthor: Me\ncriteria:\n - nonexistent');
copyLib(reg);
const result = (() => {
try {
execFileSync(process.execPath, [path.join(reg, 'bin', 'cli.js'), 'install', '--agent', 'bad-criteria'], {
cwd: reg, env: { ...process.env, HOME: home }, encoding: 'utf8', timeout: 30000
});
return { status: 0 };
} catch (e) {
return { status: 1, stderr: (e.stderr || '').toString(), stdout: (e.stdout || '').toString() };
}
})();
check('missing criteria rejects install', result.status !== 0);
rmrf(reg); rmrf(home);
}
console.log('\n--- Agent Without Criteria Unchanged ---');
{
const reg = tmpDir();
const home = tmpDir();
writeAgent(reg, 'no-criteria', 'name: no-criteria\ndescription: N\nversion: 1.0.0\nauthor: Me', 'Plain body.');
copyLib(reg);
execFileSync(process.execPath, [path.join(reg, 'bin', 'cli.js'), 'install', '--agent', 'no-criteria'], {
cwd: reg, env: { ...process.env, HOME: home }, encoding: 'utf8', timeout: 30000
});
const dst = path.join(home, '.claude', 'commands', 'no-criteria.md');
if (fs.existsSync(dst)) {
const content = fs.readFileSync(dst, 'utf8');
check('no criteria markers when none declared', !content.includes('<!-- criteria:'));
check('plain body present', content.includes('Plain body.'));
}
rmrf(reg); rmrf(home);
}
console.log('\n--- Both Behaviors and Criteria ---');
{
const reg = tmpDir();
const home = tmpDir();
fs.mkdirSync(path.join(reg, 'behaviors'), { recursive: true });
fs.writeFileSync(path.join(reg, 'behaviors', 'test-behave.md'),
'---\nname: test-behave\ndescription: B\n---\n\n## Test Behavior\n\nBehavior content.\n');
fs.mkdirSync(path.join(reg, 'criteria'), { recursive: true });
fs.writeFileSync(path.join(reg, 'criteria', 'test-crit.md'),
'---\nname: test-crit\ndescription: C\ngate: true\nmetric: m\npass_when: "equals 0"\n---\n\n## Test Criteria\n\nCriteria content.\n');
writeAgent(reg, 'both-agent',
'name: both-agent\ndescription: X\nversion: 1.0.0\nauthor: Me\nbehaviors:\n - test-behave\ncriteria:\n - test-crit',
'Agent body.');
copyLib(reg);
execFileSync(process.execPath, [path.join(reg, 'bin', 'cli.js'), 'install', '--agent', 'both-agent'], {
cwd: reg, env: { ...process.env, HOME: home }, encoding: 'utf8', timeout: 30000
});
const dst = path.join(home, '.claude', 'commands', 'both-agent.md');
if (fs.existsSync(dst)) {
const content = fs.readFileSync(dst, 'utf8');
check('has both behavior and criteria markers', content.includes('<!-- behaviors:start -->') && content.includes('<!-- criteria:start -->'));
check('behaviors before criteria', content.indexOf('<!-- behaviors:start -->') < content.indexOf('<!-- criteria:start -->'));
check('criteria before body', content.indexOf('<!-- criteria:end -->') < content.indexOf('Agent body.'));
}
rmrf(reg); rmrf(home);
}
// ── List Shows Criteria ────────────────────────────────────
console.log('\n--- List Shows Criteria ---');
{
const reg = tmpDir();
fs.mkdirSync(path.join(reg, 'criteria'), { recursive: true });
// gate: true criterion — should show [gate] tag
fs.writeFileSync(path.join(reg, 'criteria', 'my-criterion.md'),
'---\nname: my-criterion\ndescription: A quality gate\ngate: true\nmetric: m\npass_when: "equals 0"\n---\n\nContent.\n');
// gate: false criterion — should show [advisory] tag
fs.writeFileSync(path.join(reg, 'criteria', 'my-advisory.md'),
'---\nname: my-advisory\ndescription: An advisory check\ngate: false\nmetric: m2\npass_when: "equals 0"\n---\n\nAdvisory content.\n');
fs.mkdirSync(path.join(reg, 'agents'), { recursive: true });
copyLib(reg);
let out = '';
try {
out = execFileSync(process.execPath, [path.join(reg, 'bin', 'cli.js'), 'list'], {
cwd: reg, encoding: 'utf8', timeout: 30000
});
} catch (e) { out = (e.stdout || '').toString(); }
check('list shows criteria section', /criteria/i.test(out));
check('list shows my-criterion', out.includes('my-criterion'));
check('list shows criteria description', out.includes('A quality gate'));
check('gate:true criterion shows [gate] tag', out.includes('[gate]'));
check('gate:false criterion shows [advisory] tag', out.includes('[advisory]'));
rmrf(reg);
}
// ── Status Shows Criteria ──────────────────────────────────
console.log('\n--- Status Shows Criteria ---');
{
const reg = tmpDir();
const home = tmpDir();
fs.mkdirSync(path.join(reg, 'criteria'), { recursive: true });
fs.writeFileSync(path.join(reg, 'criteria', 'test-gate.md'),
'---\nname: test-gate\ndescription: G\ngate: true\nmetric: m\npass_when: "equals 0"\n---\n\nContent.\n');
writeAgent(reg, 'crit-agent',
'name: crit-agent\ndescription: C\nversion: 1.0.0\nauthor: Me\ncriteria:\n - test-gate',
'Body.');
copyLib(reg);
const cli = path.join(reg, 'bin', 'cli.js');
execFileSync(process.execPath, [cli, 'install', '--agent', 'crit-agent'], {
cwd: reg, env: { ...process.env, HOME: home }, encoding: 'utf8', timeout: 30000
});
let out = '';
try {
out = execFileSync(process.execPath, [cli, 'status'], {
cwd: reg, env: { ...process.env, HOME: home }, encoding: 'utf8', timeout: 30000
});
} catch (e) { out = (e.stdout || '').toString(); }
check('status shows criteria for agent', out.includes('criteria:') && out.includes('test-gate'));
rmrf(reg); rmrf(home);
}
// ── Update Picks Up Criteria Changes ──────────────────────
console.log('\n--- Update Picks Up Criteria Changes ---');
{
const reg = tmpDir();
const home = tmpDir();
fs.mkdirSync(path.join(reg, 'criteria'), { recursive: true });
fs.writeFileSync(path.join(reg, 'criteria', 'evolving.md'),
'---\nname: evolving\ndescription: V1\ngate: true\nmetric: m\npass_when: "equals 0"\n---\n\n## Evolving\n\nVersion one.\n');
writeAgent(reg, 'crit-up-agent',
'name: crit-up-agent\ndescription: U\nversion: 1.0.0\nauthor: Me\ncriteria:\n - evolving',
'Agent body.');
copyLib(reg);
const cli = path.join(reg, 'bin', 'cli.js');
execFileSync(process.execPath, [cli, 'install', '--agent', 'crit-up-agent'], {
cwd: reg, env: { ...process.env, HOME: home }, encoding: 'utf8', timeout: 30000
});
const dst = path.join(home, '.claude', 'commands', 'crit-up-agent.md');
check('initial criteria install has v1', fs.readFileSync(dst, 'utf8').includes('Version one.'));
// Update the criteria file
fs.writeFileSync(path.join(reg, 'criteria', 'evolving.md'),
'---\nname: evolving\ndescription: V2\ngate: true\nmetric: m\npass_when: "equals 0"\n---\n\n## Evolving\n\nVersion two updated.\n');
// Run update
execFileSync(process.execPath, [cli, 'update'], {
cwd: reg, env: { ...process.env, HOME: home }, encoding: 'utf8', timeout: 30000
});
check('after update has v2 criteria', fs.readFileSync(dst, 'utf8').includes('Version two updated.'));
check('after update v1 criteria gone', !fs.readFileSync(dst, 'utf8').includes('Version one.'));
rmrf(reg); rmrf(home);
}
// ── List Shows Profiles ───────────────────────────────────
console.log('\n--- List Shows Profiles ---');
{
const reg = tmpDir();
fs.mkdirSync(path.join(reg, 'profiles'), { recursive: true });
fs.writeFileSync(path.join(reg, 'profiles', 'frontend.md'),
'---\nname: frontend\ndescription: React/TS frontend\ndetect-files: [package.json, tsconfig.json]\ndetect-priority: 10\n' +
'criteria-feature: [a]\ncriteria-bugfix: [b]\ncriteria-refactor: [c]\n---\n');
fs.mkdirSync(path.join(reg, 'agents'), { recursive: true });
copyLib(reg);
let out = '';
try {
out = execFileSync(process.execPath, [path.join(reg, 'bin', 'cli.js'), 'list'], {
cwd: reg, encoding: 'utf8', timeout: 30000
});
} catch (e) { out = (e.stdout || '').toString(); }
check('list shows profiles section', /profiles/i.test(out));
check('list shows frontend profile', out.includes('frontend'));
check('list shows detect files', out.includes('package.json'));
check('list shows profile description', out.includes('React/TS frontend'));
rmrf(reg);
}
// ── Agent Definition Install (.claude/agents/) ─────────────
console.log('\n--- Agent Definition With Color ---');
{
const reg = tmpDir();
const home = tmpDir();
writeAgent(reg, 'colored-agent',
'name: colored-agent\ndescription: A colored agent\nversion: 1.0.0\nauthor: Me\nmodel: sonnet\ncolor: blue\ntools:\n - gh\n - playwright',
'Colored agent body.');
copyLib(reg);
execFileSync(process.execPath, [path.join(reg, 'bin', 'cli.js'), 'install', '--agent', 'colored-agent'], {
cwd: reg, env: { ...process.env, HOME: home }, encoding: 'utf8', timeout: 30000
});
const cmdDst = path.join(home, '.claude', 'commands', 'colored-agent.md');
const agentDst = path.join(home, '.claude', 'agents', 'colored-agent.md');
check('command file created', fs.existsSync(cmdDst));
check('agent definition file created', fs.existsSync(agentDst));
if (fs.existsSync(agentDst)) {
const content = fs.readFileSync(agentDst, 'utf8');
check('agent def has color', content.includes('color: blue'));
check('agent def has name', content.includes('name: colored-agent'));
check('agent def has description', content.includes('description: A colored agent'));
check('agent def has model', content.includes('model: sonnet'));
check('agent def has tools as csv', content.includes('tools: gh, playwright'));
check('agent def has body', content.includes('Colored agent body.'));
check('agent def has registry path', content.includes('agent-registry-path:'));
}
rmrf(reg); rmrf(home);
}
console.log('\n--- Agent Without Color Skips Agent Definition ---');
{
const reg = tmpDir();
const home = tmpDir();
writeAgent(reg, 'plain-agent',
'name: plain-agent\ndescription: No color\nversion: 1.0.0\nauthor: Me',
'Plain body.');
copyLib(reg);
execFileSync(process.execPath, [path.join(reg, 'bin', 'cli.js'), 'install', '--agent', 'plain-agent'], {
cwd: reg, env: { ...process.env, HOME: home }, encoding: 'utf8', timeout: 30000
});
const cmdDst = path.join(home, '.claude', 'commands', 'plain-agent.md');
const agentDst = path.join(home, '.claude', 'agents', 'plain-agent.md');
check('command file created', fs.existsSync(cmdDst));
check('no agent definition when no color', !fs.existsSync(agentDst));
rmrf(reg); rmrf(home);
}
console.log('\n--- Uninstall Removes Agent Definition ---');
{
const reg = tmpDir();
const home = tmpDir();
writeAgent(reg, 'rm-colored',
'name: rm-colored\ndescription: Remove me\nversion: 1.0.0\nauthor: Me\ncolor: purple',
'Body.');
copyLib(reg);
const cli = path.join(reg, 'bin', 'cli.js');
execFileSync(process.execPath, [cli, 'install', '--agent', 'rm-colored'], {
cwd: reg, env: { ...process.env, HOME: home }, encoding: 'utf8', timeout: 30000
});
const cmdDst = path.join(home, '.claude', 'commands', 'rm-colored.md');
const agentDst = path.join(home, '.claude', 'agents', 'rm-colored.md');
check('both files exist before uninstall', fs.existsSync(cmdDst) && fs.existsSync(agentDst));
execFileSync(process.execPath, [cli, 'uninstall', '--agent', 'rm-colored'], {
cwd: reg, env: { ...process.env, HOME: home }, encoding: 'utf8', timeout: 30000
});
check('command file removed', !fs.existsSync(cmdDst));
check('agent definition removed', !fs.existsSync(agentDst));
rmrf(reg); rmrf(home);
}
console.log('\n--- isAgentInstalled Checks Both Directories ---');
{
const reg = tmpDir();
const home = tmpDir();
writeAgent(reg, 'dual-agent',
'name: dual-agent\ndescription: Dual\nversion: 1.0.0\nauthor: Me\ncolor: green',
'Body.');
copyLib(reg);
const cli = path.join(reg, 'bin', 'cli.js');
execFileSync(process.execPath, [cli, 'install', '--agent', 'dual-agent'], {
cwd: reg, env: { ...process.env, HOME: home }, encoding: 'utf8', timeout: 30000
});
// Remove only the commands file, leaving the agents file
const cmdDst = path.join(home, '.claude', 'commands', 'dual-agent.md');
fs.unlinkSync(cmdDst);
check('command file removed', !fs.existsSync(cmdDst));
// Status should still show as installed via agents dir
let out = '';
try {
out = execFileSync(process.execPath, [cli, 'status'], {
cwd: reg, env: { ...process.env, HOME: home }, encoding: 'utf8', timeout: 30000
});
} catch (e) { out = (e.stdout || '').toString(); }
check('still detected as installed via agents dir', out.includes('installed') && out.includes('dual-agent'));
rmrf(reg); rmrf(home);
}
// ── Skill Type Detection ───────────────────────────────────
console.log('\n--- getSkillType Detection ---');
{
const { getSkillType } = require('./lib/discovery');
const commandsSkill = path.join(REGISTRY_DIR, 'skills', 'slides');
const skillmdSkill = path.join(REGISTRY_DIR, 'skills', 'pr-review-loop');
const emptyDir = tmpDir();
check('commands skill returns commands', getSkillType(commandsSkill) === 'commands');
check('skillmd skill returns skillmd', getSkillType(skillmdSkill) === 'skillmd');
check('empty dir returns null', getSkillType(emptyDir) === null);
rmrf(emptyDir);
}
console.log('\n--- listSkills Discovers Both Types ---');
{
const { listSkills } = require('./lib/discovery');
const skills = listSkills(REGISTRY_DIR);
check('listSkills includes pr-review-loop', skills.includes('pr-review-loop'));
check('listSkills includes slides', skills.includes('slides'));
}
console.log('\n--- SKILL.md Skill Install and Uninstall ---');
{
const home = tmpDir();
run(['install', '--skill', 'pr-review-loop'], { HOME: home });
const skillDst = path.join(home, '.claude', 'skills', 'pr-review-loop');
const isLink = fs.existsSync(skillDst) && fs.lstatSync(skillDst).isSymbolicLink();
check('skillmd symlink created', isLink);
if (isLink) {
const target = fs.realpathSync(skillDst);
check('symlink points to registry skill dir', target === path.join(REGISTRY_DIR, 'skills', 'pr-review-loop'));
}
// Verify SKILL.md accessible through symlink
check('SKILL.md accessible via symlink', fs.existsSync(path.join(skillDst, 'SKILL.md')));
// Uninstall
run(['uninstall', '--skill', 'pr-review-loop'], { HOME: home });
check('skillmd symlink removed after uninstall', !fs.existsSync(skillDst));
rmrf(home);
}
// ── Summary ─────────────────────────────────────────────────
console.log(`\n=== Results: ${passed} passed, ${failed} failed ===`);
process.exit(failed === 0 ? 0 : 1);