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