Open History to see the last commit
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 collectProviderUpdateCandidates,
15 collectProviderUpdateOutcomeSnapshots,
16 collectUpdatedProviderSnapshots,
17 deriveEnvironmentDisplayLabel,
18 environmentGroupsWithUpdates,
19 firstFailedProviderUpdateMessage,
20 firstRejectedProviderUpdateMessage,
21 getProviderUpdateInitialToastView,
22 getProviderUpdateProgressToastView,
23 getProviderUpdateRejectedToastView,
24 getProviderUpdateRunToastView,
25 getProviderUpdateSidebarPillView,
26 hasOneClickUpdateProviderCandidate,
27 isProviderUpdateCandidate,
28 isProviderSettingsUpdateCandidate,
29 isTerminalProviderUpdatePhase,
30 localEnvironmentUpdateNotificationKey,
31 providerUpdateNotificationKey,
32 resolveEnvironmentUpdateRowStatus,
33 shouldShowPrimaryProviderUpdateToast,
34 type LocalEnvironmentProvidersInput,
35 type LocalEnvironmentUpdateGroup,
36 type LocalProviderUpdateOutcome,
37 type ProviderUpdateCandidate,
38 type ProviderUpdateSidebarPillView,
39 type ProviderUpdateToastView,
40} from "./ProviderUpdateLaunchNotification.logic";
41
42const checkedAt = "2026-04-23T10:00:00.000Z";
43const sessionStartedAt = "2026-04-23T09:59:00.000Z";
44const laterCheckedAt = "2026-04-23T10:01:00.000Z";
45
46const driver = (value: string) => ProviderDriverKind.make(value);
47const instanceId = (value: string) => ProviderInstanceId.make(value);
48
49function provider(input: {
50 readonly driver: ReturnType<typeof ProviderDriverKind.make>;
51 readonly instanceId?: ReturnType<typeof ProviderInstanceId.make>;
52 readonly enabled?: boolean;
53 readonly version?: string | null;
54 readonly latestVersion?: string | null;
55 readonly canUpdate?: boolean;
56 readonly updateCommand?: string | null;
57 readonly updateState?: ServerProvider["updateState"];
58 readonly advisoryStatus?: NonNullable<ServerProvider["versionAdvisory"]>["status"];
59}): ServerProvider {
60 const result: ServerProvider = {
61 instanceId: input.instanceId ?? instanceId(String(input.driver)),
62 driver: input.driver,
63 enabled: input.enabled ?? true,
64 installed: true,
65 version: input.version ?? "1.0.0",
66 status: "ready",
67 auth: { status: "authenticated" },
68 checkedAt,
69 models: [],
70 slashCommands: [],
71 skills: [],
72 versionAdvisory: {
73 status: input.advisoryStatus ?? "behind_latest",
74 currentVersion: input.version ?? "1.0.0",
75 latestVersion: "latestVersion" in input ? input.latestVersion : "1.1.0",
76 updateCommand: "updateCommand" in input ? input.updateCommand : "npm install -g provider",
77 canUpdate: input.canUpdate ?? true,
78 checkedAt,
79 message: "Update available.",
80 },
81 };
82
83 if (input.updateState) {
84 return { ...result, updateState: input.updateState };
85 }
86
87 return result;
88}
89
90function updateCandidate(input: Parameters<typeof provider>[0]): ProviderUpdateCandidate {
91 return provider(input) as ProviderUpdateCandidate;
92}
93
94describe("provider update launch notification logic", () => {
95 it("detects enabled providers with a latest-version advisory", () => {
96 expect(isProviderUpdateCandidate(provider({ driver: driver("codex") }))).toBe(true);
97 expect(isProviderUpdateCandidate(provider({ driver: driver("codex"), enabled: false }))).toBe(
98 false,
99 );
100 expect(
101 isProviderUpdateCandidate(
102 provider({ driver: driver("codex"), advisoryStatus: "current", latestVersion: null }),
103 ),
104 ).toBe(false);
105 expect(
106 isProviderUpdateCandidate(provider({ driver: driver("codex"), latestVersion: null })),
107 ).toBe(false);
108 });
109
110 it("deduplicates multi-instance provider candidates by driver", () => {
111 expect(
112 collectProviderUpdateCandidates([
113 provider({
114 driver: driver("codex"),
115 instanceId: instanceId("codex_personal"),
116 latestVersion: "1.1.0",
117 }),
118 provider({
119 driver: driver("codex"),
120 instanceId: instanceId("codex"),
121 latestVersion: "1.1.0",
122 }),
123 provider({ driver: driver("cursor"), latestVersion: "0.3.0" }),
124 ]),
125 ).toHaveLength(2);
126 });
127
128 it("disables one-click updates when provider instances disagree on the update command", () => {
129 const candidate = updateCandidate({
130 driver: driver("claudeAgent"),
131 instanceId: instanceId("claude_personal"),
132 latestVersion: "2.1.123",
133 });
134
135 expect(
136 canOneClickUpdateProviderCandidate(candidate, [
137 candidate,
138 provider({
139 driver: driver("claudeAgent"),
140 instanceId: instanceId("claude_work"),
141 latestVersion: "2.1.123",
142 canUpdate: true,
143 updateCommand: "bun add -g @anthropic-ai/claude-code@latest",
144 }),
145 ]),
146 ).toBe(false);
147 });
148
149 it("keeps one-click updates enabled when sibling instances are already current", () => {
150 const candidate = updateCandidate({
151 driver: driver("claudeAgent"),
152 instanceId: instanceId("claude_personal"),
153 latestVersion: "2.1.123",
154 updateCommand: "npm install -g @anthropic-ai/claude-code@latest",
155 });
156
157 expect(
158 hasOneClickUpdateProviderCandidate(candidate, [
159 candidate,
160 provider({
161 driver: driver("claudeAgent"),
162 instanceId: instanceId("claude_work"),
163 version: "2.1.123",
164 latestVersion: "2.1.123",
165 advisoryStatus: "current",
166 canUpdate: false,
167 updateCommand: null,
168 }),
169 ]),
170 ).toBe(true);
171 expect(
172 canOneClickUpdateProviderCandidate(candidate, [
173 candidate,
174 provider({
175 driver: driver("claudeAgent"),
176 instanceId: instanceId("claude_work"),
177 version: "2.1.123",
178 latestVersion: "2.1.123",
179 advisoryStatus: "current",
180 canUpdate: false,
181 updateCommand: null,
182 }),
183 ]),
184 ).toBe(true);
185 });
186
187 it("keeps the inline update action available while a provider update is already running", () => {
188 const candidate = updateCandidate({
189 driver: driver("codex"),
190 updateState: {
191 status: "running",
192 startedAt: checkedAt,
193 finishedAt: null,
194 message: "Updating provider.",
195 output: null,
196 },
197 });
198
199 expect(hasOneClickUpdateProviderCandidate(candidate, [candidate])).toBe(true);
200 expect(canOneClickUpdateProviderCandidate(candidate, [candidate])).toBe(false);
201 });
202
203 it("builds a notification key from provider latest versions", () => {
204 const codex = updateCandidate({
205 driver: driver("codex"),
206 version: "1.0.0",
207 latestVersion: "1.1.0",
208 });
209 const cursor = updateCandidate({
210 driver: driver("cursor"),
211 version: "0.2.0",
212 latestVersion: "0.3.0",
213 });
214
215 expect(providerUpdateNotificationKey([codex, cursor])).toBe("codex:1.1.0|cursor:0.3.0");
216 expect(providerUpdateNotificationKey([])).toBeNull();
217 });
218
219 it("keeps the same notification key while the published update version is unchanged", () => {
220 const first = updateCandidate({
221 driver: driver("codex"),
222 version: "1.0.0",
223 latestVersion: "1.2.0",
224 });
225 const second = updateCandidate({
226 driver: driver("codex"),
227 version: "1.1.0",
228 latestVersion: "1.2.0",
229 });
230 const nextPublishedVersion = updateCandidate({
231 driver: driver("codex"),
232 version: "1.1.0",
233 latestVersion: "1.3.0",
234 });
235
236 expect(providerUpdateNotificationKey([first])).toBe(providerUpdateNotificationKey([second]));
237 expect(providerUpdateNotificationKey([nextPublishedVersion])).not.toBe(
238 providerUpdateNotificationKey([first]),
239 );
240 });
241
242 it("tracks updated provider snapshots by instance instead of collapsing to a sibling driver", () => {
243 const targetInstanceId = instanceId("codex_personal");
244 const siblingInstanceId = instanceId("codex");
245 const updatedPersonal = provider({
246 driver: driver("codex"),
247 instanceId: targetInstanceId,
248 version: "1.1.0",
249 latestVersion: "1.1.0",
250 advisoryStatus: "current",
251 updateState: {
252 status: "succeeded",
253 startedAt: checkedAt,
254 finishedAt: checkedAt,
255 message: "Provider updated.",
256 output: null,
257 },
258 });
259 const currentDefaultSibling = provider({
260 driver: driver("codex"),
261 instanceId: siblingInstanceId,
262 version: "1.1.0",
263 latestVersion: "1.1.0",
264 advisoryStatus: "current",
265 updateState: undefined,
266 });
267
268 expect(
269 collectUpdatedProviderSnapshots({
270 results: [
271 AsyncResult.success({
272 providers: [updatedPersonal, currentDefaultSibling],
273 }),
274 ],
275 providerInstanceIds: new Set([targetInstanceId]),
276 }),
277 ).toEqual([updatedPersonal]);
278 });
279
280 it("describes a single one-click update", () => {
281 const view = getProviderUpdateInitialToastView({
282 updateProviders: [updateCandidate({ driver: driver("codex"), latestVersion: "1.1.0" })],
283 oneClickProviders: [updateCandidate({ driver: driver("codex"), latestVersion: "1.1.0" })],
284 });
285
286 expect(view).toMatchObject({
287 phase: "initial",
288 type: "warning",
289 title: "Update Available: Codex v1.1.0",
290 description: "Install the update now or review provider settings.",
291 });
292 });
293
294 it("describes settings-only updates without one-click support", () => {
295 const view = getProviderUpdateInitialToastView({
296 updateProviders: [
297 updateCandidate({ driver: driver("codex") }),
298 updateCandidate({ driver: driver("cursor") }),
299 ],
300 oneClickProviders: [],
301 });
302
303 expect(view.description).toBe("Codex and Cursor can be updated from provider settings.");
304 });
305
306 it("sends installs the server cannot update to the tool that installed them", () => {
307 const manual = { canUpdate: false, updateCommand: null };
308 expect(
309 getProviderUpdateInitialToastView({
310 updateProviders: [updateCandidate({ driver: driver("codex"), ...manual })],
311 oneClickProviders: [],
312 }).description,
313 ).toBe("Update Codex with the app or tool that installed it.");
314 expect(
315 getProviderUpdateInitialToastView({
316 updateProviders: [
317 updateCandidate({ driver: driver("codex"), ...manual }),
318 updateCandidate({ driver: driver("cursor") }),
319 ],
320 oneClickProviders: [],
321 }).description,
322 ).toBe(
323 "Cursor can be updated from provider settings. Update Codex with the app or tool that installed it.",
324 );
325 const oneClick = updateCandidate({ driver: driver("claudeAgent") });
326 expect(
327 getProviderUpdateInitialToastView({
328 updateProviders: [oneClick, updateCandidate({ driver: driver("codex"), ...manual })],
329 oneClickProviders: [oneClick],
330 }).description,
331 ).toBe(
332 "Install the update now or review provider settings. Update Codex with the app or tool that installed it.",
333 );
334 });
335
336 it("uses server update state for running progress", () => {
337 const view = getProviderUpdateProgressToastView({
338 providers: [
339 provider({
340 driver: driver("codex"),
341 updateState: {
342 status: "running",
343 startedAt: checkedAt,
344 finishedAt: null,
345 message: "Updating provider.",
346 output: null,
347 },
348 }),
349 ],
350 providerCount: 1,
351 });
352
353 expect(view).toMatchObject({
354 phase: "running",
355 type: "loading",
356 title: "Updating provider",
357 });
358 expect(shouldShowPrimaryProviderUpdateToast(view)).toBe(false);
359 });
360
361 it("keeps the initial prompt and terminal outcomes visible as toasts", () => {
362 expect(
363 shouldShowPrimaryProviderUpdateToast(
364 getProviderUpdateInitialToastView({
365 updateProviders: [updateCandidate({ driver: driver("codex") })],
366 oneClickProviders: [updateCandidate({ driver: driver("codex") })],
367 }),
368 ),
369 ).toBe(true);
370 expect(
371 shouldShowPrimaryProviderUpdateToast(getProviderUpdateRejectedToastView(1, "boom")),
372 ).toBe(true);
373 });
374
375 it("uses server failure state for failed progress", () => {
376 const view = getProviderUpdateProgressToastView({
377 providers: [
378 provider({
379 driver: driver("codex"),
380 updateState: {
381 status: "failed",
382 startedAt: checkedAt,
383 finishedAt: checkedAt,
384 message: "command failed",
385 output: "stderr",
386 },
387 }),
388 ],
389 providerCount: 1,
390 });
391
392 expect(view).toMatchObject({
393 phase: "failed",
394 type: "error",
395 title: "Provider update failed",
396 description: "command failed",
397 });
398 });
399
400 it("keeps unchanged providers actionable from settings", () => {
401 const view = getProviderUpdateProgressToastView({
402 providers: [
403 provider({
404 driver: driver("cursor"),
405 updateState: {
406 status: "unchanged",
407 startedAt: checkedAt,
408 finishedAt: checkedAt,
409 message: "still old",
410 output: null,
411 },
412 }),
413 ],
414 providerCount: 1,
415 });
416
417 expect(view).toMatchObject({
418 phase: "unchanged",
419 type: "warning",
420 title: "Provider still needs an update",
421 description: "Cursor still appears outdated. Check provider settings for details.",
422 });
423 });
424
425 it("marks progress succeeded once every attempted provider is no longer outdated", () => {
426 const view = getProviderUpdateProgressToastView({
427 providers: [
428 provider({
429 driver: driver("codex"),
430 version: "1.1.0",
431 latestVersion: "1.1.0",
432 advisoryStatus: "current",
433 updateState: {
434 status: "succeeded",
435 startedAt: checkedAt,
436 finishedAt: checkedAt,
437 message: "Provider updated.",
438 output: null,
439 },
440 }),
441 ],
442 providerCount: 1,
443 });
444
445 expect(view).toMatchObject({
446 phase: "succeeded",
447 type: "success",
448 title: "Provider updated",
449 description: "New sessions will use the updated provider.",
450 dismissAfterVisibleMs: 3_000,
451 });
452 });
453
454 it("falls back to a rejected RPC message for transport-level failures", () => {
455 const results = [AsyncResult.failure(Cause.die(new Error("WebSocket closed")))];
456
457 expect(firstFailedProviderUpdateMessage(results)).toBe("WebSocket closed");
458 expect(getProviderUpdateRejectedToastView(2, "WebSocket closed")).toMatchObject({
459 phase: "failed",
460 title: "Provider updates failed",
461 description: "WebSocket closed",
462 });
463 });
464
465 it("collects only attempted provider snapshots from update responses", () => {
466 const codex = provider({ driver: driver("codex") });
467 const cursor = provider({ driver: driver("cursor") });
468 const results = [AsyncResult.success({ providers: [codex, cursor] })];
469
470 expect(
471 collectUpdatedProviderSnapshots({
472 results,
473 providerInstanceIds: new Set([cursor.instanceId]),
474 }),
475 ).toEqual([cursor]);
476 });
477
478 it("summarizes active provider updates for the sidebar pill", () => {
479 const view = getProviderUpdateSidebarPillView([
480 provider({
481 driver: driver("codex"),
482 updateState: {
483 status: "running",
484 startedAt: checkedAt,
485 finishedAt: null,
486 message: "Updating provider.",
487 output: null,
488 },
489 }),
490 provider({
491 driver: driver("cursor"),
492 updateState: {
493 status: "queued",
494 startedAt: null,
495 finishedAt: null,
496 message: "Waiting for another provider update to finish.",
497 output: null,
498 },
499 }),
500 ]);
501
502 expect(view).toMatchObject({
503 tone: "loading",
504 title: "Updating 2 providers",
505 description: "Codex and Cursor updates are in progress.",
506 });
507 });
508
509 it("uses the provider name for single active sidebar pill updates", () => {
510 const view = getProviderUpdateSidebarPillView([
511 provider({
512 driver: driver("codex"),
513 updateState: {
514 status: "running",
515 startedAt: checkedAt,
516 finishedAt: null,
517 message: "Updating provider.",
518 output: null,
519 },
520 }),
521 ]);
522
523 expect(view).toMatchObject({
524 key: "loading:codex:running",
525 tone: "loading",
526 title: "Updating Codex",
527 description: "Codex update in progress.",
528 });
529 });
530
531 it("shows the active sidebar pill for an update on a non-default instance", () => {
532 const view = getProviderUpdateSidebarPillView([
533 provider({ driver: driver("claudeAgent") }),
534 provider({
535 driver: driver("claudeAgent"),
536 instanceId: instanceId("claude_work"),
537 updateState: {
538 status: "running",
539 startedAt: checkedAt,
540 finishedAt: null,
541 message: "Updating provider.",
542 output: null,
543 },
544 }),
545 ]);
546
547 expect(view).toMatchObject({
548 key: "loading:claudeAgent:running",
549 tone: "loading",
550 title: "Updating Claude",
551 description: "Claude update in progress.",
552 });
553 });
554
555 it("shows the latest terminal sidebar pill across instances of a driver", () => {
556 const view = getProviderUpdateSidebarPillView(
557 [
558 provider({
559 driver: driver("claudeAgent"),
560 updateState: {
561 status: "failed",
562 startedAt: checkedAt,
563 finishedAt: checkedAt,
564 message: "Update command exited with code 1.",
565 output: null,
566 },
567 }),
568 provider({
569 driver: driver("claudeAgent"),
570 instanceId: instanceId("claude_work"),
571 version: "1.1.0",
572 latestVersion: "1.1.0",
573 advisoryStatus: "current",
574 updateState: {
575 status: "succeeded",
576 startedAt: laterCheckedAt,
577 finishedAt: laterCheckedAt,
578 message: "Provider updated.",
579 output: null,
580 },
581 }),
582 ],
583 { visibleAfterIso: sessionStartedAt },
584 );
585
586 expect(view).toMatchObject({
587 key: "succeeded:claudeAgent:2026-04-23T10:01:00.000Z:Provider updated.",
588 tone: "success",
589 title: "Claude updated: v1.1.0",
590 });
591 });
592
593 it("uses the provider name for single failed sidebar pill updates", () => {
594 const view = getProviderUpdateSidebarPillView(
595 [
596 provider({
597 driver: driver("claudeAgent"),
598 updateState: {
599 status: "failed",
600 startedAt: checkedAt,
601 finishedAt: checkedAt,
602 message: "Update command exited with code 1.",
603 output: null,
604 },
605 }),
606 ],
607 { visibleAfterIso: sessionStartedAt },
608 );
609
610 expect(view).toMatchObject({
611 key: "failed:claudeAgent:2026-04-23T10:00:00.000Z:Update command exited with code 1.",
612 tone: "error",
613 title: "Claude v1.1.0 update failed",
614 description: "Update command exited with code 1.",
615 dismissible: true,
616 });
617 });
618
619 it("shows a short-lived success sidebar pill after a single provider update succeeds", () => {
620 const view = getProviderUpdateSidebarPillView(
621 [
622 provider({
623 driver: driver("codex"),
624 version: "1.1.0",
625 latestVersion: "1.1.0",
626 advisoryStatus: "current",
627 updateState: {
628 status: "succeeded",
629 startedAt: checkedAt,
630 finishedAt: checkedAt,
631 message: "Provider updated.",
632 output: null,
633 },
634 }),
635 ],
636 { visibleAfterIso: sessionStartedAt },
637 );
638
639 expect(view).toMatchObject({
640 key: "succeeded:codex:2026-04-23T10:00:00.000Z:Provider updated.",
641 tone: "success",
642 title: "Codex updated: v1.1.0",
643 description: "New sessions will use the updated provider.",
644 dismissAfterVisibleMs: 3_000,
645 });
646 });
647
648 it("keeps unchanged sidebar pill states dismissible", () => {
649 const view = getProviderUpdateSidebarPillView(
650 [
651 provider({
652 driver: driver("cursor"),
653 updateState: {
654 status: "unchanged",
655 startedAt: checkedAt,
656 finishedAt: checkedAt,
657 message: "still old",
658 output: null,
659 },
660 }),
661 ],
662 { visibleAfterIso: sessionStartedAt },
663 );
664
665 expect(view).toMatchObject({
666 key: "unchanged:cursor:2026-04-23T10:00:00.000Z:still old",
667 tone: "warning",
668 title: "Cursor still needs an update",
669 dismissible: true,
670 });
671 });
672
673 it("does not show sidebar terminal states from before the current app session", () => {
674 expect(
675 getProviderUpdateSidebarPillView(
676 [
677 provider({
678 driver: driver("codex"),
679 updateState: {
680 status: "failed",
681 startedAt: checkedAt,
682 finishedAt: checkedAt,
683 message: "command failed",
684 output: "stderr",
685 },
686 }),
687 ],
688 { visibleAfterIso: "2026-04-23T10:00:01.000Z" },
689 ),
690 ).toBeNull();
691 });
692
693 it("shows a newer success before falling back to an older failure", () => {
694 const providers = [
695 provider({
696 driver: driver("claudeAgent"),
697 updateState: {
698 status: "failed",
699 startedAt: checkedAt,
700 finishedAt: checkedAt,
701 message: "Update command exited with code 1.",
702 output: null,
703 },
704 }),
705 provider({
706 driver: driver("codex"),
707 version: "1.2.0",
708 latestVersion: "1.2.0",
709 advisoryStatus: "current",
710 updateState: {
711 status: "succeeded",
712 startedAt: laterCheckedAt,
713 finishedAt: laterCheckedAt,
714 message: "Provider updated.",
715 output: null,
716 },
717 }),
718 ] satisfies ReadonlyArray<ServerProvider>;
719
720 const successView = getProviderUpdateSidebarPillView(providers, {
721 visibleAfterIso: sessionStartedAt,
722 });
723 expect(successView).toMatchObject({
724 key: "succeeded:codex:2026-04-23T10:01:00.000Z:Provider updated.",
725 tone: "success",
726 title: "Codex updated: v1.2.0",
727 });
728
729 const failureView = getProviderUpdateSidebarPillView(providers, {
730 visibleAfterIso: sessionStartedAt,
731 dismissedKeys: new Set(["succeeded:codex:2026-04-23T10:01:00.000Z:Provider updated."]),
732 });
733 expect(failureView).toMatchObject({
734 key: "failed:claudeAgent:2026-04-23T10:00:00.000Z:Update command exited with code 1.",
735 tone: "error",
736 title: "Claude v1.1.0 update failed",
737 });
738 });
739
740 it("does not show a sidebar pill for passive update availability", () => {
741 expect(
742 getProviderUpdateSidebarPillView([
743 provider({ driver: driver("codex"), canUpdate: true }),
744 provider({ driver: driver("cursor"), canUpdate: false }),
745 ]),
746 ).toBeNull();
747 });
748
749 describe("multi-backend update outcomes", () => {
750 const terminalState = (
751 status: "succeeded" | "failed" | "unchanged",
752 message: string,
753 ): NonNullable<ServerProvider["updateState"]> => ({
754 status,
755 startedAt: checkedAt,
756 finishedAt: checkedAt,
757 message,
758 output: null,
759 });
760
761 const fulfilledOutcome = (
762 isPrimary: boolean,
763 snapshot: ServerProvider | null,
764 environment = "env",
765 ): PromiseSettledResult<LocalProviderUpdateOutcome> => ({
766 status: "fulfilled",
767 value: {
768 environmentId: environment as LocalProviderUpdateOutcome["environmentId"],
769 isPrimary,
770 driver: snapshot?.driver ?? driver("codex"),
771 instanceId: snapshot?.instanceId ?? instanceId("codex"),
772 provider: snapshot,
773 },
774 });
775
776 it("surfaces a secondary backend's failed update over the primary's success", () => {
777 const snapshots = collectProviderUpdateOutcomeSnapshots([
778 fulfilledOutcome(
779 true,
780 provider({
781 driver: driver("codex"),
782 updateState: terminalState("succeeded", "Provider updated."),
783 }),
784 ),
785 fulfilledOutcome(
786 false,
787 provider({
788 driver: driver("codex"),
789 updateState: terminalState("failed", "npm: NotFound"),
790 }),
791 ),
792 ]);
793
794 expect(snapshots).toHaveLength(1);
795 expect(snapshots[0]?.updateState?.status).toBe("failed");
796 expect(
797 getProviderUpdateProgressToastView({ providers: snapshots, providerCount: 1 }),
798 ).toMatchObject({ phase: "failed" });
799 });
800
801 it("surfaces a secondary backend that stayed outdated over the primary's success", () => {
802 const snapshots = collectProviderUpdateOutcomeSnapshots([
803 fulfilledOutcome(
804 true,
805 provider({
806 driver: driver("codex"),
807 updateState: terminalState("succeeded", "Provider updated."),
808 }),
809 ),
810 fulfilledOutcome(
811 false,
812 provider({
813 driver: driver("codex"),
814 updateState: terminalState("unchanged", "still outdated"),
815 }),
816 ),
817 ]);
818
819 expect(snapshots[0]?.updateState?.status).toBe("unchanged");
820 expect(
821 getProviderUpdateProgressToastView({ providers: snapshots, providerCount: 1 }),
822 ).toMatchObject({ phase: "unchanged" });
823 });
824
825 it("reports success only when every backend succeeded", () => {
826 const snapshots = collectProviderUpdateOutcomeSnapshots([
827 fulfilledOutcome(
828 true,
829 provider({
830 driver: driver("codex"),
831 updateState: terminalState("succeeded", "Provider updated."),
832 }),
833 ),
834 fulfilledOutcome(
835 false,
836 provider({
837 driver: driver("codex"),
838 updateState: terminalState("succeeded", "Provider updated."),
839 }),
840 ),
841 ]);
842
843 expect(
844 getProviderUpdateProgressToastView({ providers: snapshots, providerCount: 1 }),
845 ).toMatchObject({ phase: "succeeded" });
846 });
847
848 it("ignores backends that did not return the targeted instance", () => {
849 const primary = provider({
850 driver: driver("codex"),
851 updateState: terminalState("succeeded", "Provider updated."),
852 });
853 const snapshots = collectProviderUpdateOutcomeSnapshots([
854 fulfilledOutcome(true, primary),
855 fulfilledOutcome(false, null),
856 ]);
857
858 expect(snapshots).toEqual([primary]);
859 });
860
861 it("treats a rejected dispatch as not contributing a snapshot", () => {
862 const primary = provider({
863 driver: driver("codex"),
864 updateState: terminalState("succeeded", "Provider updated."),
865 });
866 const results: PromiseSettledResult<LocalProviderUpdateOutcome>[] = [
867 fulfilledOutcome(true, primary),
868 { status: "rejected", reason: new Error("WebSocket closed") },
869 ];
870
871 expect(collectProviderUpdateOutcomeSnapshots(results)).toEqual([primary]);
872 expect(firstRejectedProviderUpdateMessage(results)).toBe("WebSocket closed");
873 });
874 });
875
876 describe("per-environment update grouping", () => {
877 const environment = (
878 input: {
879 readonly environmentId: string;
880 readonly providers: ReadonlyArray<ServerProvider>;
881 } & Partial<Omit<LocalEnvironmentProvidersInput, "environmentId" | "providers">>,
882 ): LocalEnvironmentProvidersInput => ({
883 environmentId: input.environmentId as EnvironmentId,
884 label: input.label ?? input.environmentId,
885 isPrimary: input.isPrimary ?? false,
886 connectionState: input.connectionState ?? "ready",
887 providers: input.providers,
888 });
889
890 it("groups each environment's outdated one-click candidates", () => {
891 const result = buildLocalEnvironmentUpdateGroups([
892 environment({
893 environmentId: "env-windows",
894 label: "Windows",
895 isPrimary: true,
896 providers: [provider({ driver: driver("codex"), latestVersion: "1.1.0" })],
897 }),
898 environment({
899 environmentId: "env-wsl",
900 label: "WSL",
901 providers: [provider({ driver: driver("codex"), latestVersion: "1.1.0" })],
902 }),
903 ]);
904
905 expect(result.isAnySettling).toBe(false);
906 expect(result.groups.map((group) => group.label)).toEqual(["Windows", "WSL"]);
907 expect(result.groups.every((group) => group.candidates.length === 1)).toBe(true);
908 });
909
910 it("flags settling while a secondary backend is still connecting", () => {
911 const result = buildLocalEnvironmentUpdateGroups([
912 environment({
913 environmentId: "env-windows",
914 isPrimary: true,
915 providers: [provider({ driver: driver("codex") })],
916 }),
917 environment({ environmentId: "env-wsl", connectionState: "connecting", providers: [] }),
918 ]);
919
920 expect(result.isAnySettling).toBe(true);
921 expect(
922 result.groups.find((group) => group.environmentId === ("env-wsl" as EnvironmentId))
923 ?.isSettling,
924 ).toBe(true);
925 });
926
927 it("keeps only environments that have a one-click update on offer", () => {
928 const { groups } = buildLocalEnvironmentUpdateGroups([
929 environment({
930 environmentId: "env-windows",
931 isPrimary: true,
932 providers: [provider({ driver: driver("codex") })],
933 }),
934 environment({
935 environmentId: "env-wsl",
936 providers: [
937 provider({ driver: driver("codex"), advisoryStatus: "current", latestVersion: null }),
938 ],
939 }),
940 ]);
941
942 expect(environmentGroupsWithUpdates(groups).map((group) => group.environmentId)).toEqual([
943 "env-windows",
944 ]);
945 });
946
947 it("keys the notification by environment, driver and latest version", () => {
948 const noUpdates = buildLocalEnvironmentUpdateGroups([
949 environment({
950 environmentId: "env-windows",
951 isPrimary: true,
952 providers: [
953 provider({ driver: driver("codex"), advisoryStatus: "current", latestVersion: null }),
954 ],
955 }),
956 ]);
957 expect(localEnvironmentUpdateNotificationKey(noUpdates.groups)).toBeNull();
958
959 const both = buildLocalEnvironmentUpdateGroups([
960 environment({
961 environmentId: "env-windows",
962 isPrimary: true,
963 providers: [provider({ driver: driver("codex"), latestVersion: "1.1.0" })],
964 }),
965 environment({
966 environmentId: "env-wsl",
967 providers: [provider({ driver: driver("codex"), latestVersion: "1.1.0" })],
968 }),
969 ]);
970 const key = localEnvironmentUpdateNotificationKey(both.groups);
971 expect(key).toContain("env-windows=codex:1.1.0");
972 expect(key).toContain("env-wsl=codex:1.1.0");
973 });
974
975 it("labels environments by platform so they are distinguishable", () => {
976 expect(
977 deriveEnvironmentDisplayLabel({
978 isWsl: false,
979 wslDistro: null,
980 platformOs: "windows",
981 fallbackLabel: "Jgratton24",
982 }),
983 ).toBe("Windows");
984 expect(
985 deriveEnvironmentDisplayLabel({
986 isWsl: true,
987 wslDistro: null,
988 platformOs: "linux",
989 fallbackLabel: "Jgratton24",
990 }),
991 ).toBe("WSL");
992 expect(
993 deriveEnvironmentDisplayLabel({
994 isWsl: true,
995 wslDistro: "ubuntu",
996 platformOs: "linux",
997 fallbackLabel: "Jgratton24",
998 }),
999 ).toBe("WSL · ubuntu");
1000 expect(
1001 deriveEnvironmentDisplayLabel({
1002 isWsl: false,
1003 wslDistro: null,
1004 platformOs: undefined,
1005 fallbackLabel: "My Device",
1006 }),
1007 ).toBe("My Device");
1008 });
1009 });
1010
1011 describe("isTerminalProviderUpdatePhase", () => {
1012 it("treats succeeded/failed/unchanged as terminal", () => {
1013 expect(isTerminalProviderUpdatePhase("succeeded")).toBe(true);
1014 expect(isTerminalProviderUpdatePhase("failed")).toBe(true);
1015 expect(isTerminalProviderUpdatePhase("unchanged")).toBe(true);
1016 });
1017
1018 it("treats running/initial as non-terminal so they are not persisted", () => {
1019 expect(isTerminalProviderUpdatePhase("running")).toBe(false);
1020 expect(isTerminalProviderUpdatePhase("initial")).toBe(false);
1021 });
1022 });
1023
1024 describe("resolveEnvironmentUpdateRowStatus", () => {
1025 const group: LocalEnvironmentUpdateGroup = {
1026 environmentId: "env-wsl" as EnvironmentId,
1027 label: "WSL",
1028 isPrimary: false,
1029 isSettling: false,
1030 candidates: [updateCandidate({ driver: driver("codex"), latestVersion: "1.1.0" })],
1031 providers: [],
1032 };
1033 const runningResult: ProviderUpdateToastView = {
1034 phase: "running",
1035 type: "loading",
1036 title: "Updating providers",
1037 description: "Running provider update command.",
1038 };
1039 const succeededResult: ProviderUpdateToastView = {
1040 phase: "succeeded",
1041 type: "success",
1042 title: "Provider updated",
1043 description: "New sessions will use the updated provider.",
1044 };
1045 const successPill: ProviderUpdateSidebarPillView = {
1046 key: "succeeded:codex",
1047 tone: "success",
1048 title: "Codex updated",
1049 description: "New sessions will use the updated provider.",
1050 };
1051
1052 it("prefers a transport error", () => {
1053 expect(
1054 resolveEnvironmentUpdateRowStatus({
1055 group,
1056 error: "boom",
1057 result: succeededResult,
1058 pill: successPill,
1059 isPending: true,
1060 }),
1061 ).toMatchObject({ kind: "failed", text: "boom" });
1062 });
1063
1064 it("uses a terminal result snapshot", () => {
1065 expect(
1066 resolveEnvironmentUpdateRowStatus({
1067 group,
1068 error: undefined,
1069 result: succeededResult,
1070 pill: null,
1071 isPending: false,
1072 }),
1073 ).toMatchObject({ kind: "success" });
1074 });
1075
1076 it("falls through a non-terminal result to live server state", () => {
1077 expect(
1078 resolveEnvironmentUpdateRowStatus({
1079 group,
1080 error: undefined,
1081 result: runningResult,
1082 pill: successPill,
1083 isPending: true,
1084 }),
1085 ).toMatchObject({ kind: "success" });
1086 });
1087
1088 it("shows the pending spinner before any signal arrives", () => {
1089 expect(
1090 resolveEnvironmentUpdateRowStatus({
1091 group,
1092 error: undefined,
1093 result: runningResult,
1094 pill: null,
1095 isPending: true,
1096 }),
1097 ).toMatchObject({ kind: "loading" });
1098 });
1099
1100 it("keeps a non-terminal result on loading even after pending clears", () => {
1101 expect(
1102 resolveEnvironmentUpdateRowStatus({
1103 group,
1104 error: undefined,
1105 result: runningResult,
1106 pill: null,
1107 isPending: false,
1108 }),
1109 ).toMatchObject({ kind: "loading" });
1110 });
1111
1112 it("lists the providers when idle", () => {
1113 expect(
1114 resolveEnvironmentUpdateRowStatus({
1115 group,
1116 error: undefined,
1117 result: undefined,
1118 pill: null,
1119 isPending: false,
1120 }),
1121 ).toMatchObject({ kind: "idle", text: "Codex" });
1122 });
1123 });
1124});
1125
1126it("does not offer incompatible latest versions and restores suggestions after policy relaxation", () => {
1127 const installed = provider({ driver: driver("codex") });
1128 for (const latestVersionStatus of ["broken", "unsupported", "supported", "unknown"] as const) {
1129 const snapshot: ServerProvider = {
1130 ...installed,
1131 compatibilityAdvisory: {
1132 status: "supported",
1133 latestVersionStatus,
1134 message: null,
1135 recommendedRange: null,
1136 recommendedVersion: null,
1137 },
1138 };
1139 const expected = latestVersionStatus === "supported" || latestVersionStatus === "unknown";
1140 expect(isProviderUpdateCandidate(snapshot)).toBe(expected);
1141 expect(isProviderSettingsUpdateCandidate(snapshot)).toBe(expected);
1142 }
1143});
1144
1145describe("getProviderUpdateRunToastView", () => {
1146 const updateState = (
1147 status: "succeeded" | "failed",
1148 message: string,
1149 ): ServerProvider["updateState"] => ({
1150 status,
1151 startedAt: checkedAt,
1152 finishedAt: laterCheckedAt,
1153 message,
1154 output: null,
1155 });
1156 const run = (
1157 machineLabel: string,
1158 providerDriver: string,
1159 result: Parameters<typeof getProviderUpdateRunToastView>[0][number]["result"],
1160 ) => ({
1161 environmentId: EnvironmentId.make(machineLabel),
1162 machineLabel,
1163 driver: driver(providerDriver),
1164 instanceId: instanceId(providerDriver),
1165 result,
1166 });
1167
1168 it("lists every failed update and ignores interrupted ones", () => {
1169 const view = getProviderUpdateRunToastView([
1170 run(
1171 "Mac Studio",
1172 "codex",
1173 AsyncResult.success({
1174 providers: [
1175 provider({
1176 driver: driver("codex"),
1177 updateState: updateState("succeeded", "Provider updated."),
1178 }),
1179 ],
1180 }),
1181 ),
1182 run(
1183 "Mac Studio",
1184 "claudeAgent",
1185 AsyncResult.success({
1186 providers: [
1187 provider({
1188 driver: driver("claudeAgent"),
1189 updateState: updateState("failed", "npm exited with code 1."),
1190 }),
1191 ],
1192 }),
1193 ),
1194 run("Laptop", "codex", AsyncResult.failure(Cause.die(new Error("WebSocket closed")))),
1195 run("Server", "codex", AsyncResult.failure(Cause.interrupt())),
1196 ]);
1197
1198 expect(view).toEqual({
1199 type: "error",
1200 title: "2 of 3 provider updates failed",
1201 description: "Mac Studio · Claude: npm exited with code 1.\nLaptop · Codex: WebSocket closed",
1202 });
1203 });
1204
1205 it("reports success when every update succeeded", () => {
1206 const succeeded = AsyncResult.success({
1207 providers: [
1208 provider({
1209 driver: driver("codex"),
1210 updateState: updateState("succeeded", "Provider updated."),
1211 }),
1212 ],
1213 });
1214
1215 expect(
1216 getProviderUpdateRunToastView([
1217 run("Mac Studio", "codex", succeeded),
1218 run("Laptop", "codex", succeeded),
1219 ]),
1220 ).toEqual({
1221 type: "success",
1222 title: "2 providers updated",
1223 description: "New sessions will use the updated providers.",
1224 });
1225 expect(
1226 getProviderUpdateRunToastView([
1227 run("Server", "codex", AsyncResult.failure(Cause.interrupt())),
1228 ]),
1229 ).toBeNull();
1230 });
1231});