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

ProviderUpdateLaunchNotification.logic.test.ts

1,040 lines32.3 KB

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