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