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

updateEverything.logic.test.ts

574 lines20.7 KB

1import { EnvironmentId, type ServerSelfUpdateCapability } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3​
4import {
5 collectChannelSwitchTargets,
6 collectRestartingMachines,
7 localSwitchBlockReason,
8 collectServerUpdateTargets,
9 describeUpdateCapsule,
10 describeUpdateEverything,
11 resolveFleetTargetVersion,
12 resolveRestoredRetry,
13 runUpdateEverything,
14 type UpdateEverythingMachine,
15 type UpdateRunMachine,
16} from "./updateEverything.logic";
17​
18function machine(
19 id: string,
20 serverVersion: string,
21 selfUpdate: ServerSelfUpdateCapability | undefined,
22 overrides: Partial<UpdateEverythingMachine> = {},
23): UpdateEverythingMachine {
24 return {
25 environmentId: EnvironmentId.make(id),
26 label: id,
27 serverConfig: {
28 environment: {
29 environmentId: EnvironmentId.make(id),
30 label: id,
31 platform: { os: "linux", arch: "x64" },
32 serverVersion,
33 capabilities: {
34 repositoryIdentity: true,
35 ...(selfUpdate ? { serverSelfUpdate: selfUpdate } : {}),
36 ...(selfUpdate === "desktop-managed"
37 ? { desktopAppUpdate: true, desktopAppChannelSwitch: true }
38 : {}),
39 },
40 },
41 settings: { continueThreadsAfterServerUpdate: false },
42 },
43 connected: true,
44 hostedByThisApp: false,
45 canMaintain: true,
46 updateState: { status: "idle" },
47 ...overrides,
48 };
50​
51// A desktop-mode server the desktop app cannot control, such as a WSL backend or an older app.
52function withoutDesktopControl(base: UpdateEverythingMachine): UpdateEverythingMachine {
53 return {
54 ...base,
55 serverConfig: {
56 ...base.serverConfig!,
57 environment: {
58 ...base.serverConfig!.environment,
59 capabilities: { repositoryIdentity: true, serverSelfUpdate: "desktop-managed" },
60 },
61 },
62 };
64​
65describe("resolveFleetTargetVersion", () => {
66 it("moves servers to a newer release on this app's track only", () => {
67 const offered = "0.1.14-nightly.20261201.5550";
68 expect(resolveFleetTargetVersion(offered, "0.1.14-nightly.20261201.5564")).toBe(
69 "0.1.14-nightly.20261201.5564",
70 );
71 expect(resolveFleetTargetVersion(offered, "0.1.14-nightly.20261201.5530")).toBe(offered);
72 expect(resolveFleetTargetVersion(offered, "0.1.15")).toBe(offered);
73 expect(resolveFleetTargetVersion(offered, null)).toBe(offered);
74 });
75});
76​
77describe("collectServerUpdateTargets", () => {
78 it("updates remote servers that can move to the target and names every skipped one", () => {
79 const { targets, skipped } = collectServerUpdateTargets(
80 [
81 machine("this-app", "0.0.40", "desktop-managed", { hostedByThisApp: true }),
82 machine("service", "0.0.40", "boot-service"),
83 machine("nightly-service", "0.0.49-nightly.20261001.3", "boot-service"),
84 machine("other-desktop", "0.0.49-nightly.20261001.3", "desktop-managed"),
85 machine("offline", "0.0.40", "boot-service", { connected: false }),
86 machine("never-connected", "0.0.40", "boot-service", { serverConfig: null }),
87 machine("no-scope", "0.0.40", "boot-service", { canMaintain: false }),
88 machine("busy", "0.0.40", "boot-service", {
89 updateState: {
90 status: "running",
91 stage: "downloading",
92 fromVersion: "0.0.40",
93 targetVersion: "0.0.50",
94 },
95 }),
96 machine("current", "0.0.50", "boot-service"),
97 machine("current-offline", "0.0.50", "boot-service", { connected: false }),
98 machine("manual", "0.0.40", undefined),
99 withoutDesktopControl(machine("wsl-backend", "0.0.40", "desktop-managed")),
100 ],
101 "0.0.50",
102 );
103​
104 expect(targets.map((target) => [target.serverLabel, target.targetVersion])).toEqual([
105 ["service", "0.0.50"],
106 ["other-desktop", "0.0.50"],
107 ]);
108 expect(skipped.map((entry) => [entry.label, entry.reason])).toEqual([
109 ["nightly-service", "on the nightly channel"],
110 ["offline", "not connected"],
111 ["never-connected", "still loading"],
112 ["no-scope", "no permission"],
113 ["busy", "already updating"],
114 ["manual", "needs a manual update"],
115 ["wsl-backend", "update T3 Code on that machine"],
116 ]);
117 });
118​
119 it("says why a disconnected server was skipped when its connection says", () => {
120 const replaced =
121 "This address now runs a different T3 Code install (b7a4, not 0359). Remove this connection and add it again.";
122 const { skipped } = collectServerUpdateTargets(
123 [
124 machine("replaced", "0.0.40", "boot-service", {
125 connected: false,
126 serverConfig: null,
127 connectionError: replaced,
128 }),
129 machine("silent", "0.0.40", "boot-service", { connected: false, connectionError: null }),
130 machine("switched-off", "0.0.40", "boot-service", {
131 connected: false,
132 serverConfig: null,
133 turnedOff: true,
134 }),
135 ],
136 "0.0.50",
137 );
138​
139 expect(skipped.map((entry) => [entry.label, entry.reason])).toEqual([
140 ["replaced", replaced],
141 ["silent", "not connected"],
142 ["switched-off", "turned off"],
143 ]);
144 });
145​
146 it("does not offer a Nightly build to a Stable desktop app, whose own feed has nothing newer", () => {
147 const { targets, skipped } = collectServerUpdateTargets(
148 [machine("stable-desktop", "0.1.9", "desktop-managed")],
149 "0.1.10-nightly.20261116.4761",
150 );
151​
152 expect(targets.map((target) => target.serverLabel)).toEqual([]);
153 expect(skipped.map((entry) => [entry.label, entry.reason])).toEqual([
154 ["stable-desktop", "on the stable channel"],
155 ]);
156 });
157​
158 it("skips servers whose policy refuses the update, with the policy's message", () => {
159 const managed = (
160 id: string,
161 updates: { mode: "off" } | { mode: "auto"; allowNightly: false },
162 selfUpdate: "boot-service" | "desktop-managed" = "boot-service",
163 ) => {
164 const base = machine(id, "0.0.40", selfUpdate);
165 return {
166 ...base,
167 serverConfig: {
168 ...base.serverConfig!,
169 managedPolicy: { organization: "Example Corp", lockedSettings: [], limits: { updates } },
170 },
171 };
172 };
173 const machines = [
174 managed("off", { mode: "off" }),
175 managed("no-nightly", { mode: "auto", allowNightly: false }),
176 // A desktop app updates from its own feed, whatever track this app is on.
177 managed("desktop-no-nightly", { mode: "auto", allowNightly: false }, "desktop-managed"),
178 ];
179​
180 expect(
181 collectServerUpdateTargets(machines, "0.0.50").skipped.map((entry) => [
182 entry.label,
183 entry.reason,
184 ]),
185 ).toEqual([["off", "Example Corp manages T3 Code updates."]]);
186 expect(
187 collectServerUpdateTargets(
188 machines.map((entry) => ({
189 ...entry,
190 serverConfig: {
191 ...entry.serverConfig,
192 environment: {
193 ...entry.serverConfig.environment,
194 serverVersion: "0.0.49-nightly.20261027.1",
195 },
196 },
197 })),
198 "0.0.50-nightly.20261028.1",
199 ).skipped.map((entry) => entry.label),
200 ).toEqual(["off", "no-nightly"]);
201 });
202});
203​
204describe("collectChannelSwitchTargets", () => {
205 const NIGHTLY = "0.1.4-nightly.20261027.3796";
206 const OLD_NIGHTLY = "0.1.3-nightly.20261001.3500";
207 const NEWER_NIGHTLY = "0.1.4-nightly.20261020.3700";
208 // What each server reports as its database's highest migration.
209 const host = (
210 base: UpdateEverythingMachine,
211 databaseMigration: number | undefined,
212 os: "linux" | "darwin" = "linux",
213 ): UpdateEverythingMachine => ({
214 ...base,
215 serverConfig: {
216 ...base.serverConfig!,
217 environment: {
218 ...base.serverConfig!.environment,
219 platform: { os, arch: "x64" },
220 ...(databaseMigration === undefined ? {} : { databaseMigration }),
221 },
222 },
223 });
224​
225 it("moves nightly hosts to stable only when stable knows their database", () => {
226 const stable = { channel: "latest" as const, version: "0.1.3", databaseMigration: 69 };
227 const { targets, skipped } = collectChannelSwitchTargets(
228 [
229 host(machine("this-app", NIGHTLY, "desktop-managed", { hostedByThisApp: true }), 72),
230 host(machine("old-service", OLD_NIGHTLY, "boot-service"), 69),
231 // Service launchers refuse older versions, even when the database allows it.
232 host(machine("newer-service", NEWER_NIGHTLY, "boot-service"), 69),
233 host(machine("linux-desktop", OLD_NIGHTLY, "desktop-managed"), 69),
234 host(machine("new-service", NIGHTLY, "boot-service"), 72),
235 host(machine("old-server", "0.1.2-nightly.20260901.3000", "boot-service"), undefined),
236 host(machine("mac-desktop", OLD_NIGHTLY, "desktop-managed"), 69, "darwin"),
237 host(machine("preview", "0.1.3-preview.20261026.3702", "boot-service"), 69),
238 host(machine("stable", "0.1.3", "boot-service"), 69),
239 host(machine("stable-desktop", "0.1.2", "desktop-managed"), 68),
240 // Already current, but its saved track may still say Nightly.
241 host(machine("current-desktop", "0.1.3", "desktop-managed"), 69, "darwin"),
242 ],
243 stable,
244 );
245​
246 expect(
247 targets.map((target) => [
248 target.serverLabel,
249 target.targetVersion,
250 target.channel,
251 target.trackOnly ?? false,
252 ]),
253 ).toEqual([
254 ["old-service", "0.1.3", undefined, false],
255 ["linux-desktop", "0.1.3", "latest", false],
256 ["stable-desktop", "0.1.3", "latest", false],
257 ["current-desktop", "0.1.3", "latest", true],
258 ]);
259 expect(targets.every((target) => target.targetDatabaseMigration === 69)).toBe(true);
260 expect(skipped.map((entry) => [entry.label, entry.reason])).toEqual([
261 ["newer-service", "run `t3 update --channel stable --allow-downgrade` there"],
262 [
263 "new-service",
264 "its database has changes 0.1.3 does not support yet; stay on Nightly until Stable has them",
265 ],
266 ["old-server", "its server predates database checks; update it on Nightly first"],
267 ["mac-desktop", "switch the Mac app on that Mac"],
268 ["preview", "runs a preview build"],
269 ]);
270 });
271​
272 const managedHost = (base: UpdateEverythingMachine): UpdateEverythingMachine => ({
273 ...base,
274 serverConfig: {
275 ...base.serverConfig!,
276 managedPolicy: {
277 organization: "Example Corp",
278 lockedSettings: [],
279 limits: { updates: { mode: "auto", allowNightly: false } },
280 },
281 },
282 });
283​
284 it("moves stable hosts to nightly only when nightly knows their database", () => {
285 const nightly = { channel: "nightly" as const, version: NIGHTLY, databaseMigration: 72 };
286 const { targets, skipped } = collectChannelSwitchTargets(
287 [
288 host(machine("service", "0.1.2", "boot-service"), 70),
289 // A Stable tag built a migration the latest Nightly does not have yet.
290 host(machine("ahead", "0.1.3", "boot-service"), 73),
291 withoutDesktopControl(machine("old-desktop", "0.1.2", "desktop-managed")),
292 // Already on Nightly, so it needs an update rather than a channel switch.
293 withoutDesktopControl(machine("behind-desktop", OLD_NIGHTLY, "desktop-managed")),
294 machine("current", NIGHTLY, "boot-service"),
295 // The server refuses a track its policy rules out, so the dialog skips it up front.
296 managedHost(host(machine("no-nightly", "0.1.2", "boot-service"), 70)),
297 ],
298 nightly,
299 );
300​
301 expect(targets.map((target) => [target.serverLabel, target.targetVersion])).toEqual([
302 ["service", NIGHTLY],
303 ]);
304 expect(skipped.map((entry) => [entry.label, entry.reason])).toEqual([
305 [
306 "ahead",
307 `its database has changes ${NIGHTLY} does not support yet; stay on Stable until Nightly has them`,
308 ],
309 ["old-desktop", "change the update track on that machine"],
310 ["behind-desktop", "update T3 Code on that machine"],
311 [
312 "no-nightly",
313 "Example Corp manages T3 Code updates. Only stable releases can be installed.",
314 ],
315 ]);
316 });
317});
318​
319describe("localSwitchBlockReason", () => {
320 it("checks every server this app hosts, and only while connected", () => {
321 const stable = { channel: "latest" as const, version: "0.1.3", databaseMigration: 69 };
322 const nightly = "0.1.4-nightly.20261027.3796";
323​
324 const windows = { connected: true, databaseMigration: 69 };
325 expect(localSwitchBlockReason(nightly, stable, [windows], 1, true)).toBeNull();
326 // A WSL server's newer migration blocks the reinstall that would restart it.
327 expect(
328 localSwitchBlockReason(
329 nightly,
330 stable,
331 [windows, { connected: true, databaseMigration: 72 }],
332 2,
333 true,
334 ),
335 ).toContain("does not support yet");
336 // A WSL backend still starting has no client entry, so its database is unchecked.
337 expect(localSwitchBlockReason(nightly, stable, [windows], 2, true)).toBe(
338 "This app cannot switch: its server is not connected, so its database cannot be checked.",
339 );
340 // A cached head from a disconnected server proves nothing.
341 expect(
342 localSwitchBlockReason(
343 nightly,
344 stable,
345 [{ connected: false, databaseMigration: 69 }],
346 1,
347 true,
348 ),
349 ).toBe(
350 "This app cannot switch: its server is not connected, so its database cannot be checked.",
351 );
352 expect(localSwitchBlockReason("0.1.3", stable, [], 1, true)).toBeNull();
353 // A primary backend still starting has no entry or bootstrap yet, unless Local environment is off.
354 expect(localSwitchBlockReason(nightly, stable, [], 0, true)).toBe(
355 "This app cannot switch: its server is not connected, so its database cannot be checked.",
356 );
357 expect(localSwitchBlockReason(nightly, stable, [], 0, false)).toBeNull();
358 });
359});
360​
361describe("collectRestartingMachines", () => {
362 it("lists updated servers, and this app's servers only when it installs", () => {
363 const withContinuation = (
364 id: string,
365 overrides: Partial<UpdateEverythingMachine> = {},
366 enabled = false,
367 ) => {
368 const base = machine(id, "0.0.40", "boot-service", overrides);
369 return {
370 ...base,
371 serverConfig: {
372 environment: base.serverConfig!.environment,
373 settings: { continueThreadsAfterServerUpdate: enabled },
374 },
375 };
376 };
377 const machines = [
378 withContinuation("this-app", { hostedByThisApp: true }, true),
379 withContinuation("alvin", {}, true),
380 withContinuation("opted-out", {}, false),
381 machine("old", "0.0.40", "boot-service"),
382 withContinuation("providers-only"),
383 ];
384 const servers = ["alvin", "opted-out", "old"].map((id) => ({
385 environmentId: EnvironmentId.make(id),
386 }));
387​
388 expect(collectRestartingMachines(machines, servers, true)).toEqual([
389 { environmentId: "this-app", continuesThreads: true },
390 { environmentId: "alvin", continuesThreads: true },
391 { environmentId: "opted-out", continuesThreads: false },
392 { environmentId: "old", continuesThreads: false },
393 ]);
394 expect(
395 collectRestartingMachines(machines, servers, false).map((entry) => entry.environmentId),
396 ).toEqual(["alvin", "opted-out", "old"]);
397 });
398});
399​
400describe("runUpdateEverything", () => {
401 it("still updates remote machines when the local download fails", async () => {
402 const calls: string[] = [];
403 const result = await runUpdateEverything({
404 downloadLocal: async () => false,
405 updateProviders: async () => {
406 calls.push("providers");
407 },
408 updateServers: async () => {
409 calls.push("servers");
410 },
411 });
412​
413 expect(calls).toEqual(["providers", "servers"]);
414 expect(result.localReady).toBe(false);
415 });
416​
417 it("stages servers while providers install and updates each once its own stage settles", async () => {
418 const calls: string[] = [];
419 const [slow, fast] = [EnvironmentId.make("slow"), EnvironmentId.make("fast")];
420 let finishProviders = () => {};
421 let failSlowStage = () => {};
422 const providers = new Promise<void>((resolve) => (finishProviders = resolve));
423 const run = runUpdateEverything({
424 updateProviders: () => {
425 calls.push("providers");
426 return providers.then(() => {
427 calls.push("providers done");
428 });
429 },
430 stageServers: () => {
431 calls.push("stage");
432 return new Map([
433 [slow, new Promise((_, reject) => (failSlowStage = () => reject(new Error("failed"))))],
434 [fast, Promise.resolve()],
435 ]);
436 },
437 updateServers: (staged) => {
438 calls.push("servers");
439 return Promise.all(
440 [slow, fast].map(async (id) => {
441 await staged.get(id);
442 calls.push(`server ${id}`);
443 if (id === fast) failSlowStage();
444 }),
445 );
446 },
447 });
448 finishProviders();
449 await run;
450​
451 expect(calls).toEqual([
452 "stage",
453 "providers",
454 "providers done",
455 "servers",
456 "server fast",
457 "server slow",
458 ]);
459 });
460});
461​
462describe("describeUpdateEverything", () => {
463 it("gives each machine one row with its T3 Code and provider updates", () => {
464 const id = EnvironmentId.make;
465 const { rows } = describeUpdateEverything({
466 localVersion: "1.1.0",
467 localMachines: [
468 { environmentId: id("local"), label: "Mainbook" },
469 { environmentId: id("wsl"), label: "Mainbook WSL" },
470 ],
471 servers: [
472 {
473 environmentId: id("npm"),
474 serverLabel: "bb-1",
475 selfUpdate: "respawn",
476 targetVersion: "1.1.0",
477 },
478 ],
479 providerMachines: [
480 { environmentId: id("npm"), label: "bb-1", providers: ["Codex", "Claude"] },
481 { environmentId: id("local"), label: "Mainbook", providers: ["Codex"] },
482 { environmentId: id("manual"), label: "alvin", providers: ["Claude"] },
483 ],
484 skipped: [
485 { environmentId: id("manual"), label: "alvin", reason: "needs a manual update" },
486 { environmentId: id("offline"), label: "TheoRoom", reason: "not connected" },
487 ],
488 });
489​
490 expect(
491 rows.map(({ label, app, skipped, providers }) => [label, app, skipped, providers]),
492 ).toEqual([
493 ["Mainbook", "this-app", null, ["Codex"]],
494 ["Mainbook WSL", "this-app", null, []],
495 ["bb-1", "server", null, ["Codex", "Claude"]],
496 ["alvin", null, "needs a manual update", ["Claude"]],
497 ["TheoRoom", null, "not connected", []],
498 ]);
499 });
500});
501​
502describe("describeUpdateCapsule", () => {
503 it("sends failed track switches back through the planner instead of retrying their target", () => {
504 const failed = {
505 machine: {
506 environmentId: EnvironmentId.make("remote"),
507 label: "remote",
508 updatesProviders: false,
509 server: {
510 environmentId: EnvironmentId.make("remote"),
511 serverLabel: "remote",
512 selfUpdate: "boot-service" as const,
513 targetVersion: "0.1.3",
514 },
515 handedOffVersion: null,
516 startedAt: 0,
517 result: "failed" as const,
518 failure: "offline",
519 providerFailure: null,
520 },
521 status: "failed" as const,
522 restoredRetry: null,
523 };
524 const run = { localVersion: null, phase: "servers" as const, restartAt: null, machines: [] };
525​
526 expect(describeUpdateCapsule(run, [failed], null, 0).action).toBe("retry");
527 expect(describeUpdateCapsule({ ...run, trackSwitch: true }, [failed], null, 0).action).toBe(
528 "dismiss",
529 );
530 });
531});
532​
533describe("resolveRestoredRetry", () => {
534 // A row the relaunched app rebuilt from its saved handoff: no target, only the version.
535 const restored = (id: string): UpdateRunMachine => ({
536 environmentId: EnvironmentId.make(id),
537 label: id,
538 updatesProviders: false,
539 server: null,
540 handedOffVersion: "0.1.12",
541 startedAt: 0,
542 result: "failed",
543 failure: "Not back after 32 minutes",
544 providerFailure: null,
545 });
546​
547 it("rebuilds the target from the server as it is now, so the capsule offers Retry", () => {
548 const retry = resolveRestoredRetry(restored("remote"), [
549 machine("other", "0.1.10", "boot-service"),
550 machine("remote", "0.1.10", "boot-service"),
551 ]);
552 expect(retry).toEqual({
553 environmentId: "remote",
554 serverLabel: "remote",
555 selfUpdate: "boot-service",
556 installation: undefined,
557 desktopAppUpdate: false,
558 targetVersion: "0.1.12",
559 });
560 const run = { localVersion: null, phase: "servers" as const, restartAt: null, machines: [] };
561 const view = { machine: restored("remote"), status: "failed" as const, restoredRetry: retry };
562 expect(describeUpdateCapsule(run, [view], null, 0).action).toBe("retry");
563 expect(describeUpdateCapsule(run, [{ ...view, restoredRetry: null }], null, 0).action).toBe(
564 "dismiss",
565 );
566 });
567​
568 it("marks a server already on the target and offers nothing for one that is gone", () => {
569 expect(
570 resolveRestoredRetry(restored("remote"), [machine("remote", "0.1.12", "boot-service")]),
571 ).toBe("current");
572 expect(resolveRestoredRetry(restored("remote"), [])).toBeNull();
573 });
574});