Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { expect, it } from "@effect/vitest";
3import type { ManagedPolicyDocument } from "@t3tools/contracts";
4import * as HostProcess from "@t3tools/shared/HostProcess";
5import * as Deferred from "effect/Deferred";
6import * as Duration from "effect/Duration";
7import * as Effect from "effect/Effect";
8import * as FileSystem from "effect/FileSystem";
9import * as Fiber from "effect/Fiber";
10import * as Layer from "effect/Layer";
11import * as Path from "effect/Path";
12import * as Stream from "effect/Stream";
13import * as TestClock from "effect/testing/TestClock";
14import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http";
15import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
16
17import packageJson from "../../package.json" with { type: "json" };
18import * as ServerConfig from "../config.ts";
19import * as DesktopAppUpdate from "../desktopUpdate/DesktopAppUpdate.ts";
20import * as ManagedPolicy from "../policy/ManagedPolicy.ts";
21import * as ProcessRunner from "../processRunner.ts";
22import * as ServiceLauncherClient from "./serviceLauncherClient.ts";
23import { SERVICE_LAUNCHER_PROTOCOL } from "./serviceProtocol.ts";
24import * as ServerSelfUpdate from "./selfUpdate.ts";
25
26interface HarnessOptions {
27 readonly mode?: "web" | "desktop";
28 readonly managed?: boolean;
29 readonly launcherProtocol?: 3 | typeof SERVICE_LAUNCHER_PROTOCOL;
30 readonly preflight?: "ready" | "blocked";
31 readonly preflightDuration?: Duration.Input;
32 readonly preflightStarted?: Deferred.Deferred<void>;
33 readonly beforeDownload?: Effect.Effect<void>;
34 readonly httpClient?: HttpClient.HttpClient;
35 readonly requestUpdate?: ServiceLauncherClient.ServiceLauncherClient["Service"]["requestUpdate"];
36 readonly desktopAppUpdate?: DesktopAppUpdate.DesktopAppUpdate["Service"];
37 readonly policy?: ManagedPolicyDocument;
38}
39
40const archiveBytes = new TextEncoder().encode("not really a tarball");
41const releaseHttpClient = (order: string[], urls: string[], beforeDownload?: Effect.Effect<void>) =>
42 HttpClient.make((request) =>
43 Effect.gen(function* () {
44 urls.push(request.url);
45 if (request.url.endsWith("/SHA256SUMS")) {
46 const digest = yield* Effect.promise(() => crypto.subtle.digest("SHA-256", archiveBytes));
47 const hex = Array.from(new Uint8Array(digest), (byte) =>
48 byte.toString(16).padStart(2, "0"),
49 ).join("");
50 return HttpClientResponse.fromWeb(
51 request,
52 new Response(`${hex} t3-1.1.0-linux-x64.tar.gz\n`),
53 );
54 }
55 if (beforeDownload) yield* beforeDownload;
56 order.push("download");
57 return HttpClientResponse.fromWeb(request, new Response(archiveBytes));
58 }),
59 );
60
61const makeHarness = Effect.fn("test.make_self_update_harness")(function* (
62 options: HarnessOptions = {},
63) {
64 const fs = yield* FileSystem.FileSystem;
65 const path = yield* Path.Path;
66 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-self-update-test-" });
67 const order: string[] = [];
68 const urls: string[] = [];
69 const preflightProtocols: Array<string | undefined> = [];
70 const runner = ProcessRunner.ProcessRunner.of({
71 run: (input) =>
72 Effect.gen(function* () {
73 if (input.command === "busctl") {
74 // No user bus: the unit directory falls back to XDG_CONFIG_HOME or ~/.config.
75 return {
76 stdout: "",
77 stderr: "",
78 code: ChildProcessSpawner.ExitCode(1),
79 timedOut: false,
80 stdoutTruncated: false,
81 stderrTruncated: false,
82 stdoutInvalidUtf8: false,
83 stderrInvalidUtf8: false,
84 };
85 }
86 if (input.command === "tar") {
87 order.push("extract");
88 const stagingDir = input.args[input.args.indexOf("-C") + 1];
89 if (stagingDir === undefined) return yield* Effect.die("missing tar target");
90 yield* fs.writeFileString(path.join(stagingDir, "t3"), "#!/bin/sh\n").pipe(Effect.orDie);
91 return {
92 stdout: "",
93 stderr: "",
94 code: ChildProcessSpawner.ExitCode(0),
95 timedOut: false,
96 stdoutTruncated: false,
97 stderrTruncated: false,
98 stdoutInvalidUtf8: false,
99 stderrInvalidUtf8: false,
100 };
101 }
102 if (options.preflightStarted) yield* Deferred.succeed(options.preflightStarted, undefined);
103 if (options.preflightDuration !== undefined) {
104 // Like the real runner, fail once the caller's timeout elapses.
105 const timeout = Duration.fromInputUnsafe(input.timeout ?? "60 seconds");
106 const finished = yield* Effect.sleep(options.preflightDuration).pipe(
107 Effect.timeoutOption(timeout),
108 );
109 if (finished._tag === "None") {
110 return yield* new ProcessRunner.ProcessTimeoutError({
111 command: input.command,
112 argumentCount: input.args.length,
113 timeoutMs: Duration.toMillis(timeout),
114 });
115 }
116 }
117 order.push("preflight");
118 // Like a release, report back the launcher protocol the server named.
119 const named = input.args[input.args.indexOf("--launcher-protocol") + 1];
120 preflightProtocols.push(named);
121 const result =
122 options.preflight === "blocked"
123 ? { status: "blocked", version: "1.1.0", reason: "local update required" }
124 : { status: "ready", version: "1.1.0", launcherProtocol: Number(named) };
125 return {
126 stdout: JSON.stringify(result),
127 stderr: "",
128 code: ChildProcessSpawner.ExitCode(0),
129 timedOut: false,
130 stdoutTruncated: false,
131 stderrTruncated: false,
132 stdoutInvalidUtf8: false,
133 stderrInvalidUtf8: false,
134 };
135 }),
136 });
137 const managed = options.managed ?? true;
138 const launcher = ServiceLauncherClient.ServiceLauncherClient.of({
139 managed,
140 protocol: managed ? (options.launcherProtocol ?? SERVICE_LAUNCHER_PROTOCOL) : undefined,
141 requestUpdate:
142 options.requestUpdate ??
143 (() =>
144 Effect.sync(() => {
145 order.push("accept");
146 return "launcher-id";
147 })),
148 prepareTrial: () => Effect.undefined,
149 });
150 const config = yield* ServerConfig.ServerConfig.pipe(
151 Effect.provide(ServerConfig.layerTest(process.cwd(), baseDir)),
152 );
153 const selfUpdate = yield* ServerSelfUpdate.make().pipe(
154 Effect.provideService(ProcessRunner.ProcessRunner, runner),
155 Effect.provideService(ServiceLauncherClient.ServiceLauncherClient, launcher),
156 Effect.provideService(
157 DesktopAppUpdate.DesktopAppUpdate,
158 options.desktopAppUpdate ?? {
159 available: false,
160 run: () => Effect.die("unexpected desktop app update run"),
161 },
162 ),
163 Effect.provideService(
164 HttpClient.HttpClient,
165 options.httpClient ?? releaseHttpClient(order, urls, options.beforeDownload),
166 ),
167 Effect.provideService(HostProcess.Platform, "linux"),
168 Effect.provideService(HostProcess.Architecture, "x64"),
169 Effect.provide(
170 Layer.merge(
171 ServerConfig.layer({ ...config, mode: options.mode ?? "web" }),
172 ManagedPolicy.layerTest(options.policy),
173 ),
174 ),
175 );
176 return { selfUpdate, order, urls, baseDir, preflightProtocols };
177});
178
179it.layer(NodeServices.layer)("server self update", (it) => {
180 it.effect.each([
181 { asset: "checksums", status: 504 },
182 { asset: "archive", status: 503 },
183 { asset: "checksums", status: 403 },
184 ])("reports an HTTP $status downloading $asset and allows retry", ({ asset, status }) =>
185 Effect.gen(function* () {
186 let failDownload = true;
187 const { selfUpdate, order, baseDir } = yield* makeHarness({
188 httpClient: releaseHttpClient([], []).pipe(
189 HttpClient.transform((response, request) =>
190 failDownload && request.url.endsWith("/SHA256SUMS") === (asset === "checksums")
191 ? Effect.succeed(
192 HttpClientResponse.fromWeb(
193 HttpClientRequest.get("https://user:password@cdn.example/private?token=secret"),
194 new Response("private response body", { status }),
195 ),
196 )
197 : response,
198 ),
199 ),
200 });
201 const error = yield* selfUpdate.update({ targetVersion: "1.1.0" }).pipe(Effect.flip);
202 expect(error.reason).toBe(
203 `Could not prepare t3@1.1.0 while downloading the t3 release ${asset} (HTTP ${status} from cdn.example).`,
204 );
205 expect(order).toEqual([]);
206 const fs = yield* FileSystem.FileSystem;
207 const path = yield* Path.Path;
208 expect(yield* fs.readDirectory(path.join(baseDir, "runtime", "versions"))).toEqual([]);
209 failDownload = false;
210 expect((yield* selfUpdate.update({ targetVersion: "1.1.0" })).updateId).toBe("launcher-id");
211 expect(order).toEqual(["extract", "preflight", "accept"]);
212 }),
213 );
214
215 // regression: #34502 a remote update never replaces the launcher, so it can be older than the server
216 it.effect("names its launcher's protocol to the staged preflight, not its own", () =>
217 Effect.gen(function* () {
218 const { selfUpdate, preflightProtocols } = yield* makeHarness({ launcherProtocol: 3 });
219 expect((yield* selfUpdate.update({ targetVersion: "1.1.0" })).updateId).toBe("launcher-id");
220 expect(preflightProtocols).toEqual(["3"]);
221 }),
222 );
223
224 // regression: #5181 make remote updates rollback-safe
225 it.effect("rejects invalid versions and desktop-managed servers before staging", () =>
226 Effect.gen(function* () {
227 const web = yield* makeHarness();
228 expect(
229 (yield* web.selfUpdate.update({ targetVersion: "latest" }).pipe(Effect.flip)).reason,
230 ).toBe("'latest' is not an exact t3 version.");
231 const desktop = yield* makeHarness({ mode: "desktop" });
232 expect(
233 (yield* desktop.selfUpdate.update({ targetVersion: "1.1.0" }).pipe(Effect.flip)).reason,
234 ).toContain("desktop app");
235 expect([...web.order, ...desktop.order]).toEqual([]);
236 }),
237 );
238
239 it.effect("refuses every update when the managed policy turns updates off", () =>
240 Effect.gen(function* () {
241 const policy: ManagedPolicyDocument = {
242 schemaVersion: 1,
243 organization: "Example Corp",
244 limits: { updates: { mode: "off" } },
245 };
246 const service = yield* makeHarness({ policy });
247 const desktop = yield* makeHarness({
248 mode: "desktop",
249 policy,
250 desktopAppUpdate: {
251 available: true,
252 run: () => Effect.die("unexpected desktop app update run"),
253 commit: () => Effect.never,
254 },
255 });
256 for (const harness of [service, desktop]) {
257 expect(
258 (yield* harness.selfUpdate.update({ targetVersion: "1.1.0" }).pipe(Effect.flip)).reason,
259 ).toBe("Example Corp manages T3 Code updates.");
260 }
261 expect([...service.order, ...desktop.order]).toEqual([]);
262 }),
263 );
264
265 it.effect("lets a user start an update, but only to stable without nightly", () =>
266 Effect.gen(function* () {
267 const { selfUpdate, order, urls } = yield* makeHarness({
268 policy: {
269 schemaVersion: 1,
270 organization: "Example Corp",
271 limits: {
272 updates: {
273 mode: "auto",
274 allowNightly: false,
275 feedUrl: "https://updates.example.com/t3code/",
276 },
277 },
278 },
279 });
280 expect(
281 (yield* selfUpdate.update({ targetVersion: "1.2.0-nightly.20261028.1" }).pipe(Effect.flip))
282 .reason,
283 ).toBe("Example Corp manages T3 Code updates. Only stable releases can be installed.");
284 expect(order).toEqual([]);
285 // The mirror lists only 1.1.0's archive.
286 expect((yield* selfUpdate.update({ targetVersion: "1.2.0" }).pipe(Effect.flip)).reason).toBe(
287 "Example Corp's update mirror has no t3@1.2.0 for this machine.",
288 );
289 expect((yield* selfUpdate.update({ targetVersion: "1.1.0" })).updateId).toBe("launcher-id");
290 // The release comes from the policy's mirror, not GitHub.
291 expect(urls.length).toBeGreaterThan(0);
292 for (const url of urls)
293 expect(url).toMatch(/^https:\/\/updates\.example\.com\/t3code\/v1\.[12]\.0\//);
294 }),
295 );
296
297 it.effect("delegates desktop-managed updates to the desktop app when available", () =>
298 Effect.gen(function* () {
299 const stages: string[] = [];
300 const { selfUpdate, order } = yield* makeHarness({
301 mode: "desktop",
302 desktopAppUpdate: {
303 available: true,
304 run: (reportProgress) =>
305 reportProgress("downloading").pipe(
306 Effect.andThen(reportProgress("installing")),
307 Effect.as({ targetVersion: "1.2.0", method: "desktop-app" as const }),
308 ),
309 commit: () => Effect.never,
310 },
311 });
312 const result = yield* selfUpdate.update({ targetVersion: "1.1.0" }, (stage) =>
313 Effect.sync(() => void stages.push(stage)),
314 );
315 expect(result).toEqual({ targetVersion: "1.2.0", method: "desktop-app" });
316 expect(stages).toEqual(["downloading", "installing"]);
317 expect(order).toEqual([]);
318 }),
319 );
320
321 it.effect("allows only one update at a time", () =>
322 Effect.gen(function* () {
323 const requested = yield* Deferred.make<void>();
324 const accepted = yield* Deferred.make<string>();
325 const { selfUpdate } = yield* makeHarness({
326 requestUpdate: () =>
327 Deferred.succeed(requested, undefined).pipe(Effect.andThen(Deferred.await(accepted))),
328 });
329 const first = yield* Effect.forkChild(selfUpdate.update({ targetVersion: "1.1.0" }), {
330 startImmediately: true,
331 });
332 yield* Deferred.await(requested);
333 expect((yield* selfUpdate.update({ targetVersion: "1.1.1" }).pipe(Effect.flip)).reason).toBe(
334 "A server update is already in progress.",
335 );
336 yield* Deferred.succeed(accepted, "launcher-id");
337 expect((yield* Fiber.join(first)).updateId).toBe("launcher-id");
338 }),
339 );
340
341 // regression: #28198 a slow staged preflight on a loaded host no longer fails the update
342 it.effect("waits for a staged preflight that takes a minute on a loaded host", () =>
343 Effect.gen(function* () {
344 const preflightStarted = yield* Deferred.make<void>();
345 const { selfUpdate, order } = yield* makeHarness({
346 preflightDuration: "60 seconds",
347 preflightStarted,
348 });
349 const update = yield* Effect.forkChild(selfUpdate.update({ targetVersion: "1.1.0" }), {
350 startImmediately: true,
351 });
352 yield* Deferred.await(preflightStarted);
353 yield* TestClock.adjust("60 seconds");
354 const exit = yield* Fiber.await(update);
355 expect(exit._tag === "Failure" ? String(exit.cause) : "launcher handoff").toBe(
356 "launcher handoff",
357 );
358 expect(order).toEqual(["download", "extract", "preflight", "accept"]);
359 }),
360 );
361
362 it.effect("keeps preparing after the progress stream closes", () =>
363 Effect.gen(function* () {
364 const releaseDownload = yield* Deferred.make<void>();
365 const sawDownloading = yield* Deferred.make<void>();
366 const accepted = yield* Deferred.make<void>();
367 const { selfUpdate, order } = yield* makeHarness({
368 beforeDownload: Deferred.await(releaseDownload),
369 requestUpdate: () =>
370 Effect.sync(() => {
371 order.push("accept");
372 }).pipe(Effect.andThen(Deferred.succeed(accepted, undefined)), Effect.as("launcher-id")),
373 });
374
375 const consumed = yield* ServerSelfUpdate.updateProgressStream(selfUpdate, {
376 targetVersion: "1.1.0",
377 }).pipe(
378 Stream.tap((event) =>
379 event.type === "progress" && event.stage === "downloading"
380 ? Deferred.succeed(sawDownloading, undefined)
381 : Effect.void,
382 ),
383 Stream.runDrain,
384 Effect.forkChild,
385 );
386
387 yield* Deferred.await(sawDownloading);
388 yield* Fiber.interrupt(consumed);
389 expect((yield* selfUpdate.update({ targetVersion: "1.2.0" }).pipe(Effect.flip)).reason).toBe(
390 "A server update is already in progress.",
391 );
392
393 yield* Deferred.succeed(releaseDownload, undefined);
394 yield* Deferred.await(accepted);
395 expect(order).toEqual(["download", "extract", "preflight", "accept"]);
396 expect((yield* selfUpdate.update({ targetVersion: "1.1.0" }).pipe(Effect.flip)).reason).toBe(
397 "A server update is already in progress.",
398 );
399 }),
400 );
401});
402
403it.layer(NodeServices.layer)("pruning runtimes on start", (it) => {
404 const staleVersion = "0.0.1";
405 const otherProcesses = ProcessRunner.ProcessRunner.of({
406 run: () =>
407 Effect.succeed({
408 stdout: "p42\nftxt\nn/bin/sh\n",
409 stderr: "",
410 code: ChildProcessSpawner.ExitCode(0),
411 timedOut: false,
412 stdoutTruncated: false,
413 stderrTruncated: false,
414 stdoutInvalidUtf8: false,
415 stderrInvalidUtf8: false,
416 }),
417 });
418
419 const pruneOnStart = Effect.fn("test.prune_on_start")(function* (input: {
420 readonly executable: "own runtime" | "elsewhere";
421 readonly managed?: boolean;
422 readonly symlinkedHome?: boolean;
423 readonly servicePlistStarts?: string;
424 readonly servicePlistThroughLink?: boolean;
425 }) {
426 const fs = yield* FileSystem.FileSystem;
427 const path = yield* Path.Path;
428 const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-prune-on-start-" });
429 const realHome = path.join(tempDir, "home");
430 const baseDir = input.symlinkedHome ? path.join(tempDir, "linked-home") : realHome;
431 const versionsDir = path.join(baseDir, "runtime", "versions");
432 yield* TestClock.setTime(24 * 60 * 60 * 1000);
433 for (const version of [staleVersion, packageJson.version]) {
434 const directory = path.join(realHome, "runtime", "versions", version);
435 yield* fs.makeDirectory(directory, { recursive: true });
436 yield* fs.writeFileString(path.join(directory, "t3"), "");
437 const sentinel = path.join(directory, ".install-complete");
438 yield* fs.writeFileString(sentinel, `${version}\n`);
439 yield* fs.utimes(sentinel, 0, 0);
440 }
441 if (input.symlinkedHome) yield* fs.symlink(realHome, baseDir);
442 const userHome = path.join(tempDir, "user");
443 if (input.servicePlistStarts !== undefined) {
444 const agentsDir = path.join(userHome, "Library", "LaunchAgents");
445 yield* fs.makeDirectory(agentsDir, { recursive: true });
446 const programHome = input.servicePlistThroughLink
447 ? path.join(tempDir, "alias-home")
448 : baseDir;
449 if (input.servicePlistThroughLink) yield* fs.symlink(realHome, programHome);
450 const program = path.join(programHome, "runtime", "versions", input.servicePlistStarts, "t3");
451 yield* fs.writeFileString(
452 path.join(agentsDir, "com.t3tools.t3code.service.plist"),
453 `<plist><dict>\n<key>ProgramArguments</key>\n<array>\n<string>${program}</string>\n<string>__service-launcher</string>\n</array>\n</dict></plist>\n`,
454 );
455 }
456 // Like `process.execPath`, the running runtime is reported by its real path.
457 const executablePath =
458 input.executable === "own runtime"
459 ? yield* fs.realPath(path.join(versionsDir, packageJson.version, "t3"))
460 : "/usr/local/bin/node";
461 const removed = yield* ServerSelfUpdate.pruneRuntimesOnStart({
462 baseDir,
463 managed: input.managed ?? false,
464 }).pipe(
465 Effect.provideService(HostProcess.ExecutablePath, executablePath),
466 Effect.provideService(HostProcess.Platform, "darwin"),
467 Effect.provideService(HostProcess.Environment, {}),
468 Effect.provideService(HostProcess.HomeDirectory, userHome),
469 Effect.provideService(ProcessRunner.ProcessRunner, otherProcesses),
470 );
471 return { removed, remaining: (yield* fs.readDirectory(versionsDir)).toSorted() };
472 });
473
474 // The SSH launcher runs `t3 serve` straight from the pinned runtime, with no boot service.
475 it.effect("prunes for a server started from its home's pinned runtime", () =>
476 Effect.gen(function* () {
477 const { removed, remaining } = yield* pruneOnStart({ executable: "own runtime" });
478 expect(removed).toEqual([staleVersion]);
479 expect(remaining).toEqual([packageJson.version]);
480 }),
481 );
482
483 it.effect("recognizes its runtime through a symlinked T3 home", () =>
484 Effect.gen(function* () {
485 const { removed } = yield* pruneOnStart({ executable: "own runtime", symlinkedHome: true });
486 expect(removed).toEqual([staleVersion]);
487 }),
488 );
489
490 it.effect("prunes for a boot-service server wherever it runs from", () =>
491 Effect.gen(function* () {
492 const { removed } = yield* pruneOnStart({ executable: "elsewhere", managed: true });
493 expect(removed).toEqual([staleVersion]);
494 }),
495 );
496
497 // In-place updates move the active version but leave the launch agent on the launcher's runtime.
498 it.effect.each([
499 { name: "", throughLink: false },
500 { name: " through a symlinked T3 home", throughLink: true },
501 ])("keeps the runtime a stopped service's launch agent starts$name", ({ throughLink }) =>
502 Effect.gen(function* () {
503 const { removed, remaining } = yield* pruneOnStart({
504 executable: "own runtime",
505 servicePlistStarts: staleVersion,
506 servicePlistThroughLink: throughLink,
507 });
508 expect(removed).toEqual([]);
509 expect(remaining).toEqual([staleVersion, packageJson.version].toSorted());
510 }),
511 );
512
513 it.effect("leaves runtimes alone for an unmanaged server started elsewhere", () =>
514 Effect.gen(function* () {
515 const { removed, remaining } = yield* pruneOnStart({ executable: "elsewhere" });
516 expect(removed).toEqual([]);
517 expect(remaining).toEqual([staleVersion, packageJson.version].toSorted());
518 }),
519 );
520});
521
522it("a host crosses tracks only to a release that knows its whole database", () => {
523 const cross = (
524 serverVersion: string,
525 targetVersion: string,
526 targetDatabaseMigration: number | undefined,
527 databaseMigration = 72,
528 ) =>
529 ServerSelfUpdate.trackSwitchBlockReason({
530 serverVersion,
531 targetVersion,
532 targetDatabaseMigration,
533 databaseMigration: () => databaseMigration,
534 });
535 const nightly = "0.1.4-nightly.20261027.3796";
536
537 expect(cross(nightly, "0.1.4", 72)).toBeNull();
538 // Another client moved this host to a newer Nightly after the switch was planned.
539 expect(cross(nightly, "0.1.4", 72, 73)).toContain("does not support yet");
540 expect(cross(nightly, "0.1.4", undefined)).toContain("Cannot confirm");
541 // A Stable tag can carry a migration the latest Nightly lacks.
542 expect(cross("0.1.4", nightly, 72, 73)).toContain("does not support yet");
543 expect(cross("0.1.3", "0.1.4", undefined)).toBeNull();
544 // A database that exists but cannot be read proves nothing.
545 expect(
546 ServerSelfUpdate.trackSwitchBlockReason({
547 serverVersion: nightly,
548 targetVersion: "0.1.4",
549 targetDatabaseMigration: 72,
550 databaseMigration: () => undefined,
551 }),
552 ).toContain("Cannot confirm");
553});