Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ClaudeSkills.test.ts

205 lines7.3 KB

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("prefers project skills over user skills on name collisions", () =>
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(configDir, "skills"),
79 "deploy",
80 ["---", "name: deploy", "description: User deploy.", "---"].join("\n"),
81 );
82 yield* writeSkill(
83 path.join(workspace, ".claude", "skills"),
84 "deploy",
85 ["---", "name: deploy", "description: Project deploy.", "---"].join("\n"),
86 );
87​
88 const skills = yield* discoverClaudeSkills({ homePath: configDir }, workspace);
89​
90 assert.equal(skills.length, 1);
91 assert.equal(skills[0]?.scope, "project");
92 assert.equal(skills[0]?.description, "Project deploy.");
93 }),
94 );
95​
96 it.effect("falls back to the directory name and skips malformed frontmatter", () =>
97 Effect.gen(function* () {
98 const fs = yield* FileSystem.FileSystem;
99 const path = yield* Path.Path;
100 const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-claude-skills-" });
101 const configDir = path.join(tempDir, "claude-home");
102 const skillsDir = path.join(configDir, "skills");
103​
104 yield* writeSkill(skillsDir, "no-frontmatter", "# Just a heading\n");
105 yield* writeSkill(skillsDir, "broken-yaml", "---\nname: [unclosed\n---\n");
106 // A stray file (not a directory with SKILL.md) must be skipped.
107 yield* fs.makeDirectory(skillsDir, { recursive: true });
108 yield* fs.writeFileString(path.join(skillsDir, "README.md"), "not a skill");
109​
110 const skills = yield* discoverClaudeSkills({ homePath: configDir }, undefined);
111​
112 // A skill with no frontmatter falls back to its directory name; a skill
113 // whose frontmatter fails to parse is skipped entirely (Claude Code
114 // won't load it either).
115 assert.deepEqual(
116 skills.map((skill) => skill.name),
117 ["no-frontmatter"],
118 );
119 assert.equal(skills[0]?.description, undefined);
120 }),
121 );
122​
123 it.effect("honors CLAUDE_CONFIG_DIR from the environment when homePath is unset", () =>
124 Effect.gen(function* () {
125 const fs = yield* FileSystem.FileSystem;
126 const path = yield* Path.Path;
127 const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-claude-skills-" });
128 const environmentConfigDir = path.join(tempDir, "env-config");
129​
130 yield* writeSkill(
131 path.join(environmentConfigDir, "skills"),
132 "env-skill",
133 ["---", "name: env-skill", "description: From env config dir.", "---"].join("\n"),
134 );
135​
136 const skills = yield* discoverClaudeSkills({ homePath: "" }, undefined, {
137 CLAUDE_CONFIG_DIR: environmentConfigDir,
138 });
139​
140 assert.deepEqual(
141 skills.map((skill) => skill.name),
142 ["env-skill"],
143 );
144​
145 // An explicit homePath wins over the environment variable, matching
146 // makeClaudeEnvironment which overwrites CLAUDE_CONFIG_DIR for the CLI.
147 const explicitHome = path.join(tempDir, "explicit-home");
148 yield* writeSkill(
149 path.join(explicitHome, "skills"),
150 "explicit-skill",
151 ["---", "name: explicit-skill", "---"].join("\n"),
152 );
153 const explicitSkills = yield* discoverClaudeSkills({ homePath: explicitHome }, undefined, {
154 CLAUDE_CONFIG_DIR: environmentConfigDir,
155 });
156 assert.deepEqual(
157 explicitSkills.map((skill) => skill.name),
158 ["explicit-skill"],
159 );
160 }),
161 );
162​
163 it.effect("resolves a relative CLAUDE_CONFIG_DIR against the workspace cwd", () =>
164 Effect.gen(function* () {
165 const fs = yield* FileSystem.FileSystem;
166 const path = yield* Path.Path;
167 const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-claude-skills-" });
168 const workspace = path.join(tempDir, "workspace");
169 yield* fs.makeDirectory(workspace, { recursive: true });
170​
171 // The spawned CLI resolves a relative CLAUDE_CONFIG_DIR against its own
172 // cwd (the workspace), so discovery must do the same.
173 yield* writeSkill(
174 path.join(workspace, "relative-config", "skills"),
175 "relative-skill",
176 ["---", "name: relative-skill", "---"].join("\n"),
177 );
178​
179 const skills = yield* discoverClaudeSkills({ homePath: "" }, workspace, {
180 CLAUDE_CONFIG_DIR: "relative-config",
181 });
182​
183 assert.deepEqual(
184 skills.map((skill) => skill.name),
185 ["relative-skill"],
186 );
187 assert.equal(skills[0]?.scope, "user");
188 }),
189 );
190​
191 it.effect("returns an empty list when no skill roots exist", () =>
192 Effect.gen(function* () {
193 const fs = yield* FileSystem.FileSystem;
194 const path = yield* Path.Path;
195 const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-claude-skills-" });
196​
197 const skills = yield* discoverClaudeSkills(
198 { homePath: path.join(tempDir, "missing-home") },
199 path.join(tempDir, "missing-workspace"),
200 );
201​
202 assert.deepEqual(skills, []);
203 }),
204 );
205});