Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Path from "effect/Path";
6
7import { discoverClaudeSkills } from "./ClaudeSkills.ts";
8
9const writeSkill = Effect.fn(function* (
10 skillsDir: string,
11 directoryName: string,
12 contents: string,
13) {
14 const fs = yield* FileSystem.FileSystem;
15 const path = yield* Path.Path;
16 const skillDir = path.join(skillsDir, directoryName);
17 yield* fs.makeDirectory(skillDir, { recursive: true });
18 yield* fs.writeFileString(path.join(skillDir, "SKILL.md"), contents);
19});
20
21it.layer(NodeServices.layer)("discoverClaudeSkills", (it) => {
22 it.effect("discovers user and project skills with frontmatter metadata", () =>
23 Effect.gen(function* () {
24 const fs = yield* FileSystem.FileSystem;
25 const path = yield* Path.Path;
26 const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-claude-skills-" });
27 const configDir = path.join(tempDir, "claude-home");
28 const workspace = path.join(tempDir, "workspace");
29
30 yield* writeSkill(
31 path.join(configDir, "skills"),
32 "codex-review",
33 [
34 "---",
35 "name: codex-review",
36 "description: Ask Codex for a review.",
37 "---",
38 "",
39 "# Body",
40 ].join("\n"),
41 );
42 yield* writeSkill(
43 path.join(workspace, ".claude", "skills"),
44 "deploy",
45 ["---", "name: deploy", "description: Deploy the app.", "---", "", "# Deploy"].join("\n"),
46 );
47
48 const skills = yield* discoverClaudeSkills({ homePath: configDir }, workspace);
49
50 assert.deepEqual(skills, [
51 {
52 name: "codex-review",
53 path: path.join(configDir, "skills", "codex-review", "SKILL.md"),
54 enabled: true,
55 scope: "user",
56 description: "Ask Codex for a review.",
57 },
58 {
59 name: "deploy",
60 path: path.join(workspace, ".claude", "skills", "deploy", "SKILL.md"),
61 enabled: true,
62 scope: "project",
63 description: "Deploy the app.",
64 },
65 ]);
66 }),
67 );
68
69 it.effect("discovers project skills from the workspace .agents directory", () =>
70 Effect.gen(function* () {
71 const fs = yield* FileSystem.FileSystem;
72 const path = yield* Path.Path;
73 const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-claude-skills-" });
74 const configDir = path.join(tempDir, "claude-home");
75 const workspace = path.join(tempDir, "workspace");
76
77 yield* writeSkill(
78 path.join(workspace, ".agents", "skills"),
79 "review",
80 ["---", "name: review", "description: Review the changes.", "---"].join("\n"),
81 );
82
83 const skills = yield* discoverClaudeSkills({ homePath: configDir }, workspace);
84
85 assert.deepEqual(skills, [
86 {
87 name: "review",
88 path: path.join(workspace, ".agents", "skills", "review", "SKILL.md"),
89 enabled: true,
90 scope: "project",
91 description: "Review the changes.",
92 },
93 ]);
94 }),
95 );
96
97 it.effect("prefers workspace .claude skills on three-way name collisions", () =>
98 Effect.gen(function* () {
99 const fs = yield* FileSystem.FileSystem;
100 const path = yield* Path.Path;
101 const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-claude-skills-" });
102 const configDir = path.join(tempDir, "claude-home");
103 const workspace = path.join(tempDir, "workspace");
104
105 yield* writeSkill(
106 path.join(configDir, "skills"),
107 "deploy",
108 ["---", "name: deploy", "description: User deploy.", "---"].join("\n"),
109 );
110 yield* writeSkill(
111 path.join(workspace, ".agents", "skills"),
112 "deploy",
113 ["---", "name: deploy", "description: Agents deploy.", "---"].join("\n"),
114 );
115 yield* writeSkill(
116 path.join(workspace, ".claude", "skills"),
117 "deploy",
118 ["---", "name: deploy", "description: Claude deploy.", "---"].join("\n"),
119 );
120
121 const skills = yield* discoverClaudeSkills({ homePath: configDir }, workspace);
122
123 assert.deepEqual(skills, [
124 {
125 name: "deploy",
126 path: path.join(workspace, ".claude", "skills", "deploy", "SKILL.md"),
127 enabled: true,
128 scope: "project",
129 description: "Claude deploy.",
130 },
131 ]);
132 }),
133 );
134
135 it.effect("prefers workspace .agents skills over user skills on name collisions", () =>
136 Effect.gen(function* () {
137 const fs = yield* FileSystem.FileSystem;
138 const path = yield* Path.Path;
139 const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-claude-skills-" });
140 const configDir = path.join(tempDir, "claude-home");
141 const workspace = path.join(tempDir, "workspace");
142
143 yield* writeSkill(
144 path.join(configDir, "skills"),
145 "deploy",
146 ["---", "name: deploy", "description: User deploy.", "---"].join("\n"),
147 );
148 yield* writeSkill(
149 path.join(workspace, ".agents", "skills"),
150 "deploy",
151 ["---", "name: deploy", "description: Agents deploy.", "---"].join("\n"),
152 );
153
154 const skills = yield* discoverClaudeSkills({ homePath: configDir }, workspace);
155
156 assert.deepEqual(skills, [
157 {
158 name: "deploy",
159 path: path.join(workspace, ".agents", "skills", "deploy", "SKILL.md"),
160 enabled: true,
161 scope: "project",
162 description: "Agents deploy.",
163 },
164 ]);
165 }),
166 );
167
168 it.effect("prefers project skills over user skills on name collisions", () =>
169 Effect.gen(function* () {
170 const fs = yield* FileSystem.FileSystem;
171 const path = yield* Path.Path;
172 const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-claude-skills-" });
173 const configDir = path.join(tempDir, "claude-home");
174 const workspace = path.join(tempDir, "workspace");
175
176 yield* writeSkill(
177 path.join(configDir, "skills"),
178 "deploy",
179 ["---", "name: deploy", "description: User deploy.", "---"].join("\n"),
180 );
181 yield* writeSkill(
182 path.join(workspace, ".claude", "skills"),
183 "deploy",
184 ["---", "name: deploy", "description: Project deploy.", "---"].join("\n"),
185 );
186
187 const skills = yield* discoverClaudeSkills({ homePath: configDir }, workspace);
188
189 assert.equal(skills.length, 1);
190 assert.equal(skills[0]?.scope, "project");
191 assert.equal(skills[0]?.description, "Project deploy.");
192 }),
193 );
194
195 it.effect("falls back to the directory name and skips malformed frontmatter", () =>
196 Effect.gen(function* () {
197 const fs = yield* FileSystem.FileSystem;
198 const path = yield* Path.Path;
199 const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-claude-skills-" });
200 const configDir = path.join(tempDir, "claude-home");
201 const skillsDir = path.join(configDir, "skills");
202
203 yield* writeSkill(skillsDir, "no-frontmatter", "# Just a heading\n");
204 yield* writeSkill(skillsDir, "broken-yaml", "---\nname: [unclosed\n---\n");
205 // A stray file (not a directory with SKILL.md) must be skipped.
206 yield* fs.makeDirectory(skillsDir, { recursive: true });
207 yield* fs.writeFileString(path.join(skillsDir, "README.md"), "not a skill");
208
209 const skills = yield* discoverClaudeSkills({ homePath: configDir }, undefined);
210
211 // A skill with no frontmatter falls back to its directory name; a skill
212 // whose frontmatter fails to parse is skipped entirely (Claude Code
213 // won't load it either).
214 assert.deepEqual(
215 skills.map((skill) => skill.name),
216 ["no-frontmatter"],
217 );
218 assert.equal(skills[0]?.description, undefined);
219 }),
220 );
221
222 it.effect("honors CLAUDE_CONFIG_DIR from the environment when homePath is unset", () =>
223 Effect.gen(function* () {
224 const fs = yield* FileSystem.FileSystem;
225 const path = yield* Path.Path;
226 const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-claude-skills-" });
227 const environmentConfigDir = path.join(tempDir, "env-config");
228
229 yield* writeSkill(
230 path.join(environmentConfigDir, "skills"),
231 "env-skill",
232 ["---", "name: env-skill", "description: From env config dir.", "---"].join("\n"),
233 );
234
235 const skills = yield* discoverClaudeSkills({ homePath: "" }, undefined, {
236 CLAUDE_CONFIG_DIR: environmentConfigDir,
237 });
238
239 assert.deepEqual(
240 skills.map((skill) => skill.name),
241 ["env-skill"],
242 );
243
244 // An explicit homePath wins over the environment variable, matching
245 // makeClaudeEnvironment which overwrites CLAUDE_CONFIG_DIR for the CLI.
246 const explicitHome = path.join(tempDir, "explicit-home");
247 yield* writeSkill(
248 path.join(explicitHome, "skills"),
249 "explicit-skill",
250 ["---", "name: explicit-skill", "---"].join("\n"),
251 );
252 const explicitSkills = yield* discoverClaudeSkills({ homePath: explicitHome }, undefined, {
253 CLAUDE_CONFIG_DIR: environmentConfigDir,
254 });
255 assert.deepEqual(
256 explicitSkills.map((skill) => skill.name),
257 ["explicit-skill"],
258 );
259 }),
260 );
261
262 it.effect("resolves a relative CLAUDE_CONFIG_DIR against the workspace cwd", () =>
263 Effect.gen(function* () {
264 const fs = yield* FileSystem.FileSystem;
265 const path = yield* Path.Path;
266 const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-claude-skills-" });
267 const workspace = path.join(tempDir, "workspace");
268 yield* fs.makeDirectory(workspace, { recursive: true });
269
270 // The spawned CLI resolves a relative CLAUDE_CONFIG_DIR against its own
271 // cwd (the workspace), so discovery must do the same.
272 yield* writeSkill(
273 path.join(workspace, "relative-config", "skills"),
274 "relative-skill",
275 ["---", "name: relative-skill", "---"].join("\n"),
276 );
277
278 const skills = yield* discoverClaudeSkills({ homePath: "" }, workspace, {
279 CLAUDE_CONFIG_DIR: "relative-config",
280 });
281
282 assert.deepEqual(
283 skills.map((skill) => skill.name),
284 ["relative-skill"],
285 );
286 assert.equal(skills[0]?.scope, "user");
287 }),
288 );
289
290 it.effect("returns an empty list when no skill roots exist", () =>
291 Effect.gen(function* () {
292 const fs = yield* FileSystem.FileSystem;
293 const path = yield* Path.Path;
294 const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-claude-skills-" });
295
296 const skills = yield* discoverClaudeSkills(
297 { homePath: path.join(tempDir, "missing-home") },
298 path.join(tempDir, "missing-workspace"),
299 );
300
301 assert.deepEqual(skills, []);
302 }),
303 );
304});