@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 477 lines 17.1 KB
Raw
1 import { EnvironmentId, type ServerSelfUpdateCapability } from "@t3tools/contracts";
2 import { describe, expect, it } from "vite-plus/test";
3
4 import {
5 collectChannelSwitchTargets,
6 collectRestartingMachines,
7 localSwitchBlockReason,
8 collectServerUpdateTargets,
9 describeUpdateCapsule,
10 describeUpdateEverything,
11 runUpdateEverything,
12 type UpdateEverythingMachine,
13 } from "./updateEverything.logic";
14
15 function 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
48 describe("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("does not offer a Nightly build to a Stable desktop app, whose own feed has nothing newer", () => {
89 const { targets, skipped } = collectServerUpdateTargets(
90 [machine("stable-desktop", "0.1.9", "desktop-managed")],
91 "0.1.10-nightly.20261116.4761",
92 );
93
94 expect(targets.map((target) => target.serverLabel)).toEqual([]);
95 expect(skipped.map((entry) => [entry.label, entry.reason])).toEqual([
96 ["stable-desktop", "on the stable channel"],
97 ]);
98 });
99
100 it("skips servers whose policy refuses the update, with the policy's message", () => {
101 const managed = (
102 id: string,
103 updates: { mode: "off" } | { mode: "auto"; allowNightly: false },
104 selfUpdate: "boot-service" | "desktop-managed" = "boot-service",
105 ) => {
106 const base = machine(id, "0.0.40", selfUpdate);
107 return {
108 ...base,
109 serverConfig: {
110 ...base.serverConfig!,
111 managedPolicy: { organization: "Example Corp", lockedSettings: [], limits: { updates } },
112 },
113 };
114 };
115 const machines = [
116 managed("off", { mode: "off" }),
117 managed("no-nightly", { mode: "auto", allowNightly: false }),
118 // A desktop app updates from its own feed, whatever track this app is on.
119 managed("desktop-no-nightly", { mode: "auto", allowNightly: false }, "desktop-managed"),
120 ];
121
122 expect(
123 collectServerUpdateTargets(machines, "0.0.50").skipped.map((entry) => [
124 entry.label,
125 entry.reason,
126 ]),
127 ).toEqual([["off", "Example Corp manages T3 Code updates."]]);
128 expect(
129 collectServerUpdateTargets(
130 machines.map((entry) => ({
131 ...entry,
132 serverConfig: {
133 ...entry.serverConfig,
134 environment: {
135 ...entry.serverConfig.environment,
136 serverVersion: "0.0.49-nightly.20261027.1",
137 },
138 },
139 })),
140 "0.0.50-nightly.20261028.1",
141 ).skipped.map((entry) => entry.label),
142 ).toEqual(["off", "no-nightly"]);
143 });
144 });
145
146 describe("collectChannelSwitchTargets", () => {
147 const NIGHTLY = "0.1.4-nightly.20261027.3796";
148 const OLD_NIGHTLY = "0.1.3-nightly.20261001.3500";
149 const NEWER_NIGHTLY = "0.1.4-nightly.20261020.3700";
150 // What each server reports as its database's highest migration.
151 const host = (
152 base: UpdateEverythingMachine,
153 databaseMigration: number | undefined,
154 os: "linux" | "darwin" = "linux",
155 ): UpdateEverythingMachine => ({
156 ...base,
157 serverConfig: {
158 ...base.serverConfig!,
159 environment: {
160 ...base.serverConfig!.environment,
161 platform: { os, arch: "x64" },
162 ...(databaseMigration === undefined ? {} : { databaseMigration }),
163 },
164 },
165 });
166
167 it("moves nightly hosts to stable only when stable knows their database", () => {
168 const stable = { channel: "latest" as const, version: "0.1.3", databaseMigration: 69 };
169 const { targets, skipped } = collectChannelSwitchTargets(
170 [
171 host(machine("this-app", NIGHTLY, "desktop-managed", { hostedByThisApp: true }), 72),
172 host(machine("old-service", OLD_NIGHTLY, "boot-service"), 69),
173 // Service launchers refuse older versions, even when the database allows it.
174 host(machine("newer-service", NEWER_NIGHTLY, "boot-service"), 69),
175 host(machine("linux-desktop", OLD_NIGHTLY, "desktop-managed"), 69),
176 host(machine("new-service", NIGHTLY, "boot-service"), 72),
177 host(machine("old-server", "0.1.2-nightly.20260901.3000", "boot-service"), undefined),
178 host(machine("mac-desktop", OLD_NIGHTLY, "desktop-managed"), 69, "darwin"),
179 host(machine("preview", "0.1.3-preview.20261026.3702", "boot-service"), 69),
180 host(machine("stable", "0.1.3", "boot-service"), 69),
181 host(machine("stable-desktop", "0.1.2", "desktop-managed"), 68),
182 // Already current, but its saved track may still say Nightly.
183 host(machine("current-desktop", "0.1.3", "desktop-managed"), 69, "darwin"),
184 ],
185 stable,
186 );
187
188 expect(
189 targets.map((target) => [
190 target.serverLabel,
191 target.targetVersion,
192 target.channel,
193 target.trackOnly ?? false,
194 ]),
195 ).toEqual([
196 ["old-service", "0.1.3", undefined, false],
197 ["linux-desktop", "0.1.3", "latest", false],
198 ["stable-desktop", "0.1.3", "latest", false],
199 ["current-desktop", "0.1.3", "latest", true],
200 ]);
201 expect(targets.every((target) => target.targetDatabaseMigration === 69)).toBe(true);
202 expect(skipped.map((entry) => [entry.label, entry.reason])).toEqual([
203 ["newer-service", "run `t3 update --channel stable --allow-downgrade` there"],
204 [
205 "new-service",
206 "its database has changes 0.1.3 does not support yet; stay on Nightly until Stable has them",
207 ],
208 ["old-server", "its server predates database checks; update it on Nightly first"],
209 ["mac-desktop", "switch the Mac app on that Mac"],
210 ["preview", "runs a preview build"],
211 ]);
212 });
213
214 const managedHost = (base: UpdateEverythingMachine): UpdateEverythingMachine => ({
215 ...base,
216 serverConfig: {
217 ...base.serverConfig!,
218 managedPolicy: {
219 organization: "Example Corp",
220 lockedSettings: [],
221 limits: { updates: { mode: "auto", allowNightly: false } },
222 },
223 },
224 });
225
226 it("moves stable hosts to nightly only when nightly knows their database", () => {
227 const nightly = { channel: "nightly" as const, version: NIGHTLY, databaseMigration: 72 };
228 const { targets, skipped } = collectChannelSwitchTargets(
229 [
230 host(machine("service", "0.1.2", "boot-service"), 70),
231 // A Stable tag built a migration the latest Nightly does not have yet.
232 host(machine("ahead", "0.1.3", "boot-service"), 73),
233 machine("old-desktop", "0.1.2", "desktop-managed", {
234 serverConfig: {
235 ...machine("old-desktop", "0.1.2", "desktop-managed").serverConfig!,
236 environment: {
237 ...machine("old-desktop", "0.1.2", "desktop-managed").serverConfig!.environment,
238 capabilities: { repositoryIdentity: true, serverSelfUpdate: "desktop-managed" },
239 },
240 },
241 }),
242 machine("current", NIGHTLY, "boot-service"),
243 // The server refuses a track its policy rules out, so the dialog skips it up front.
244 managedHost(host(machine("no-nightly", "0.1.2", "boot-service"), 70)),
245 ],
246 nightly,
247 );
248
249 expect(targets.map((target) => [target.serverLabel, target.targetVersion])).toEqual([
250 ["service", NIGHTLY],
251 ]);
252 expect(skipped.map((entry) => [entry.label, entry.reason])).toEqual([
253 [
254 "ahead",
255 `its database has changes ${NIGHTLY} does not support yet; stay on Stable until Nightly has them`,
256 ],
257 ["old-desktop", "needs a newer desktop app"],
258 [
259 "no-nightly",
260 "Example Corp manages T3 Code updates. Only stable releases can be installed.",
261 ],
262 ]);
263 });
264 });
265
266 describe("localSwitchBlockReason", () => {
267 it("checks every server this app hosts, and only while connected", () => {
268 const stable = { channel: "latest" as const, version: "0.1.3", databaseMigration: 69 };
269 const nightly = "0.1.4-nightly.20261027.3796";
270
271 const windows = { connected: true, databaseMigration: 69 };
272 expect(localSwitchBlockReason(nightly, stable, [windows], 1, true)).toBeNull();
273 // A WSL server's newer migration blocks the reinstall that would restart it.
274 expect(
275 localSwitchBlockReason(
276 nightly,
277 stable,
278 [windows, { connected: true, databaseMigration: 72 }],
279 2,
280 true,
281 ),
282 ).toContain("does not support yet");
283 // A WSL backend still starting has no client entry, so its database is unchecked.
284 expect(localSwitchBlockReason(nightly, stable, [windows], 2, true)).toBe(
285 "This app cannot switch: its server is not connected, so its database cannot be checked.",
286 );
287 // A cached head from a disconnected server proves nothing.
288 expect(
289 localSwitchBlockReason(
290 nightly,
291 stable,
292 [{ connected: false, databaseMigration: 69 }],
293 1,
294 true,
295 ),
296 ).toBe(
297 "This app cannot switch: its server is not connected, so its database cannot be checked.",
298 );
299 expect(localSwitchBlockReason("0.1.3", stable, [], 1, true)).toBeNull();
300 // A primary backend still starting has no entry or bootstrap yet, unless Local environment is off.
301 expect(localSwitchBlockReason(nightly, stable, [], 0, true)).toBe(
302 "This app cannot switch: its server is not connected, so its database cannot be checked.",
303 );
304 expect(localSwitchBlockReason(nightly, stable, [], 0, false)).toBeNull();
305 });
306 });
307
308 describe("collectRestartingMachines", () => {
309 it("lists updated servers, and this app's servers only when it installs", () => {
310 const withContinuation = (
311 id: string,
312 overrides: Partial<UpdateEverythingMachine> = {},
313 enabled = false,
314 ) => {
315 const base = machine(id, "0.0.40", "boot-service", overrides);
316 return {
317 ...base,
318 serverConfig: {
319 environment: base.serverConfig!.environment,
320 settings: { continueThreadsAfterServerUpdate: enabled },
321 },
322 };
323 };
324 const machines = [
325 withContinuation("this-app", { hostedByThisApp: true }, true),
326 withContinuation("alvin", {}, true),
327 withContinuation("opted-out", {}, false),
328 machine("old", "0.0.40", "boot-service"),
329 withContinuation("providers-only"),
330 ];
331 const servers = ["alvin", "opted-out", "old"].map((id) => ({
332 environmentId: EnvironmentId.make(id),
333 }));
334
335 expect(collectRestartingMachines(machines, servers, true)).toEqual([
336 { environmentId: "this-app", continuesThreads: true },
337 { environmentId: "alvin", continuesThreads: true },
338 { environmentId: "opted-out", continuesThreads: false },
339 { environmentId: "old", continuesThreads: false },
340 ]);
341 expect(
342 collectRestartingMachines(machines, servers, false).map((entry) => entry.environmentId),
343 ).toEqual(["alvin", "opted-out", "old"]);
344 });
345 });
346
347 describe("runUpdateEverything", () => {
348 it("still updates remote machines when the local download fails", async () => {
349 const calls: string[] = [];
350 const result = await runUpdateEverything({
351 downloadLocal: async () => false,
352 updateProviders: async () => {
353 calls.push("providers");
354 },
355 updateServers: async () => {
356 calls.push("servers");
357 },
358 });
359
360 expect(calls).toEqual(["providers", "servers"]);
361 expect(result.localReady).toBe(false);
362 });
363
364 it("stages servers while providers install and updates each once its own stage settles", async () => {
365 const calls: string[] = [];
366 const [slow, fast] = [EnvironmentId.make("slow"), EnvironmentId.make("fast")];
367 let finishProviders = () => {};
368 let failSlowStage = () => {};
369 const providers = new Promise<void>((resolve) => (finishProviders = resolve));
370 const run = runUpdateEverything({
371 updateProviders: () => {
372 calls.push("providers");
373 return providers.then(() => {
374 calls.push("providers done");
375 });
376 },
377 stageServers: () => {
378 calls.push("stage");
379 return new Map([
380 [slow, new Promise((_, reject) => (failSlowStage = () => reject(new Error("failed"))))],
381 [fast, Promise.resolve()],
382 ]);
383 },
384 updateServers: (staged) => {
385 calls.push("servers");
386 return Promise.all(
387 [slow, fast].map(async (id) => {
388 await staged.get(id);
389 calls.push(`server ${id}`);
390 if (id === fast) failSlowStage();
391 }),
392 );
393 },
394 });
395 finishProviders();
396 await run;
397
398 expect(calls).toEqual([
399 "stage",
400 "providers",
401 "providers done",
402 "servers",
403 "server fast",
404 "server slow",
405 ]);
406 });
407 });
408
409 describe("describeUpdateEverything", () => {
410 it("gives each machine one row with its T3 Code and provider updates", () => {
411 const id = EnvironmentId.make;
412 const { rows } = describeUpdateEverything({
413 localVersion: "1.1.0",
414 localMachines: [
415 { environmentId: id("local"), label: "Mainbook" },
416 { environmentId: id("wsl"), label: "Mainbook WSL" },
417 ],
418 servers: [
419 {
420 environmentId: id("npm"),
421 serverLabel: "bb-1",
422 selfUpdate: "respawn",
423 targetVersion: "1.1.0",
424 },
425 ],
426 providerMachines: [
427 { environmentId: id("npm"), label: "bb-1", providers: ["Codex", "Claude"] },
428 { environmentId: id("local"), label: "Mainbook", providers: ["Codex"] },
429 { environmentId: id("manual"), label: "alvin", providers: ["Claude"] },
430 ],
431 skipped: [
432 { environmentId: id("manual"), label: "alvin", reason: "needs a manual update" },
433 { environmentId: id("offline"), label: "TheoRoom", reason: "not connected" },
434 ],
435 });
436
437 expect(
438 rows.map(({ label, app, skipped, providers }) => [label, app, skipped, providers]),
439 ).toEqual([
440 ["Mainbook", "this-app", null, ["Codex"]],
441 ["Mainbook WSL", "this-app", null, []],
442 ["bb-1", "server", null, ["Codex", "Claude"]],
443 ["alvin", null, "needs a manual update", ["Claude"]],
444 ["TheoRoom", null, "not connected", []],
445 ]);
446 });
447 });
448
449 describe("describeUpdateCapsule", () => {
450 it("sends failed track switches back through the planner instead of retrying their target", () => {
451 const failed = {
452 machine: {
453 environmentId: EnvironmentId.make("remote"),
454 label: "remote",
455 updatesProviders: false,
456 server: {
457 environmentId: EnvironmentId.make("remote"),
458 serverLabel: "remote",
459 selfUpdate: "boot-service" as const,
460 targetVersion: "0.1.3",
461 },
462 handedOffVersion: null,
463 startedAt: 0,
464 result: "failed" as const,
465 failure: "offline",
466 providerFailure: null,
467 },
468 status: "failed" as const,
469 };
470 const run = { localVersion: null, phase: "servers" as const, restartAt: null, machines: [] };
471
472 expect(describeUpdateCapsule(run, [failed], null, 0).action).toBe("retry");
473 expect(describeUpdateCapsule({ ...run, trackSwitch: true }, [failed], null, 0).action).toBe(
474 "dismiss",
475 );
476 });
477 });