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

CursorSkills.test.ts

186 lines7.3 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 "./CursorSkills.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.skipIf(!symlinksSupported)(
106 "treats a symlinked skill outside the root as a package boundary",
107 async () =>
108 await runNode(
109 Effect.gen(function* () {
110 const fileSystem = yield* FileSystem.FileSystem;
111 const path = yield* Path.Path;
112 const userHome = yield* fileSystem.makeTempDirectoryScoped({
113 directory: NodeOS.tmpdir(),
114 prefix: "cursor-skills-home-",
115 });
116 const workspace = yield* fileSystem
117 .makeTempDirectoryScoped({
118 directory: NodeOS.tmpdir(),
119 prefix: "cursor-skills-workspace-",
120 })
121 .pipe(Effect.flatMap((directory) => fileSystem.realPath(directory)));
122 const library = yield* fileSystem.makeTempDirectoryScoped({
123 directory: NodeOS.tmpdir(),
124 prefix: "cursor-skills-library-",
125 });
126 const writeSkill = Effect.fn("writeCursorSkill")(function* (
127 directory: string,
128 contents: string,
129 ) {
130 yield* fileSystem.makeDirectory(directory, { recursive: true });
131 yield* fileSystem.writeFileString(path.join(directory, "SKILL.md"), contents);
132 });
133​
134 // A skill package managed in a config repo and installed by symlink.
135 // Its own SKILL.md must be discovered under the link name, but nothing
136 // below the target may be walked.
137 yield* writeSkill(path.join(library, "shared-review"), "---\ndescription: shared\n---\n");
138 yield* writeSkill(path.join(library, "shared-review", "hidden"), "---\n---\n");
139 const root = path.join(workspace, ".cursor", "skills");
140 yield* fileSystem.makeDirectory(root, { recursive: true });
141 yield* fileSystem.symlink(path.join(library, "shared-review"), path.join(root, "review"));
142​
143 const skills = yield* discoverCursorSkills(workspace, { HOME: userHome });
144 expect(skills).toEqual([
145 {
146 name: "review",
147 description: "shared",
148 path: path.join(root, "review", "SKILL.md"),
149 scope: "project",
150 enabled: true,
151 },
152 ]);
153 expect(
154 (yield* probeCursorSkills(workspace, { HOME: userHome }).pipe(Effect.result))._tag,
155 ).toBe("Success");
156 }),
157 ),
158 );
159​
160 it("rewrites only discovered skill mentions into Cursor slash invocations", () => {
161 expect(hasCursorSkillMention("use $Review_Pr:V2 here")).toBe(true);
162 expect(hasCursorSkillMention("please $review this")).toBe(true);
163 expect(
164 rewriteCursorSkillMentions("use $review, keep $HOME and 5$review", new Set(["review"])),
165 ).toBe("use $review, keep $HOME and 5$review");
166 expect(rewriteCursorSkillMentions("please $review this", new Set(["review"]))).toBe(
167 "please /review this",
168 );
169 });
170 it("detects and invokes digit-leading Cursor skills without rewriting money", () => {
171 const names = new Set(["2spec", "20k", "100M", "1e6"]);
172 expect(hasCursorSkillMention("use $2spec here")).toBe(true);
173 expect(hasCursorSkillMention("use $2spec here")).toBe(true);
174 expect(rewriteCursorSkillMentions("use $2spec here", names)).toBe("use /2spec here");
175 expect(rewriteCursorSkillMentions("use $2spec here", new Set())).toBe("use $2spec here");
176 for (const text of [
177 "pay $20 tomorrow",
178 "budget $20k here",
179 "cost $100M total",
180 "limit $1e6 here",
181 ]) {
182 expect(hasCursorSkillMention(text)).toBe(false);
183 expect(rewriteCursorSkillMentions(text, names)).toBe(text);
184 }
185 });
186});