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
89describe("collectChannelSwitchTargets", () => {
90 const NIGHTLY = "0.1.4-nightly.20261027.3796";
91 const OLD_NIGHTLY = "0.1.3-nightly.20261001.3500";
92 const NEWER_NIGHTLY = "0.1.4-nightly.20261020.3700";
93 // What each server reports as its database's highest migration.
94 const host = (
95 base: UpdateEverythingMachine,
96 databaseMigration: number | undefined,
97 os: "linux" | "darwin" = "linux",
98 ): UpdateEverythingMachine => ({
99 ...base,
100 serverConfig: {
101 ...base.serverConfig!,
102 environment: {
103 ...base.serverConfig!.environment,
104 platform: { os, arch: "x64" },
105 ...(databaseMigration === undefined ? {} : { databaseMigration }),
106 },
107 },
108 });
109
110 it("moves nightly hosts to stable only when stable knows their database", () => {
111 const stable = { channel: "latest" as const, version: "0.1.3", databaseMigration: 69 };
112 const { targets, skipped } = collectChannelSwitchTargets(
113 [
114 host(machine("this-app", NIGHTLY, "desktop-managed", { hostedByThisApp: true }), 72),
115 host(machine("old-service", OLD_NIGHTLY, "boot-service"), 69),
116 // Service launchers refuse older versions, even when the database allows it.
117 host(machine("newer-service", NEWER_NIGHTLY, "boot-service"), 69),
118 host(machine("linux-desktop", OLD_NIGHTLY, "desktop-managed"), 69),
119 host(machine("new-service", NIGHTLY, "boot-service"), 72),
120 host(machine("old-server", "0.1.2-nightly.20260901.3000", "boot-service"), undefined),
121 host(machine("mac-desktop", OLD_NIGHTLY, "desktop-managed"), 69, "darwin"),
122 host(machine("preview", "0.1.3-preview.20261026.3702", "boot-service"), 69),
123 host(machine("stable", "0.1.3", "boot-service"), 69),
124 host(machine("stable-desktop", "0.1.2", "desktop-managed"), 68),
125 // Already current, but its saved track may still say Nightly.
126 host(machine("current-desktop", "0.1.3", "desktop-managed"), 69, "darwin"),
127 ],
128 stable,
129 );
130
131 expect(
132 targets.map((target) => [
133 target.serverLabel,
134 target.targetVersion,
135 target.channel,
136 target.trackOnly ?? false,
137 ]),
138 ).toEqual([
139 ["old-service", "0.1.3", undefined, false],
140 ["linux-desktop", "0.1.3", "latest", false],
141 ["stable-desktop", "0.1.3", "latest", false],
142 ["current-desktop", "0.1.3", "latest", true],
143 ]);
144 expect(targets.every((target) => target.targetDatabaseMigration === 69)).toBe(true);
145 expect(skipped.map((entry) => [entry.label, entry.reason])).toEqual([
146 ["newer-service", "run `t3 update --channel stable --allow-downgrade` there"],
147 [
148 "new-service",
149 "its database has changes 0.1.3 does not support yet; stay on Nightly until Stable has them",
150 ],
151 ["old-server", "its server predates database checks; update it on Nightly first"],
152 ["mac-desktop", "switch the Mac app on that Mac"],
153 ["preview", "runs a preview build"],
154 ]);
155 });
156
157 it("moves stable hosts to nightly only when nightly knows their database", () => {
158 const nightly = { channel: "nightly" as const, version: NIGHTLY, databaseMigration: 72 };
159 const { targets, skipped } = collectChannelSwitchTargets(
160 [
161 host(machine("service", "0.1.2", "boot-service"), 70),
162 // A Stable tag built a migration the latest Nightly does not have yet.
163 host(machine("ahead", "0.1.3", "boot-service"), 73),
164 machine("old-desktop", "0.1.2", "desktop-managed", {
165 serverConfig: {
166 ...machine("old-desktop", "0.1.2", "desktop-managed").serverConfig!,
167 environment: {
168 ...machine("old-desktop", "0.1.2", "desktop-managed").serverConfig!.environment,
169 capabilities: { repositoryIdentity: true, serverSelfUpdate: "desktop-managed" },
170 },
171 },
172 }),
173 machine("current", NIGHTLY, "boot-service"),
174 ],
175 nightly,
176 );
177
178 expect(targets.map((target) => [target.serverLabel, target.targetVersion])).toEqual([
179 ["service", NIGHTLY],
180 ]);
181 expect(skipped.map((entry) => [entry.label, entry.reason])).toEqual([
182 [
183 "ahead",
184 `its database has changes ${NIGHTLY} does not support yet; stay on Stable until Nightly has them`,
185 ],
186 ["old-desktop", "needs a newer desktop app"],
187 ]);
188 });
189});
190
191describe("localSwitchBlockReason", () => {
192 it("checks every server this app hosts, and only while connected", () => {
193 const stable = { channel: "latest" as const, version: "0.1.3", databaseMigration: 69 };
194 const nightly = "0.1.4-nightly.20261027.3796";
195
196 const windows = { connected: true, databaseMigration: 69 };
197 expect(localSwitchBlockReason(nightly, stable, [windows], 1)).toBeNull();
198 // A WSL server's newer migration blocks the reinstall that would restart it.
199 expect(
200 localSwitchBlockReason(
201 nightly,
202 stable,
203 [windows, { connected: true, databaseMigration: 72 }],
204 2,
205 ),
206 ).toContain("does not support yet");
207 // A WSL backend still starting has no client entry, so its database is unchecked.
208 expect(localSwitchBlockReason(nightly, stable, [windows], 2)).toBe(
209 "its server is not connected, so its database cannot be checked",
210 );
211 // A cached head from a disconnected server proves nothing.
212 expect(
213 localSwitchBlockReason(nightly, stable, [{ connected: false, databaseMigration: 69 }], 1),
214 ).toBe("its server is not connected, so its database cannot be checked");
215 expect(localSwitchBlockReason("0.1.3", stable, [], 1)).toBeNull();
216 });
217});
218
219describe("collectRestartingMachines", () => {
220 it("lists updated servers, and this app's servers only when it installs", () => {
221 const withContinuation = (
222 id: string,
223 overrides: Partial<UpdateEverythingMachine> = {},
224 enabled = false,
225 ) => {
226 const base = machine(id, "0.0.40", "boot-service", overrides);
227 return {
228 ...base,
229 serverConfig: {
230 environment: base.serverConfig!.environment,
231 settings: { continueThreadsAfterServerUpdate: enabled },
232 },
233 };
234 };
235 const machines = [
236 withContinuation("this-app", { hostedByThisApp: true }, true),
237 withContinuation("alvin", {}, true),
238 withContinuation("opted-out", {}, false),
239 machine("old", "0.0.40", "boot-service"),
240 withContinuation("providers-only"),
241 ];
242 const servers = ["alvin", "opted-out", "old"].map((id) => ({
243 environmentId: EnvironmentId.make(id),
244 }));
245
246 expect(collectRestartingMachines(machines, servers, true)).toEqual([
247 { environmentId: "this-app", continuesThreads: true },
248 { environmentId: "alvin", continuesThreads: true },
249 { environmentId: "opted-out", continuesThreads: false },
250 { environmentId: "old", continuesThreads: false },
251 ]);
252 expect(
253 collectRestartingMachines(machines, servers, false).map((entry) => entry.environmentId),
254 ).toEqual(["alvin", "opted-out", "old"]);
255 });
256});
257
258describe("runUpdateEverything", () => {
259 it("still updates remote machines when the local download fails", async () => {
260 const calls: string[] = [];
261 const result = await runUpdateEverything({
262 downloadLocal: async () => false,
263 updateProviders: async () => {
264 calls.push("providers");
265 },
266 updateServers: async () => {
267 calls.push("servers");
268 },
269 });
270
271 expect(calls).toEqual(["providers", "servers"]);
272 expect(result.localReady).toBe(false);
273 });
274});
275
276describe("describeUpdateEverything", () => {
277 it("gives each machine one row with its T3 Code and provider updates", () => {
278 const id = EnvironmentId.make;
279 const { rows } = describeUpdateEverything({
280 localVersion: "1.1.0",
281 localMachines: [
282 { environmentId: id("local"), label: "Mainbook" },
283 { environmentId: id("wsl"), label: "Mainbook WSL" },
284 ],
285 servers: [
286 {
287 environmentId: id("npm"),
288 serverLabel: "bb-1",
289 selfUpdate: "respawn",
290 targetVersion: "1.1.0",
291 },
292 ],
293 providerMachines: [
294 { environmentId: id("npm"), label: "bb-1", providers: ["Codex", "Claude"] },
295 { environmentId: id("local"), label: "Mainbook", providers: ["Codex"] },
296 { environmentId: id("manual"), label: "alvin", providers: ["Claude"] },
297 ],
298 skipped: [
299 { environmentId: id("manual"), label: "alvin", reason: "needs a manual update" },
300 { environmentId: id("offline"), label: "TheoRoom", reason: "not connected" },
301 ],
302 });
303
304 expect(
305 rows.map(({ label, app, skipped, providers }) => [label, app, skipped, providers]),
306 ).toEqual([
307 ["Mainbook", "this-app", null, ["Codex"]],
308 ["Mainbook WSL", "this-app", null, []],
309 ["bb-1", "server", null, ["Codex", "Claude"]],
310 ["alvin", null, "needs a manual update", ["Claude"]],
311 ["TheoRoom", null, "not connected", []],
312 ]);
313 });
314});
315
316describe("describeUpdateCapsule", () => {
317 it("sends failed track switches back through the planner instead of retrying their target", () => {
318 const failed = {
319 machine: {
320 environmentId: EnvironmentId.make("remote"),
321 label: "remote",
322 updatesProviders: false,
323 server: {
324 environmentId: EnvironmentId.make("remote"),
325 serverLabel: "remote",
326 selfUpdate: "boot-service" as const,
327 targetVersion: "0.1.3",
328 },
329 handedOffVersion: null,
330 startedAt: 0,
331 result: "failed" as const,
332 failure: "offline",
333 providerFailure: null,
334 },
335 status: "failed" as const,
336 };
337 const run = { localVersion: null, phase: "servers" as const, restartAt: null, machines: [] };
338
339 expect(describeUpdateCapsule(run, [failed], null, 0).action).toBe("retry");
340 expect(describeUpdateCapsule({ ...run, trackSwitch: true }, [failed], null, 0).action).toBe(
341 "dismiss",
342 );
343 });
344});