@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 199 lines 8.31 KB
Raw
1 import * as NodeServices from "@effect/platform-node/NodeServices";
2 import { assert, describe, it } from "@effect/vitest";
3 import * as Effect from "effect/Effect";
4 import * as FileSystem from "effect/FileSystem";
5 import * as Path from "effect/Path";
6
7 import { discoverFxSkills } from "./skills.ts";
8
9 describe("discoverFxSkills", () => {
10 // fx 0.0.13 scans workspace roots from the workspace up to HOME, then
11 // ~/.fx/skills, then home compatibility roots (src/builtins/skills.zig), and
12 // lists ~/.fx/skills first, then workspace roots, then compatibility roots
13 // in scan order (skillGroupRank in src/core/skills/skill_runtime.zig).
14 it.effect("lists skills in fx's own order", () =>
15 Effect.gen(function* () {
16 const fileSystem = yield* FileSystem.FileSystem;
17 const path = yield* Path.Path;
18 // The workspace sits under the fake HOME, so the upward walk stops there.
19 const home = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-fx-skills-home-" });
20 const parent = path.join(home, "projects");
21 const workspace = path.join(parent, "app");
22 const writeSkill = (root: string, name: string) =>
23 fileSystem
24 .makeDirectory(path.join(root, name), { recursive: true })
25 .pipe(
26 Effect.andThen(
27 fileSystem.writeFileString(
28 path.join(root, name, "SKILL.md"),
29 `---\nname: ${name}\ndescription: ${name} skill\n---\n\nBody.\n`,
30 ),
31 ),
32 );
33 yield* writeSkill(path.join(workspace, ".claude/skills"), "workspace-claude");
34 yield* writeSkill(path.join(workspace, ".fx/skills"), "workspace-fx");
35 yield* writeSkill(path.join(parent, ".agents/skills"), "parent-agents");
36 yield* writeSkill(path.join(parent, "skills"), "parent-shared");
37 yield* writeSkill(path.join(home, ".claude/skills"), "home-claude");
38 yield* writeSkill(path.join(home, ".fx/skills"), "home-fx");
39 // HOME is not a workspace ancestor, and ~/skills is no fx root.
40 yield* writeSkill(path.join(home, "skills"), "home-shared");
41
42 const skills = yield* discoverFxSkills({ cwd: workspace, environment: { HOME: home } });
43
44 assert.deepEqual(
45 skills.map((skill) => skill.name),
46 [
47 "home-fx",
48 "workspace-fx",
49 "parent-shared",
50 "workspace-claude",
51 "parent-agents",
52 "home-claude",
53 ],
54 );
55 }).pipe(Effect.provide(NodeServices.layer)),
56 );
57
58 // For `$review` with two skills of exactly that name, fx's model loads the
59 // first in scan order, so only that one is offered; `Review` is a separate
60 // skill `$Review` loads. fx skips a SKILL.md whose frontmatter has no name
61 // or never closes, and a symlinked folder in its managed ~/.fx/skills.
62 it.effect("offers the skill fx loads for each name", () =>
63 Effect.gen(function* () {
64 const fileSystem = yield* FileSystem.FileSystem;
65 const path = yield* Path.Path;
66 const home = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-fx-skills-home-" });
67 const workspace = path.join(home, "app");
68 const writeSkillFile = (folder: string, contents: string) =>
69 fileSystem
70 .makeDirectory(folder, { recursive: true })
71 .pipe(
72 Effect.andThen(fileSystem.writeFileString(path.join(folder, "SKILL.md"), contents)),
73 );
74 yield* writeSkillFile(
75 path.join(workspace, ".fx/skills/review"),
76 "---\nname: review\n---\nBody.\n",
77 );
78 yield* writeSkillFile(path.join(home, ".claude/skills/review"), "---\nname: Review\n---\n");
79 yield* writeSkillFile(path.join(home, ".fx/skills/plain"), "No frontmatter.\n");
80 yield* writeSkillFile(
81 path.join(home, ".fx/skills/unnamed"),
82 "---\ndescription: Missing name\n---\n",
83 );
84 yield* writeSkillFile(path.join(home, ".fx/skills/unclosed"), "---\nname: unclosed\n");
85 // A continued plain value is fx's `unsupported_multiline`, and fx trims
86 // only spaces and tabs, so a trailing vertical tab is a control byte:
87 // both these `review`s are invalid and the valid ones stay the only two.
88 yield* writeSkillFile(
89 path.join(home, ".codex/skills/review"),
90 "---\nname: review\u000b\n---\n",
91 );
92 yield* writeSkillFile(
93 path.join(home, ".agents/skills/review"),
94 "---\nname: review\ndescription: first line\n second line\n---\n",
95 );
96 // fx sees no frontmatter after a byte-order mark, so this one is
97 // `bom-folder`, not `bom-name`, and its body may run past 1 MB.
98 yield* fileSystem.makeDirectory(path.join(home, ".fx/skills/bom-folder"), {
99 recursive: true,
100 });
101 yield* fileSystem.writeFile(
102 path.join(home, ".fx/skills/bom-folder/SKILL.md"),
103 new Uint8Array([
104 0xef,
105 0xbb,
106 0xbf,
107 ...new TextEncoder().encode(`---\nname: bom-name\n---\n${"x".repeat(1_100_000)}`),
108 ]),
109 );
110 // A literal U+FFFD is valid text to fx; an invalid UTF-8 byte is not.
111 yield* writeSkillFile(
112 path.join(home, ".fx/skills/replacement"),
113 "---\nname: replacement\ndescription: Replace \ufffd in imported text.\n---\n",
114 );
115 yield* fileSystem.makeDirectory(path.join(home, ".fx/skills/bad-bytes"), { recursive: true });
116 yield* fileSystem.writeFile(
117 path.join(home, ".fx/skills/bad-bytes/SKILL.md"),
118 new Uint8Array([
119 ...new TextEncoder().encode("---\nname: bad"),
120 0xff,
121 0x0a,
122 0x2d,
123 0x2d,
124 0x2d,
125 0x0a,
126 ]),
127 );
128 // fx folds only ASCII case, so these two are distinct.
129 yield* writeSkillFile(
130 path.join(home, ".fx/skills/accent-upper"),
131 "---\nname: \u00c9t\u00e9\n---\n",
132 );
133 yield* writeSkillFile(
134 path.join(home, ".fx/skills/accent-lower"),
135 "---\nname: \u00e9t\u00e9\n---\n",
136 );
137 yield* writeSkillFile(path.join(home, "library/linked"), "---\nname: linked\n---\n");
138 yield* fileSystem.symlink(
139 path.join(home, "library/linked"),
140 path.join(home, ".fx/skills/linked"),
141 );
142 yield* fileSystem.makeDirectory(path.join(home, ".fx/skills/linked-file"));
143 yield* fileSystem.symlink(
144 path.join(home, "library/linked/SKILL.md"),
145 path.join(home, ".fx/skills/linked-file/SKILL.md"),
146 );
147
148 const skills = yield* discoverFxSkills({ cwd: workspace, environment: { HOME: home } });
149
150 assert.deepEqual(
151 skills.map((skill) => [skill.name, skill.path]),
152 [
153 ["été", path.join(home, ".fx/skills/accent-lower/SKILL.md")],
154 ["Été", path.join(home, ".fx/skills/accent-upper/SKILL.md")],
155 ["bom-folder", path.join(home, ".fx/skills/bom-folder/SKILL.md")],
156 ["plain", path.join(home, ".fx/skills/plain/SKILL.md")],
157 ["replacement", path.join(home, ".fx/skills/replacement/SKILL.md")],
158 ["review", path.join(workspace, ".fx/skills/review/SKILL.md")],
159 ["Review", path.join(home, ".claude/skills/review/SKILL.md")],
160 ],
161 );
162 }).pipe(Effect.provide(NodeServices.layer)),
163 );
164
165 // Nix home-manager and dotfile setups link ~/.fx/skills; fx then loads
166 // nothing from it, while the compatibility roots still count.
167 it.effect("lists nothing from a linked ~/.fx/skills", () =>
168 Effect.gen(function* () {
169 const fileSystem = yield* FileSystem.FileSystem;
170 const path = yield* Path.Path;
171 const home = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-fx-skills-home-" });
172 for (const [root, name] of [
173 ["dotfiles/fx-skills", "managed"],
174 [".claude/skills", "compat"],
175 ] as const) {
176 yield* fileSystem.makeDirectory(path.join(home, root, name), { recursive: true });
177 yield* fileSystem.writeFileString(
178 path.join(home, root, name, "SKILL.md"),
179 `---\nname: ${name}\n---\n`,
180 );
181 }
182 yield* fileSystem.makeDirectory(path.join(home, ".fx"));
183 yield* fileSystem.symlink(
184 path.join(home, "dotfiles/fx-skills"),
185 path.join(home, ".fx/skills"),
186 );
187
188 const skills = yield* discoverFxSkills({
189 cwd: path.join(home, "app"),
190 environment: { HOME: home },
191 });
192
193 assert.deepEqual(
194 skills.map((skill) => skill.name),
195 ["compat"],
196 );
197 }).pipe(Effect.provide(NodeServices.layer)),
198 );
199 });