opencode-swarm-plugin 0.31.7 → 0.32.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/.turbo/turbo-build.log +10 -9
  2. package/.turbo/turbo-test.log +319 -317
  3. package/CHANGELOG.md +134 -0
  4. package/README.md +7 -4
  5. package/bin/swarm.ts +388 -128
  6. package/dist/compaction-hook.d.ts +1 -1
  7. package/dist/compaction-hook.d.ts.map +1 -1
  8. package/dist/hive.d.ts.map +1 -1
  9. package/dist/index.d.ts +0 -2
  10. package/dist/index.d.ts.map +1 -1
  11. package/dist/index.js +123 -134
  12. package/dist/memory-tools.d.ts.map +1 -1
  13. package/dist/memory.d.ts +5 -4
  14. package/dist/memory.d.ts.map +1 -1
  15. package/dist/plugin.js +118 -131
  16. package/dist/swarm-orchestrate.d.ts +29 -5
  17. package/dist/swarm-orchestrate.d.ts.map +1 -1
  18. package/dist/swarm-prompts.d.ts +7 -0
  19. package/dist/swarm-prompts.d.ts.map +1 -1
  20. package/dist/swarm.d.ts +0 -2
  21. package/dist/swarm.d.ts.map +1 -1
  22. package/evals/lib/{data-loader.test.ts → data-loader.evalite-test.ts} +7 -6
  23. package/evals/lib/data-loader.ts +1 -1
  24. package/evals/scorers/{outcome-scorers.test.ts → outcome-scorers.evalite-test.ts} +1 -1
  25. package/examples/plugin-wrapper-template.ts +19 -4
  26. package/global-skills/swarm-coordination/SKILL.md +118 -8
  27. package/package.json +2 -2
  28. package/src/compaction-hook.ts +5 -3
  29. package/src/hive.integration.test.ts +83 -1
  30. package/src/hive.ts +37 -12
  31. package/src/mandate-storage.integration.test.ts +601 -0
  32. package/src/memory-tools.ts +6 -4
  33. package/src/memory.integration.test.ts +117 -49
  34. package/src/memory.test.ts +41 -217
  35. package/src/memory.ts +12 -8
  36. package/src/repo-crawl.integration.test.ts +441 -0
  37. package/src/skills.integration.test.ts +1056 -0
  38. package/src/structured.integration.test.ts +817 -0
  39. package/src/swarm-deferred.integration.test.ts +157 -0
  40. package/src/swarm-deferred.test.ts +38 -0
  41. package/src/swarm-mail.integration.test.ts +15 -19
  42. package/src/swarm-orchestrate.integration.test.ts +282 -0
  43. package/src/swarm-orchestrate.ts +96 -201
  44. package/src/swarm-prompts.test.ts +92 -0
  45. package/src/swarm-prompts.ts +69 -0
  46. package/src/swarm-review.integration.test.ts +290 -0
  47. package/src/swarm.integration.test.ts +23 -20
  48. package/src/tool-adapter.integration.test.ts +1221 -0
