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
|
import { describe, expect, it } from "vitest";
import {
isPathWithinDir,
isValidSkillName,
mergeCatalog,
parseSkillMeta,
renderDescription,
stripLoadedBody,
} from "./pure.js";
describe("parseSkillMeta", () => {
it("extracts summary when line 2 is ---", () => {
const content = "Use this for web searches\n---\nBody content here";
const result = parseSkillMeta(content);
expect(result.hasMeta).toBe(true);
expect(result.summary).toBe("Use this for web searches");
});
it("extracts summary with trailing whitespace on separator", () => {
const content = "Summary text\n--- \nBody";
const result = parseSkillMeta(content);
expect(result.hasMeta).toBe(true);
expect(result.summary).toBe("Summary text");
});
it("reports no metadata when line 2 is not ---", () => {
const content = "Some content\nNot a separator\nBody";
const result = parseSkillMeta(content);
expect(result.hasMeta).toBe(false);
expect(result.summary).toBeUndefined();
});
it("reports no metadata for single-line content", () => {
const content = "Only one line";
const result = parseSkillMeta(content);
expect(result.hasMeta).toBe(false);
});
it("reports no metadata for empty content", () => {
const result = parseSkillMeta("");
expect(result.hasMeta).toBe(false);
});
it("treats empty summary as undefined", () => {
const content = "\n---\nBody";
const result = parseSkillMeta(content);
expect(result.hasMeta).toBe(true);
expect(result.summary).toBeUndefined();
});
});
describe("stripLoadedBody", () => {
it("removes the first two lines when metadata is present", () => {
const content = "Summary\n---\nLine 3\nLine 4";
const result = stripLoadedBody(content, true);
expect(result).toBe("Line 3\nLine 4");
});
it("returns the whole file when malformed", () => {
const content = "No metadata here\nJust content";
const result = stripLoadedBody(content, false);
expect(result).toBe("No metadata here\nJust content");
});
it("handles file with only metadata and no body", () => {
const content = "Summary\n---\n";
const result = stripLoadedBody(content, true);
expect(result).toBe("");
});
});
describe("mergeCatalog", () => {
it("merges disjoint entries", () => {
const home = [{ name: "a" }, { name: "b" }];
const cwd = [{ name: "c" }];
const result = mergeCatalog(home, cwd);
expect(result.map((e) => e.name)).toEqual(["a", "b", "c"]);
});
it("cwd skill shadows home skill of the same name", () => {
const home = [{ name: "shared", summary: "home summary" }];
const cwd = [{ name: "shared", summary: "cwd summary" }];
const result = mergeCatalog(home, cwd);
expect(result).toHaveLength(1);
expect(result[0]?.summary).toBe("cwd summary");
});
it("sorts by name", () => {
const home = [{ name: "z" }, { name: "a" }];
const cwd = [{ name: "m" }];
const result = mergeCatalog(home, cwd);
expect(result.map((e) => e.name)).toEqual(["a", "m", "z"]);
});
it("handles empty inputs", () => {
expect(mergeCatalog([], [])).toEqual([]);
});
});
describe("renderDescription", () => {
it("lists all skills by name and appends summaries only for valid ones", () => {
const catalog = [
{ name: "web-search", summary: "Use for web searches" },
{ name: "malformed-skill" },
];
const result = renderDescription(catalog);
expect(result).toBe(
"Load a skill by name. Available skills:\n- web-search: Use for web searches\n- malformed-skill",
);
});
it("returns a plain message when no skills are available", () => {
const result = renderDescription([]);
expect(result).toBe("Load a skill by name. No skills are currently available.");
});
it("lists skills with summaries and without", () => {
const catalog = [
{ name: "alpha", summary: "First skill" },
{ name: "beta" },
{ name: "gamma", summary: "Third skill" },
];
const result = renderDescription(catalog);
expect(result).toContain("- alpha: First skill");
expect(result).toContain("- beta");
expect(result).toContain("- gamma: Third skill");
});
});
describe("isValidSkillName", () => {
it("accepts a bare skill name", () => {
expect(isValidSkillName("web-search")).toBe(true);
});
it("rejects a name containing /", () => {
expect(isValidSkillName("../escape")).toBe(false);
});
it("rejects a name containing \\", () => {
expect(isValidSkillName("path\\name")).toBe(false);
});
it("rejects a name containing ..", () => {
expect(isValidSkillName("skill..evil")).toBe(false);
});
it("rejects an empty string", () => {
expect(isValidSkillName("")).toBe(false);
});
it("rejects a non-string value", () => {
expect(isValidSkillName(123)).toBe(false);
expect(isValidSkillName(null)).toBe(false);
expect(isValidSkillName(undefined)).toBe(false);
});
});
describe("isPathWithinDir", () => {
it("accepts a path within the directory", () => {
expect(isPathWithinDir("/tmp/base/file.txt", "/tmp/base")).toBe(true);
});
it("accepts the directory itself", () => {
expect(isPathWithinDir("/tmp/base", "/tmp/base")).toBe(true);
});
it("rejects a path outside the directory", () => {
expect(isPathWithinDir("/tmp/other/file.txt", "/tmp/base")).toBe(false);
});
it("rejects a prefix attack", () => {
expect(isPathWithinDir("/tmp/base-evil/file.txt", "/tmp/base")).toBe(false);
});
});
|