Open History to see the last commit
1import { EnvironmentId, type ServerSelfUpdateCapability } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3
4import {
5 collectRestartingMachines,
6 collectServerUpdateTargets,
7 closeFinishedUpdateRun,
8 collectUpdateEverythingResult,
9 describeUpdateCapsule,
10 describeUpdateEverything,
11 getUpdateEverythingToastView,
12 resolveUpdateRunMachineStatus,
13 runUpdateEverything,
14 type UpdateEverythingMachine,
15 type UpdateEverythingRun,
16 type UpdateRunMachine,
17 type UpdateRunMachineStatus,
18 UPDATE_SLOW_AFTER_MS,
19} from "./updateEverything.logic";
20
21const runMachine = (overrides: Partial<UpdateRunMachine> = {}): UpdateRunMachine => ({
22 environmentId: EnvironmentId.make("alvin"),
23 label: "alvin",
24 updatesProviders: true,
25 server: {
26 environmentId: EnvironmentId.make("alvin"),
27 serverLabel: "alvin",
28 selfUpdate: "boot-service",
29 targetVersion: "0.0.50",
30 },
31 handedOffVersion: null,
32 startedAt: 0,
33 result: null,
34 failure: null,
35 providerFailure: null,
36 ...overrides,
37});
38
39function machine(
40 id: string,
41 serverVersion: string,
42 selfUpdate: ServerSelfUpdateCapability | undefined,
43 overrides: Partial<UpdateEverythingMachine> = {},
44): UpdateEverythingMachine {
45 return {
46 environmentId: EnvironmentId.make(id),
47 label: id,
48 serverConfig: {
49 environment: {
50 environmentId: EnvironmentId.make(id),
51 label: id,
52 platform: { os: "linux", arch: "x64" },
53 serverVersion,
54 capabilities: {
55 repositoryIdentity: true,
56 ...(selfUpdate ? { serverSelfUpdate: selfUpdate } : {}),
57 ...(selfUpdate === "desktop-managed" ? { desktopAppUpdate: true } : {}),
58 },
59 },
60 settings: { continueThreadsAfterServerUpdate: false },
61 },
62 connected: true,
63 hostedByThisApp: false,
64 canMaintain: true,
65 updateState: { status: "idle" },
66 ...overrides,
67 };
68}
69
70describe("collectServerUpdateTargets", () => {
71 it("updates remote servers that can move to the target and names every skipped one", () => {
72 const { targets, skipped } = collectServerUpdateTargets(
73 [
74 machine("this-app", "0.0.40", "desktop-managed", { hostedByThisApp: true }),
75 machine("service", "0.0.40", "boot-service"),
76 machine("nightly-service", "0.0.49-nightly.20261001.3", "boot-service"),
77 machine("other-desktop", "0.0.49-nightly.20261001.3", "desktop-managed"),
78 machine("offline", "0.0.40", "boot-service", { connected: false }),
79 machine("never-connected", "0.0.40", "boot-service", { serverConfig: null }),
80 machine("no-scope", "0.0.40", "boot-service", { canMaintain: false }),
81 machine("busy", "0.0.40", "boot-service", {
82 updateState: {
83 status: "running",
84 stage: "downloading",
85 fromVersion: "0.0.40",
86 targetVersion: "0.0.50",
87 },
88 }),
89 machine("current", "0.0.50", "boot-service"),
90 machine("current-offline", "0.0.50", "boot-service", { connected: false }),
91 machine("manual", "0.0.40", undefined),
92 ],
93 "0.0.50",
94 );
95
96 expect(targets.map((target) => [target.serverLabel, target.targetVersion])).toEqual([
97 ["service", "0.0.50"],
98 ["other-desktop", "0.0.50"],
99 ]);
100 expect(skipped.map((entry) => [entry.label, entry.reason])).toEqual([
101 ["nightly-service", "on the nightly channel"],
102 ["offline", "not connected"],
103 ["never-connected", "not connected"],
104 ["no-scope", "no permission"],
105 ["busy", "already updating"],
106 ["manual", "needs a manual update"],
107 ]);
108 });
109});
110
111describe("collectRestartingMachines", () => {
112 it("lists updated servers, and this app's servers only when it installs", () => {
113 const withContinuation = (
114 id: string,
115 overrides: Partial<UpdateEverythingMachine> = {},
116 enabled = false,
117 ) => {
118 const base = machine(id, "0.0.40", "boot-service", overrides);
119 return {
120 ...base,
121 serverConfig: {
122 environment: {
123 ...base.serverConfig!.environment,
124 capabilities: {
125 ...base.serverConfig!.environment.capabilities,
126 threadRestartContinuation: true,
127 },
128 },
129 settings: { continueThreadsAfterServerUpdate: enabled },
130 },
131 };
132 };
133 const machines = [
134 withContinuation("this-app", { hostedByThisApp: true }, true),
135 withContinuation("alvin", {}, true),
136 withContinuation("opted-out", {}, false),
137 machine("old", "0.0.40", "boot-service"),
138 withContinuation("providers-only"),
139 ];
140 const servers = ["alvin", "opted-out", "old"].map((id) => ({
141 environmentId: EnvironmentId.make(id),
142 }));
143
144 expect(collectRestartingMachines(machines, servers, true)).toEqual([
145 { environmentId: "this-app", continuesThreads: true },
146 { environmentId: "alvin", continuesThreads: true },
147 { environmentId: "opted-out", continuesThreads: false },
148 { environmentId: "old", continuesThreads: false },
149 ]);
150 expect(
151 collectRestartingMachines(machines, servers, false).map((entry) => entry.environmentId),
152 ).toEqual(["alvin", "opted-out", "old"]);
153 });
154});
155
156describe("runUpdateEverything", () => {
157 it("restarts servers after providers finish and returns every result", async () => {
158 const calls: string[] = [];
159 let finishProviders = (_value: string) => {};
160 const run = runUpdateEverything({
161 downloadLocal: async () => {
162 calls.push("download");
163 return true;
164 },
165 updateProviders: () =>
166 new Promise<string>((resolve) => {
167 calls.push("providers");
168 finishProviders = resolve;
169 }),
170 updateServers: async () => {
171 calls.push("servers");
172 return 2;
173 },
174 });
175
176 await new Promise((resolve) => setTimeout(resolve, 0));
177 expect(calls).toEqual(["download", "providers"]);
178 finishProviders("provider results");
179 expect(await run).toEqual({ localReady: true, providers: "provider results", servers: 2 });
180 expect(calls).toEqual(["download", "providers", "servers"]);
181 });
182
183 it("still updates remote machines when the local download fails", async () => {
184 const calls: string[] = [];
185 const result = await runUpdateEverything({
186 downloadLocal: async () => false,
187 updateProviders: async () => {
188 calls.push("providers");
189 },
190 updateServers: async () => {
191 calls.push("servers");
192 },
193 });
194
195 expect(calls).toEqual(["providers", "servers"]);
196 expect(result.localReady).toBe(false);
197 });
198});
199
200describe("resolveUpdateRunMachineStatus", () => {
201 const running = (stage: "downloading" | "installing" | "resuming") =>
202 ({ status: "running", stage, fromVersion: "0.0.40", targetVersion: "0.0.50" }) as const;
203
204 it("follows the run phase, then the server's own update stage", () => {
205 const idle = { status: "idle" } as const;
206 expect(resolveUpdateRunMachineStatus(runMachine(), "providers", idle)).toBe("providers");
207 expect(resolveUpdateRunMachineStatus(runMachine(), "servers", idle)).toBe("waiting");
208 expect(resolveUpdateRunMachineStatus(runMachine(), "servers", running("installing"))).toBe(
209 "downloading",
210 );
211 expect(resolveUpdateRunMachineStatus(runMachine(), "servers", running("resuming"))).toBe(
212 "restarting",
213 );
214 expect(
215 resolveUpdateRunMachineStatus(
216 runMachine({ updatesProviders: false }),
217 "providers",
218 running("downloading"),
219 ),
220 ).toBe("downloading");
221 expect(resolveUpdateRunMachineStatus(runMachine({ result: "failed" }), "servers", idle)).toBe(
222 "failed",
223 );
224 // After this app relaunched, a handed-off server has no live update state.
225 expect(
226 resolveUpdateRunMachineStatus(
227 runMachine({ server: null, handedOffVersion: "0.0.50" }),
228 "servers",
229 idle,
230 ),
231 ).toBe("restarting");
232 });
233});
234
235describe("update capsule", () => {
236 const run = (overrides: Partial<UpdateEverythingRun> = {}): UpdateEverythingRun => ({
237 localVersion: null,
238 phase: "servers",
239 restartAt: null,
240 machines: [],
241 ...overrides,
242 });
243 const view = (
244 label: string,
245 status: UpdateRunMachineStatus,
246 overrides: Partial<UpdateRunMachine> = {},
247 ) => ({
248 machine: runMachine({ environmentId: EnvironmentId.make(label), label, ...overrides }),
249 status,
250 });
251
252 it("counts down this app's restart before anything else", () => {
253 expect(
254 describeUpdateCapsule(
255 run({ phase: "countdown", restartAt: 5_000, localVersion: "0.0.50" }),
256 [view("alvin", "restarting")],
257 { status: "waiting" },
258 1_200,
259 ),
260 ).toEqual({ tone: "default", label: "Restart in 4s", action: "later" });
261 });
262
263 it("names the slowest machine once it passes the slow threshold", () => {
264 const machines = [
265 view("bb-1", "done"),
266 view("zbook", "downloading", { startedAt: 60_000 }),
267 view("alvin", "restarting", { startedAt: 0 }),
268 ];
269 expect(describeUpdateCapsule(run(), machines, null, 60_000)).toEqual({
270 tone: "default",
271 label: "1 of 3 updated",
272 action: null,
273 });
274 expect(describeUpdateCapsule(run(), machines, null, UPDATE_SLOW_AFTER_MS + 6 * 60_000)).toEqual(
275 {
276 tone: "slow",
277 label: "alvin",
278 slow: { status: "restarting", elapsed: "9m" },
279 action: null,
280 },
281 );
282 });
283
284 it("offers a retry only for failed servers it can update again", () => {
285 expect(describeUpdateCapsule(run(), [view("alvin", "failed")], null, 0)).toEqual({
286 tone: "failed",
287 label: "alvin failed",
288 action: "retry",
289 });
290 expect(
291 describeUpdateCapsule(
292 run(),
293 [view("alvin", "failed", { server: null }), view("box", "failed", { server: null })],
294 null,
295 0,
296 ),
297 ).toEqual({ tone: "failed", label: "2 failed", action: "dismiss" });
298 });
299
300 it("ends the run only when every machine is done and this app is not installing", () => {
301 const done = runMachine({ result: "done" });
302 expect(closeFinishedUpdateRun(run({ machines: [done] }))).toBeNull();
303 expect(
304 closeFinishedUpdateRun(run({ machines: [done], localVersion: "0.0.50" })),
305 ).not.toBeNull();
306 expect(
307 closeFinishedUpdateRun(run({ machines: [runMachine({ result: "failed" })] })),
308 ).not.toBeNull();
309 expect(closeFinishedUpdateRun(run({ machines: [done], phase: "providers" }))).not.toBeNull();
310 });
311});
312
313describe("update everything summary", () => {
314 const result = collectUpdateEverythingResult({
315 localVersion: "0.0.50",
316 localFailures: ["Could not download update: Network error."],
317 servers: [
318 { label: "alvin", outcome: { status: "failed", message: "Download failed." } },
319 { label: "box", outcome: { status: "updated" } },
320 { label: "studio", outcome: { status: "updated" } },
321 { label: "busy", outcome: { status: "skipped" } },
322 ],
323 providers: [
324 { status: "updated" },
325 { status: "interrupted" },
326 { status: "failed", line: "box · Codex: npm exited with code 1." },
327 ],
328 });
329
330 it("counts updates and names the machine of each failure", () => {
331 expect(result).toEqual({
332 localVersion: "0.0.50",
333 updatedServers: 2,
334 updatedProviders: 1,
335 restarting: [],
336 failures: [
337 "This app: Could not download update: Network error.",
338 "alvin: Download failed.",
339 "box · Codex: npm exited with code 1.",
340 ],
341 });
342 });
343
344 it("includes this app only when the relaunch landed on its version", () => {
345 expect(getUpdateEverythingToastView(result, "0.0.50")).toEqual({
346 type: "error",
347 title: "Updated this app, 2 servers, and 1 provider",
348 description: [
349 "This app: Could not download update: Network error.",
350 "alvin: Download failed.",
351 "box · Codex: npm exited with code 1.",
352 ].join("\n"),
353 });
354 expect(getUpdateEverythingToastView({ ...result, failures: [] }, "0.0.40")).toEqual({
355 type: "success",
356 title: "Updated 2 servers and 1 provider",
357 });
358 });
359
360 it("reports a failure when nothing updated, and nothing when nothing finished", () => {
361 const empty = { localVersion: null, updatedServers: 0, updatedProviders: 0, restarting: [] };
362 expect(
363 getUpdateEverythingToastView({ ...empty, failures: ["alvin: offline"] }, "0.0.50"),
364 ).toEqual({ type: "error", title: "Update failed", description: "alvin: offline" });
365 expect(getUpdateEverythingToastView({ ...empty, failures: [] }, "0.0.50")).toBeNull();
366 });
367
368 it("names servers still restarting when this app relaunched", () => {
369 expect(
370 getUpdateEverythingToastView(
371 { ...result, failures: [], restarting: ["alvin", "zbook"] },
372 "0.0.50",
373 ),
374 ).toEqual({
375 type: "success",
376 title: "Updated this app, 2 servers, and 1 provider",
377 description: "alvin and zbook are still restarting.",
378 });
379 });
380});
381
382describe("describeUpdateEverything", () => {
383 it("summarizes the run and lists each machine once", () => {
384 const alvin = EnvironmentId.make("alvin");
385 expect(
386 describeUpdateEverything({
387 localVersion: "0.0.50",
388 servers: [
389 {
390 environmentId: alvin,
391 serverLabel: "alvin",
392 selfUpdate: "boot-service",
393 targetVersion: "0.0.50",
394 },
395 {
396 environmentId: EnvironmentId.make("studio"),
397 serverLabel: "studio",
398 selfUpdate: "desktop-managed",
399 targetVersion: "0.0.50",
400 },
401 ],
402 providerMachines: [
403 { environmentId: alvin, label: "alvin", providers: ["Codex", "Claude"] },
404 { environmentId: EnvironmentId.make("box"), label: "box", providers: ["Cursor"] },
405 ],
406 }),
407 ).toEqual({
408 summary: "Update this app, 2 servers, and 3 providers",
409 lines: [
410 "This app: 0.0.50, restarts last",
411 "alvin: server to 0.0.50, Codex, Claude",
412 "studio: desktop app update",
413 "box: Cursor",
414 ],
415 });
416 });
417
418 it("reads naturally with one kind of update", () => {
419 expect(
420 describeUpdateEverything({
421 localVersion: null,
422 servers: [],
423 providerMachines: [
424 { environmentId: EnvironmentId.make("alvin"), label: "alvin", providers: ["Codex"] },
425 ],
426 }).summary,
427 ).toBe("Update 1 provider");
428 });
429});