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

updateEverything.logic.test.ts

206 lines6.4 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 only remote servers that can move to the target on their channel", () => {
45 const targets = 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("no-scope", "0.0.40", "boot-service", { canMaintain: false }),
53 machine("current", "0.0.50", "boot-service"),
54 machine("manual", "0.0.40", undefined),
55 ],
56 "0.0.50",
57 );
58​
59 expect(targets.map((target) => [target.serverLabel, target.targetVersion])).toEqual([
60 ["service", "0.0.50"],
61 ["other-desktop", "0.0.50"],
62 ]);
63 });
64});
65​
66describe("collectRestartingMachines", () => {
67 it("lists updated servers, and this app's servers only when it installs", () => {
68 const withContinuation = (
69 id: string,
70 overrides: Partial<UpdateEverythingMachine> = {},
71 enabled = false,
72 ) => {
73 const base = machine(id, "0.0.40", "boot-service", overrides);
74 return {
75 ...base,
76 serverConfig: {
77 environment: {
78 ...base.serverConfig!.environment,
79 capabilities: {
80 ...base.serverConfig!.environment.capabilities,
81 threadRestartContinuation: true,
82 },
83 },
84 settings: { continueThreadsAfterServerUpdate: enabled },
85 },
86 };
87 };
88 const machines = [
89 withContinuation("this-app", { hostedByThisApp: true }, true),
90 withContinuation("alvin", {}, true),
91 withContinuation("opted-out", {}, false),
92 machine("old", "0.0.40", "boot-service"),
93 withContinuation("providers-only"),
94 ];
95 const servers = ["alvin", "opted-out", "old"].map((id) => ({
96 environmentId: EnvironmentId.make(id),
97 }));
98​
99 expect(collectRestartingMachines(machines, servers, true)).toEqual([
100 { environmentId: "this-app", continuesThreads: true },
101 { environmentId: "alvin", continuesThreads: true },
102 { environmentId: "opted-out", continuesThreads: false },
103 { environmentId: "old", continuesThreads: false },
104 ]);
105 expect(
106 collectRestartingMachines(machines, servers, false).map((entry) => entry.environmentId),
107 ).toEqual(["alvin", "opted-out", "old"]);
108 });
109});
110​
111describe("runUpdateEverything", () => {
112 it("restarts servers after providers finish and installs this app last", async () => {
113 const calls: string[] = [];
114 let finishProviders = () => {};
115 const run = runUpdateEverything({
116 downloadLocal: async () => {
117 calls.push("download");
118 return true;
119 },
120 updateProviders: () =>
121 new Promise<void>((resolve) => {
122 calls.push("providers");
123 finishProviders = resolve;
124 }),
125 updateServers: async () => {
126 calls.push("servers");
127 },
128 installLocal: async () => {
129 calls.push("install");
130 },
131 });
132​
133 await new Promise((resolve) => setTimeout(resolve, 0));
134 expect(calls).toEqual(["download", "providers"]);
135 finishProviders();
136 await run;
137 expect(calls).toEqual(["download", "providers", "servers", "install"]);
138 });
139​
140 it("still updates remote machines when the local download fails", async () => {
141 const calls: string[] = [];
142 await runUpdateEverything({
143 downloadLocal: async () => false,
144 updateProviders: async () => {
145 calls.push("providers");
146 },
147 updateServers: async () => {
148 calls.push("servers");
149 },
150 installLocal: async () => {
151 calls.push("install");
152 },
153 });
154​
155 expect(calls).toEqual(["providers", "servers"]);
156 });
157});
158​
159describe("describeUpdateEverything", () => {
160 it("summarizes the run and lists each machine once", () => {
161 const alvin = EnvironmentId.make("alvin");
162 expect(
163 describeUpdateEverything({
164 localVersion: "0.0.50",
165 servers: [
166 {
167 environmentId: alvin,
168 serverLabel: "alvin",
169 selfUpdate: "boot-service",
170 targetVersion: "0.0.50",
171 },
172 {
173 environmentId: EnvironmentId.make("studio"),
174 serverLabel: "studio",
175 selfUpdate: "desktop-managed",
176 targetVersion: "0.0.50",
177 },
178 ],
179 providerMachines: [
180 { environmentId: alvin, label: "alvin", providers: ["Codex", "Claude"] },
181 { environmentId: EnvironmentId.make("box"), label: "box", providers: ["Cursor"] },
182 ],
183 }),
184 ).toEqual({
185 summary: "Update this app, 2 servers, and 3 providers",
186 lines: [
187 "This app: 0.0.50, restarts last",
188 "alvin: server to 0.0.50, Codex, Claude",
189 "studio: desktop app update",
190 "box: Cursor",
191 ],
192 });
193 });
194​
195 it("reads naturally with one kind of update", () => {
196 expect(
197 describeUpdateEverything({
198 localVersion: null,
199 servers: [],
200 providerMachines: [
201 { environmentId: EnvironmentId.make("alvin"), label: "alvin", providers: ["Codex"] },
202 ],
203 }).summary,
204 ).toBe("Update 1 provider");
205 });
206});