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