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

providerSkills.test.ts

464 lines17.7 KB

1import {
2 EnvironmentId,
3 ProviderDriverKind,
4 ProviderInstanceId,
5 type ServerProvider,
6} from "@t3tools/contracts";
7import * as Effect from "effect/Effect";
8import { Atom, AtomRegistry } from "effect/reactivity";
9import { afterEach, describe, expect, it, vi } from "vite-plus/test";
10​
11import {
12 createProviderWorkspaceSnapshotRefreshAtomFamily,
13 getProviderSkillsForSlashMenu,
14 hasCompleteProviderWorkspaceSnapshot,
15 isProviderWorkspaceSnapshotScanning,
16 isSkillReloadCommand,
17 resolveProviderSkillsForCwd,
18 resolveProviderSlashCommandsForCwd,
19 resolveProvidersForCwd,
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("getProviderSkillsForSlashMenu", () => {
45 it("shows one row when enabled skills share a name", () => {
46 const skills = [
47 {
48 name: "babysit-pr",
49 path: "/Users/matt/.codex/skills/babysit-pr/SKILL.md",
50 enabled: true,
51 },
52 {
53 name: "browser",
54 path: "/Users/matt/.agents/skills/browser/SKILL.md",
55 enabled: true,
56 },
57 {
58 name: "babysit-pr",
59 path: "/Users/matt/.agents/skills/babysit-pr/SKILL.md",
60 enabled: true,
61 },
62 ];
63​
64 expect(getProviderSkillsForSlashMenu(skills, true).map((skill) => skill.name)).toEqual([
65 "babysit-pr",
66 "browser",
67 ]);
68 });
69​
70 it("keeps an enabled skill when a disabled duplicate appears first", () => {
71 const enabledSkill = {
72 name: "babysit-pr",
73 path: "/Users/matt/.agents/skills/babysit-pr/SKILL.md",
74 enabled: true,
75 };
76 const skills = [
77 {
78 name: "babysit-pr",
79 path: "/Users/matt/.codex/skills/babysit-pr/SKILL.md",
80 enabled: false,
81 },
82 enabledSkill,
83 ];
84​
85 expect(getProviderSkillsForSlashMenu(skills, true)).toEqual([enabledSkill]);
86 });
87});
88​
89describe("isSkillReloadCommand", () => {
90 it("matches only a bare reload command", () => {
91 expect(isSkillReloadCommand(" /reload-skills\n")).toBe(true);
92 expect(isSkillReloadCommand("/Reload-Plugins")).toBe(true);
93 expect(isSkillReloadCommand("/reload-skills then use it")).toBe(false);
94 expect(isSkillReloadCommand("reload-skills")).toBe(false);
95 });
96});
97​
98describe("workspace provider snapshots", () => {
99 it("uses the cwd snapshot after a provider session has populated it", () => {
100 expect(resolveProviderSkillsForCwd(provider, "/workspace/project-a")).toEqual([
101 { name: "project", path: "/workspace/project-a/SKILL.md", enabled: true },
102 ]);
103 expect(resolveProviderSlashCommandsForCwd(provider, "/workspace/project-a")).toEqual([
104 { name: "project" },
105 ]);
106 });
107​
108 it("keeps the machine snapshot before this cwd has a provider snapshot", () => {
109 expect(resolveProviderSkillsForCwd(provider, "/workspace/project-b")).toEqual(provider.skills);
110 expect(resolveProviderSlashCommandsForCwd(provider, null)).toEqual(provider.slashCommands);
111 });
112​
113 it("uses partial workspace skills and commands while keeping discovery retryable", () => {
114 const partial = {
115 ...provider,
116 workspaceSnapshots: provider.workspaceSnapshots.map((snapshot) => ({
117 ...snapshot,
118 slashCommands: [{ name: "compact" }],
119 slashCommandsPending: true,
120 })),
121 } satisfies ServerProvider;
122 expect(resolveProviderSkillsForCwd(partial, "/workspace/project-a")).toEqual(
123 provider.workspaceSnapshots[0]?.skills,
124 );
125 expect(resolveProviderSlashCommandsForCwd(partial, "/workspace/project-a")).toEqual([
126 { name: "compact" },
127 ]);
128 expect(hasCompleteProviderWorkspaceSnapshot(partial, "/workspace/project-a")).toBe(false);
129 expect(hasCompleteProviderWorkspaceSnapshot(provider, "/workspace/project-a")).toBe(true);
130 expect(hasCompleteProviderWorkspaceSnapshot(provider, "/workspace/project-b")).toBe(false);
131 expect(hasCompleteProviderWorkspaceSnapshot(undefined, "/workspace/project-a")).toBe(false);
132 expect(hasCompleteProviderWorkspaceSnapshot(provider, null)).toBe(false);
133 });
134​
135 it("lists a project's own models only for that project", () => {
136 const projectModel = {
137 slug: "uni/medium-gpt",
138 name: "Medium GPT",
139 isCustom: false,
140 capabilities: null,
141 } satisfies ServerProvider["models"][number];
142 const withProjectModels = {
143 ...provider,
144 workspaceSnapshots: provider.workspaceSnapshots.map((snapshot) => ({
145 ...snapshot,
146 models: [projectModel],
147 })),
148 } satisfies ServerProvider;
149 const providers = [provider, withProjectModels];
150​
151 expect(resolveProvidersForCwd(providers, "/workspace/project-a").map((p) => p.models)).toEqual([
152 [],
153 [projectModel],
154 ]);
155 expect(resolveProvidersForCwd(providers, "/workspace/project-a")[0]).toBe(provider);
156 expect(resolveProvidersForCwd(providers, "/workspace/project-b")).toBe(providers);
157 });
158});
159​
160describe("provider workspace snapshot refresh atom", () => {
161 afterEach(() => {
162 vi.useRealTimers();
163 });
164​
165 const cwd = "/workspace/project-a";
166 const environmentId = EnvironmentId.make("env");
167 const withSnapshot = (
168 checkedAt: string,
169 slashCommandsPending?: boolean,
170 ): ReadonlyArray<ServerProvider> => [
171 {
172 ...provider,
173 workspaceSnapshots: [
174 {
175 ...provider.workspaceSnapshots[0]!,
176 checkedAt,
177 ...(slashCommandsPending ? { slashCommandsPending } : {}),
178 },
179 ],
180 },
181 ];
182 const noSnapshot: ReadonlyArray<ServerProvider> = [{ ...provider, workspaceSnapshots: [] }];
183​
184 type Response = () =>
185 | ReadonlyArray<ServerProvider>
186 | null
187 | Promise<ReadonlyArray<ServerProvider> | null>;
188​
189 function setup(
190 initialProviders: ReadonlyArray<ServerProvider>,
191 initialResponses?: Response[],
192 // The client drops a reply whose provider changed while it was in flight.
193 options?: { readonly droppedReplies?: number },
194 ) {
195 let droppedReplies = options?.droppedReplies ?? 0;
196 const providersAtom = Atom.make<ReadonlyArray<ServerProvider> | null>(initialProviders);
197 const responses: Response[] = [...(initialResponses ?? [])];
198 const run = vi.fn(() =>
199 Effect.promise(async () => (responses.shift() ?? (() => null))()).pipe(
200 Effect.flatMap((providers) => {
201 if (providers === null) return Effect.fail("offline");
202 if (droppedReplies > 0) droppedReplies -= 1;
203 else registry.set(providersAtom, providers);
204 return Effect.succeed({ providers });
205 }),
206 ),
207 );
208 const family = createProviderWorkspaceSnapshotRefreshAtomFamily({
209 providers: () => providersAtom,
210 refreshProviders: { label: "refresh", run },
211 });
212 const registry = AtomRegistry.make();
213 const atom = family({ environmentId, instanceId: provider.instanceId, cwd });
214 const unmount = registry.mount(atom);
215 return { registry, atom, family, providersAtom, responses, run, unmount };
216 }
217​
218 it("scans a missing snapshot once and skips a repeat inside the TTL", async () => {
219 vi.useFakeTimers({ now: Date.parse("2026-01-01T00:00:00.000Z") });
220 const { registry, atom, family, responses, run, unmount } = setup(noSnapshot);
221 // A second composer on the same target shares the scan.
222 registry.mount(family({ environmentId, instanceId: provider.instanceId, cwd }))();
223 expect(run).toHaveBeenCalledTimes(1);
224 expect(run).toHaveBeenCalledWith(expect.anything(), {
225 environmentId,
226 input: { instanceId: provider.instanceId, cwd },
227 });
228 responses.push(() => withSnapshot("2026-01-01T00:00:00.000Z"));
229 await vi.advanceTimersByTimeAsync(0);
230 registry.refresh(atom);
231 expect(run).toHaveBeenCalledTimes(1);
232 unmount();
233 });
234​
235 it.each(["already expired", "expires during discovery"])(
236 "retries an unchanged catalog that %s after the short cooldown",
237 async (expiry) => {
238 const start = Date.parse("2026-01-01T00:05:00.000Z");
239 vi.useFakeTimers({ now: expiry === "expires during discovery" ? start - 1 : start });
240 const stale = withSnapshot("2026-01-01T00:00:00.000Z");
241 const { registry, atom, responses, run, unmount } = setup(
242 expiry === "expires during discovery"
243 ? withSnapshot("2026-01-01T00:00:00.000Z", true)
244 : stale,
245 [
246 () => {
247 if (expiry === "expires during discovery") vi.setSystemTime(start + 1);
248 return stale;
249 },
250 ],
251 );
252 await vi.advanceTimersByTimeAsync(0);
253 await vi.advanceTimersByTimeAsync(9_999);
254 registry.refresh(atom);
255 expect(run).toHaveBeenCalledTimes(1);
256 responses.push(() => withSnapshot("2026-01-01T00:05:10.000Z"));
257 await vi.advanceTimersByTimeAsync(1);
258 registry.refresh(atom);
259 expect(run).toHaveBeenCalledTimes(2);
260 unmount();
261 },
262 );
263​
264 it("asks again at once when the client dropped a complete reply", async () => {
265 vi.useFakeTimers({ now: Date.parse("2026-01-01T00:00:00.000Z") });
266 const scanned = withSnapshot("2026-01-01T00:00:00.000Z");
267 const { registry, atom, run, unmount } = setup(noSnapshot, [() => scanned, () => scanned], {
268 droppedReplies: 1,
269 });
270 await vi.advanceTimersByTimeAsync(0);
271 expect(run).toHaveBeenCalledTimes(2);
272 expect(isProviderWorkspaceSnapshotScanning(registry.get(atom))).toBe(false);
273 registry.refresh(atom);
274 expect(run).toHaveBeenCalledTimes(2);
275 unmount();
276 });
277​
278 it("keeps the TTL guard for a newly scanned catalog when the client clock is ahead", async () => {
279 vi.useFakeTimers({ now: Date.parse("2026-01-01T01:00:00.000Z") });
280 const { registry, atom, run, unmount } = setup(withSnapshot("2026-01-01T00:00:00.000Z"), [
281 () => withSnapshot("2026-01-01T00:01:00.000Z"),
282 ]);
283 await vi.advanceTimersByTimeAsync(10_000);
284 registry.refresh(atom);
285 expect(run).toHaveBeenCalledTimes(1);
286 unmount();
287 });
288​
289 it("bounds retries when discovery repeatedly returns the same expired catalog", async () => {
290 vi.useFakeTimers({ now: Date.parse("2026-01-01T00:05:00.000Z") });
291 const stale = withSnapshot("2026-01-01T00:00:00.000Z");
292 const { registry, atom, run, unmount } = setup(stale, [() => stale, () => stale, () => stale]);
293 await vi.advanceTimersByTimeAsync(0);
294 await vi.advanceTimersByTimeAsync(10_000);
295 registry.refresh(atom);
296 await vi.advanceTimersByTimeAsync(0);
297 await vi.advanceTimersByTimeAsync(10_000);
298 registry.refresh(atom);
299 await vi.advanceTimersByTimeAsync(0);
300 await vi.advanceTimersByTimeAsync(10_000);
301 registry.refresh(atom);
302 expect(run).toHaveBeenCalledTimes(3);
303 unmount();
304 });
305​
306 it("does not rescan a current snapshot, and rescans once it ages past the TTL", () => {
307 vi.useFakeTimers({ now: Date.parse("2026-01-01T00:01:00.000Z") });
308 const { registry, atom, run, unmount } = setup(withSnapshot("2026-01-01T00:00:00.000Z"));
309 expect(run).not.toHaveBeenCalled();
310 vi.setSystemTime(Date.parse("2026-01-01T00:05:00.000Z"));
311 registry.refresh(atom);
312 expect(run).toHaveBeenCalledTimes(1);
313 unmount();
314 });
315​
316 it("cools down a failed scan, then retries on its own while the scan stays pending", async () => {
317 vi.useFakeTimers({ now: Date.parse("2026-01-01T00:00:00.000Z") });
318 const { registry, atom, run, unmount } = setup(withSnapshot("2026-01-01T00:00:00.000Z", true));
319 expect(run).toHaveBeenCalledTimes(1);
320 await vi.advanceTimersByTimeAsync(0);
321 registry.refresh(atom);
322 expect(run).toHaveBeenCalledTimes(1);
323 await vi.advanceTimersByTimeAsync(9_999);
324 expect(run).toHaveBeenCalledTimes(1);
325 await vi.advanceTimersByTimeAsync(1);
326 expect(run).toHaveBeenCalledTimes(2);
327 unmount();
328 await vi.advanceTimersByTimeAsync(20_000);
329 expect(run).toHaveBeenCalledTimes(2);
330 expect(vi.getTimerCount()).toBe(0);
331 });
332​
333 it("rescans a current snapshot whose commands are complete but models are pending", () => {
334 vi.useFakeTimers({ now: Date.parse("2026-01-01T00:00:00.000Z") });
335 const [current] = withSnapshot("2026-01-01T00:00:00.000Z");
336 const { run, unmount } = setup([
337 {
338 ...current!,
339 workspaceSnapshots: current!.workspaceSnapshots!.map((snapshot) => ({
340 ...snapshot,
341 modelsPending: true,
342 })),
343 },
344 ]);
345 expect(run).toHaveBeenCalledTimes(1);
346 unmount();
347 });
348​
349 it("stops retrying after three failed scans until the TTL lapses", async () => {
350 vi.useFakeTimers({ now: Date.parse("2026-01-01T00:00:00.000Z") });
351 const { registry, atom, run, unmount } = setup(withSnapshot("2026-01-01T00:00:00.000Z", true));
352 await vi.advanceTimersByTimeAsync(60_000);
353 expect(run).toHaveBeenCalledTimes(3);
354 registry.refresh(atom);
355 await vi.advanceTimersByTimeAsync(0);
356 expect(run).toHaveBeenCalledTimes(3);
357 expect(vi.getTimerCount()).toBe(0);
358 vi.setSystemTime(Date.parse("2026-01-01T00:10:00.000Z"));
359 registry.refresh(atom);
360 expect(run).toHaveBeenCalledTimes(4);
361 unmount();
362 });
363​
364 it("reports skills loading while a missing snapshot is scanned, until it arrives", async () => {
365 vi.useFakeTimers({ now: Date.parse("2026-01-01T00:00:00.000Z") });
366 const scanned = withSnapshot("2026-01-01T00:00:00.000Z");
367 const { registry, atom, providersAtom, unmount } = setup(noSnapshot, [() => scanned]);
368 expect(isProviderWorkspaceSnapshotScanning(registry.get(atom))).toBe(true);
369 registry.set(providersAtom, scanned);
370 await vi.advanceTimersByTimeAsync(0);
371 expect(isProviderWorkspaceSnapshotScanning(registry.get(atom))).toBe(false);
372 unmount();
373 });
374​
375 it("stops reporting skills loading when the scan fails", async () => {
376 vi.useFakeTimers({ now: Date.parse("2026-01-01T00:00:00.000Z") });
377 const { registry, atom, unmount } = setup(noSnapshot);
378 expect(isProviderWorkspaceSnapshotScanning(registry.get(atom))).toBe(true);
379 await vi.advanceTimersByTimeAsync(0);
380 expect(isProviderWorkspaceSnapshotScanning(registry.get(atom))).toBe(false);
381 unmount();
382 });
383​
384 it("keeps reporting skills loading while the scan returns a pending snapshot", async () => {
385 vi.useFakeTimers({ now: Date.parse("2026-01-01T00:00:00.000Z") });
386 const pending = withSnapshot("2026-01-01T00:00:00.000Z", true);
387 const { registry, atom, providersAtom, unmount } = setup(pending, [() => pending]);
388 await vi.advanceTimersByTimeAsync(0);
389 expect(isProviderWorkspaceSnapshotScanning(registry.get(atom))).toBe(true);
390 registry.set(providersAtom, withSnapshot("2026-01-01T00:00:00.000Z"));
391 expect(isProviderWorkspaceSnapshotScanning(registry.get(atom))).toBe(false);
392 unmount();
393 });
394​
395 it("stops reporting skills loading once a pending snapshot exhausts its retries", async () => {
396 vi.useFakeTimers({ now: Date.parse("2026-01-01T00:00:00.000Z") });
397 const { registry, atom, run, unmount } = setup(withSnapshot("2026-01-01T00:00:00.000Z", true));
398 await vi.advanceTimersByTimeAsync(60_000);
399 expect(run).toHaveBeenCalledTimes(3);
400 expect(isProviderWorkspaceSnapshotScanning(registry.get(atom))).toBe(false);
401 unmount();
402 });
403​
404 it("does not report skills loading for a current snapshot", () => {
405 vi.useFakeTimers({ now: Date.parse("2026-01-01T00:01:00.000Z") });
406 const { registry, atom, unmount } = setup(withSnapshot("2026-01-01T00:00:00.000Z"));
407 expect(isProviderWorkspaceSnapshotScanning(registry.get(atom))).toBe(false);
408 unmount();
409 });
410​
411 it("does not retry on a timer when the failed scan left no pending snapshot", async () => {
412 vi.useFakeTimers({ now: Date.parse("2026-01-01T00:00:00.000Z") });
413 const { registry, atom, run, unmount } = setup(noSnapshot);
414 await vi.advanceTimersByTimeAsync(10_000);
415 expect(run).toHaveBeenCalledTimes(1);
416 registry.refresh(atom);
417 expect(run).toHaveBeenCalledTimes(2);
418 unmount();
419 });
420​
421 it("rescans at once when the server drops the snapshot, lifting TTL and cooldown", async () => {
422 vi.useFakeTimers({ now: Date.parse("2026-01-01T00:00:00.000Z") });
423 const { registry, providersAtom, run, unmount } = setup(noSnapshot);
424 await vi.advanceTimersByTimeAsync(0);
425 expect(run).toHaveBeenCalledTimes(1);
426 registry.set(providersAtom, withSnapshot("2026-01-01T00:00:00.000Z"));
427 expect(run).toHaveBeenCalledTimes(1);
428 registry.set(providersAtom, noSnapshot);
429 expect(run).toHaveBeenCalledTimes(2);
430 unmount();
431 });
432​
433 it("does not scan again when a scan fails after the last subscriber left", async () => {
434 vi.useFakeTimers({ now: Date.parse("2026-01-01T00:00:00.000Z") });
435 let release = () => {};
436 const gate = new Promise<void>((resolve) => {
437 release = resolve;
438 });
439 const { run, unmount } = setup(withSnapshot("2026-01-01T00:00:00.000Z", true), [
440 async () => {
441 await gate;
442 return null;
443 },
444 ]);
445 expect(run).toHaveBeenCalledTimes(1);
446 unmount();
447 await vi.advanceTimersByTimeAsync(0);
448 release();
449 await vi.advanceTimersByTimeAsync(20_000);
450 expect(run).toHaveBeenCalledTimes(1);
451 expect(vi.getTimerCount()).toBe(0);
452 });
453​
454 it("ignores provider updates that leave the snapshot status unchanged", () => {
455 vi.useFakeTimers({ now: Date.parse("2026-01-01T00:01:00.000Z") });
456 const { registry, providersAtom, run, unmount } = setup(
457 withSnapshot("2026-01-01T00:00:00.000Z"),
458 );
459 vi.setSystemTime(Date.parse("2026-01-01T00:06:00.000Z"));
460 registry.set(providersAtom, withSnapshot("2026-01-01T00:00:00.000Z"));
461 expect(run).not.toHaveBeenCalled();
462 unmount();
463 });
464});