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

providerSkills.test.ts

402 lines15.5 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} from "./providerSkills.ts";
19​
20const provider = {
21 instanceId: ProviderInstanceId.make("codex"),
22 driver: ProviderDriverKind.make("codex"),
23 enabled: true,
24 installed: true,
25 version: "1.0.0",
26 status: "ready",
27 auth: { status: "authenticated" },
28 checkedAt: "2026-01-01T00:00:00.000Z",
29 models: [],
30 slashCommands: [{ name: "global" }],
31 skills: [{ name: "global", path: "/global/SKILL.md", enabled: true }],
32 workspaceSnapshots: [
33 {
34 cwd: "/workspace/project-a",
35 checkedAt: "2026-01-01T00:01:00.000Z",
36 slashCommands: [{ name: "project" }],
37 skills: [{ name: "project", path: "/workspace/project-a/SKILL.md", enabled: true }],
38 },
39 ],
40} satisfies ServerProvider;
41​
42describe("getProviderSkillsForSlashMenu", () => {
43 it("keeps the skill alias when the provider also exposes it as a slash command", () => {
44 const askMatt = {
45 name: "ask-matt",
46 path: "/Users/matt/.agents/skills/ask-matt/SKILL.md",
47 enabled: true,
48 };
49 expect(getProviderSkillsForSlashMenu([askMatt], true).map((skill) => skill.name)).toEqual([
50 "ask-matt",
51 ]);
52 });
53​
54 it("shows one row when enabled skills share a name", () => {
55 const skills = [
56 {
57 name: "babysit-pr",
58 path: "/Users/matt/.codex/skills/babysit-pr/SKILL.md",
59 enabled: true,
60 },
61 {
62 name: "browser",
63 path: "/Users/matt/.agents/skills/browser/SKILL.md",
64 enabled: true,
65 },
66 {
67 name: "babysit-pr",
68 path: "/Users/matt/.agents/skills/babysit-pr/SKILL.md",
69 enabled: true,
70 },
71 ];
72​
73 expect(getProviderSkillsForSlashMenu(skills, true).map((skill) => skill.name)).toEqual([
74 "babysit-pr",
75 "browser",
76 ]);
77 });
78​
79 it("keeps an enabled skill when a disabled duplicate appears first", () => {
80 const enabledSkill = {
81 name: "babysit-pr",
82 path: "/Users/matt/.agents/skills/babysit-pr/SKILL.md",
83 enabled: true,
84 };
85 const skills = [
86 {
87 name: "babysit-pr",
88 path: "/Users/matt/.codex/skills/babysit-pr/SKILL.md",
89 enabled: false,
90 },
91 enabledSkill,
92 ];
93​
94 expect(getProviderSkillsForSlashMenu(skills, true)).toEqual([enabledSkill]);
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​
136describe("provider workspace snapshot refresh atom", () => {
137 afterEach(() => {
138 vi.useRealTimers();
139 });
140​
141 const cwd = "/workspace/project-a";
142 const environmentId = EnvironmentId.make("env");
143 const withSnapshot = (
144 checkedAt: string,
145 slashCommandsPending?: boolean,
146 ): ReadonlyArray<ServerProvider> => [
147 {
148 ...provider,
149 workspaceSnapshots: [
150 {
151 ...provider.workspaceSnapshots[0]!,
152 checkedAt,
153 ...(slashCommandsPending ? { slashCommandsPending } : {}),
154 },
155 ],
156 },
157 ];
158 const noSnapshot: ReadonlyArray<ServerProvider> = [{ ...provider, workspaceSnapshots: [] }];
159​
160 type Response = () =>
161 | ReadonlyArray<ServerProvider>
162 | null
163 | Promise<ReadonlyArray<ServerProvider> | null>;
164​
165 function setup(initialProviders: ReadonlyArray<ServerProvider>, initialResponses?: Response[]) {
166 const providersAtom = Atom.make<ReadonlyArray<ServerProvider> | null>(initialProviders);
167 const responses: Response[] = [...(initialResponses ?? [])];
168 const run = vi.fn(async () => {
169 const providers = await (responses.shift() ?? (() => null))();
170 return providers === null
171 ? AsyncResult.failure<{ providers: ReadonlyArray<ServerProvider> }, string>(
172 Cause.fail("offline"),
173 )
174 : AsyncResult.success({ providers });
175 });
176 const family = createProviderWorkspaceSnapshotRefreshAtomFamily({
177 providers: () => providersAtom,
178 refreshProviders: { label: "refresh", run },
179 });
180 const registry = AtomRegistry.make();
181 const atom = family({ environmentId, instanceId: provider.instanceId, cwd });
182 const unmount = registry.mount(atom);
183 return { registry, atom, family, providersAtom, responses, run, unmount };
184 }
185​
186 it("scans a missing snapshot once and skips a repeat inside the TTL", async () => {
187 vi.useFakeTimers({ now: Date.parse("2026-01-01T00:00:00.000Z") });
188 const { registry, atom, family, responses, run, unmount } = setup(noSnapshot);
189 // A second composer on the same target shares the scan.
190 registry.mount(family({ environmentId, instanceId: provider.instanceId, cwd }))();
191 expect(run).toHaveBeenCalledTimes(1);
192 expect(run).toHaveBeenCalledWith(expect.anything(), {
193 environmentId,
194 input: { instanceId: provider.instanceId, cwd },
195 });
196 responses.push(() => withSnapshot("2026-01-01T00:00:00.000Z"));
197 await vi.advanceTimersByTimeAsync(0);
198 registry.refresh(atom);
199 expect(run).toHaveBeenCalledTimes(1);
200 unmount();
201 });
202​
203 it.each(["already expired", "expires during discovery"])(
204 "retries an unchanged catalog that %s after the short cooldown",
205 async (expiry) => {
206 const start = Date.parse("2026-01-01T00:05:00.000Z");
207 vi.useFakeTimers({ now: expiry === "expires during discovery" ? start - 1 : start });
208 const stale = withSnapshot("2026-01-01T00:00:00.000Z");
209 const { registry, atom, responses, run, unmount } = setup(
210 expiry === "expires during discovery"
211 ? withSnapshot("2026-01-01T00:00:00.000Z", true)
212 : stale,
213 [
214 () => {
215 if (expiry === "expires during discovery") vi.setSystemTime(start + 1);
216 return stale;
217 },
218 ],
219 );
220 await vi.advanceTimersByTimeAsync(0);
221 await vi.advanceTimersByTimeAsync(9_999);
222 registry.refresh(atom);
223 expect(run).toHaveBeenCalledTimes(1);
224 responses.push(() => withSnapshot("2026-01-01T00:05:10.000Z"));
225 await vi.advanceTimersByTimeAsync(1);
226 registry.refresh(atom);
227 expect(run).toHaveBeenCalledTimes(2);
228 unmount();
229 },
230 );
231​
232 it("keeps the TTL guard for a newly scanned catalog when the client clock is ahead", async () => {
233 vi.useFakeTimers({ now: Date.parse("2026-01-01T01:00:00.000Z") });
234 const { registry, atom, run, unmount } = setup(withSnapshot("2026-01-01T00:00:00.000Z"), [
235 () => withSnapshot("2026-01-01T00:01:00.000Z"),
236 ]);
237 await vi.advanceTimersByTimeAsync(10_000);
238 registry.refresh(atom);
239 expect(run).toHaveBeenCalledTimes(1);
240 unmount();
241 });
242​
243 it("bounds retries when discovery repeatedly returns the same expired catalog", async () => {
244 vi.useFakeTimers({ now: Date.parse("2026-01-01T00:05:00.000Z") });
245 const stale = withSnapshot("2026-01-01T00:00:00.000Z");
246 const { registry, atom, run, unmount } = setup(stale, [() => stale, () => stale, () => stale]);
247 await vi.advanceTimersByTimeAsync(0);
248 await vi.advanceTimersByTimeAsync(10_000);
249 registry.refresh(atom);
250 await vi.advanceTimersByTimeAsync(0);
251 await vi.advanceTimersByTimeAsync(10_000);
252 registry.refresh(atom);
253 await vi.advanceTimersByTimeAsync(0);
254 await vi.advanceTimersByTimeAsync(10_000);
255 registry.refresh(atom);
256 expect(run).toHaveBeenCalledTimes(3);
257 unmount();
258 });
259​
260 it("does not rescan a current snapshot, and rescans once it ages past the TTL", () => {
261 vi.useFakeTimers({ now: Date.parse("2026-01-01T00:01:00.000Z") });
262 const { registry, atom, run, unmount } = setup(withSnapshot("2026-01-01T00:00:00.000Z"));
263 expect(run).not.toHaveBeenCalled();
264 vi.setSystemTime(Date.parse("2026-01-01T00:05:00.000Z"));
265 registry.refresh(atom);
266 expect(run).toHaveBeenCalledTimes(1);
267 unmount();
268 });
269​
270 it("cools down a failed scan, then retries on its own while the scan stays pending", async () => {
271 vi.useFakeTimers({ now: Date.parse("2026-01-01T00:00:00.000Z") });
272 const { registry, atom, run, unmount } = setup(withSnapshot("2026-01-01T00:00:00.000Z", true));
273 expect(run).toHaveBeenCalledTimes(1);
274 await vi.advanceTimersByTimeAsync(0);
275 registry.refresh(atom);
276 expect(run).toHaveBeenCalledTimes(1);
277 await vi.advanceTimersByTimeAsync(9_999);
278 expect(run).toHaveBeenCalledTimes(1);
279 await vi.advanceTimersByTimeAsync(1);
280 expect(run).toHaveBeenCalledTimes(2);
281 unmount();
282 await vi.advanceTimersByTimeAsync(20_000);
283 expect(run).toHaveBeenCalledTimes(2);
284 expect(vi.getTimerCount()).toBe(0);
285 });
286​
287 it("stops retrying after three failed scans until the TTL lapses", async () => {
288 vi.useFakeTimers({ now: Date.parse("2026-01-01T00:00:00.000Z") });
289 const { registry, atom, run, unmount } = setup(withSnapshot("2026-01-01T00:00:00.000Z", true));
290 await vi.advanceTimersByTimeAsync(60_000);
291 expect(run).toHaveBeenCalledTimes(3);
292 registry.refresh(atom);
293 await vi.advanceTimersByTimeAsync(0);
294 expect(run).toHaveBeenCalledTimes(3);
295 expect(vi.getTimerCount()).toBe(0);
296 vi.setSystemTime(Date.parse("2026-01-01T00:10:00.000Z"));
297 registry.refresh(atom);
298 expect(run).toHaveBeenCalledTimes(4);
299 unmount();
300 });
301​
302 it("reports skills loading while a missing snapshot is scanned, until it arrives", async () => {
303 vi.useFakeTimers({ now: Date.parse("2026-01-01T00:00:00.000Z") });
304 const scanned = withSnapshot("2026-01-01T00:00:00.000Z");
305 const { registry, atom, providersAtom, unmount } = setup(noSnapshot, [() => scanned]);
306 expect(isProviderWorkspaceSnapshotScanning(registry.get(atom))).toBe(true);
307 registry.set(providersAtom, scanned);
308 await vi.advanceTimersByTimeAsync(0);
309 expect(isProviderWorkspaceSnapshotScanning(registry.get(atom))).toBe(false);
310 unmount();
311 });
312​
313 it("stops reporting skills loading when the scan fails", async () => {
314 vi.useFakeTimers({ now: Date.parse("2026-01-01T00:00:00.000Z") });
315 const { registry, atom, unmount } = setup(noSnapshot);
316 expect(isProviderWorkspaceSnapshotScanning(registry.get(atom))).toBe(true);
317 await vi.advanceTimersByTimeAsync(0);
318 expect(isProviderWorkspaceSnapshotScanning(registry.get(atom))).toBe(false);
319 unmount();
320 });
321​
322 it("keeps reporting skills loading while the scan returns a pending snapshot", async () => {
323 vi.useFakeTimers({ now: Date.parse("2026-01-01T00:00:00.000Z") });
324 const pending = withSnapshot("2026-01-01T00:00:00.000Z", true);
325 const { registry, atom, providersAtom, unmount } = setup(pending, [() => pending]);
326 await vi.advanceTimersByTimeAsync(0);
327 expect(isProviderWorkspaceSnapshotScanning(registry.get(atom))).toBe(true);
328 registry.set(providersAtom, withSnapshot("2026-01-01T00:00:00.000Z"));
329 expect(isProviderWorkspaceSnapshotScanning(registry.get(atom))).toBe(false);
330 unmount();
331 });
332​
333 it("stops reporting skills loading once a pending snapshot exhausts its retries", async () => {
334 vi.useFakeTimers({ now: Date.parse("2026-01-01T00:00:00.000Z") });
335 const { registry, atom, run, unmount } = setup(withSnapshot("2026-01-01T00:00:00.000Z", true));
336 await vi.advanceTimersByTimeAsync(60_000);
337 expect(run).toHaveBeenCalledTimes(3);
338 expect(isProviderWorkspaceSnapshotScanning(registry.get(atom))).toBe(false);
339 unmount();
340 });
341​
342 it("does not report skills loading for a current snapshot", () => {
343 vi.useFakeTimers({ now: Date.parse("2026-01-01T00:01:00.000Z") });
344 const { registry, atom, unmount } = setup(withSnapshot("2026-01-01T00:00:00.000Z"));
345 expect(isProviderWorkspaceSnapshotScanning(registry.get(atom))).toBe(false);
346 unmount();
347 });
348​
349 it("does not retry on a timer when the failed scan left no pending snapshot", async () => {
350 vi.useFakeTimers({ now: Date.parse("2026-01-01T00:00:00.000Z") });
351 const { registry, atom, run, unmount } = setup(noSnapshot);
352 await vi.advanceTimersByTimeAsync(10_000);
353 expect(run).toHaveBeenCalledTimes(1);
354 registry.refresh(atom);
355 expect(run).toHaveBeenCalledTimes(2);
356 unmount();
357 });
358​
359 it("rescans at once when the server drops the snapshot, lifting TTL and cooldown", async () => {
360 vi.useFakeTimers({ now: Date.parse("2026-01-01T00:00:00.000Z") });
361 const { registry, providersAtom, run, unmount } = setup(noSnapshot);
362 await vi.advanceTimersByTimeAsync(0);
363 expect(run).toHaveBeenCalledTimes(1);
364 registry.set(providersAtom, withSnapshot("2026-01-01T00:00:00.000Z"));
365 expect(run).toHaveBeenCalledTimes(1);
366 registry.set(providersAtom, noSnapshot);
367 expect(run).toHaveBeenCalledTimes(2);
368 unmount();
369 });
370​
371 it("does not scan again when a scan fails after the last subscriber left", async () => {
372 vi.useFakeTimers({ now: Date.parse("2026-01-01T00:00:00.000Z") });
373 let release = () => {};
374 const gate = new Promise<void>((resolve) => {
375 release = resolve;
376 });
377 const { run, unmount } = setup(withSnapshot("2026-01-01T00:00:00.000Z", true), [
378 async () => {
379 await gate;
380 return null;
381 },
382 ]);
383 expect(run).toHaveBeenCalledTimes(1);
384 unmount();
385 await vi.advanceTimersByTimeAsync(0);
386 release();
387 await vi.advanceTimersByTimeAsync(20_000);
388 expect(run).toHaveBeenCalledTimes(1);
389 expect(vi.getTimerCount()).toBe(0);
390 });
391​
392 it("ignores provider updates that leave the snapshot status unchanged", () => {
393 vi.useFakeTimers({ now: Date.parse("2026-01-01T00:01:00.000Z") });
394 const { registry, providersAtom, run, unmount } = setup(
395 withSnapshot("2026-01-01T00:00:00.000Z"),
396 );
397 vi.setSystemTime(Date.parse("2026-01-01T00:06:00.000Z"));
398 registry.set(providersAtom, withSnapshot("2026-01-01T00:00:00.000Z"));
399 expect(run).not.toHaveBeenCalled();
400 unmount();
401 });
402});