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

providerSkills.test.ts

455 lines17.4 KB

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