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

providerSkills.test.ts

364 lines11.3 KB

1import {
2 PROVIDER_WORKSPACE_SNAPSHOT_TTL_MS,
3 ProviderDriverKind,
4 ProviderInstanceId,
5 type ServerProvider,
6} from "@t3tools/contracts";
7import { formatProviderSkillDisplayName } from "@t3tools/shared/inlineSkills";
8import { describe, expect, it } from "vite-plus/test";
9​
10import {
11 dedupeProviderSkillsByName,
12 getProviderSlashCommandsForSlashMenu,
13 getProviderSkillsForSlashMenu,
14 hasCompleteProviderWorkspaceSnapshot,
15 hasCurrentProviderWorkspaceSnapshot,
16 resolveProviderForCwd,
17 resolveProviderSkillsForCwd,
18 resolveProviderSlashCommandsForCwd,
19 resolveProviderSkillSourceKind,
20} from "./providerSkills.ts";
21​
22const provider = {
23 instanceId: ProviderInstanceId.make("codex"),
24 driver: ProviderDriverKind.make("codex"),
25 enabled: true,
26 installed: true,
27 version: "1.0.0",
28 status: "ready",
29 auth: { status: "authenticated" },
30 checkedAt: "2026-01-01T00:00:00.000Z",
31 models: [],
32 slashCommands: [{ name: "global" }],
33 skills: [{ name: "global", path: "/global/SKILL.md", enabled: true }],
34 workspaceSnapshots: [
35 {
36 cwd: "/workspace/project-a",
37 checkedAt: "2026-01-01T00:01:00.000Z",
38 slashCommands: [{ name: "project" }],
39 skills: [{ name: "project", path: "/workspace/project-a/SKILL.md", enabled: true }],
40 },
41 ],
42} satisfies ServerProvider;
43​
44describe("formatProviderSkillDisplayName", () => {
45 it("prefers the provider display name", () => {
46 expect(
47 formatProviderSkillDisplayName({
48 name: "review-follow-up",
49 displayName: "Review Follow-up",
50 }),
51 ).toBe("Review Follow-up");
52 });
53​
54 it("falls back to a title-cased skill name", () => {
55 expect(
56 formatProviderSkillDisplayName({
57 name: "review-follow-up",
58 }),
59 ).toBe("Review Follow Up");
60 });
61});
62​
63describe("dedupeProviderSkillsByName", () => {
64 it("keeps the first resolved skill and preserves unrelated skill order", () => {
65 const firstSkill = {
66 name: "branch-audit",
67 path: "/Users/matt/.codex/skills/branch-audit/SKILL.md",
68 enabled: true,
69 };
70 const otherSkill = {
71 name: "browser",
72 path: "/Users/matt/.agents/skills/browser/SKILL.md",
73 enabled: true,
74 };
75 const duplicateSkill = {
76 name: "Branch-Audit",
77 path: "/Users/matt/.agents/skills/branch-audit/SKILL.md",
78 enabled: true,
79 };
80​
81 expect(dedupeProviderSkillsByName([firstSkill, otherSkill, duplicateSkill])).toEqual([
82 firstSkill,
83 otherSkill,
84 ]);
85 });
86});
87​
88describe("getProviderSkillsForSlashMenu", () => {
89 it("keeps the skill alias when the provider also exposes it as a slash command", () => {
90 const askMatt = {
91 name: "ask-matt",
92 path: "/Users/matt/.agents/skills/ask-matt/SKILL.md",
93 enabled: true,
94 };
95 expect(getProviderSkillsForSlashMenu([askMatt], true).map((skill) => skill.name)).toEqual([
96 "ask-matt",
97 ]);
98 });
99​
100 it("shows one row when enabled skills share a name", () => {
101 const skills = [
102 {
103 name: "babysit-pr",
104 path: "/Users/matt/.codex/skills/babysit-pr/SKILL.md",
105 enabled: true,
106 },
107 {
108 name: "browser",
109 path: "/Users/matt/.agents/skills/browser/SKILL.md",
110 enabled: true,
111 },
112 {
113 name: "babysit-pr",
114 path: "/Users/matt/.agents/skills/babysit-pr/SKILL.md",
115 enabled: true,
116 },
117 ];
118​
119 expect(getProviderSkillsForSlashMenu(skills, true).map((skill) => skill.name)).toEqual([
120 "babysit-pr",
121 "browser",
122 ]);
123 });
124​
125 it("keeps an enabled skill when a disabled duplicate appears first", () => {
126 const enabledSkill = {
127 name: "babysit-pr",
128 path: "/Users/matt/.agents/skills/babysit-pr/SKILL.md",
129 enabled: true,
130 };
131 const skills = [
132 {
133 name: "babysit-pr",
134 path: "/Users/matt/.codex/skills/babysit-pr/SKILL.md",
135 enabled: false,
136 },
137 enabledSkill,
138 ];
139​
140 expect(getProviderSkillsForSlashMenu(skills, true)).toEqual([enabledSkill]);
141 });
142});
143​
144describe("getProviderSkillsForSlashMenu", () => {
145 it("drops a skill the provider reserves for the agent", () => {
146 const skills = [
147 {
148 name: "legacy-system-context",
149 path: "/Users/matt/.claude/skills/legacy-system-context/SKILL.md",
150 enabled: true,
151 userInvocable: false,
152 },
153 {
154 name: "deploy",
155 path: "/Users/matt/.claude/skills/deploy/SKILL.md",
156 enabled: true,
157 userInvocationOnly: true,
158 },
159 ];
160​
161 expect(getProviderSkillsForSlashMenu(skills, true).map((skill) => skill.name)).toEqual([
162 "deploy",
163 ]);
164 });
165});
166​
167describe("getProviderSlashCommandsForSlashMenu", () => {
168 const commands = [
169 { name: "ask-matt", description: "Ask which skill fits your situation." },
170 { name: "compact", description: "Compact the conversation." },
171 ];
172 const skills = [
173 {
174 name: "ask-matt",
175 path: "/Users/matt/.agents/skills/ask-matt/SKILL.md",
176 enabled: true,
177 },
178 ];
179​
180 it("lets the skill alias win when a provider command has the same name", () => {
181 expect(
182 getProviderSlashCommandsForSlashMenu(commands, skills).map((command) => command.name),
183 ).toEqual(["compact"]);
184 });
185​
186 it("keeps the provider command when the matching skill alias is hidden", () => {
187 const visibleSkills = getProviderSkillsForSlashMenu(skills, false);
188​
189 expect(
190 getProviderSlashCommandsForSlashMenu(commands, visibleSkills).map((command) => command.name),
191 ).toEqual(["ask-matt", "compact"]);
192 });
193});
194​
195describe("resolveProviderSkillSourceKind", () => {
196 it("marks plugin-backed skills as app installs", () => {
197 expect(
198 resolveProviderSkillSourceKind({
199 path: "/Users/julius/.codex/plugins/cache/openai-curated/github/skills/gh-fix-ci/SKILL.md",
200 scope: "user",
201 }),
202 ).toBe("app");
203 });
204​
205 it("maps standard scopes to source kinds", () => {
206 expect(
207 resolveProviderSkillSourceKind({
208 path: "/workspace/.codex/skills/review-follow-up/SKILL.md",
209 scope: "repo",
210 }),
211 ).toBe("repo");
212 expect(
213 resolveProviderSkillSourceKind({
214 path: "/workspace/.codex/skills/review-follow-up/SKILL.md",
215 scope: "project",
216 }),
217 ).toBe("project");
218 expect(
219 resolveProviderSkillSourceKind({
220 path: "/Users/julius/.agents/skills/agent-browser/SKILL.md",
221 scope: "user",
222 }),
223 ).toBe("personal");
224 expect(
225 resolveProviderSkillSourceKind({
226 path: "/usr/local/share/codex/skills/imagegen/SKILL.md",
227 scope: "system",
228 }),
229 ).toBe("system");
230 });
231​
232 it("keeps unknown and missing scopes usable", () => {
233 expect(
234 resolveProviderSkillSourceKind({
235 path: "/opt/skills/team-review/SKILL.md",
236 scope: "team_shared",
237 }),
238 ).toBe("other");
239 expect(
240 resolveProviderSkillSourceKind({
241 path: "/opt/skills/team-review/SKILL.md",
242 }),
243 ).toBe("other");
244 });
245});
246​
247describe("workspace provider snapshots", () => {
248 it("uses the cwd snapshot after a provider session has populated it", () => {
249 expect(resolveProviderSkillsForCwd(provider, "/workspace/project-a")).toEqual([
250 { name: "project", path: "/workspace/project-a/SKILL.md", enabled: true },
251 ]);
252 expect(resolveProviderSlashCommandsForCwd(provider, "/workspace/project-a")).toEqual([
253 { name: "project" },
254 ]);
255 });
256​
257 it("keeps the machine snapshot before this cwd has a provider snapshot", () => {
258 expect(resolveProviderSkillsForCwd(provider, "/workspace/project-b")).toEqual(provider.skills);
259 expect(resolveProviderSlashCommandsForCwd(provider, null)).toEqual(provider.slashCommands);
260 });
261​
262 it("uses partial workspace skills and commands while keeping discovery retryable", () => {
263 const partial = {
264 ...provider,
265 workspaceSnapshots: provider.workspaceSnapshots.map((snapshot) => ({
266 ...snapshot,
267 slashCommands: [{ name: "compact" }],
268 slashCommandsPending: true,
269 })),
270 } satisfies ServerProvider;
271 expect(resolveProviderSkillsForCwd(partial, "/workspace/project-a")).toEqual(
272 provider.workspaceSnapshots[0]?.skills,
273 );
274 expect(resolveProviderSlashCommandsForCwd(partial, "/workspace/project-a")).toEqual([
275 { name: "compact" },
276 ]);
277 expect(hasCompleteProviderWorkspaceSnapshot(partial, "/workspace/project-a")).toBe(false);
278 expect(hasCompleteProviderWorkspaceSnapshot(provider, "/workspace/project-a")).toBe(true);
279 expect(hasCompleteProviderWorkspaceSnapshot(provider, "/workspace/project-b")).toBe(false);
280 expect(hasCompleteProviderWorkspaceSnapshot(undefined, "/workspace/project-a")).toBe(false);
281 expect(hasCompleteProviderWorkspaceSnapshot(provider, null)).toBe(false);
282 });
283​
284 it("asks for a rescan once the workspace snapshot outlives its TTL", () => {
285 const scannedAt = Date.parse("2026-01-01T00:01:00.000Z");
286 const cwd = "/workspace/project-a";
287 expect(hasCurrentProviderWorkspaceSnapshot(provider, cwd, scannedAt)).toBe(true);
288 expect(
289 hasCurrentProviderWorkspaceSnapshot(
290 provider,
291 cwd,
292 scannedAt + PROVIDER_WORKSPACE_SNAPSHOT_TTL_MS - 1,
293 ),
294 ).toBe(true);
295 expect(
296 hasCurrentProviderWorkspaceSnapshot(
297 provider,
298 cwd,
299 scannedAt + PROVIDER_WORKSPACE_SNAPSHOT_TTL_MS,
300 ),
301 ).toBe(false);
302 expect(hasCurrentProviderWorkspaceSnapshot(provider, "/workspace/project-b", scannedAt)).toBe(
303 false,
304 );
305 });
306});
307​
308describe("resolveProviderForCwd", () => {
309 it("switches workspace options for every model without changing the machine catalog", () => {
310 const reasoning = {
311 id: "reasoning",
312 label: "Reasoning",
313 type: "select" as const,
314 options: [{ id: "high", label: "High" }],
315 };
316 const globalAgent = {
317 id: "agent",
318 label: "Agent",
319 type: "select" as const,
320 options: [{ id: "build", label: "Build" }],
321 };
322 const workspaceAgent = {
323 ...globalAgent,
324 options: [{ id: "readonly", label: "Readonly" }],
325 };
326 const emptyAgent = { ...globalAgent, options: [] };
327 const catalog: ServerProvider = {
328 ...provider,
329 models: [
330 {
331 slug: "model",
332 name: "Model",
333 isCustom: false,
334 capabilities: { optionDescriptors: [globalAgent, reasoning] },
335 },
336 { slug: "custom", name: "Custom", isCustom: true, capabilities: null },
337 { slug: "legacy", name: "Legacy", isCustom: false, capabilities: {} },
338 ],
339 workspaceSnapshots: [
340 ...provider.workspaceSnapshots,
341 ...[workspaceAgent, emptyAgent].map((agent, index) => ({
342 ...provider.workspaceSnapshots[0]!,
343 cwd: `/workspace/options-${index}`,
344 optionDescriptors: [agent],
345 })),
346 ],
347 };
348 const original = structuredClone(catalog);
349​
350 for (const [index, agent] of [workspaceAgent, emptyAgent, workspaceAgent].entries()) {
351 const scoped = resolveProviderForCwd(catalog, `/workspace/options-${index % 2}`);
352 expect(scoped.models.map((model) => model.capabilities?.optionDescriptors)).toEqual([
353 expect.arrayContaining([reasoning, agent]),
354 [agent],
355 [agent],
356 ]);
357 expect(scoped.models[0]?.capabilities?.optionDescriptors).toHaveLength(2);
358 }
359 expect(catalog).toEqual(original);
360 expect(resolveProviderForCwd(catalog, "/workspace/unknown")).toBe(catalog);
361 expect(resolveProviderForCwd(catalog, "/workspace/project-a")).toBe(catalog);
362 expect(resolveProviderForCwd(catalog, null)).toBe(catalog);
363 });
364});