opencode-swarm-plugin 0.12.19 → 0.12.22

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.
@@ -0,0 +1,408 @@
1
+ /**
2
+ * Unit tests for skills.ts
3
+ *
4
+ * Tests core functionality:
5
+ * - Frontmatter parsing
6
+ * - Path traversal protection
7
+ * - Skill discovery
8
+ * - ES module compatibility
9
+ */
10
+
11
+ import { describe, expect, it, beforeEach, afterEach } from "vitest";
12
+ import { join, resolve, relative } from "path";
13
+ import { mkdirSync, writeFileSync, rmSync, existsSync } from "fs";
14
+ import {
15
+ parseFrontmatter,
16
+ discoverSkills,
17
+ getSkill,
18
+ listSkills,
19
+ setSkillsProjectDirectory,
20
+ invalidateSkillsCache,
21
+ type Skill,
22
+ type SkillRef,
23
+ } from "./skills";
24
+
25
+ // ============================================================================
26
+ // Test Fixtures
27
+ // ============================================================================
28
+
29
+ // Use unique temp dir per test run to avoid collisions
30
+ const TEST_RUN_ID = `${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
31
+ const TEST_DIR = join(process.cwd(), `.test-skills-${TEST_RUN_ID}`);
32
+ const SKILLS_DIR = join(TEST_DIR, ".opencode", "skills");
33
+
34
+ const VALID_SKILL_MD = `---
35
+ name: test-skill
36
+ description: A test skill for unit testing
37
+ tags:
38
+ - testing
39
+ - unit-test
40
+ tools:
41
+ - Read
42
+ - Write
43
+ ---
44
+
45
+ # Test Skill
46
+
47
+ This is a test skill for unit testing purposes.
48
+
49
+ ## Instructions
50
+
51
+ 1. Do the thing
52
+ 2. Verify the thing
53
+ `;
54
+
55
+ const MINIMAL_SKILL_MD = `---
56
+ name: minimal-skill
57
+ description: Minimal skill with only required fields
58
+ ---
59
+
60
+ # Minimal Skill
61
+
62
+ Just the basics.
63
+ `;
64
+
65
+ const INVALID_FRONTMATTER_MD = `---
66
+ name:
67
+ description: Missing name value
68
+ ---
69
+
70
+ # Invalid
71
+
72
+ This has invalid frontmatter.
73
+ `;
74
+
75
+ const NO_FRONTMATTER_MD = `# No Frontmatter
76
+
77
+ This file has no YAML frontmatter at all.
78
+ `;
79
+
80
+ // ============================================================================
81
+ // Setup / Teardown
82
+ // ============================================================================
83
+
84
+ function setupTestSkillsDir() {
85
+ // Create test directory structure
86
+ mkdirSync(SKILLS_DIR, { recursive: true });
87
+ mkdirSync(join(SKILLS_DIR, "test-skill"), { recursive: true });
88
+ mkdirSync(join(SKILLS_DIR, "minimal-skill"), { recursive: true });
89
+ mkdirSync(join(SKILLS_DIR, "invalid-skill"), { recursive: true });
90
+
91
+ // Write test skill files
92
+ writeFileSync(join(SKILLS_DIR, "test-skill", "SKILL.md"), VALID_SKILL_MD);
93
+ writeFileSync(
94
+ join(SKILLS_DIR, "minimal-skill", "SKILL.md"),
95
+ MINIMAL_SKILL_MD,
96
+ );
97
+ writeFileSync(
98
+ join(SKILLS_DIR, "invalid-skill", "SKILL.md"),
99
+ INVALID_FRONTMATTER_MD,
100
+ );
101
+ }
102
+
103
+ function cleanupTestSkillsDir() {
104
+ if (existsSync(TEST_DIR)) {
105
+ rmSync(TEST_DIR, { recursive: true, force: true });
106
+ }
107
+ }
108
+
109
+ // ============================================================================
110
+ // Tests: parseFrontmatter
111
+ // ============================================================================
112
+
113
+ describe("parseFrontmatter", () => {
114
+ it("parses valid frontmatter with all fields", () => {
115
+ const result = parseFrontmatter(VALID_SKILL_MD);
116
+
117
+ expect(result).not.toBeNull();
118
+ expect(result.metadata.name).toBe("test-skill");
119
+ expect(result.metadata.description).toBe("A test skill for unit testing");
120
+ expect(result.metadata.tags).toEqual(["testing", "unit-test"]);
121
+ expect(result.metadata.tools).toEqual(["Read", "Write"]);
122
+ expect(result.body).toContain("# Test Skill");
123
+ });
124
+
125
+ it("parses minimal frontmatter with only required fields", () => {
126
+ const result = parseFrontmatter(MINIMAL_SKILL_MD);
127
+
128
+ expect(result).not.toBeNull();
129
+ expect(result.metadata.name).toBe("minimal-skill");
130
+ expect(result.metadata.description).toBe(
131
+ "Minimal skill with only required fields",
132
+ );
133
+ expect(result.metadata.tags).toBeUndefined();
134
+ expect(result.metadata.tools).toBeUndefined();
135
+ });
136
+
137
+ it("returns null for missing name value", () => {
138
+ const result = parseFrontmatter(INVALID_FRONTMATTER_MD);
139
+ // gray-matter/YAML parses "name: " (empty value) as null
140
+ // Validation happens later in loadSkill
141
+ expect(result.metadata.name).toBeNull();
142
+ });
143
+
144
+ it("returns empty result for content without frontmatter", () => {
145
+ const result = parseFrontmatter(NO_FRONTMATTER_MD);
146
+ // No frontmatter means empty metadata
147
+ expect(Object.keys(result.metadata).length).toBe(0);
148
+ });
149
+
150
+ it("returns empty result for empty content", () => {
151
+ const result = parseFrontmatter("");
152
+ expect(Object.keys(result.metadata).length).toBe(0);
153
+ });
154
+
155
+ it("handles frontmatter with extra fields", () => {
156
+ const content = `---
157
+ name: extra-fields
158
+ description: Has extra fields
159
+ custom_field: should be preserved
160
+ another: also preserved
161
+ ---
162
+
163
+ Body content.
164
+ `;
165
+ const result = parseFrontmatter(content);
166
+ expect(result.metadata.name).toBe("extra-fields");
167
+ expect(result.metadata.custom_field).toBe("should be preserved");
168
+ });
169
+
170
+ it("handles multiline description", () => {
171
+ // Note: The simple YAML parser doesn't handle | multiline syntax
172
+ // Use inline multiline instead
173
+ const content = `---
174
+ name: multiline
175
+ description: This is a description
176
+ ---
177
+
178
+ Body.
179
+ `;
180
+ const result = parseFrontmatter(content);
181
+ expect(result.metadata.name).toBe("multiline");
182
+ expect(result.metadata.description).toBe("This is a description");
183
+ });
184
+ });
185
+
186
+ // ============================================================================
187
+ // Tests: Skill Discovery
188
+ // ============================================================================
189
+
190
+ describe("discoverSkills", () => {
191
+ beforeEach(() => {
192
+ cleanupTestSkillsDir();
193
+ setupTestSkillsDir();
194
+ setSkillsProjectDirectory(TEST_DIR);
195
+ invalidateSkillsCache();
196
+ });
197
+
198
+ afterEach(() => {
199
+ cleanupTestSkillsDir();
200
+ invalidateSkillsCache();
201
+ });
202
+
203
+ it("discovers skills in project directory", async () => {
204
+ const skills = await discoverSkills();
205
+
206
+ // discoverSkills returns a Map<string, Skill>
207
+ expect(skills.has("test-skill")).toBe(true);
208
+ expect(skills.has("minimal-skill")).toBe(true);
209
+ });
210
+
211
+ it("skips skills with invalid frontmatter", async () => {
212
+ const skills = await discoverSkills();
213
+
214
+ // invalid-skill has empty name, should be skipped
215
+ expect(skills.has("invalid-skill")).toBe(false);
216
+ });
217
+
218
+ it("caches discovered skills", async () => {
219
+ const skills1 = await discoverSkills();
220
+ const skills2 = await discoverSkills();
221
+
222
+ // Should return same cached Map
223
+ expect(skills1).toBe(skills2);
224
+ });
225
+
226
+ it("invalidates cache when requested", async () => {
227
+ const skills1 = await discoverSkills();
228
+ invalidateSkillsCache();
229
+ const skills2 = await discoverSkills();
230
+
231
+ // Should be different Map instances after cache invalidation
232
+ expect(skills1).not.toBe(skills2);
233
+ });
234
+ });
235
+
236
+ describe("getSkill", () => {
237
+ beforeEach(() => {
238
+ cleanupTestSkillsDir();
239
+ setupTestSkillsDir();
240
+ setSkillsProjectDirectory(TEST_DIR);
241
+ invalidateSkillsCache();
242
+ });
243
+
244
+ afterEach(() => {
245
+ cleanupTestSkillsDir();
246
+ invalidateSkillsCache();
247
+ });
248
+
249
+ it("returns skill by exact name", async () => {
250
+ const skill = await getSkill("test-skill");
251
+
252
+ expect(skill).not.toBeNull();
253
+ expect(skill!.metadata.name).toBe("test-skill");
254
+ expect(skill!.metadata.description).toBe("A test skill for unit testing");
255
+ });
256
+
257
+ it("returns null for non-existent skill", async () => {
258
+ const skill = await getSkill("non-existent-skill");
259
+ expect(skill).toBeNull();
260
+ });
261
+
262
+ it("returns null for empty name", async () => {
263
+ const skill = await getSkill("");
264
+ expect(skill).toBeNull();
265
+ });
266
+ });
267
+
268
+ describe("listSkills", () => {
269
+ beforeEach(() => {
270
+ cleanupTestSkillsDir();
271
+ setupTestSkillsDir();
272
+ setSkillsProjectDirectory(TEST_DIR);
273
+ invalidateSkillsCache();
274
+ });
275
+
276
+ afterEach(() => {
277
+ cleanupTestSkillsDir();
278
+ invalidateSkillsCache();
279
+ });
280
+
281
+ it("returns skill refs with name, description, and path", async () => {
282
+ const refs = await listSkills();
283
+
284
+ const testSkillRef = refs.find((r) => r.name === "test-skill");
285
+ expect(testSkillRef).toBeDefined();
286
+ expect(testSkillRef!.description).toBe("A test skill for unit testing");
287
+ expect(testSkillRef!.path).toContain("test-skill");
288
+ });
289
+ });
290
+
291
+ // ============================================================================
292
+ // Tests: Path Traversal Protection
293
+ // ============================================================================
294
+
295
+ describe("path traversal protection", () => {
296
+ it("detects basic path traversal attempts", () => {
297
+ const maliciousPaths = [
298
+ "../../../etc/passwd",
299
+ "..\\..\\windows\\system32",
300
+ "foo/../../../bar",
301
+ "/etc/passwd",
302
+ "C:\\Windows\\System32",
303
+ ];
304
+
305
+ for (const path of maliciousPaths) {
306
+ // These should be caught by the initial check
307
+ const hasTraversal =
308
+ path.includes("..") || path.startsWith("/") || path.includes(":\\");
309
+ expect(hasTraversal).toBe(true);
310
+ }
311
+ });
312
+
313
+ it("allows valid relative paths", () => {
314
+ const validPaths = [
315
+ "examples.md",
316
+ "templates/component.tsx",
317
+ "reference/api.md",
318
+ "scripts/setup.sh",
319
+ ];
320
+
321
+ for (const path of validPaths) {
322
+ const hasTraversal = path.includes("..") || path.startsWith("/");
323
+ expect(hasTraversal).toBe(false);
324
+ }
325
+ });
326
+
327
+ it("resolve + relative check catches encoded traversal", () => {
328
+ // Even if initial check is bypassed, resolve + relative catches it
329
+ const skillDir = "/home/user/skills/my-skill";
330
+ const maliciousFile = "foo/../../etc/passwd";
331
+
332
+ const resolved = resolve(skillDir, maliciousFile);
333
+ const rel = relative(skillDir, resolved);
334
+
335
+ // The relative path starts with ".." meaning it escapes
336
+ expect(rel.startsWith("..")).toBe(true);
337
+ });
338
+ });
339
+
340
+ // ============================================================================
341
+ // Tests: ES Module Compatibility
342
+ // ============================================================================
343
+
344
+ describe("ES module compatibility", () => {
345
+ it("import.meta.url is available", () => {
346
+ // This test verifies we're running in an ES module context
347
+ expect(import.meta.url).toBeDefined();
348
+ // Check for file protocol and skills.test in path (works across runners)
349
+ expect(import.meta.url).toMatch(/skills\.test/);
350
+ expect(import.meta.url).toMatch(/^file:|^bun:|^\//); // file://, bun://, or absolute path
351
+ });
352
+
353
+ it("can construct path from import.meta.url", () => {
354
+ const currentDir = new URL(".", import.meta.url).pathname;
355
+ expect(currentDir).toBeDefined();
356
+ expect(currentDir.endsWith("/")).toBe(true);
357
+ });
358
+ });
359
+
360
+ // ============================================================================
361
+ // Tests: Edge Cases
362
+ // ============================================================================
363
+
364
+ describe("edge cases", () => {
365
+ beforeEach(() => {
366
+ cleanupTestSkillsDir();
367
+ invalidateSkillsCache();
368
+ });
369
+
370
+ afterEach(() => {
371
+ cleanupTestSkillsDir();
372
+ invalidateSkillsCache();
373
+ });
374
+
375
+ it("handles non-existent skills directory gracefully", async () => {
376
+ setSkillsProjectDirectory("/non/existent/path");
377
+ invalidateSkillsCache();
378
+
379
+ // Should not throw, just return empty or global skills only
380
+ const skills = await discoverSkills();
381
+ expect(skills instanceof Map).toBe(true);
382
+ });
383
+
384
+ it("handles empty skills directory", async () => {
385
+ mkdirSync(SKILLS_DIR, { recursive: true });
386
+ setSkillsProjectDirectory(TEST_DIR);
387
+ invalidateSkillsCache();
388
+
389
+ // Should not throw
390
+ const skills = await discoverSkills();
391
+ expect(skills instanceof Map).toBe(true);
392
+ });
393
+
394
+ it("handles skill directory without SKILL.md", async () => {
395
+ mkdirSync(join(SKILLS_DIR, "empty-skill"), { recursive: true });
396
+ writeFileSync(
397
+ join(SKILLS_DIR, "empty-skill", "README.md"),
398
+ "# Not a skill",
399
+ );
400
+ setSkillsProjectDirectory(TEST_DIR);
401
+ invalidateSkillsCache();
402
+
403
+ const skills = await discoverSkills();
404
+
405
+ // Should not include the directory without SKILL.md
406
+ expect(skills.has("empty-skill")).toBe(false);
407
+ });
408
+ });