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

updateEverything.logic.test.ts

179 lines6.2 KB

1import { EnvironmentId, type ServerSelfUpdateCapability } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3​
4import {
5 collectRestartingMachines,
6 collectServerUpdateTargets,
7 describeUpdateEverything,
8 runUpdateEverything,
9 type UpdateEverythingMachine,
10} from "./updateEverything.logic";
11​
12function machine(
13 id: string,
14 serverVersion: string,
15 selfUpdate: ServerSelfUpdateCapability | undefined,
16 overrides: Partial<UpdateEverythingMachine> = {},
17): UpdateEverythingMachine {
18 return {
19 environmentId: EnvironmentId.make(id),
20 label: id,
21 serverConfig: {
22 environment: {
23 environmentId: EnvironmentId.make(id),
24 label: id,
25 platform: { os: "linux", arch: "x64" },
26 serverVersion,
27 capabilities: {
28 repositoryIdentity: true,
29 ...(selfUpdate ? { serverSelfUpdate: selfUpdate } : {}),
30 ...(selfUpdate === "desktop-managed" ? { desktopAppUpdate: true } : {}),
31 },
32 },
33 settings: { continueThreadsAfterServerUpdate: false },
34 },
35 connected: true,
36 hostedByThisApp: false,
37 canMaintain: true,
38 updateState: { status: "idle" },
39 ...overrides,
40 };
42​
43describe("collectServerUpdateTargets", () => {
44 it("updates remote servers that can move to the target and names every skipped one", () => {
45 const { targets, skipped } = collectServerUpdateTargets(
46 [
47 machine("this-app", "0.0.40", "desktop-managed", { hostedByThisApp: true }),
48 machine("service", "0.0.40", "boot-service"),
49 machine("nightly-service", "0.0.49-nightly.20261001.3", "boot-service"),
50 machine("other-desktop", "0.0.49-nightly.20261001.3", "desktop-managed"),
51 machine("offline", "0.0.40", "boot-service", { connected: false }),
52 machine("never-connected", "0.0.40", "boot-service", { serverConfig: null }),
53 machine("no-scope", "0.0.40", "boot-service", { canMaintain: false }),
54 machine("busy", "0.0.40", "boot-service", {
55 updateState: {
56 status: "running",
57 stage: "downloading",
58 fromVersion: "0.0.40",
59 targetVersion: "0.0.50",
60 },
61 }),
62 machine("current", "0.0.50", "boot-service"),
63 machine("current-offline", "0.0.50", "boot-service", { connected: false }),
64 machine("manual", "0.0.40", undefined),
65 ],
66 "0.0.50",
67 );
68​
69 expect(targets.map((target) => [target.serverLabel, target.targetVersion])).toEqual([
70 ["service", "0.0.50"],
71 ["other-desktop", "0.0.50"],
72 ]);
73 expect(skipped.map((entry) => [entry.label, entry.reason])).toEqual([
74 ["nightly-service", "on the nightly channel"],
75 ["offline", "not connected"],
76 ["never-connected", "not connected"],
77 ["no-scope", "no permission"],
78 ["busy", "already updating"],
79 ["manual", "needs a manual update"],
80 ]);
81 });
82});
83​
84describe("collectRestartingMachines", () => {
85 it("lists updated servers, and this app's servers only when it installs", () => {
86 const withContinuation = (
87 id: string,
88 overrides: Partial<UpdateEverythingMachine> = {},
89 enabled = false,
90 ) => {
91 const base = machine(id, "0.0.40", "boot-service", overrides);
92 return {
93 ...base,
94 serverConfig: {
95 environment: base.serverConfig!.environment,
96 settings: { continueThreadsAfterServerUpdate: enabled },
97 },
98 };
99 };
100 const machines = [
101 withContinuation("this-app", { hostedByThisApp: true }, true),
102 withContinuation("alvin", {}, true),
103 withContinuation("opted-out", {}, false),
104 machine("old", "0.0.40", "boot-service"),
105 withContinuation("providers-only"),
106 ];
107 const servers = ["alvin", "opted-out", "old"].map((id) => ({
108 environmentId: EnvironmentId.make(id),
109 }));
110​
111 expect(collectRestartingMachines(machines, servers, true)).toEqual([
112 { environmentId: "this-app", continuesThreads: true },
113 { environmentId: "alvin", continuesThreads: true },
114 { environmentId: "opted-out", continuesThreads: false },
115 { environmentId: "old", continuesThreads: false },
116 ]);
117 expect(
118 collectRestartingMachines(machines, servers, false).map((entry) => entry.environmentId),
119 ).toEqual(["alvin", "opted-out", "old"]);
120 });
121});
122​
123describe("runUpdateEverything", () => {
124 it("still updates remote machines when the local download fails", async () => {
125 const calls: string[] = [];
126 const result = await runUpdateEverything({
127 downloadLocal: async () => false,
128 updateProviders: async () => {
129 calls.push("providers");
130 },
131 updateServers: async () => {
132 calls.push("servers");
133 },
134 });
135​
136 expect(calls).toEqual(["providers", "servers"]);
137 expect(result.localReady).toBe(false);
138 });
139});
140​
141describe("describeUpdateEverything", () => {
142 it("gives each machine one row with its T3 Code and provider updates", () => {
143 const id = EnvironmentId.make;
144 const { rows } = describeUpdateEverything({
145 localVersion: "1.1.0",
146 localMachines: [
147 { environmentId: id("local"), label: "Mainbook" },
148 { environmentId: id("wsl"), label: "Mainbook WSL" },
149 ],
150 servers: [
151 {
152 environmentId: id("npm"),
153 serverLabel: "bb-1",
154 selfUpdate: "respawn",
155 targetVersion: "1.1.0",
156 },
157 ],
158 providerMachines: [
159 { environmentId: id("npm"), label: "bb-1", providers: ["Codex", "Claude"] },
160 { environmentId: id("local"), label: "Mainbook", providers: ["Codex"] },
161 { environmentId: id("manual"), label: "alvin", providers: ["Claude"] },
162 ],
163 skipped: [
164 { environmentId: id("manual"), label: "alvin", reason: "needs a manual update" },
165 { environmentId: id("offline"), label: "TheoRoom", reason: "not connected" },
166 ],
167 });
168​
169 expect(
170 rows.map(({ label, app, skipped, providers }) => [label, app, skipped, providers]),
171 ).toEqual([
172 ["Mainbook", "this-app", null, ["Codex"]],
173 ["Mainbook WSL", "this-app", null, []],
174 ["bb-1", "server", null, ["Codex", "Claude"]],
175 ["alvin", null, "needs a manual update", ["Claude"]],
176 ["TheoRoom", null, "not connected", []],
177 ]);
178 });
179});