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 runUpdateEverything,
12 type UpdateEverythingMachine,
13} from "./updateEverything.logic";
14
15function machine(
16 id: string,
17 serverVersion: string,
18 selfUpdate: ServerSelfUpdateCapability | undefined,
19 overrides: Partial<UpdateEverythingMachine> = {},
20): UpdateEverythingMachine {
21 return {
22 environmentId: EnvironmentId.make(id),
23 label: id,
24 serverConfig: {
25 environment: {
26 environmentId: EnvironmentId.make(id),
27 label: id,
28 platform: { os: "linux", arch: "x64" },
29 serverVersion,
30 capabilities: {
31 repositoryIdentity: true,
32 ...(selfUpdate ? { serverSelfUpdate: selfUpdate } : {}),
33 ...(selfUpdate === "desktop-managed"
34 ? { desktopAppUpdate: true, desktopAppChannelSwitch: true }
35 : {}),
36 },
37 },
38 settings: { continueThreadsAfterServerUpdate: false },
39 },
40 connected: true,
41 hostedByThisApp: false,
42 canMaintain: true,
43 updateState: { status: "idle" },
44 ...overrides,
45 };
46}
47
48describe("collectServerUpdateTargets", () => {
49 it("updates remote servers that can move to the target and names every skipped one", () => {
50 const { targets, skipped } = collectServerUpdateTargets(
51 [
52 machine("this-app", "0.0.40", "desktop-managed", { hostedByThisApp: true }),
53 machine("service", "0.0.40", "boot-service"),
54 machine("nightly-service", "0.0.49-nightly.20261001.3", "boot-service"),
55 machine("other-desktop", "0.0.49-nightly.20261001.3", "desktop-managed"),
56 machine("offline", "0.0.40", "boot-service", { connected: false }),
57 machine("never-connected", "0.0.40", "boot-service", { serverConfig: null }),
58 machine("no-scope", "0.0.40", "boot-service", { canMaintain: false }),
59 machine("busy", "0.0.40", "boot-service", {
60 updateState: {
61 status: "running",
62 stage: "downloading",
63 fromVersion: "0.0.40",
64 targetVersion: "0.0.50",
65 },
66 }),
67 machine("current", "0.0.50", "boot-service"),
68 machine("current-offline", "0.0.50", "boot-service", { connected: false }),
69 machine("manual", "0.0.40", undefined),
70 ],
71 "0.0.50",
72 );
73
74 expect(targets.map((target) => [target.serverLabel, target.targetVersion])).toEqual([
75 ["service", "0.0.50"],
76 ["other-desktop", "0.0.50"],
77 ]);
78 expect(skipped.map((entry) => [entry.label, entry.reason])).toEqual([
79 ["nightly-service", "on the nightly channel"],
80 ["offline", "not connected"],
81 ["never-connected", "not connected"],
82 ["no-scope", "no permission"],
83 ["busy", "already updating"],
84 ["manual", "needs a manual update"],
85 ]);
86 });
87
88 it("skips servers whose policy refuses the update, with the policy's message", () => {
89 const managed = (
90 id: string,
91 updates: { mode: "off" } | { mode: "auto"; allowNightly: false },
92 selfUpdate: "boot-service" | "desktop-managed" = "boot-service",
93 ) => {
94 const base = machine(id, "0.0.40", selfUpdate);
95 return {
96 ...base,
97 serverConfig: {
98 ...base.serverConfig!,
99 managedPolicy: { organization: "Example Corp", lockedSettings: [], limits: { updates } },
100 },
101 };
102 };
103 const machines = [
104 managed("off", { mode: "off" }),
105 managed("no-nightly", { mode: "auto", allowNightly: false }),
106 // A desktop app updates from its own feed, whatever track this app is on.
107 managed("desktop-no-nightly", { mode: "auto", allowNightly: false }, "desktop-managed"),
108 ];
109
110 expect(
111 collectServerUpdateTargets(machines, "0.0.50").skipped.map((entry) => [
112 entry.label,
113 entry.reason,
114 ]),
115 ).toEqual([["off", "Example Corp manages T3 Code updates."]]);
116 expect(
117 collectServerUpdateTargets(
118 machines.map((entry) => ({
119 ...entry,
120 serverConfig: {
121 ...entry.serverConfig,
122 environment: { ...entry.serverConfig.environment, serverVersion: "0.0.49-nightly.1" },
123 },
124 })),
125 "0.0.50-nightly.20261028.1",
126 ).skipped.map((entry) => entry.label),
127 ).toEqual(["off", "no-nightly"]);
128 });
129});
130
131describe("collectChannelSwitchTargets", () => {
132 const NIGHTLY = "0.1.4-nightly.20261027.3796";
133 const OLD_NIGHTLY = "0.1.3-nightly.20261001.3500";
134 const NEWER_NIGHTLY = "0.1.4-nightly.20261020.3700";
135 // What each server reports as its database's highest migration.
136 const host = (
137 base: UpdateEverythingMachine,
138 databaseMigration: number | undefined,
139 os: "linux" | "darwin" = "linux",
140 ): UpdateEverythingMachine => ({
141 ...base,
142 serverConfig: {
143 ...base.serverConfig!,
144 environment: {
145 ...base.serverConfig!.environment,
146 platform: { os, arch: "x64" },
147 ...(databaseMigration === undefined ? {} : { databaseMigration }),
148 },
149 },
150 });
151
152 it("moves nightly hosts to stable only when stable knows their database", () => {
153 const stable = { channel: "latest" as const, version: "0.1.3", databaseMigration: 69 };
154 const { targets, skipped } = collectChannelSwitchTargets(
155 [
156 host(machine("this-app", NIGHTLY, "desktop-managed", { hostedByThisApp: true }), 72),
157 host(machine("old-service", OLD_NIGHTLY, "boot-service"), 69),
158 // Service launchers refuse older versions, even when the database allows it.
159 host(machine("newer-service", NEWER_NIGHTLY, "boot-service"), 69),
160 host(machine("linux-desktop", OLD_NIGHTLY, "desktop-managed"), 69),
161 host(machine("new-service", NIGHTLY, "boot-service"), 72),
162 host(machine("old-server", "0.1.2-nightly.20260901.3000", "boot-service"), undefined),
163 host(machine("mac-desktop", OLD_NIGHTLY, "desktop-managed"), 69, "darwin"),
164 host(machine("preview", "0.1.3-preview.20261026.3702", "boot-service"), 69),
165 host(machine("stable", "0.1.3", "boot-service"), 69),
166 host(machine("stable-desktop", "0.1.2", "desktop-managed"), 68),
167 // Already current, but its saved track may still say Nightly.
168 host(machine("current-desktop", "0.1.3", "desktop-managed"), 69, "darwin"),
169 ],
170 stable,
171 );
172
173 expect(
174 targets.map((target) => [
175 target.serverLabel,
176 target.targetVersion,
177 target.channel,
178 target.trackOnly ?? false,
179 ]),
180 ).toEqual([
181 ["old-service", "0.1.3", undefined, false],
182 ["linux-desktop", "0.1.3", "latest", false],
183 ["stable-desktop", "0.1.3", "latest", false],
184 ["current-desktop", "0.1.3", "latest", true],
185 ]);
186 expect(targets.every((target) => target.targetDatabaseMigration === 69)).toBe(true);
187 expect(skipped.map((entry) => [entry.label, entry.reason])).toEqual([
188 ["newer-service", "run `t3 update --channel stable --allow-downgrade` there"],
189 [
190 "new-service",
191 "its database has changes 0.1.3 does not support yet; stay on Nightly until Stable has them",
192 ],
193 ["old-server", "its server predates database checks; update it on Nightly first"],
194 ["mac-desktop", "switch the Mac app on that Mac"],
195 ["preview", "runs a preview build"],
196 ]);
197 });
198
199 const managedHost = (base: UpdateEverythingMachine): UpdateEverythingMachine => ({
200 ...base,
201 serverConfig: {
202 ...base.serverConfig!,
203 managedPolicy: {
204 organization: "Example Corp",
205 lockedSettings: [],
206 limits: { updates: { mode: "auto", allowNightly: false } },
207 },
208 },
209 });
210
211 it("moves stable hosts to nightly only when nightly knows their database", () => {
212 const nightly = { channel: "nightly" as const, version: NIGHTLY, databaseMigration: 72 };
213 const { targets, skipped } = collectChannelSwitchTargets(
214 [
215 host(machine("service", "0.1.2", "boot-service"), 70),
216 // A Stable tag built a migration the latest Nightly does not have yet.
217 host(machine("ahead", "0.1.3", "boot-service"), 73),
218 machine("old-desktop", "0.1.2", "desktop-managed", {
219 serverConfig: {
220 ...machine("old-desktop", "0.1.2", "desktop-managed").serverConfig!,
221 environment: {
222 ...machine("old-desktop", "0.1.2", "desktop-managed").serverConfig!.environment,
223 capabilities: { repositoryIdentity: true, serverSelfUpdate: "desktop-managed" },
224 },
225 },
226 }),
227 machine("current", NIGHTLY, "boot-service"),
228 // The server refuses a track its policy rules out, so the dialog skips it up front.
229 managedHost(host(machine("no-nightly", "0.1.2", "boot-service"), 70)),
230 ],
231 nightly,
232 );
233
234 expect(targets.map((target) => [target.serverLabel, target.targetVersion])).toEqual([
235 ["service", NIGHTLY],
236 ]);
237 expect(skipped.map((entry) => [entry.label, entry.reason])).toEqual([
238 [
239 "ahead",
240 `its database has changes ${NIGHTLY} does not support yet; stay on Stable until Nightly has them`,
241 ],
242 ["old-desktop", "needs a newer desktop app"],
243 [
244 "no-nightly",
245 "Example Corp manages T3 Code updates. Only stable releases can be installed.",
246 ],
247 ]);
248 });
249});
250
251describe("localSwitchBlockReason", () => {
252 it("checks every server this app hosts, and only while connected", () => {
253 const stable = { channel: "latest" as const, version: "0.1.3", databaseMigration: 69 };
254 const nightly = "0.1.4-nightly.20261027.3796";
255
256 const windows = { connected: true, databaseMigration: 69 };
257 expect(localSwitchBlockReason(nightly, stable, [windows], 1)).toBeNull();
258 // A WSL server's newer migration blocks the reinstall that would restart it.
259 expect(
260 localSwitchBlockReason(
261 nightly,
262 stable,
263 [windows, { connected: true, databaseMigration: 72 }],
264 2,
265 ),
266 ).toContain("does not support yet");
267 // A WSL backend still starting has no client entry, so its database is unchecked.
268 expect(localSwitchBlockReason(nightly, stable, [windows], 2)).toBe(
269 "its server is not connected, so its database cannot be checked",
270 );
271 // A cached head from a disconnected server proves nothing.
272 expect(
273 localSwitchBlockReason(nightly, stable, [{ connected: false, databaseMigration: 69 }], 1),
274 ).toBe("its server is not connected, so its database cannot be checked");
275 expect(localSwitchBlockReason("0.1.3", stable, [], 1)).toBeNull();
276 });
277});
278
279describe("collectRestartingMachines", () => {
280 it("lists updated servers, and this app's servers only when it installs", () => {
281 const withContinuation = (
282 id: string,
283 overrides: Partial<UpdateEverythingMachine> = {},
284 enabled = false,
285 ) => {
286 const base = machine(id, "0.0.40", "boot-service", overrides);
287 return {
288 ...base,
289 serverConfig: {
290 environment: base.serverConfig!.environment,
291 settings: { continueThreadsAfterServerUpdate: enabled },
292 },
293 };
294 };
295 const machines = [
296 withContinuation("this-app", { hostedByThisApp: true }, true),
297 withContinuation("alvin", {}, true),
298 withContinuation("opted-out", {}, false),
299 machine("old", "0.0.40", "boot-service"),
300 withContinuation("providers-only"),
301 ];
302 const servers = ["alvin", "opted-out", "old"].map((id) => ({
303 environmentId: EnvironmentId.make(id),
304 }));
305
306 expect(collectRestartingMachines(machines, servers, true)).toEqual([
307 { environmentId: "this-app", continuesThreads: true },
308 { environmentId: "alvin", continuesThreads: true },
309 { environmentId: "opted-out", continuesThreads: false },
310 { environmentId: "old", continuesThreads: false },
311 ]);
312 expect(
313 collectRestartingMachines(machines, servers, false).map((entry) => entry.environmentId),
314 ).toEqual(["alvin", "opted-out", "old"]);
315 });
316});
317
318describe("runUpdateEverything", () => {
319 it("still updates remote machines when the local download fails", async () => {
320 const calls: string[] = [];
321 const result = await runUpdateEverything({
322 downloadLocal: async () => false,
323 updateProviders: async () => {
324 calls.push("providers");
325 },
326 updateServers: async () => {
327 calls.push("servers");
328 },
329 });
330
331 expect(calls).toEqual(["providers", "servers"]);
332 expect(result.localReady).toBe(false);
333 });
334});
335
336describe("describeUpdateEverything", () => {
337 it("gives each machine one row with its T3 Code and provider updates", () => {
338 const id = EnvironmentId.make;
339 const { rows } = describeUpdateEverything({
340 localVersion: "1.1.0",
341 localMachines: [
342 { environmentId: id("local"), label: "Mainbook" },
343 { environmentId: id("wsl"), label: "Mainbook WSL" },
344 ],
345 servers: [
346 {
347 environmentId: id("npm"),
348 serverLabel: "bb-1",
349 selfUpdate: "respawn",
350 targetVersion: "1.1.0",
351 },
352 ],
353 providerMachines: [
354 { environmentId: id("npm"), label: "bb-1", providers: ["Codex", "Claude"] },
355 { environmentId: id("local"), label: "Mainbook", providers: ["Codex"] },
356 { environmentId: id("manual"), label: "alvin", providers: ["Claude"] },
357 ],
358 skipped: [
359 { environmentId: id("manual"), label: "alvin", reason: "needs a manual update" },
360 { environmentId: id("offline"), label: "TheoRoom", reason: "not connected" },
361 ],
362 });
363
364 expect(
365 rows.map(({ label, app, skipped, providers }) => [label, app, skipped, providers]),
366 ).toEqual([
367 ["Mainbook", "this-app", null, ["Codex"]],
368 ["Mainbook WSL", "this-app", null, []],
369 ["bb-1", "server", null, ["Codex", "Claude"]],
370 ["alvin", null, "needs a manual update", ["Claude"]],
371 ["TheoRoom", null, "not connected", []],
372 ]);
373 });
374});
375
376describe("describeUpdateCapsule", () => {
377 it("sends failed track switches back through the planner instead of retrying their target", () => {
378 const failed = {
379 machine: {
380 environmentId: EnvironmentId.make("remote"),
381 label: "remote",
382 updatesProviders: false,
383 server: {
384 environmentId: EnvironmentId.make("remote"),
385 serverLabel: "remote",
386 selfUpdate: "boot-service" as const,
387 targetVersion: "0.1.3",
388 },
389 handedOffVersion: null,
390 startedAt: 0,
391 result: "failed" as const,
392 failure: "offline",
393 providerFailure: null,
394 },
395 status: "failed" as const,
396 };
397 const run = { localVersion: null, phase: "servers" as const, restartAt: null, machines: [] };
398
399 expect(describeUpdateCapsule(run, [failed], null, 0).action).toBe("retry");
400 expect(describeUpdateCapsule({ ...run, trackSwitch: true }, [failed], null, 0).action).toBe(
401 "dismiss",
402 );
403 });
404});