Open History to see the last commit
1import { EnvironmentId, type ServerConfig } from "@t3tools/contracts";
2import { Atom } from "effect/reactivity";
3import { afterEach, describe, expect, it, vi } from "vite-plus/test";
4
5import { appAtomRegistry } from "~/rpc/atomRegistry";
6import { serverEnvironment } from "~/state/server";
7import type { ServerUpdateOutcome, ServerUpdateTarget } from "../ServerUpdateAction";
8import type { UpdateEverythingRun } from "./updateEverything.logic";
9import {
10 retryFailedUpdateRunServers,
11 setUpdateEverythingRun,
12 startDesktopUpdateLaunchNotice,
13} from "./updateEverythingRun";
14
15function relaunchWhileRestarting(environmentId: EnvironmentId) {
16 const now = 1_800_000_000_000;
17 vi.useFakeTimers({ now });
18 const store = new Map([
19 [
20 "t3code:update-everything-result:v1",
21 JSON.stringify({
22 savedAt: now,
23 localVersion: null,
24 updatedServers: 0,
25 updatedProviders: 0,
26 failures: [],
27 handedOff: [{ environmentId, label: "Slow", targetVersion: "9.9.9", startedAt: now }],
28 }),
29 ],
30 ]);
31 vi.stubGlobal("window", {
32 localStorage: {
33 getItem: (key: string) => store.get(key) ?? null,
34 removeItem: (key: string) => store.delete(key),
35 },
36 });
37 return startDesktopUpdateLaunchNotice();
38}
39
40describe("startDesktopUpdateLaunchNotice", () => {
41 afterEach(() => {
42 vi.useRealTimers();
43 vi.unstubAllGlobals();
44 });
45
46 it("keeps a server that is still restarting from looking idle after the app relaunches", async () => {
47 const environmentId = EnvironmentId.make("slow-machine");
48 const stop = relaunchWhileRestarting(environmentId);
49 try {
50 // Lets the registry run its cleanup of unobserved atoms before the UI mounts.
51 await vi.advanceTimersByTimeAsync(1_000);
52 expect(appAtomRegistry.get(serverEnvironment.updateStateAtom(environmentId))).toMatchObject({
53 status: "running",
54 stage: "resuming",
55 targetVersion: "9.9.9",
56 });
57 } finally {
58 await vi.advanceTimersByTimeAsync(32 * 60 * 1000);
59 stop();
60 }
61 });
62
63 it("clears the restored state when the server never comes back", async () => {
64 const environmentId = EnvironmentId.make("lost-machine");
65 const stateAtom = serverEnvironment.updateStateAtom(environmentId);
66 const stop = relaunchWhileRestarting(environmentId);
67 // Observed like the UI does, so the registry cannot reset it by dropping the atom.
68 const release = appAtomRegistry.mount(stateAtom);
69 try {
70 await vi.advanceTimersByTimeAsync(32 * 60 * 1000);
71 expect(appAtomRegistry.get(stateAtom)).toEqual({ status: "idle" });
72 } finally {
73 release();
74 stop();
75 }
76 });
77});
78
79describe("retryFailedUpdateRunServers", () => {
80 it("settles a failed server that already restarted on the target and retries only the others", () => {
81 const targetVersion = "9.9.9";
82 const serverVersions = new Map([
83 [EnvironmentId.make("already-updated"), targetVersion],
84 [EnvironmentId.make("behind"), "9.9.8"],
85 ]);
86 using _config = vi
87 .spyOn(serverEnvironment, "configValueAtom")
88 .mockImplementation((environmentId) =>
89 Atom.make({
90 environment: { serverVersion: serverVersions.get(environmentId!) },
91 } as unknown as ServerConfig),
92 );
93 const rejection = "Remote updates must select a newer server version.";
94 const failedMachine = (environmentId: EnvironmentId) => ({
95 environmentId,
96 label: environmentId,
97 updatesProviders: false,
98 server: {
99 environmentId,
100 serverLabel: environmentId,
101 selfUpdate: "boot-service" as const,
102 targetVersion,
103 },
104 handedOffVersion: null,
105 startedAt: 0,
106 result: "failed" as const,
107 failure: rejection,
108 providerFailure: null,
109 });
110 setUpdateEverythingRun(() => ({
111 localVersion: null,
112 phase: "servers",
113 restartAt: null,
114 machines: [...serverVersions.keys()].map(failedMachine),
115 }));
116 const update = vi.fn(
117 (_target: ServerUpdateTarget) => new Promise<ServerUpdateOutcome>(() => {}),
118 );
119
120 retryFailedUpdateRunServers(update);
121
122 let run: UpdateEverythingRun | null = null;
123 setUpdateEverythingRun((current) => {
124 run = current;
125 return null;
126 });
127 expect(update.mock.calls.map(([target]) => target.environmentId)).toEqual(["behind"]);
128 expect(
129 (run as UpdateEverythingRun | null)?.machines.map(({ environmentId, result, server }) => ({
130 environmentId,
131 result,
132 retryable: server !== null,
133 })),
134 ).toEqual([
135 { environmentId: "already-updated", result: "done", retryable: false },
136 { environmentId: "behind", result: null, retryable: true },
137 ]);
138 });
139});