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

skills.test.ts

254 lines9.7 KB

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 project, user, then built-in skills by 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 const builtinRoot = path.join(userHome, ".cursor", "skills-cursor");
70 yield* writeSkill(builtinRoot, "loop", "---\ndescription: builtin loop\n---\n");
71 yield* writeSkill(builtinRoot, "review", "---\ndescription: builtin review\n---\n");
72 yield* writeSkill(builtinRoot, "ide-only", "---\nmetadata:\n surfaces:\n - ide\n---\n");
73 yield* fileSystem.makeDirectory(path.join(userHome, ".codex"), { recursive: true });
74 yield* fileSystem.writeFileString(
75 path.join(userHome, ".codex", "skills"),
76 "not a directory",
77 );
78​
79 const skills = yield* discoverCursorSkills(workspace, { HOME: userHome });
80 expect(skills).toEqual([
81 {
82 name: "internal",
83 path: path.join(workspace, ".cursor", "skills", "internal", "SKILL.md"),
84 scope: "project",
85 enabled: true,
86 userInvocable: false,
87 },
88 {
89 name: "loop",
90 description: "builtin loop",
91 path: path.join(builtinRoot, "loop", "SKILL.md"),
92 scope: "system",
93 enabled: true,
94 },
95 {
96 name: "oversized",
97 path: path.join(workspace, ".cursor", "skills", "oversized", "SKILL.md"),
98 scope: "project",
99 enabled: true,
100 },
101 {
102 name: "review",
103 displayName: "Review changes",
104 description: "project review",
105 path: path.join(workspace, ".agents", "skills", "nested", "review", "SKILL.md"),
106 scope: "project",
107 enabled: true,
108 },
109 ]);
110 expect(
111 (yield* probeCursorSkills(workspace, { HOME: userHome }).pipe(Effect.result))._tag,
112 ).toBe("Failure");
113 }),
114 ));
115​
116 it("skips a skill tree deeper than the limit instead of failing discovery", async () =>
117 await runNode(
118 Effect.gen(function* () {
119 const fileSystem = yield* FileSystem.FileSystem;
120 const path = yield* Path.Path;
121 const tempDirectory = (prefix: string) =>
122 fileSystem
123 .makeTempDirectoryScoped({ directory: NodeOS.tmpdir(), prefix })
124 .pipe(Effect.flatMap((directory) => fileSystem.realPath(directory)));
125 const userHome = yield* tempDirectory("cursor-skills-home-");
126 const workspace = yield* tempDirectory("cursor-skills-workspace-");
127 const writeSkill = Effect.fn("writeCursorSkill")(function* (directory: string) {
128 yield* fileSystem.makeDirectory(directory, { recursive: true });
129 yield* fileSystem.writeFileString(path.join(directory, "SKILL.md"), "---\n---\n");
130 });
131 const userRoot = path.join(userHome, ".claude", "skills");
132 const atLimit = path.join(
133 userRoot,
134 "deep",
135 "2",
136 "3",
137 "4",
138 "5",
139 "6",
140 "7",
141 "8",
142 "9",
143 "deep-skill",
144 );
145​
146 yield* writeSkill(path.join(workspace, ".agents", "skills", "review"));
147 yield* writeSkill(atLimit);
148 yield* writeSkill(path.join(atLimit, "below"));
149 yield* writeSkill(path.join(userRoot, "later"));
150​
151 const expected = [
152 {
153 name: "deep-skill",
154 path: path.join(atLimit, "SKILL.md"),
155 scope: "user",
156 enabled: true,
157 },
158 {
159 name: "later",
160 path: path.join(userRoot, "later", "SKILL.md"),
161 scope: "user",
162 enabled: true,
163 },
164 {
165 name: "review",
166 path: path.join(workspace, ".agents", "skills", "review", "SKILL.md"),
167 scope: "project",
168 enabled: true,
169 },
170 ];
171 expect(yield* discoverCursorSkills(workspace, { HOME: userHome })).toEqual(expected);
172 expect(yield* probeCursorSkills(workspace, { HOME: userHome })).toEqual(expected);
173 }),
174 ));
175​
176 it.skipIf(!symlinksSupported)(
177 "treats a symlinked skill outside the root as a package boundary",
178 async () =>
179 await runNode(
180 Effect.gen(function* () {
181 const fileSystem = yield* FileSystem.FileSystem;
182 const path = yield* Path.Path;
183 const userHome = yield* fileSystem.makeTempDirectoryScoped({
184 directory: NodeOS.tmpdir(),
185 prefix: "cursor-skills-home-",
186 });
187 const workspace = yield* fileSystem
188 .makeTempDirectoryScoped({
189 directory: NodeOS.tmpdir(),
190 prefix: "cursor-skills-workspace-",
191 })
192 .pipe(Effect.flatMap((directory) => fileSystem.realPath(directory)));
193 const library = yield* fileSystem.makeTempDirectoryScoped({
194 directory: NodeOS.tmpdir(),
195 prefix: "cursor-skills-library-",
196 });
197 const writeSkill = Effect.fn("writeCursorSkill")(function* (
198 directory: string,
199 contents: string,
200 ) {
201 yield* fileSystem.makeDirectory(directory, { recursive: true });
202 yield* fileSystem.writeFileString(path.join(directory, "SKILL.md"), contents);
203 });
204​
205 yield* writeSkill(path.join(library, "shared-review"), "---\ndescription: shared\n---\n");
206 yield* writeSkill(path.join(library, "shared-review", "hidden"), "---\n---\n");
207 const root = path.join(workspace, ".cursor", "skills");
208 yield* fileSystem.makeDirectory(root, { recursive: true });
209 yield* fileSystem.symlink(path.join(library, "shared-review"), path.join(root, "review"));
210​
211 const skills = yield* discoverCursorSkills(workspace, { HOME: userHome });
212 expect(skills).toEqual([
213 {
214 name: "review",
215 description: "shared",
216 path: path.join(root, "review", "SKILL.md"),
217 scope: "project",
218 enabled: true,
219 },
220 ]);
221 expect(
222 (yield* probeCursorSkills(workspace, { HOME: userHome }).pipe(Effect.result))._tag,
223 ).toBe("Success");
224 }),
225 ),
226 );
227​
228 it("rewrites only discovered skill mentions into Cursor slash invocations", () => {
229 expect(hasCursorSkillMention("use $Review_Pr:V2 here")).toBe(true);
230 expect(hasCursorSkillMention("please $review this")).toBe(true);
231 expect(
232 rewriteCursorSkillMentions("use $review, keep $HOME and 5$review", new Set(["review"])),
233 ).toBe("use $review, keep $HOME and 5$review");
234 expect(rewriteCursorSkillMentions("please $review this", new Set(["review"]))).toBe(
235 "please /review this",
236 );
237 });
238 it("detects and invokes digit-leading Cursor skills without rewriting money", () => {
239 const names = new Set(["2spec", "20k", "100M", "1e6"]);
240 expect(hasCursorSkillMention("use $2spec here")).toBe(true);
241 expect(hasCursorSkillMention("use $2spec here")).toBe(true);
242 expect(rewriteCursorSkillMentions("use $2spec here", names)).toBe("use /2spec here");
243 expect(rewriteCursorSkillMentions("use $2spec here", new Set())).toBe("use $2spec here");
244 for (const text of [
245 "pay $20 tomorrow",
246 "budget $20k here",
247 "cost $100M total",
248 "limit $1e6 here",
249 ]) {
250 expect(hasCursorSkillMention(text)).toBe(false);
251 expect(rewriteCursorSkillMentions(text, names)).toBe(text);
252 }
253 });
254});