@@ -0,0 +1,1056 @@
1
+ /**
2
+ * Skills Integration Tests
3
+ *
4
+ * Tests all skills_* tools with real filesystem operations.
5
+ * These are happy-path integration tests verifying tools work end-to-end.
6
+ *
7
+ * Tools under test:
8
+ * - skills_list
9
+ * - skills_read
10
+ * - skills_use
11
+ * - skills_create
12
+ * - skills_update
13
+ * - skills_delete
14
+ * - skills_init
15
+ * - skills_add_script
16
+ * - skills_execute
17
+ */
18
+
19
+ import { describe, expect, it, afterAll, beforeEach } from "vitest";
20
+ import { chmodSync, existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
21
+ import { join } from "node:path";
22
+ import {
23
+ skills_list,
24
+ skills_read,
25
+ skills_use,
26
+ skills_create,
27
+ skills_update,
28
+ skills_delete,
29
+ skills_init,
30
+ skills_add_script,
31
+ skills_execute,
32
+ setSkillsProjectDirectory,
33
+ invalidateSkillsCache,
34
+ } from "./skills";
35
+
36
+ // =============================================================================
37
+ // Test Setup
38
+ // =============================================================================
39
+
40
+ const TEST_RUN_ID = `${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
41
+ const TEST_DIR = join(process.cwd(), `.test-skills-integration-${TEST_RUN_ID}`);
42
+ const SKILLS_DIR = join(TEST_DIR, ".opencode", "skills");
43
+
44
+ function cleanupTestDir() {
45
+ if (existsSync(TEST_DIR)) {
46
+ rmSync(TEST_DIR, { recursive: true, force: true });
47
+ }
48
+ }
49
+
50
+ function setupTestDir() {
51
+ cleanupTestDir();
52
+ mkdirSync(SKILLS_DIR, { recursive: true });
53
+ setSkillsProjectDirectory(TEST_DIR);
54
+ invalidateSkillsCache();
55
+ }
56
+
57
+ // =============================================================================
58
+ // skills_list Tool
59
+ // =============================================================================
60
+
61
+ describe("skills_list tool", () => {
62
+ beforeEach(() => {
63
+ setupTestDir();
64
+ });
65
+
66
+ afterAll(() => {
67
+ cleanupTestDir();
68
+ });
69
+
70
+ it("should list empty skills directory", async () => {
71
+ const result = await skills_list.execute({});
72
+
73
+ // May find global skills, so just verify it doesn't error
74
+ expect(result).toBeDefined();
75
+ expect(typeof result).toBe("string");
76
+ });
77
+
78
+ it("should list discovered skills with metadata", async () => {
79
+ // Create a test skill first
80
+ await skills_create.execute({
81
+ name: "test-skill",
82
+ description: "Use when testing - this is a test skill",
83
+ body: "# Test Instructions\n\nDo the thing.",
84
+ tags: ["testing"],
85
+ });
86
+
87
+ invalidateSkillsCache();
88
+ const result = await skills_list.execute({});
89
+
90
+ // Should include our test skill (may include global skills too)
91
+ expect(result).toContain("test-skill");
92
+ expect(result).toContain("Use when testing");
93
+ expect(result).toContain("(testing)");
94
+ });
95
+
96
+ it("should filter skills by tag", async () => {
97
+ await skills_create.execute({
98
+ name: "skill-a",
99
+ description: "Use when A",
100
+ body: "A",
101
+ tags: ["frontend"],
102
+ });
103
+
104
+ await skills_create.execute({
105
+ name: "skill-b",
106
+ description: "Use when B",
107
+ body: "B",
108
+ tags: ["backend"],
109
+ });
110
+
111
+ invalidateSkillsCache();
112
+ const result = await skills_list.execute({ tag: "frontend" });
113
+
114
+ expect(result).toContain("skill-a");
115
+ expect(result).not.toContain("skill-b");
116
+ });
117
+
118
+ it("should show [has scripts] indicator", async () => {
119
+ await skills_create.execute({
120
+ name: "with-scripts",
121
+ description: "Use when scripting",
122
+ body: "Scripts",
123
+ });
124
+
125
+ await skills_add_script.execute({
126
+ skill: "with-scripts",
127
+ script_name: "helper.sh",
128
+ content: "#!/bin/bash\necho hi",
129
+ });
130
+
131
+ invalidateSkillsCache();
132
+ const result = await skills_list.execute({});
133
+
134
+ expect(result).toContain("with-scripts");
135
+ expect(result).toContain("[has scripts]");
136
+ });
137
+ });
138
+
139
+ // =============================================================================
140
+ // skills_use Tool
141
+ // =============================================================================
142
+
143
+ describe("skills_use tool", () => {
144
+ beforeEach(() => {
145
+ setupTestDir();
146
+ });
147
+
148
+ afterAll(() => {
149
+ cleanupTestDir();
150
+ });
151
+
152
+ it("should activate a skill and return full content", async () => {
153
+ const body = "# Test Skill\n\nThese are the instructions.";
154
+ await skills_create.execute({
155
+ name: "test-skill",
156
+ description: "Use when testing",
157
+ body,
158
+ });
159
+
160
+ invalidateSkillsCache();
161
+ const result = await skills_use.execute({ name: "test-skill" });
162
+
163
+ expect(result).toContain("# Skill: test-skill");
164
+ expect(result).toContain(body);
165
+ });
166
+
167
+ it("should list available scripts when skill has them", async () => {
168
+ await skills_create.execute({
169
+ name: "scripted-skill",
170
+ description: "Use when scripting",
171
+ body: "Instructions",
172
+ });
173
+
174
+ await skills_add_script.execute({
175
+ skill: "scripted-skill",
176
+ script_name: "setup.sh",
177
+ content: "#!/bin/bash\necho setup",
178
+ });
179
+
180
+ invalidateSkillsCache();
181
+ const result = await skills_use.execute({ name: "scripted-skill" });
182
+
183
+ expect(result).toContain("Available Scripts");
184
+ expect(result).toContain("setup.sh");
185
+ expect(result).toContain("skills_execute");
186
+ });
187
+
188
+ it("should exclude scripts when include_scripts=false", async () => {
189
+ await skills_create.execute({
190
+ name: "scripted-skill",
191
+ description: "Use when scripting",
192
+ body: "Instructions",
193
+ });
194
+
195
+ await skills_add_script.execute({
196
+ skill: "scripted-skill",
197
+ script_name: "setup.sh",
198
+ content: "#!/bin/bash\necho setup",
199
+ });
200
+
201
+ invalidateSkillsCache();
202
+ const result = await skills_use.execute({
203
+ name: "scripted-skill",
204
+ include_scripts: false,
205
+ });
206
+
207
+ expect(result).not.toContain("Available Scripts");
208
+ expect(result).not.toContain("setup.sh");
209
+ });
210
+
211
+ it("should return error for non-existent skill", async () => {
212
+ const result = await skills_use.execute({ name: "non-existent" });
213
+
214
+ expect(result).toContain("not found");
215
+ expect(result).toContain("Available skills:");
216
+ });
217
+ });
218
+
219
+ // =============================================================================
220
+ // skills_read Tool
221
+ // =============================================================================
222
+
223
+ describe("skills_read tool", () => {
224
+ beforeEach(() => {
225
+ setupTestDir();
226
+ });
227
+
228
+ afterAll(() => {
229
+ cleanupTestDir();
230
+ });
231
+
232
+ it("should read a resource file from skill directory", async () => {
233
+ await skills_create.execute({
234
+ name: "documented-skill",
235
+ description: "Use when documenting",
236
+ body: "Instructions",
237
+ });
238
+
239
+ // Manually create a reference file
240
+ const skillDir = join(SKILLS_DIR, "documented-skill");
241
+ const exampleContent = "# Examples\n\nExample content here.";
242
+ writeFileSync(join(skillDir, "examples.md"), exampleContent);
243
+
244
+ const result = await skills_read.execute({
245
+ skill: "documented-skill",
246
+ file: "examples.md",
247
+ });
248
+
249
+ expect(result).toBe(exampleContent);
250
+ });
251
+
252
+ it("should prevent path traversal attacks", async () => {
253
+ await skills_create.execute({
254
+ name: "secure-skill",
255
+ description: "Use when securing",
256
+ body: "Secure",
257
+ });
258
+
259
+ const maliciousPaths = [
260
+ "../../../etc/passwd",
261
+ "../../..",
262
+ "/etc/passwd",
263
+ "..\\..\\windows\\system32",
264
+ ];
265
+
266
+ for (const path of maliciousPaths) {
267
+ const result = await skills_read.execute({
268
+ skill: "secure-skill",
269
+ file: path,
270
+ });
271
+
272
+ expect(result).toContain("Invalid file path");
273
+ }
274
+ });
275
+
276
+ it("should return error for non-existent skill", async () => {
277
+ const result = await skills_read.execute({
278
+ skill: "non-existent",
279
+ file: "anything.md",
280
+ });
281
+
282
+ expect(result).toContain("not found");
283
+ });
284
+
285
+ it("should return error for non-existent file", async () => {
286
+ await skills_create.execute({
287
+ name: "empty-skill",
288
+ description: "Use when empty",
289
+ body: "Empty",
290
+ });
291
+
292
+ const result = await skills_read.execute({
293
+ skill: "empty-skill",
294
+ file: "non-existent.md",
295
+ });
296
+
297
+ expect(result).toContain("Failed to read");
298
+ });
299
+ });
300
+
301
+ // =============================================================================
302
+ // skills_create Tool
303
+ // =============================================================================
304
+
305
+ describe("skills_create tool", () => {
306
+ beforeEach(() => {
307
+ setupTestDir();
308
+ });
309
+
310
+ afterAll(() => {
311
+ cleanupTestDir();
312
+ });
313
+
314
+ it("should create a new skill with minimal fields", async () => {
315
+ const result = await skills_create.execute({
316
+ name: "minimal-skill",
317
+ description: "Use when minimal",
318
+ body: "# Minimal\n\nInstructions here.",
319
+ });
320
+
321
+ const parsed = JSON.parse(result);
322
+ expect(parsed.success).toBe(true);
323
+ expect(parsed.skill).toBe("minimal-skill");
324
+ expect(parsed.path).toContain("minimal-skill");
325
+
326
+ // Verify file exists
327
+ const skillPath = join(SKILLS_DIR, "minimal-skill", "SKILL.md");
328
+ expect(existsSync(skillPath)).toBe(true);
329
+
330
+ // Verify content (description may or may not be quoted depending on content)
331
+ const content = readFileSync(skillPath, "utf-8");
332
+ expect(content).toContain("name: minimal-skill");
333
+ expect(content).toContain("Use when minimal"); // Either quoted or unquoted
334
+ expect(content).toContain("# Minimal");
335
+ });
336
+
337
+ it("should create skill with tags and tools", async () => {
338
+ const result = await skills_create.execute({
339
+ name: "full-skill",
340
+ description: "Use when full",
341
+ body: "Full body",
342
+ tags: ["testing", "automation"],
343
+ tools: ["Read", "Write", "Bash"],
344
+ });
345
+
346
+ const parsed = JSON.parse(result);
347
+ expect(parsed.success).toBe(true);
348
+
349
+ const skillPath = join(SKILLS_DIR, "full-skill", "SKILL.md");
350
+ const content = readFileSync(skillPath, "utf-8");
351
+
352
+ expect(content).toContain("tags:");
353
+ expect(content).toContain("- testing");
354
+ expect(content).toContain("- automation");
355
+ expect(content).toContain("tools:");
356
+ expect(content).toContain("- Read");
357
+ });
358
+
359
+ it("should return CSO warnings for non-compliant metadata", async () => {
360
+ const result = await skills_create.execute({
361
+ name: "bad-skill",
362
+ description: "I can help you with testing", // First-person, no "Use when"
363
+ body: "Body",
364
+ });
365
+
366
+ const parsed = JSON.parse(result);
367
+ expect(parsed.success).toBe(true); // Still creates, just warns
368
+ expect(parsed.cso_warnings).toBeDefined();
369
+ expect(parsed.cso_warnings).toContain("first-person");
370
+ expect(parsed.cso_warnings).toContain("Use when");
371
+ });
372
+
373
+ it("should prevent duplicate skill creation", async () => {
374
+ await skills_create.execute({
375
+ name: "duplicate-skill",
376
+ description: "Use when duplicating",
377
+ body: "First",
378
+ });
379
+
380
+ invalidateSkillsCache();
381
+
382
+ const result = await skills_create.execute({
383
+ name: "duplicate-skill",
384
+ description: "Use when duplicating again",
385
+ body: "Second",
386
+ });
387
+
388
+ expect(result).toContain("already exists");
389
+ expect(result).toContain("skills_update");
390
+ });
391
+
392
+ it("should invalidate cache after creation", async () => {
393
+ await skills_create.execute({
394
+ name: "cache-test",
395
+ description: "Use when caching",
396
+ body: "Cache",
397
+ });
398
+
399
+ // Should be immediately discoverable without manual cache clear
400
+ const skill = await skills_list.execute({});
401
+ expect(skill).toContain("cache-test");
402
+ });
403
+ });
404
+
405
+ // =============================================================================
406
+ // skills_update Tool
407
+ // =============================================================================
408
+
409
+ describe("skills_update tool", () => {
410
+ beforeEach(() => {
411
+ setupTestDir();
412
+ });
413
+
414
+ afterAll(() => {
415
+ cleanupTestDir();
416
+ });
417
+
418
+ it("should update skill description", async () => {
419
+ await skills_create.execute({
420
+ name: "update-test",
421
+ description: "Use when old",
422
+ body: "Old body",
423
+ });
424
+
425
+ invalidateSkillsCache();
426
+
427
+ const result = await skills_update.execute({
428
+ name: "update-test",
429
+ description: "Use when new",
430
+ });
431
+
432
+ const parsed = JSON.parse(result);
433
+ expect(parsed.success).toBe(true);
434
+ expect(parsed.updated.description).toBe(true);
435
+
436
+ const skillPath = join(SKILLS_DIR, "update-test", "SKILL.md");
437
+ const content = readFileSync(skillPath, "utf-8");
438
+ expect(content).toContain("Use when new");
439
+ });
440
+
441
+ it("should update skill body with content parameter", async () => {
442
+ await skills_create.execute({
443
+ name: "body-test",
444
+ description: "Use when body",
445
+ body: "Old body",
446
+ });
447
+
448
+ invalidateSkillsCache();
449
+
450
+ const result = await skills_update.execute({
451
+ name: "body-test",
452
+ content: "New body content",
453
+ });
454
+
455
+ const parsed = JSON.parse(result);
456
+ expect(parsed.success).toBe(true);
457
+ expect(parsed.updated.content).toBe(true);
458
+
459
+ const skillPath = join(SKILLS_DIR, "body-test", "SKILL.md");
460
+ const content = readFileSync(skillPath, "utf-8");
461
+ expect(content).toContain("New body content");
462
+ });
463
+
464
+ it("should append to existing body", async () => {
465
+ await skills_create.execute({
466
+ name: "append-test",
467
+ description: "Use when appending",
468
+ body: "Original content",
469
+ });
470
+
471
+ invalidateSkillsCache();
472
+
473
+ const result = await skills_update.execute({
474
+ name: "append-test",
475
+ append_body: "\n\nAppended content",
476
+ });
477
+
478
+ const parsed = JSON.parse(result);
479
+ expect(parsed.success).toBe(true);
480
+
481
+ const skillPath = join(SKILLS_DIR, "append-test", "SKILL.md");
482
+ const content = readFileSync(skillPath, "utf-8");
483
+ expect(content).toContain("Original content");
484
+ expect(content).toContain("Appended content");
485
+ });
486
+
487
+ it("should replace tags", async () => {
488
+ await skills_create.execute({
489
+ name: "tags-test",
490
+ description: "Use when tagging",
491
+ body: "Body",
492
+ tags: ["old", "tag"],
493
+ });
494
+
495
+ invalidateSkillsCache();
496
+
497
+ const result = await skills_update.execute({
498
+ name: "tags-test",
499
+ tags: ["new", "tags"],
500
+ });
501
+
502
+ const parsed = JSON.parse(result);
503
+ expect(parsed.success).toBe(true);
504
+
505
+ const skillPath = join(SKILLS_DIR, "tags-test", "SKILL.md");
506
+ const content = readFileSync(skillPath, "utf-8");
507
+ expect(content).toContain("- new");
508
+ expect(content).toContain("- tags");
509
+ expect(content).not.toContain("- old");
510
+ });
511
+
512
+ it("should add tags to existing", async () => {
513
+ await skills_create.execute({
514
+ name: "add-tags-test",
515
+ description: "Use when adding tags",
516
+ body: "Body",
517
+ tags: ["existing"],
518
+ });
519
+
520
+ invalidateSkillsCache();
521
+
522
+ const result = await skills_update.execute({
523
+ name: "add-tags-test",
524
+ add_tags: ["new", "additional"],
525
+ });
526
+
527
+ const parsed = JSON.parse(result);
528
+ expect(parsed.success).toBe(true);
529
+
530
+ const skillPath = join(SKILLS_DIR, "add-tags-test", "SKILL.md");
531
+ const content = readFileSync(skillPath, "utf-8");
532
+ expect(content).toContain("- existing");
533
+ expect(content).toContain("- new");
534
+ expect(content).toContain("- additional");
535
+ });
536
+
537
+ it("should deduplicate tags when adding", async () => {
538
+ await skills_create.execute({
539
+ name: "dedup-test",
540
+ description: "Use when deduping",
541
+ body: "Body",
542
+ tags: ["tag1"],
543
+ });
544
+
545
+ invalidateSkillsCache();
546
+
547
+ await skills_update.execute({
548
+ name: "dedup-test",
549
+ add_tags: ["tag1", "tag2"], // tag1 already exists
550
+ });
551
+
552
+ const skillPath = join(SKILLS_DIR, "dedup-test", "SKILL.md");
553
+ const content = readFileSync(skillPath, "utf-8");
554
+
555
+ // Count occurrences of "tag1"
556
+ const matches = content.match(/- tag1/g);
557
+ expect(matches?.length).toBe(1); // Should only appear once
558
+ });
559
+
560
+ it("should return error for non-existent skill", async () => {
561
+ const result = await skills_update.execute({
562
+ name: "non-existent",
563
+ description: "New desc",
564
+ });
565
+
566
+ expect(result).toContain("not found");
567
+ expect(result).toContain("Available:");
568
+ });
569
+ });
570
+
571
+ // =============================================================================
572
+ // skills_delete Tool
573
+ // =============================================================================
574
+
575
+ describe("skills_delete tool", () => {
576
+ beforeEach(() => {
577
+ setupTestDir();
578
+ });
579
+
580
+ afterAll(() => {
581
+ cleanupTestDir();
582
+ });
583
+
584
+ it("should delete a skill when confirm=true", async () => {
585
+ await skills_create.execute({
586
+ name: "delete-me",
587
+ description: "Use when deleting",
588
+ body: "Delete this",
589
+ });
590
+
591
+ const skillDir = join(SKILLS_DIR, "delete-me");
592
+ expect(existsSync(skillDir)).toBe(true);
593
+
594
+ const result = await skills_delete.execute({
595
+ name: "delete-me",
596
+ confirm: true,
597
+ });
598
+
599
+ const parsed = JSON.parse(result);
600
+ expect(parsed.success).toBe(true);
601
+ expect(parsed.skill).toBe("delete-me");
602
+
603
+ // Verify directory removed
604
+ expect(existsSync(skillDir)).toBe(false);
605
+ });
606
+
607
+ it("should refuse deletion without confirm", async () => {
608
+ await skills_create.execute({
609
+ name: "keep-me",
610
+ description: "Use when keeping",
611
+ body: "Keep this",
612
+ });
613
+
614
+ const result = await skills_delete.execute({
615
+ name: "keep-me",
616
+ confirm: false,
617
+ });
618
+
619
+ expect(result).toContain("not confirmed");
620
+ expect(result).toContain("confirm=true");
621
+
622
+ // Verify still exists
623
+ const skillDir = join(SKILLS_DIR, "keep-me");
624
+ expect(existsSync(skillDir)).toBe(true);
625
+ });
626
+
627
+ it("should return error for non-existent skill", async () => {
628
+ const result = await skills_delete.execute({
629
+ name: "non-existent",
630
+ confirm: true,
631
+ });
632
+
633
+ expect(result).toContain("not found");
634
+ });
635
+
636
+ it("should invalidate cache after deletion", async () => {
637
+ await skills_create.execute({
638
+ name: "cache-delete-test",
639
+ description: "Use when cache testing",
640
+ body: "Cache",
641
+ });
642
+
643
+ await skills_delete.execute({
644
+ name: "cache-delete-test",
645
+ confirm: true,
646
+ });
647
+
648
+ // Should be immediately gone from list
649
+ const result = await skills_list.execute({});
650
+ expect(result).not.toContain("cache-delete-test");
651
+ });
652
+ });
653
+
654
+ // =============================================================================
655
+ // skills_init Tool
656
+ // =============================================================================
657
+
658
+ describe("skills_init tool", () => {
659
+ beforeEach(() => {
660
+ setupTestDir();
661
+ });
662
+
663
+ afterAll(() => {
664
+ cleanupTestDir();
665
+ });
666
+
667
+ it("should initialize skill with full template structure", async () => {
668
+ const result = await skills_init.execute({
669
+ name: "init-test",
670
+ });
671
+
672
+ const parsed = JSON.parse(result);
673
+ expect(parsed.success).toBe(true);
674
+ expect(parsed.skill).toBe("init-test");
675
+ expect(parsed.created_files).toContain("SKILL.md");
676
+ expect(parsed.created_files).toContain("scripts/example.sh");
677
+ expect(parsed.created_files).toContain("references/guide.md");
678
+
679
+ // Verify files exist
680
+ const skillDir = join(SKILLS_DIR, "init-test");
681
+ expect(existsSync(join(skillDir, "SKILL.md"))).toBe(true);
682
+ expect(existsSync(join(skillDir, "scripts", "example.sh"))).toBe(true);
683
+ expect(existsSync(join(skillDir, "references", "guide.md"))).toBe(true);
684
+ });
685
+
686
+ it("should create SKILL.md with TODO placeholders", async () => {
687
+ await skills_init.execute({ name: "todo-test" });
688
+
689
+ const skillPath = join(SKILLS_DIR, "todo-test", "SKILL.md");
690
+ const content = readFileSync(skillPath, "utf-8");
691
+
692
+ expect(content).toContain("[TODO:");
693
+ expect(content).toContain("## When to Use This Skill");
694
+ expect(content).toContain("## Instructions");
695
+ expect(content).toContain("## Examples");
696
+ });
697
+
698
+ it("should exclude example script when include_example_script=false", async () => {
699
+ const result = await skills_init.execute({
700
+ name: "no-scripts",
701
+ include_example_script: false,
702
+ });
703
+
704
+ const parsed = JSON.parse(result);
705
+ expect(parsed.created_files).not.toContain("scripts/example.sh");
706
+
707
+ const scriptPath = join(SKILLS_DIR, "no-scripts", "scripts", "example.sh");
708
+ expect(existsSync(scriptPath)).toBe(false);
709
+ });
710
+
711
+ it("should exclude reference when include_reference=false", async () => {
712
+ const result = await skills_init.execute({
713
+ name: "no-refs",
714
+ include_reference: false,
715
+ });
716
+
717
+ const parsed = JSON.parse(result);
718
+ expect(parsed.created_files).not.toContain("references/guide.md");
719
+
720
+ const refPath = join(SKILLS_DIR, "no-refs", "references", "guide.md");
721
+ expect(existsSync(refPath)).toBe(false);
722
+ });
723
+
724
+ it("should use provided description in frontmatter", async () => {
725
+ await skills_init.execute({
726
+ name: "custom-desc",
727
+ description: "Use when custom description",
728
+ });
729
+
730
+ const skillPath = join(SKILLS_DIR, "custom-desc", "SKILL.md");
731
+ const content = readFileSync(skillPath, "utf-8");
732
+
733
+ expect(content).toContain("Use when custom description"); // May or may not be quoted
734
+ });
735
+
736
+ it("should prevent duplicate skill initialization", async () => {
737
+ // Provide a valid description so the skill is discoverable
738
+ await skills_init.execute({
739
+ name: "duplicate-init",
740
+ description: "Use when duplicate testing",
741
+ });
742
+
743
+ invalidateSkillsCache();
744
+
745
+ const result = await skills_init.execute({ name: "duplicate-init" });
746
+
747
+ const parsed = JSON.parse(result);
748
+ expect(parsed.success).toBe(false);
749
+ expect(parsed.error).toContain("already exists");
750
+ });
751
+ });
752
+
753
+ // =============================================================================
754
+ // skills_add_script Tool
755
+ // =============================================================================
756
+
757
+ describe("skills_add_script tool", () => {
758
+ beforeEach(() => {
759
+ setupTestDir();
760
+ });
761
+
762
+ afterAll(() => {
763
+ cleanupTestDir();
764
+ });
765
+
766
+ it("should add executable script to skill", async () => {
767
+ await skills_create.execute({
768
+ name: "script-test",
769
+ description: "Use when scripting",
770
+ body: "Body",
771
+ });
772
+
773
+ const scriptContent = "#!/bin/bash\necho 'Hello from script'";
774
+ const result = await skills_add_script.execute({
775
+ skill: "script-test",
776
+ script_name: "hello.sh",
777
+ content: scriptContent,
778
+ });
779
+
780
+ const parsed = JSON.parse(result);
781
+ expect(parsed.success).toBe(true);
782
+ expect(parsed.script).toBe("hello.sh");
783
+ // executable defaults to true if not specified
784
+ expect(parsed.executable !== false).toBe(true);
785
+
786
+ const scriptPath = join(SKILLS_DIR, "script-test", "scripts", "hello.sh");
787
+ expect(existsSync(scriptPath)).toBe(true);
788
+
789
+ const content = readFileSync(scriptPath, "utf-8");
790
+ expect(content).toBe(scriptContent);
791
+ });
792
+
793
+ it("should create scripts directory if needed", async () => {
794
+ await skills_create.execute({
795
+ name: "new-scripts",
796
+ description: "Use when new scripts",
797
+ body: "Body",
798
+ });
799
+
800
+ const scriptsDir = join(SKILLS_DIR, "new-scripts", "scripts");
801
+ expect(existsSync(scriptsDir)).toBe(false);
802
+
803
+ await skills_add_script.execute({
804
+ skill: "new-scripts",
805
+ script_name: "first.sh",
806
+ content: "#!/bin/bash\necho first",
807
+ });
808
+
809
+ expect(existsSync(scriptsDir)).toBe(true);
810
+ });
811
+
812
+ it("should add non-executable script when executable=false", async () => {
813
+ await skills_create.execute({
814
+ name: "data-script",
815
+ description: "Use when data",
816
+ body: "Body",
817
+ });
818
+
819
+ await skills_add_script.execute({
820
+ skill: "data-script",
821
+ script_name: "data.json",
822
+ content: '{"key": "value"}',
823
+ executable: false,
824
+ });
825
+
826
+ const parsed = await skills_add_script.execute({
827
+ skill: "data-script",
828
+ script_name: "config.yaml",
829
+ content: "key: value",
830
+ executable: false,
831
+ });
832
+
833
+ const result = JSON.parse(parsed);
834
+ expect(result.executable).toBe(false);
835
+ });
836
+
837
+ it("should prevent path traversal in script names", async () => {
838
+ await skills_create.execute({
839
+ name: "secure-scripts",
840
+ description: "Use when secure",
841
+ body: "Body",
842
+ });
843
+
844
+ const maliciousNames = [
845
+ "../../../etc/passwd.sh",
846
+ "../../bad.sh",
847
+ "/etc/script.sh",
848
+ "subdir/script.sh", // No subdirectories allowed
849
+ ];
850
+
851
+ for (const name of maliciousNames) {
852
+ const result = await skills_add_script.execute({
853
+ skill: "secure-scripts",
854
+ script_name: name,
855
+ content: "echo bad",
856
+ });
857
+
858
+ expect(result).toContain("Invalid script name");
859
+ }
860
+ });
861
+
862
+ it("should return error for non-existent skill", async () => {
863
+ const result = await skills_add_script.execute({
864
+ skill: "non-existent",
865
+ script_name: "test.sh",
866
+ content: "echo test",
867
+ });
868
+
869
+ expect(result).toContain("not found");
870
+ });
871
+
872
+ it("should invalidate cache after adding script", async () => {
873
+ await skills_create.execute({
874
+ name: "cache-script-test",
875
+ description: "Use when cache",
876
+ body: "Body",
877
+ });
878
+
879
+ await skills_add_script.execute({
880
+ skill: "cache-script-test",
881
+ script_name: "test.sh",
882
+ content: "echo test",
883
+ });
884
+
885
+ // Should immediately show [has scripts]
886
+ const result = await skills_list.execute({});
887
+ expect(result).toContain("cache-script-test");
888
+ expect(result).toContain("[has scripts]");
889
+ });
890
+ });
891
+
892
+ // =============================================================================
893
+ // skills_execute Tool
894
+ // =============================================================================
895
+
896
+ describe("skills_execute tool", () => {
897
+ beforeEach(() => {
898
+ setupTestDir();
899
+ });
900
+
901
+ afterAll(() => {
902
+ cleanupTestDir();
903
+ });
904
+
905
+ it("should execute a script successfully", async () => {
906
+ await skills_create.execute({
907
+ name: "exec-test",
908
+ description: "Use when executing",
909
+ body: "Body",
910
+ });
911
+
912
+ await skills_add_script.execute({
913
+ skill: "exec-test",
914
+ script_name: "echo.sh",
915
+ content: '#!/bin/bash\necho "Hello from script"',
916
+ });
917
+
918
+ // Manually ensure executable (writeFileSync mode doesn't always work)
919
+ const scriptPath = join(SKILLS_DIR, "exec-test", "scripts", "echo.sh");
920
+ chmodSync(scriptPath, 0o755);
921
+
922
+ const result = await skills_execute.execute({
923
+ skill: "exec-test",
924
+ script: "echo.sh",
925
+ });
926
+
927
+ expect(result).toContain("Hello from script");
928
+ });
929
+
930
+ it("should pass project directory as first argument", async () => {
931
+ await skills_create.execute({
932
+ name: "args-test",
933
+ description: "Use when args",
934
+ body: "Body",
935
+ });
936
+
937
+ await skills_add_script.execute({
938
+ skill: "args-test",
939
+ script_name: "check-args.sh",
940
+ content: '#!/bin/bash\necho "Project dir: $1"',
941
+ });
942
+
943
+ const scriptPath = join(SKILLS_DIR, "args-test", "scripts", "check-args.sh");
944
+ chmodSync(scriptPath, 0o755);
945
+
946
+ const result = await skills_execute.execute({
947
+ skill: "args-test",
948
+ script: "check-args.sh",
949
+ });
950
+
951
+ expect(result).toContain("Project dir:");
952
+ expect(result).toContain(TEST_DIR);
953
+ });
954
+
955
+ it("should pass additional arguments to script", async () => {
956
+ await skills_create.execute({
957
+ name: "multi-args-test",
958
+ description: "Use when multi args",
959
+ body: "Body",
960
+ });
961
+
962
+ await skills_add_script.execute({
963
+ skill: "multi-args-test",
964
+ script_name: "args.sh",
965
+ content: '#!/bin/bash\necho "Args: $1 $2 $3"',
966
+ });
967
+
968
+ const scriptPath = join(SKILLS_DIR, "multi-args-test", "scripts", "args.sh");
969
+ chmodSync(scriptPath, 0o755);
970
+
971
+ const result = await skills_execute.execute({
972
+ skill: "multi-args-test",
973
+ script: "args.sh",
974
+ args: ["arg1", "arg2"],
975
+ });
976
+
977
+ expect(result).toContain("Args:");
978
+ expect(result).toContain("arg1");
979
+ expect(result).toContain("arg2");
980
+ });
981
+
982
+ it("should return error for non-existent skill", async () => {
983
+ const result = await skills_execute.execute({
984
+ skill: "non-existent",
985
+ script: "test.sh",
986
+ });
987
+
988
+ expect(result).toContain("not found");
989
+ });
990
+
991
+ it("should return error for non-existent script", async () => {
992
+ await skills_create.execute({
993
+ name: "no-script",
994
+ description: "Use when no script",
995
+ body: "Body",
996
+ });
997
+
998
+ const result = await skills_execute.execute({
999
+ skill: "no-script",
1000
+ script: "missing.sh",
1001
+ });
1002
+
1003
+ expect(result).toContain("not found");
1004
+ expect(result).toContain("Available:");
1005
+ });
1006
+
1007
+ it("should return non-zero exit code output", async () => {
1008
+ await skills_create.execute({
1009
+ name: "fail-test",
1010
+ description: "Use when failing",
1011
+ body: "Body",
1012
+ });
1013
+
1014
+ await skills_add_script.execute({
1015
+ skill: "fail-test",
1016
+ script_name: "fail.sh",
1017
+ content: '#!/bin/bash\necho "Failed"\nexit 1',
1018
+ });
1019
+
1020
+ const scriptPath = join(SKILLS_DIR, "fail-test", "scripts", "fail.sh");
1021
+ chmodSync(scriptPath, 0o755);
1022
+
1023
+ const result = await skills_execute.execute({
1024
+ skill: "fail-test",
1025
+ script: "fail.sh",
1026
+ });
1027
+
1028
+ expect(result).toContain("exited with code 1");
1029
+ expect(result).toContain("Failed");
1030
+ });
1031
+
1032
+ it("should timeout long-running scripts", async () => {
1033
+ await skills_create.execute({
1034
+ name: "timeout-test",
1035
+ description: "Use when timing out",
1036
+ body: "Body",
1037
+ });
1038
+
1039
+ await skills_add_script.execute({
1040
+ skill: "timeout-test",
1041
+ script_name: "slow.sh",
1042
+ content: '#!/bin/bash\nsleep 120', // 2 minutes (longer than 60s timeout)
1043
+ });
1044
+
1045
+ const scriptPath = join(SKILLS_DIR, "timeout-test", "scripts", "slow.sh");
1046
+ chmodSync(scriptPath, 0o755);
1047
+
1048
+ const result = await skills_execute.execute({
1049
+ skill: "timeout-test",
1050
+ script: "slow.sh",
1051
+ });
1052
+
1053
+ expect(result).toContain("timed out");
1054
+ expect(result).toContain("60 seconds");
1055
+ }, 65000); // Allow 65s for test itself
1056
+ });