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

ProviderUpdateLaunchNotification.logic.test.ts

564 lines17.5 KB

1import { describe, expect, it } from "vite-plus/test";
2import {
3 EnvironmentId,
4 ProviderDriverKind,
5 ProviderInstanceId,
6 type ServerProvider,
7} from "@t3tools/contracts";
8import * as Cause from "effect/Cause";
9import { AsyncResult } from "effect/reactivity";
10​
11import {
12 buildLocalEnvironmentUpdateGroups,
13 canOneClickUpdateProviderCandidate,
14 collectProviderUpdateOutcomeSnapshots,
15 collectUpdatedProviderSnapshots,
16 firstRejectedProviderUpdateMessage,
17 getProviderUpdateProgressToastView,
18 getProviderUpdateRunToastView,
19 getProviderUpdateSidebarPillView,
20 hasOneClickUpdateProviderCandidate,
21 isProviderUpdateCandidate,
22 isProviderSettingsUpdateCandidate,
23 providerUpdateNotificationKey,
24 resolveEnvironmentUpdateRowStatus,
25 type LocalEnvironmentProvidersInput,
26 type LocalEnvironmentUpdateGroup,
27 type LocalProviderUpdateOutcome,
28 type ProviderUpdateCandidate,
29 type ProviderUpdateSidebarPillView,
30 type ProviderUpdateToastView,
31} from "./ProviderUpdateLaunchNotification.logic";
32​
33const checkedAt = "2026-04-23T10:00:00.000Z";
34const sessionStartedAt = "2026-04-23T09:59:00.000Z";
35const laterCheckedAt = "2026-04-23T10:01:00.000Z";
36​
37const driver = (value: string) => ProviderDriverKind.make(value);
38const instanceId = (value: string) => ProviderInstanceId.make(value);
39​
40function provider(input: {
41 readonly driver: ReturnType<typeof ProviderDriverKind.make>;
42 readonly instanceId?: ReturnType<typeof ProviderInstanceId.make>;
43 readonly enabled?: boolean;
44 readonly version?: string | null;
45 readonly latestVersion?: string | null;
46 readonly canUpdate?: boolean;
47 readonly updateCommand?: string | null;
48 readonly updateState?: ServerProvider["updateState"];
49 readonly advisoryStatus?: NonNullable<ServerProvider["versionAdvisory"]>["status"];
50}): ServerProvider {
51 const result: ServerProvider = {
52 instanceId: input.instanceId ?? instanceId(String(input.driver)),
53 driver: input.driver,
54 enabled: input.enabled ?? true,
55 installed: true,
56 version: input.version ?? "1.0.0",
57 status: "ready",
58 auth: { status: "authenticated" },
59 checkedAt,
60 models: [],
61 slashCommands: [],
62 skills: [],
63 versionAdvisory: {
64 status: input.advisoryStatus ?? "behind_latest",
65 currentVersion: input.version ?? "1.0.0",
66 latestVersion: "latestVersion" in input ? input.latestVersion : "1.1.0",
67 updateCommand: "updateCommand" in input ? input.updateCommand : "npm install -g provider",
68 canUpdate: input.canUpdate ?? true,
69 checkedAt,
70 message: "Update available.",
71 },
72 };
73​
74 if (input.updateState) {
75 return { ...result, updateState: input.updateState };
76 }
77​
78 return result;
80​
81function updateCandidate(input: Parameters<typeof provider>[0]): ProviderUpdateCandidate {
82 return provider(input) as ProviderUpdateCandidate;
84​
85describe("provider update launch notification logic", () => {
86 it("disables one-click updates when provider instances disagree on the update command", () => {
87 const candidate = updateCandidate({
88 driver: driver("claudeAgent"),
89 instanceId: instanceId("claude_personal"),
90 latestVersion: "2.1.123",
91 });
92​
93 expect(
94 canOneClickUpdateProviderCandidate(candidate, [
95 candidate,
96 provider({
97 driver: driver("claudeAgent"),
98 instanceId: instanceId("claude_work"),
99 latestVersion: "2.1.123",
100 canUpdate: true,
101 updateCommand: "bun add -g @anthropic-ai/claude-code@latest",
102 }),
103 ]),
104 ).toBe(false);
105 });
106​
107 it("keeps one-click updates enabled when sibling instances are already current", () => {
108 const candidate = updateCandidate({
109 driver: driver("claudeAgent"),
110 instanceId: instanceId("claude_personal"),
111 latestVersion: "2.1.123",
112 updateCommand: "npm install -g @anthropic-ai/claude-code@latest",
113 });
114​
115 expect(
116 hasOneClickUpdateProviderCandidate(candidate, [
117 candidate,
118 provider({
119 driver: driver("claudeAgent"),
120 instanceId: instanceId("claude_work"),
121 version: "2.1.123",
122 latestVersion: "2.1.123",
123 advisoryStatus: "current",
124 canUpdate: false,
125 updateCommand: null,
126 }),
127 ]),
128 ).toBe(true);
129 expect(
130 canOneClickUpdateProviderCandidate(candidate, [
131 candidate,
132 provider({
133 driver: driver("claudeAgent"),
134 instanceId: instanceId("claude_work"),
135 version: "2.1.123",
136 latestVersion: "2.1.123",
137 advisoryStatus: "current",
138 canUpdate: false,
139 updateCommand: null,
140 }),
141 ]),
142 ).toBe(true);
143 });
144​
145 it("keeps the inline update action available while a provider update is already running", () => {
146 const candidate = updateCandidate({
147 driver: driver("codex"),
148 updateState: {
149 status: "running",
150 startedAt: checkedAt,
151 finishedAt: null,
152 message: "Updating provider.",
153 output: null,
154 },
155 });
156​
157 expect(hasOneClickUpdateProviderCandidate(candidate, [candidate])).toBe(true);
158 expect(canOneClickUpdateProviderCandidate(candidate, [candidate])).toBe(false);
159 });
160​
161 it("keeps the same notification key while the published update version is unchanged", () => {
162 const first = updateCandidate({
163 driver: driver("codex"),
164 version: "1.0.0",
165 latestVersion: "1.2.0",
166 });
167 const second = updateCandidate({
168 driver: driver("codex"),
169 version: "1.1.0",
170 latestVersion: "1.2.0",
171 });
172 const nextPublishedVersion = updateCandidate({
173 driver: driver("codex"),
174 version: "1.1.0",
175 latestVersion: "1.3.0",
176 });
177​
178 expect(providerUpdateNotificationKey([first])).toBe(providerUpdateNotificationKey([second]));
179 expect(providerUpdateNotificationKey([nextPublishedVersion])).not.toBe(
180 providerUpdateNotificationKey([first]),
181 );
182 });
183​
184 it("tracks updated provider snapshots by instance instead of collapsing to a sibling driver", () => {
185 const targetInstanceId = instanceId("codex_personal");
186 const siblingInstanceId = instanceId("codex");
187 const updatedPersonal = provider({
188 driver: driver("codex"),
189 instanceId: targetInstanceId,
190 version: "1.1.0",
191 latestVersion: "1.1.0",
192 advisoryStatus: "current",
193 updateState: {
194 status: "succeeded",
195 startedAt: checkedAt,
196 finishedAt: checkedAt,
197 message: "Provider updated.",
198 output: null,
199 },
200 });
201 const currentDefaultSibling = provider({
202 driver: driver("codex"),
203 instanceId: siblingInstanceId,
204 version: "1.1.0",
205 latestVersion: "1.1.0",
206 advisoryStatus: "current",
207 updateState: undefined,
208 });
209​
210 expect(
211 collectUpdatedProviderSnapshots({
212 results: [
213 AsyncResult.success({
214 providers: [updatedPersonal, currentDefaultSibling],
215 }),
216 ],
217 providerInstanceIds: new Set([targetInstanceId]),
218 }),
219 ).toEqual([updatedPersonal]);
220 });
221​
222 it("shows the active sidebar pill for an update on a non-default instance", () => {
223 const view = getProviderUpdateSidebarPillView([
224 provider({ driver: driver("claudeAgent") }),
225 provider({
226 driver: driver("claudeAgent"),
227 instanceId: instanceId("claude_work"),
228 updateState: {
229 status: "running",
230 startedAt: checkedAt,
231 finishedAt: null,
232 message: "Updating provider.",
233 output: null,
234 },
235 }),
236 ]);
237​
238 expect(view).toMatchObject({
239 key: "loading:claudeAgent:running",
240 tone: "loading",
241 title: "Updating Claude",
242 description: "Claude update in progress.",
243 });
244 });
245​
246 it("shows the latest terminal sidebar pill across instances of a driver", () => {
247 const view = getProviderUpdateSidebarPillView(
248 [
249 provider({
250 driver: driver("claudeAgent"),
251 updateState: {
252 status: "failed",
253 startedAt: checkedAt,
254 finishedAt: checkedAt,
255 message: "Update command exited with code 1.",
256 output: null,
257 },
258 }),
259 provider({
260 driver: driver("claudeAgent"),
261 instanceId: instanceId("claude_work"),
262 version: "1.1.0",
263 latestVersion: "1.1.0",
264 advisoryStatus: "current",
265 updateState: {
266 status: "succeeded",
267 startedAt: laterCheckedAt,
268 finishedAt: laterCheckedAt,
269 message: "Provider updated.",
270 output: null,
271 },
272 }),
273 ],
274 { visibleAfterIso: sessionStartedAt },
275 );
276​
277 expect(view).toMatchObject({
278 key: "succeeded:claudeAgent:2026-04-23T10:01:00.000Z:Provider updated.",
279 tone: "success",
280 title: "Claude updated: v1.1.0",
281 });
282 });
283​
284 it("does not show sidebar terminal states from before the current app session", () => {
285 expect(
286 getProviderUpdateSidebarPillView(
287 [
288 provider({
289 driver: driver("codex"),
290 updateState: {
291 status: "failed",
292 startedAt: checkedAt,
293 finishedAt: checkedAt,
294 message: "command failed",
295 output: "stderr",
296 },
297 }),
298 ],
299 { visibleAfterIso: "2026-04-23T10:00:01.000Z" },
300 ),
301 ).toBeNull();
302 });
303​
304 it("shows a newer success before falling back to an older failure", () => {
305 const providers = [
306 provider({
307 driver: driver("claudeAgent"),
308 updateState: {
309 status: "failed",
310 startedAt: checkedAt,
311 finishedAt: checkedAt,
312 message: "Update command exited with code 1.",
313 output: null,
314 },
315 }),
316 provider({
317 driver: driver("codex"),
318 version: "1.2.0",
319 latestVersion: "1.2.0",
320 advisoryStatus: "current",
321 updateState: {
322 status: "succeeded",
323 startedAt: laterCheckedAt,
324 finishedAt: laterCheckedAt,
325 message: "Provider updated.",
326 output: null,
327 },
328 }),
329 ] satisfies ReadonlyArray<ServerProvider>;
330​
331 const successView = getProviderUpdateSidebarPillView(providers, {
332 visibleAfterIso: sessionStartedAt,
333 });
334 expect(successView).toMatchObject({
335 key: "succeeded:codex:2026-04-23T10:01:00.000Z:Provider updated.",
336 tone: "success",
337 title: "Codex updated: v1.2.0",
338 });
339​
340 const failureView = getProviderUpdateSidebarPillView(providers, {
341 visibleAfterIso: sessionStartedAt,
342 dismissedKeys: new Set(["succeeded:codex:2026-04-23T10:01:00.000Z:Provider updated."]),
343 });
344 expect(failureView).toMatchObject({
345 key: "failed:claudeAgent:2026-04-23T10:00:00.000Z:Update command exited with code 1.",
346 tone: "error",
347 title: "Claude v1.1.0 update failed",
348 });
349 });
350​
351 describe("multi-backend update outcomes", () => {
352 const terminalState = (
353 status: "succeeded" | "failed" | "unchanged",
354 message: string,
355 ): NonNullable<ServerProvider["updateState"]> => ({
356 status,
357 startedAt: checkedAt,
358 finishedAt: checkedAt,
359 message,
360 output: null,
361 });
362​
363 const fulfilledOutcome = (
364 isPrimary: boolean,
365 snapshot: ServerProvider | null,
366 environment = "env",
367 ): PromiseSettledResult<LocalProviderUpdateOutcome> => ({
368 status: "fulfilled",
369 value: {
370 environmentId: environment as LocalProviderUpdateOutcome["environmentId"],
371 isPrimary,
372 driver: snapshot?.driver ?? driver("codex"),
373 instanceId: snapshot?.instanceId ?? instanceId("codex"),
374 provider: snapshot,
375 },
376 });
377​
378 it("surfaces a secondary backend's failed update over the primary's success", () => {
379 const snapshots = collectProviderUpdateOutcomeSnapshots([
380 fulfilledOutcome(
381 true,
382 provider({
383 driver: driver("codex"),
384 updateState: terminalState("succeeded", "Provider updated."),
385 }),
386 ),
387 fulfilledOutcome(
388 false,
389 provider({
390 driver: driver("codex"),
391 updateState: terminalState("failed", "npm: NotFound"),
392 }),
393 ),
394 ]);
395​
396 expect(snapshots).toHaveLength(1);
397 expect(snapshots[0]?.updateState?.status).toBe("failed");
398 expect(
399 getProviderUpdateProgressToastView({ providers: snapshots, providerCount: 1 }),
400 ).toMatchObject({ phase: "failed" });
401 });
402​
403 it("treats a rejected dispatch as not contributing a snapshot", () => {
404 const primary = provider({
405 driver: driver("codex"),
406 updateState: terminalState("succeeded", "Provider updated."),
407 });
408 const results: PromiseSettledResult<LocalProviderUpdateOutcome>[] = [
409 fulfilledOutcome(true, primary),
410 { status: "rejected", reason: new Error("WebSocket closed") },
411 ];
412​
413 expect(collectProviderUpdateOutcomeSnapshots(results)).toEqual([primary]);
414 expect(firstRejectedProviderUpdateMessage(results)).toBe("WebSocket closed");
415 });
416 });
417​
418 describe("per-environment update grouping", () => {
419 const environment = (
420 input: {
421 readonly environmentId: string;
422 readonly providers: ReadonlyArray<ServerProvider>;
423 } & Partial<Omit<LocalEnvironmentProvidersInput, "environmentId" | "providers">>,
424 ): LocalEnvironmentProvidersInput => ({
425 environmentId: input.environmentId as EnvironmentId,
426 label: input.label ?? input.environmentId,
427 isPrimary: input.isPrimary ?? false,
428 connectionState: input.connectionState ?? "ready",
429 providers: input.providers,
430 });
431​
432 it("flags settling while a secondary backend is still connecting", () => {
433 const result = buildLocalEnvironmentUpdateGroups([
434 environment({
435 environmentId: "env-windows",
436 isPrimary: true,
437 providers: [provider({ driver: driver("codex") })],
438 }),
439 environment({ environmentId: "env-wsl", connectionState: "connecting", providers: [] }),
440 ]);
441​
442 expect(result.isAnySettling).toBe(true);
443 expect(
444 result.groups.find((group) => group.environmentId === ("env-wsl" as EnvironmentId))
445 ?.isSettling,
446 ).toBe(true);
447 });
448 });
449​
450 describe("resolveEnvironmentUpdateRowStatus", () => {
451 const group: LocalEnvironmentUpdateGroup = {
452 environmentId: "env-wsl" as EnvironmentId,
453 label: "WSL",
454 isPrimary: false,
455 isSettling: false,
456 candidates: [updateCandidate({ driver: driver("codex"), latestVersion: "1.1.0" })],
457 providers: [],
458 };
459 const succeededResult: ProviderUpdateToastView = {
460 phase: "succeeded",
461 type: "success",
462 title: "Provider updated",
463 description: "New sessions will use the updated provider.",
464 };
465 const successPill: ProviderUpdateSidebarPillView = {
466 key: "succeeded:codex",
467 tone: "success",
468 title: "Codex updated",
469 description: "New sessions will use the updated provider.",
470 };
471​
472 it("prefers a transport error", () => {
473 expect(
474 resolveEnvironmentUpdateRowStatus({
475 group,
476 error: "boom",
477 result: succeededResult,
478 pill: successPill,
479 isPending: true,
480 }),
481 ).toMatchObject({ kind: "failed", text: "boom" });
482 });
483 });
484});
485​
486it("does not offer incompatible latest versions and restores suggestions after policy relaxation", () => {
487 const installed = provider({ driver: driver("codex") });
488 for (const latestVersionStatus of ["broken", "unsupported", "supported", "unknown"] as const) {
489 const snapshot: ServerProvider = {
490 ...installed,
491 compatibilityAdvisory: {
492 status: "supported",
493 latestVersionStatus,
494 message: null,
495 recommendedRange: null,
496 recommendedVersion: null,
497 },
498 };
499 const expected = latestVersionStatus === "supported" || latestVersionStatus === "unknown";
500 expect(isProviderUpdateCandidate(snapshot)).toBe(expected);
501 expect(isProviderSettingsUpdateCandidate(snapshot)).toBe(expected);
502 }
503});
504​
505describe("getProviderUpdateRunToastView", () => {
506 const updateState = (
507 status: "succeeded" | "failed",
508 message: string,
509 ): ServerProvider["updateState"] => ({
510 status,
511 startedAt: checkedAt,
512 finishedAt: laterCheckedAt,
513 message,
514 output: null,
515 });
516 const run = (
517 machineLabel: string,
518 providerDriver: string,
519 result: Parameters<typeof getProviderUpdateRunToastView>[0][number]["result"],
520 ) => ({
521 environmentId: EnvironmentId.make(machineLabel),
522 machineLabel,
523 driver: driver(providerDriver),
524 instanceId: instanceId(providerDriver),
525 result,
526 });
527​
528 it("lists every failed update and ignores interrupted ones", () => {
529 const view = getProviderUpdateRunToastView([
530 run(
531 "Mac Studio",
532 "codex",
533 AsyncResult.success({
534 providers: [
535 provider({
536 driver: driver("codex"),
537 updateState: updateState("succeeded", "Provider updated."),
538 }),
539 ],
540 }),
541 ),
542 run(
543 "Mac Studio",
544 "claudeAgent",
545 AsyncResult.success({
546 providers: [
547 provider({
548 driver: driver("claudeAgent"),
549 updateState: updateState("failed", "npm exited with code 1."),
550 }),
551 ],
552 }),
553 ),
554 run("Laptop", "codex", AsyncResult.failure(Cause.die(new Error("WebSocket closed")))),
555 run("Server", "codex", AsyncResult.failure(Cause.interrupt())),
556 ]);
557​
558 expect(view).toEqual({
559 type: "error",
560 title: "2 of 3 provider updates failed",
561 description: "Mac Studio · Claude: npm exited with code 1.\nLaptop · Codex: WebSocket closed",
562 });
563 });
564});