Open History to see the last commit
1import * as NodeOS from "node:os";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import { describe, expect, it } from "vite-plus/test";
4import * as Effect from "effect/Effect";
5import * as FileSystem from "effect/FileSystem";
6import * as Path from "effect/Path";
7import type * as Scope from "effect/Scope";
8import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
9
10import {
11 discoverCursorSkills,
12 hasCursorSkillMention,
13 probeCursorSkills,
14 rewriteCursorSkillMentions,
15} from "./skills.ts";
16
17const runNode = <A, E>(
18 effect: Effect.Effect<A, E, FileSystem.FileSystem | Path.Path | Scope.Scope>,
19) => effect.pipe(Effect.scoped, Effect.provide(NodeServices.layer), Effect.runPromise);
20
21describe("Cursor skills", () => {
22 it("discovers recursive project skills with project precedence", async () =>
23 await runNode(
24 Effect.gen(function* () {
25 const fileSystem = yield* FileSystem.FileSystem;
26 const path = yield* Path.Path;
27 const userHome = yield* fileSystem
28 .makeTempDirectoryScoped({
29 directory: NodeOS.tmpdir(),
30 prefix: "cursor-skills-home-",
31 })
32 .pipe(Effect.flatMap((directory) => fileSystem.realPath(directory)));
33 const workspace = yield* fileSystem
34 .makeTempDirectoryScoped({
35 directory: NodeOS.tmpdir(),
36 prefix: "cursor-skills-workspace-",
37 })
38 .pipe(Effect.flatMap((directory) => fileSystem.realPath(directory)));
39 const writeSkill = Effect.fn("writeCursorSkill")(function* (
40 root: string,
41 name: string,
42 contents: string,
43 ) {
44 const skillDirectory = path.join(root, name);
45 yield* fileSystem.makeDirectory(skillDirectory, { recursive: true });
46 yield* fileSystem.writeFileString(path.join(skillDirectory, "SKILL.md"), contents);
47 });
48
49 yield* writeSkill(
50 path.join(userHome, ".cursor", "skills"),
51 "review",
52 "---\ndescription: user review\n---\n",
53 );
54 yield* writeSkill(
55 path.join(workspace, ".agents", "skills", "nested"),
56 "review",
57 "---\nname: Review changes\ndescription: project review\n---\n",
58 );
59 yield* writeSkill(
60 path.join(workspace, ".cursor", "skills"),
61 "internal",
62 "---\nuser-invocable: false\n---\n",
63 );
64 yield* writeSkill(
65 path.join(workspace, ".cursor", "skills"),
66 "oversized",
67 "x".repeat(1_000_001),
68 );
69 yield* fileSystem.makeDirectory(path.join(userHome, ".codex"), { recursive: true });
70 yield* fileSystem.writeFileString(
71 path.join(userHome, ".codex", "skills"),
72 "not a directory",
73 );
74
75 const skills = yield* discoverCursorSkills(workspace, { HOME: userHome });
76 expect(skills).toEqual([
77 {
78 name: "internal",
79 path: path.join(workspace, ".cursor", "skills", "internal", "SKILL.md"),
80 scope: "project",
81 enabled: true,
82 userInvocable: false,
83 },
84 {
85 name: "oversized",
86 path: path.join(workspace, ".cursor", "skills", "oversized", "SKILL.md"),
87 scope: "project",
88 enabled: true,
89 },
90 {
91 name: "review",
92 displayName: "Review changes",
93 description: "project review",
94 path: path.join(workspace, ".agents", "skills", "nested", "review", "SKILL.md"),
95 scope: "project",
96 enabled: true,
97 },
98 ]);
99 expect(
100 (yield* probeCursorSkills(workspace, { HOME: userHome }).pipe(Effect.result))._tag,
101 ).toBe("Failure");
102 }),
103 ));
104
105 it("skips a skill tree deeper than the limit instead of failing discovery", async () =>
106 await runNode(
107 Effect.gen(function* () {
108 const fileSystem = yield* FileSystem.FileSystem;
109 const path = yield* Path.Path;
110 const tempDirectory = (prefix: string) =>
111 fileSystem
112 .makeTempDirectoryScoped({ directory: NodeOS.tmpdir(), prefix })
113 .pipe(Effect.flatMap((directory) => fileSystem.realPath(directory)));
114 const userHome = yield* tempDirectory("cursor-skills-home-");
115 const workspace = yield* tempDirectory("cursor-skills-workspace-");
116 const writeSkill = Effect.fn("writeCursorSkill")(function* (directory: string) {
117 yield* fileSystem.makeDirectory(directory, { recursive: true });
118 yield* fileSystem.writeFileString(path.join(directory, "SKILL.md"), "---\n---\n");
119 });
120 const userRoot = path.join(userHome, ".claude", "skills");
121 const atLimit = path.join(
122 userRoot,
123 "deep",
124 "2",
125 "3",
126 "4",
127 "5",
128 "6",
129 "7",
130 "8",
131 "9",
132 "deep-skill",
133 );
134
135 yield* writeSkill(path.join(workspace, ".agents", "skills", "review"));
136 yield* writeSkill(atLimit);
137 yield* writeSkill(path.join(atLimit, "below"));
138 yield* writeSkill(path.join(userRoot, "later"));
139
140 const expected = [
141 {
142 name: "deep-skill",
143 path: path.join(atLimit, "SKILL.md"),
144 scope: "user",
145 enabled: true,
146 },
147 {
148 name: "later",
149 path: path.join(userRoot, "later", "SKILL.md"),
150 scope: "user",
151 enabled: true,
152 },
153 {
154 name: "review",
155 path: path.join(workspace, ".agents", "skills", "review", "SKILL.md"),
156 scope: "project",
157 enabled: true,
158 },
159 ];
160 expect(yield* discoverCursorSkills(workspace, { HOME: userHome })).toEqual(expected);
161 expect(yield* probeCursorSkills(workspace, { HOME: userHome })).toEqual(expected);
162 }),
163 ));
164
165 it("discovers skills across roots without scanning installed node_modules", async () =>
166 await runNode(
167 Effect.gen(function* () {
168 const fileSystem = yield* FileSystem.FileSystem;
169 const path = yield* Path.Path;
170 const userHome = yield* fileSystem
171 .makeTempDirectoryScoped({
172 directory: NodeOS.tmpdir(),
173 prefix: "cursor-skills-home-",
174 })
175 .pipe(Effect.flatMap((directory) => fileSystem.realPath(directory)));
176 const root = path.join(userHome, ".agents", "skills");
177 const dependencies = path.join(root, "review", "tests", "node_modules");
178 yield* fileSystem.makeDirectory(dependencies, { recursive: true });
179 yield* Effect.forEach(
180 Array.from({ length: 10_001 }, (_, index) => path.join(dependencies, `entry-${index}`)),
181 (file) => fileSystem.writeFileString(file, ""),
182 { concurrency: 64, discard: true },
183 );
184 const skillDirectories = [
185 path.join(root, "review"),
186 path.join(root, "sibling"),
187 path.join(userHome, ".claude", "skills", "later"),
188 ];
189 for (const directory of skillDirectories) {
190 yield* fileSystem.makeDirectory(directory, { recursive: true });
191 yield* fileSystem.writeFileString(path.join(directory, "SKILL.md"), "---\n---\n");
192 }
193
194 const result = yield* probeCursorSkills(undefined, { HOME: userHome }).pipe(Effect.result);
195 expect(result._tag).toBe("Success");
196 if (result._tag !== "Success") return;
197 expect(result.success).toEqual(
198 skillDirectories
199 .map((directory) => ({
200 name: path.basename(directory),
201 path: path.join(directory, "SKILL.md"),
202 scope: "user",
203 enabled: true,
204 }))
205 .sort((left, right) => left.name.localeCompare(right.name)),
206 );
207 }),
208 ));
209
210 it("still fails discovery when a root holds more entries than the scan allows", async () =>
211 await runNode(
212 Effect.gen(function* () {
213 const fileSystem = yield* FileSystem.FileSystem;
214 const path = yield* Path.Path;
215 const userHome = yield* fileSystem.makeTempDirectoryScoped({
216 directory: NodeOS.tmpdir(),
217 prefix: "cursor-skills-home-",
218 });
219 const root = path.join(userHome, ".cursor", "skills");
220 yield* fileSystem.makeDirectory(root, { recursive: true });
221 yield* Effect.forEach(
222 Array.from({ length: 10_001 }, (_, index) => path.join(root, `entry-${index}`)),
223 (file) => fileSystem.writeFileString(file, ""),
224 { concurrency: 64, discard: true },
225 );
226
227 const error = yield* Effect.flip(probeCursorSkills(undefined, { HOME: userHome }));
228 expect(error.message).toContain("scan-budget-exhausted");
229 }),
230 ));
231
232 it.skipIf(!symlinksSupported)(
233 "scans a skills root linked to another root only once",
234 async () =>
235 await runNode(
236 Effect.gen(function* () {
237 const fileSystem = yield* FileSystem.FileSystem;
238 const path = yield* Path.Path;
239 const userHome = yield* fileSystem.makeTempDirectoryScoped({
240 directory: NodeOS.tmpdir(),
241 prefix: "cursor-skills-home-",
242 });
243 // More than half the entry budget, so a second walk of the same tree runs out.
244 const claudeRoot = path.join(userHome, ".claude", "skills");
245 yield* fileSystem.makeDirectory(path.join(claudeRoot, "review"), { recursive: true });
246 yield* fileSystem.writeFileString(
247 path.join(claudeRoot, "review", "SKILL.md"),
248 "---\n---\n",
249 );
250 yield* Effect.forEach(
251 Array.from({ length: 6_000 }, (_, index) => path.join(claudeRoot, `entry-${index}`)),
252 (file) => fileSystem.writeFileString(file, ""),
253 { concurrency: 64, discard: true },
254 );
255 yield* fileSystem.makeDirectory(path.join(userHome, ".agents"), { recursive: true });
256 yield* fileSystem.symlink(claudeRoot, path.join(userHome, ".agents", "skills"));
257
258 const result = yield* probeCursorSkills(undefined, { HOME: userHome }).pipe(
259 Effect.result,
260 );
261 expect(result._tag).toBe("Success");
262 const skills = yield* discoverCursorSkills(undefined, { HOME: userHome });
263 expect(skills.map((skill) => `${skill.scope}:${skill.name}`)).toEqual(["user:review"]);
264 }),
265 ),
266 );
267
268 it.skipIf(!symlinksSupported)(
269 "treats a symlinked skill outside the root as a package boundary",
270 async () =>
271 await runNode(
272 Effect.gen(function* () {
273 const fileSystem = yield* FileSystem.FileSystem;
274 const path = yield* Path.Path;
275 const userHome = yield* fileSystem.makeTempDirectoryScoped({
276 directory: NodeOS.tmpdir(),
277 prefix: "cursor-skills-home-",
278 });
279 const workspace = yield* fileSystem
280 .makeTempDirectoryScoped({
281 directory: NodeOS.tmpdir(),
282 prefix: "cursor-skills-workspace-",
283 })
284 .pipe(Effect.flatMap((directory) => fileSystem.realPath(directory)));
285 const library = yield* fileSystem.makeTempDirectoryScoped({
286 directory: NodeOS.tmpdir(),
287 prefix: "cursor-skills-library-",
288 });
289 const writeSkill = Effect.fn("writeCursorSkill")(function* (
290 directory: string,
291 contents: string,
292 ) {
293 yield* fileSystem.makeDirectory(directory, { recursive: true });
294 yield* fileSystem.writeFileString(path.join(directory, "SKILL.md"), contents);
295 });
296
297 yield* writeSkill(path.join(library, "shared-review"), "---\ndescription: shared\n---\n");
298 yield* writeSkill(path.join(library, "shared-review", "hidden"), "---\n---\n");
299 const root = path.join(workspace, ".cursor", "skills");
300 yield* fileSystem.makeDirectory(root, { recursive: true });
301 yield* fileSystem.symlink(path.join(library, "shared-review"), path.join(root, "review"));
302
303 const skills = yield* discoverCursorSkills(workspace, { HOME: userHome });
304 expect(skills).toEqual([
305 {
306 name: "review",
307 description: "shared",
308 path: path.join(root, "review", "SKILL.md"),
309 scope: "project",
310 enabled: true,
311 },
312 ]);
313 expect(
314 (yield* probeCursorSkills(workspace, { HOME: userHome }).pipe(Effect.result))._tag,
315 ).toBe("Success");
316 }),
317 ),
318 );
319
320 it("rewrites only discovered skill mentions into Cursor slash invocations", () => {
321 expect(hasCursorSkillMention("use $Review_Pr:V2 here")).toBe(true);
322 expect(hasCursorSkillMention("please $review this")).toBe(true);
323 expect(
324 rewriteCursorSkillMentions("use $review, keep $HOME and 5$review", new Set(["review"])),
325 ).toBe("use $review, keep $HOME and 5$review");
326 expect(rewriteCursorSkillMentions("please $review this", new Set(["review"]))).toBe(
327 "please /review this",
328 );
329 });
330 it("detects and invokes digit-leading Cursor skills without rewriting money", () => {
331 const names = new Set(["2spec", "20k", "100M", "1e6"]);
332 expect(hasCursorSkillMention("use $2spec here")).toBe(true);
333 expect(hasCursorSkillMention("use $2spec here")).toBe(true);
334 expect(rewriteCursorSkillMentions("use $2spec here", names)).toBe("use /2spec here");
335 expect(rewriteCursorSkillMentions("use $2spec here", new Set())).toBe("use $2spec here");
336 for (const text of [
337 "pay $20 tomorrow",
338 "budget $20k here",
339 "cost $100M total",
340 "limit $1e6 here",
341 ]) {
342 expect(hasCursorSkillMention(text)).toBe(false);
343 expect(rewriteCursorSkillMentions(text, names)).toBe(text);
344 }
345 });
346});