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

updateEverything.logic.test.ts

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