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.skipIf(!symlinksSupported)(
166 "treats a symlinked skill outside the root as a package boundary",
167 async () =>
168 await runNode(
169 Effect.gen(function* () {
170 const fileSystem = yield* FileSystem.FileSystem;
171 const path = yield* Path.Path;
172 const userHome = yield* fileSystem.makeTempDirectoryScoped({
173 directory: NodeOS.tmpdir(),
174 prefix: "cursor-skills-home-",
175 });
176 const workspace = yield* fileSystem
177 .makeTempDirectoryScoped({
178 directory: NodeOS.tmpdir(),
179 prefix: "cursor-skills-workspace-",
180 })
181 .pipe(Effect.flatMap((directory) => fileSystem.realPath(directory)));
182 const library = yield* fileSystem.makeTempDirectoryScoped({
183 directory: NodeOS.tmpdir(),
184 prefix: "cursor-skills-library-",
185 });
186 const writeSkill = Effect.fn("writeCursorSkill")(function* (
187 directory: string,
188 contents: string,
189 ) {
190 yield* fileSystem.makeDirectory(directory, { recursive: true });
191 yield* fileSystem.writeFileString(path.join(directory, "SKILL.md"), contents);
192 });
193
194 yield* writeSkill(path.join(library, "shared-review"), "---\ndescription: shared\n---\n");
195 yield* writeSkill(path.join(library, "shared-review", "hidden"), "---\n---\n");
196 const root = path.join(workspace, ".cursor", "skills");
197 yield* fileSystem.makeDirectory(root, { recursive: true });
198 yield* fileSystem.symlink(path.join(library, "shared-review"), path.join(root, "review"));
199
200 const skills = yield* discoverCursorSkills(workspace, { HOME: userHome });
201 expect(skills).toEqual([
202 {
203 name: "review",
204 description: "shared",
205 path: path.join(root, "review", "SKILL.md"),
206 scope: "project",
207 enabled: true,
208 },
209 ]);
210 expect(
211 (yield* probeCursorSkills(workspace, { HOME: userHome }).pipe(Effect.result))._tag,
212 ).toBe("Success");
213 }),
214 ),
215 );
216
217 it("rewrites only discovered skill mentions into Cursor slash invocations", () => {
218 expect(hasCursorSkillMention("use $Review_Pr:V2 here")).toBe(true);
219 expect(hasCursorSkillMention("please $review this")).toBe(true);
220 expect(
221 rewriteCursorSkillMentions("use $review, keep $HOME and 5$review", new Set(["review"])),
222 ).toBe("use $review, keep $HOME and 5$review");
223 expect(rewriteCursorSkillMentions("please $review this", new Set(["review"]))).toBe(
224 "please /review this",
225 );
226 });
227 it("detects and invokes digit-leading Cursor skills without rewriting money", () => {
228 const names = new Set(["2spec", "20k", "100M", "1e6"]);
229 expect(hasCursorSkillMention("use $2spec here")).toBe(true);
230 expect(hasCursorSkillMention("use $2spec here")).toBe(true);
231 expect(rewriteCursorSkillMentions("use $2spec here", names)).toBe("use /2spec here");
232 expect(rewriteCursorSkillMentions("use $2spec here", new Set())).toBe("use $2spec here");
233 for (const text of [
234 "pay $20 tomorrow",
235 "budget $20k here",
236 "cost $100M total",
237 "limit $1e6 here",
238 ]) {
239 expect(hasCursorSkillMention(text)).toBe(false);
240 expect(rewriteCursorSkillMentions(text, names)).toBe(text);
241 }
242 });
243});