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 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"), canUpdate: false }),
298 updateCandidate({ driver: driver("cursor"), canUpdate: false }),
299 ],
300 oneClickProviders: [],
301 });
302
303 expect(view.description).toBe("Codex and Cursor can be updated from provider settings.");
304 });
305
306 it("uses server update state for running progress", () => {
307 const view = getProviderUpdateProgressToastView({
308 providers: [
309 provider({
310 driver: driver("codex"),
311 updateState: {
312 status: "running",
313 startedAt: checkedAt,
314 finishedAt: null,
315 message: "Updating provider.",
316 output: null,
317 },
318 }),
319 ],
320 providerCount: 1,
321 });
322
323 expect(view).toMatchObject({
324 phase: "running",
325 type: "loading",
326 title: "Updating provider",
327 });
328 });
329
330 it("uses server failure state for failed progress", () => {
331 const view = getProviderUpdateProgressToastView({
332 providers: [
333 provider({
334 driver: driver("codex"),
335 updateState: {
336 status: "failed",
337 startedAt: checkedAt,
338 finishedAt: checkedAt,
339 message: "command failed",
340 output: "stderr",
341 },
342 }),
343 ],
344 providerCount: 1,
345 });
346
347 expect(view).toMatchObject({
348 phase: "failed",
349 type: "error",
350 title: "Provider update failed",
351 description: "command failed",
352 });
353 });
354
355 it("resolves a single-provider completion view from the returned provider snapshot", () => {
356 const view = getSingleProviderUpdateProgressToastView(
357 provider({
358 driver: driver("codex"),
359 updateState: {
360 status: "failed",
361 startedAt: checkedAt,
362 finishedAt: checkedAt,
363 message: "command failed",
364 output: "stderr",
365 },
366 }),
367 );
368
369 expect(view).toMatchObject({
370 phase: "failed",
371 type: "error",
372 title: "Codex v1.1.0 update failed",
373 description: "command failed",
374 });
375 });
376
377 it("keeps unchanged providers actionable from settings", () => {
378 const view = getProviderUpdateProgressToastView({
379 providers: [
380 provider({
381 driver: driver("cursor"),
382 updateState: {
383 status: "unchanged",
384 startedAt: checkedAt,
385 finishedAt: checkedAt,
386 message: "still old",
387 output: null,
388 },
389 }),
390 ],
391 providerCount: 1,
392 });
393
394 expect(view).toMatchObject({
395 phase: "unchanged",
396 type: "warning",
397 title: "Provider still needs an update",
398 description: "Cursor still appears outdated. Check provider settings for details.",
399 });
400 });
401
402 it("marks progress succeeded once every attempted provider is no longer outdated", () => {
403 const view = getProviderUpdateProgressToastView({
404 providers: [
405 provider({
406 driver: driver("codex"),
407 version: "1.1.0",
408 latestVersion: "1.1.0",
409 advisoryStatus: "current",
410 updateState: {
411 status: "succeeded",
412 startedAt: checkedAt,
413 finishedAt: checkedAt,
414 message: "Provider updated.",
415 output: null,
416 },
417 }),
418 ],
419 providerCount: 1,
420 });
421
422 expect(view).toMatchObject({
423 phase: "succeeded",
424 type: "success",
425 title: "Provider updated",
426 description: "New sessions will use the updated provider.",
427 dismissAfterVisibleMs: 3_000,
428 });
429 });
430
431 it("uses the updated version in the single-provider success toast title", () => {
432 const view = getSingleProviderUpdateProgressToastView(
433 provider({
434 driver: driver("codex"),
435 version: "1.1.0",
436 latestVersion: "1.1.0",
437 advisoryStatus: "current",
438 updateState: {
439 status: "succeeded",
440 startedAt: checkedAt,
441 finishedAt: checkedAt,
442 message: "Provider updated.",
443 output: null,
444 },
445 }),
446 );
447
448 expect(view).toMatchObject({
449 phase: "succeeded",
450 type: "success",
451 title: "Codex updated: v1.1.0",
452 description: "New sessions will use the updated provider.",
453 });
454 });
455
456 it("falls back to a rejected RPC message for transport-level failures", () => {
457 const results = [AsyncResult.failure(Cause.die(new Error("WebSocket closed")))];
458
459 expect(firstFailedProviderUpdateMessage(results)).toBe("WebSocket closed");
460 expect(getProviderUpdateRejectedToastView(2, "WebSocket closed")).toMatchObject({
461 phase: "failed",
462 title: "Provider updates failed",
463 description: "WebSocket closed",
464 });
465 });
466
467 it("collects only attempted provider snapshots from update responses", () => {
468 const codex = provider({ driver: driver("codex") });
469 const cursor = provider({ driver: driver("cursor") });
470 const results = [AsyncResult.success({ providers: [codex, cursor] })];
471
472 expect(
473 collectUpdatedProviderSnapshots({
474 results,
475 providerInstanceIds: new Set([cursor.instanceId]),
476 }),
477 ).toEqual([cursor]);
478 });
479
480 it("summarizes active provider updates for the sidebar pill", () => {
481 const view = getProviderUpdateSidebarPillView([
482 provider({
483 driver: driver("codex"),
484 updateState: {
485 status: "running",
486 startedAt: checkedAt,
487 finishedAt: null,
488 message: "Updating provider.",
489 output: null,
490 },
491 }),
492 provider({
493 driver: driver("cursor"),
494 updateState: {
495 status: "queued",
496 startedAt: null,
497 finishedAt: null,
498 message: "Waiting for another provider update to finish.",
499 output: null,
500 },
501 }),
502 ]);
503
504 expect(view).toMatchObject({
505 tone: "loading",
506 title: "Updating 2 providers",
507 description: "Codex and Cursor updates are in progress.",
508 });
509 });
510
511 it("uses the provider name for single active sidebar pill updates", () => {
512 const view = getProviderUpdateSidebarPillView([
513 provider({
514 driver: driver("codex"),
515 updateState: {
516 status: "running",
517 startedAt: checkedAt,
518 finishedAt: null,
519 message: "Updating provider.",
520 output: null,
521 },
522 }),
523 ]);
524
525 expect(view).toMatchObject({
526 key: "loading:codex:running",
527 tone: "loading",
528 title: "Updating Codex",
529 description: "Codex update in progress.",
530 });
531 });
532
533 it("uses the provider name for single failed sidebar pill updates", () => {
534 const view = getProviderUpdateSidebarPillView(
535 [
536 provider({
537 driver: driver("claudeAgent"),
538 updateState: {
539 status: "failed",
540 startedAt: checkedAt,
541 finishedAt: checkedAt,
542 message: "Update command exited with code 1.",
543 output: null,
544 },
545 }),
546 ],
547 { visibleAfterIso: sessionStartedAt },
548 );
549
550 expect(view).toMatchObject({
551 key: "failed:claudeAgent:2026-04-23T10:00:00.000Z:Update command exited with code 1.",
552 tone: "error",
553 title: "Claude v1.1.0 update failed",
554 description: "Update command exited with code 1.",
555 dismissible: true,
556 });
557 });
558
559 it("shows a short-lived success sidebar pill after a single provider update succeeds", () => {
560 const view = getProviderUpdateSidebarPillView(
561 [
562 provider({
563 driver: driver("codex"),
564 version: "1.1.0",
565 latestVersion: "1.1.0",
566 advisoryStatus: "current",
567 updateState: {
568 status: "succeeded",
569 startedAt: checkedAt,
570 finishedAt: checkedAt,
571 message: "Provider updated.",
572 output: null,
573 },
574 }),
575 ],
576 { visibleAfterIso: sessionStartedAt },
577 );
578
579 expect(view).toMatchObject({
580 key: "succeeded:codex:2026-04-23T10:00:00.000Z:Provider updated.",
581 tone: "success",
582 title: "Codex updated: v1.1.0",
583 description: "New sessions will use the updated provider.",
584 dismissAfterVisibleMs: 3_000,
585 });
586 });
587
588 it("keeps unchanged sidebar pill states dismissible", () => {
589 const view = getProviderUpdateSidebarPillView(
590 [
591 provider({
592 driver: driver("cursor"),
593 updateState: {
594 status: "unchanged",
595 startedAt: checkedAt,
596 finishedAt: checkedAt,
597 message: "still old",
598 output: null,
599 },
600 }),
601 ],
602 { visibleAfterIso: sessionStartedAt },
603 );
604
605 expect(view).toMatchObject({
606 key: "unchanged:cursor:2026-04-23T10:00:00.000Z:still old",
607 tone: "warning",
608 title: "Cursor still needs an update",
609 dismissible: true,
610 });
611 });
612
613 it("does not show sidebar terminal states from before the current app session", () => {
614 expect(
615 getProviderUpdateSidebarPillView(
616 [
617 provider({
618 driver: driver("codex"),
619 updateState: {
620 status: "failed",
621 startedAt: checkedAt,
622 finishedAt: checkedAt,
623 message: "command failed",
624 output: "stderr",
625 },
626 }),
627 ],
628 { visibleAfterIso: "2026-04-23T10:00:01.000Z" },
629 ),
630 ).toBeNull();
631 });
632
633 it("shows a newer success before falling back to an older failure", () => {
634 const providers = [
635 provider({
636 driver: driver("claudeAgent"),
637 updateState: {
638 status: "failed",
639 startedAt: checkedAt,
640 finishedAt: checkedAt,
641 message: "Update command exited with code 1.",
642 output: null,
643 },
644 }),
645 provider({
646 driver: driver("codex"),
647 version: "1.2.0",
648 latestVersion: "1.2.0",
649 advisoryStatus: "current",
650 updateState: {
651 status: "succeeded",
652 startedAt: laterCheckedAt,
653 finishedAt: laterCheckedAt,
654 message: "Provider updated.",
655 output: null,
656 },
657 }),
658 ] satisfies ReadonlyArray<ServerProvider>;
659
660 const successView = getProviderUpdateSidebarPillView(providers, {
661 visibleAfterIso: sessionStartedAt,
662 });
663 expect(successView).toMatchObject({
664 key: "succeeded:codex:2026-04-23T10:01:00.000Z:Provider updated.",
665 tone: "success",
666 title: "Codex updated: v1.2.0",
667 });
668
669 const failureView = getProviderUpdateSidebarPillView(providers, {
670 visibleAfterIso: sessionStartedAt,
671 dismissedKeys: new Set(["succeeded:codex:2026-04-23T10:01:00.000Z:Provider updated."]),
672 });
673 expect(failureView).toMatchObject({
674 key: "failed:claudeAgent:2026-04-23T10:00:00.000Z:Update command exited with code 1.",
675 tone: "error",
676 title: "Claude v1.1.0 update failed",
677 });
678 });
679
680 it("does not show a sidebar pill for passive update availability", () => {
681 expect(
682 getProviderUpdateSidebarPillView([
683 provider({ driver: driver("codex"), canUpdate: true }),
684 provider({ driver: driver("cursor"), canUpdate: false }),
685 ]),
686 ).toBeNull();
687 });
688
689 describe("multi-backend update outcomes", () => {
690 const terminalState = (
691 status: "succeeded" | "failed" | "unchanged",
692 message: string,
693 ): NonNullable<ServerProvider["updateState"]> => ({
694 status,
695 startedAt: checkedAt,
696 finishedAt: checkedAt,
697 message,
698 output: null,
699 });
700
701 const fulfilledOutcome = (
702 isPrimary: boolean,
703 snapshot: ServerProvider | null,
704 environment = "env",
705 ): PromiseSettledResult<LocalProviderUpdateOutcome> => ({
706 status: "fulfilled",
707 value: {
708 environmentId: environment as LocalProviderUpdateOutcome["environmentId"],
709 isPrimary,
710 driver: snapshot?.driver ?? driver("codex"),
711 instanceId: snapshot?.instanceId ?? instanceId("codex"),
712 provider: snapshot,
713 },
714 });
715
716 it("surfaces a secondary backend's failed update over the primary's success", () => {
717 const snapshots = collectProviderUpdateOutcomeSnapshots([
718 fulfilledOutcome(
719 true,
720 provider({
721 driver: driver("codex"),
722 updateState: terminalState("succeeded", "Provider updated."),
723 }),
724 ),
725 fulfilledOutcome(
726 false,
727 provider({
728 driver: driver("codex"),
729 updateState: terminalState("failed", "npm: NotFound"),
730 }),
731 ),
732 ]);
733
734 expect(snapshots).toHaveLength(1);
735 expect(snapshots[0]?.updateState?.status).toBe("failed");
736 expect(
737 getProviderUpdateProgressToastView({ providers: snapshots, providerCount: 1 }),
738 ).toMatchObject({ phase: "failed" });
739 });
740
741 it("surfaces a secondary backend that stayed outdated over the primary's success", () => {
742 const snapshots = collectProviderUpdateOutcomeSnapshots([
743 fulfilledOutcome(
744 true,
745 provider({
746 driver: driver("codex"),
747 updateState: terminalState("succeeded", "Provider updated."),
748 }),
749 ),
750 fulfilledOutcome(
751 false,
752 provider({
753 driver: driver("codex"),
754 updateState: terminalState("unchanged", "still outdated"),
755 }),
756 ),
757 ]);
758
759 expect(snapshots[0]?.updateState?.status).toBe("unchanged");
760 expect(
761 getProviderUpdateProgressToastView({ providers: snapshots, providerCount: 1 }),
762 ).toMatchObject({ phase: "unchanged" });
763 });
764
765 it("reports success only when every backend succeeded", () => {
766 const snapshots = collectProviderUpdateOutcomeSnapshots([
767 fulfilledOutcome(
768 true,
769 provider({
770 driver: driver("codex"),
771 updateState: terminalState("succeeded", "Provider updated."),
772 }),
773 ),
774 fulfilledOutcome(
775 false,
776 provider({
777 driver: driver("codex"),
778 updateState: terminalState("succeeded", "Provider updated."),
779 }),
780 ),
781 ]);
782
783 expect(
784 getProviderUpdateProgressToastView({ providers: snapshots, providerCount: 1 }),
785 ).toMatchObject({ phase: "succeeded" });
786 });
787
788 it("ignores backends that did not return the targeted instance", () => {
789 const primary = provider({
790 driver: driver("codex"),
791 updateState: terminalState("succeeded", "Provider updated."),
792 });
793 const snapshots = collectProviderUpdateOutcomeSnapshots([
794 fulfilledOutcome(true, primary),
795 fulfilledOutcome(false, null),
796 ]);
797
798 expect(snapshots).toEqual([primary]);
799 });
800
801 it("flags the first unsuccessful secondary outcome, skipping the primary and successes", () => {
802 const primaryFailed = provider({
803 driver: driver("codex"),
804 updateState: terminalState("failed", "primary boom"),
805 });
806
807 expect(
808 firstUnsuccessfulSecondaryProviderOutcome([
809 fulfilledOutcome(true, primaryFailed),
810 fulfilledOutcome(
811 false,
812 provider({
813 driver: driver("codex"),
814 updateState: terminalState("succeeded", "ok"),
815 }),
816 ),
817 ]),
818 ).toBeNull();
819
820 expect(
821 firstUnsuccessfulSecondaryProviderOutcome([
822 fulfilledOutcome(true, primaryFailed),
823 fulfilledOutcome(
824 false,
825 provider({
826 driver: driver("codex"),
827 updateState: terminalState("failed", "wsl boom"),
828 }),
829 ),
830 ]),
831 ).toMatchObject({ status: "failed", provider: { updateState: { message: "wsl boom" } } });
832 });
833
834 it("treats a rejected dispatch as not contributing a snapshot", () => {
835 const primary = provider({
836 driver: driver("codex"),
837 updateState: terminalState("succeeded", "Provider updated."),
838 });
839 const results: PromiseSettledResult<LocalProviderUpdateOutcome>[] = [
840 fulfilledOutcome(true, primary),
841 { status: "rejected", reason: new Error("WebSocket closed") },
842 ];
843
844 expect(collectProviderUpdateOutcomeSnapshots(results)).toEqual([primary]);
845 expect(firstRejectedProviderUpdateMessage(results)).toBe("WebSocket closed");
846 });
847 });
848
849 describe("per-environment update grouping", () => {
850 const environment = (
851 input: {
852 readonly environmentId: string;
853 readonly providers: ReadonlyArray<ServerProvider>;
854 } & Partial<Omit<LocalEnvironmentProvidersInput, "environmentId" | "providers">>,
855 ): LocalEnvironmentProvidersInput => ({
856 environmentId: input.environmentId as EnvironmentId,
857 label: input.label ?? input.environmentId,
858 isPrimary: input.isPrimary ?? false,
859 connectionState: input.connectionState ?? "ready",
860 providers: input.providers,
861 });
862
863 it("groups each environment's outdated one-click candidates", () => {
864 const result = buildLocalEnvironmentUpdateGroups([
865 environment({
866 environmentId: "env-windows",
867 label: "Windows",
868 isPrimary: true,
869 providers: [provider({ driver: driver("codex"), latestVersion: "1.1.0" })],
870 }),
871 environment({
872 environmentId: "env-wsl",
873 label: "WSL",
874 providers: [provider({ driver: driver("codex"), latestVersion: "1.1.0" })],
875 }),
876 ]);
877
878 expect(result.isAnySettling).toBe(false);
879 expect(result.groups.map((group) => group.label)).toEqual(["Windows", "WSL"]);
880 expect(result.groups.every((group) => group.candidates.length === 1)).toBe(true);
881 });
882
883 it("flags settling while a secondary backend is still connecting", () => {
884 const result = buildLocalEnvironmentUpdateGroups([
885 environment({
886 environmentId: "env-windows",
887 isPrimary: true,
888 providers: [provider({ driver: driver("codex") })],
889 }),
890 environment({ environmentId: "env-wsl", connectionState: "connecting", providers: [] }),
891 ]);
892
893 expect(result.isAnySettling).toBe(true);
894 expect(
895 result.groups.find((group) => group.environmentId === ("env-wsl" as EnvironmentId))
896 ?.isSettling,
897 ).toBe(true);
898 });
899
900 it("keeps only environments that have a one-click update on offer", () => {
901 const { groups } = buildLocalEnvironmentUpdateGroups([
902 environment({
903 environmentId: "env-windows",
904 isPrimary: true,
905 providers: [provider({ driver: driver("codex") })],
906 }),
907 environment({
908 environmentId: "env-wsl",
909 providers: [
910 provider({ driver: driver("codex"), advisoryStatus: "current", latestVersion: null }),
911 ],
912 }),
913 ]);
914
915 expect(environmentGroupsWithUpdates(groups).map((group) => group.environmentId)).toEqual([
916 "env-windows",
917 ]);
918 });
919
920 it("keys the notification by environment, driver and latest version", () => {
921 const noUpdates = buildLocalEnvironmentUpdateGroups([
922 environment({
923 environmentId: "env-windows",
924 isPrimary: true,
925 providers: [
926 provider({ driver: driver("codex"), advisoryStatus: "current", latestVersion: null }),
927 ],
928 }),
929 ]);
930 expect(localEnvironmentUpdateNotificationKey(noUpdates.groups)).toBeNull();
931
932 const both = buildLocalEnvironmentUpdateGroups([
933 environment({
934 environmentId: "env-windows",
935 isPrimary: true,
936 providers: [provider({ driver: driver("codex"), latestVersion: "1.1.0" })],
937 }),
938 environment({
939 environmentId: "env-wsl",
940 providers: [provider({ driver: driver("codex"), latestVersion: "1.1.0" })],
941 }),
942 ]);
943 const key = localEnvironmentUpdateNotificationKey(both.groups);
944 expect(key).toContain("env-windows=codex:1.1.0");
945 expect(key).toContain("env-wsl=codex:1.1.0");
946 });
947
948 it("labels environments by platform so they are distinguishable", () => {
949 expect(
950 deriveEnvironmentDisplayLabel({
951 isWsl: false,
952 wslDistro: null,
953 platformOs: "windows",
954 fallbackLabel: "Jgratton24",
955 }),
956 ).toBe("Windows");
957 expect(
958 deriveEnvironmentDisplayLabel({
959 isWsl: true,
960 wslDistro: null,
961 platformOs: "linux",
962 fallbackLabel: "Jgratton24",
963 }),
964 ).toBe("WSL");
965 expect(
966 deriveEnvironmentDisplayLabel({
967 isWsl: true,
968 wslDistro: "ubuntu",
969 platformOs: "linux",
970 fallbackLabel: "Jgratton24",
971 }),
972 ).toBe("WSL · ubuntu");
973 expect(
974 deriveEnvironmentDisplayLabel({
975 isWsl: false,
976 wslDistro: null,
977 platformOs: undefined,
978 fallbackLabel: "My Device",
979 }),
980 ).toBe("My Device");
981 });
982
983 it("parses the WSL distro from the backend instance id", () => {
984 expect(parseWslDistroFromInstanceId("wsl:ubuntu")).toBe("ubuntu");
985 expect(parseWslDistroFromInstanceId("wsl:default")).toBeNull();
986 expect(parseWslDistroFromInstanceId("wsl:")).toBeNull();
987 expect(parseWslDistroFromInstanceId("ssh:host")).toBeNull();
988 expect(parseWslDistroFromInstanceId(undefined)).toBeNull();
989 });
990 });
991
992 describe("isTerminalProviderUpdatePhase", () => {
993 it("treats succeeded/failed/unchanged as terminal", () => {
994 expect(isTerminalProviderUpdatePhase("succeeded")).toBe(true);
995 expect(isTerminalProviderUpdatePhase("failed")).toBe(true);
996 expect(isTerminalProviderUpdatePhase("unchanged")).toBe(true);
997 });
998
999 it("treats running/initial as non-terminal so they are not persisted", () => {
1000 // The per-environment update row uses this to decide what to store. A
1001 // "running"/"initial" snapshot never re-polls, so persisting it would pin
1002 // the row's spinner forever once pending clears (see the
1003 // resolveEnvironmentUpdateRowStatus "keeps a non-terminal result on
1004 // loading even after pending clears" case). Dropping these lets the live
1005 // per-environment provider state drive the row so it self-heals.
1006 expect(isTerminalProviderUpdatePhase("running")).toBe(false);
1007 expect(isTerminalProviderUpdatePhase("initial")).toBe(false);
1008 });
1009 });
1010
1011 describe("resolveEnvironmentUpdateRowStatus", () => {
1012 const group: LocalEnvironmentUpdateGroup = {
1013 environmentId: "env-wsl" as EnvironmentId,
1014 label: "WSL",
1015 isPrimary: false,
1016 isSettling: false,
1017 candidates: [updateCandidate({ driver: driver("codex"), latestVersion: "1.1.0" })],
1018 providers: [],
1019 };
1020 const runningResult: ProviderUpdateToastView = {
1021 phase: "running",
1022 type: "loading",
1023 title: "Updating providers",
1024 description: "Running provider update command.",
1025 };
1026 const succeededResult: ProviderUpdateToastView = {
1027 phase: "succeeded",
1028 type: "success",
1029 title: "Provider updated",
1030 description: "New sessions will use the updated provider.",
1031 };
1032 const successPill: ProviderUpdateSidebarPillView = {
1033 key: "succeeded:codex",
1034 tone: "success",
1035 title: "Codex updated",
1036 description: "New sessions will use the updated provider.",
1037 };
1038
1039 it("prefers a transport error", () => {
1040 expect(
1041 resolveEnvironmentUpdateRowStatus({
1042 group,
1043 error: "boom",
1044 result: succeededResult,
1045 pill: successPill,
1046 isPending: true,
1047 }),
1048 ).toMatchObject({ kind: "failed", text: "boom" });
1049 });
1050
1051 it("uses a terminal result snapshot", () => {
1052 expect(
1053 resolveEnvironmentUpdateRowStatus({
1054 group,
1055 error: undefined,
1056 result: succeededResult,
1057 pill: null,
1058 isPending: false,
1059 }),
1060 ).toMatchObject({ kind: "success" });
1061 });
1062
1063 it("falls through a non-terminal result to live server state", () => {
1064 // The dispatch snapshot is still "running", but server state already
1065 // reports success — the row must not stay pinned on "Updating…".
1066 expect(
1067 resolveEnvironmentUpdateRowStatus({
1068 group,
1069 error: undefined,
1070 result: runningResult,
1071 pill: successPill,
1072 isPending: true,
1073 }),
1074 ).toMatchObject({ kind: "success" });
1075 });
1076
1077 it("shows the pending spinner before any signal arrives", () => {
1078 expect(
1079 resolveEnvironmentUpdateRowStatus({
1080 group,
1081 error: undefined,
1082 result: runningResult,
1083 pill: null,
1084 isPending: true,
1085 }),
1086 ).toMatchObject({ kind: "loading" });
1087 });
1088
1089 it("keeps a non-terminal result on loading even after pending clears", () => {
1090 // The dispatch returned an incomplete ("running") snapshot and pending was
1091 // cleared in finally — the row must not revert to the idle Update button.
1092 expect(
1093 resolveEnvironmentUpdateRowStatus({
1094 group,
1095 error: undefined,
1096 result: runningResult,
1097 pill: null,
1098 isPending: false,
1099 }),
1100 ).toMatchObject({ kind: "loading" });
1101 });
1102
1103 it("lists the providers when idle", () => {
1104 expect(
1105 resolveEnvironmentUpdateRowStatus({
1106 group,
1107 error: undefined,
1108 result: undefined,
1109 pill: null,
1110 isPending: false,
1111 }),
1112 ).toMatchObject({ kind: "idle", text: "Codex" });
1113 });
1114 });
1115});