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

update.test.ts

609 lines25.1 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, it } from "@effect/vitest";
3import type { ManagedUpdatesPolicy } from "@t3tools/contracts";
4import * as HostProcess from "@t3tools/shared/HostProcess";
5import * as Duration from "effect/Duration";
6import * as Effect from "effect/Effect";
7import * as Exit from "effect/Exit";
8import * as FileSystem from "effect/FileSystem";
9import * as Layer from "effect/Layer";
10import * as Option from "effect/Option";
11import * as Path from "effect/Path";
12import * as TestClock from "effect/testing/TestClock";
13import * as TestConsole from "effect/testing/TestConsole";
14import { HttpClient, HttpClientResponse } from "effect/http";
15import * as ChildProcess from "effect/process/ChildProcess";
16import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
17import { afterEach, vi } from "vite-plus/test";
18​
19import packageJson from "../../package.json" with { type: "json" };
20import * as BootService from "../cloud/bootService.ts";
21import {
22 pinnedRuntimePaths,
23 posixLauncherScript,
24 prunePinnedRuntimes,
25 readPosixLauncherTarget,
26 resolveLauncherPath,
27} from "../cloud/pinnedRuntime.ts";
28import * as ManagedPolicy from "../policy/ManagedPolicy.ts";
29import * as ProcessRunner from "../processRunner.ts";
30import {
31 repointLauncher,
32 resolveMirrorVersion,
33 resolveNewestVersion,
34 runUpdate,
35} from "./update.ts";
36​
37// lsof listing one process, so prune sees a process list in which no runtime is running.
38const unrelatedLsof = ProcessRunner.ProcessRunner.of({
39 run: () =>
40 Effect.succeed({
41 stdout: "p1\nftxt\nn/usr/bin/true\n",
42 stderr: "",
43 code: ChildProcessSpawner.ExitCode(0),
44 timedOut: false,
45 stdoutTruncated: false,
46 stderrTruncated: false,
47 stdoutInvalidUtf8: false,
48 stderrInvalidUtf8: false,
49 }),
50});
51​
52afterEach(() => vi.restoreAllMocks());
53​
54it.effect("authenticates each release page until the requested channel is found", () =>
55 Effect.gen(function* () {
56 const pages: string[] = [];
57 const httpClient = HttpClient.make((request) => {
58 const url = new URL(request.url);
59 assert.equal(url.origin, "https://api.github.com");
60 assert.equal(url.pathname, "/repos/pingdotgg/t3code/releases");
61 assert.equal(request.headers.accept, "application/vnd.github+json");
62 assert.equal(request.headers.authorization, "Bearer gh-token");
63 const page = url.searchParams.get("page") ?? "";
64 pages.push(page);
65 const tag_name = page === "1" ? "v1.2.4-preview.20260101.1" : "v1.2.3";
66 return Effect.succeed(HttpClientResponse.fromWeb(request, Response.json([{ tag_name }])));
67 });
68 const version = yield* resolveNewestVersion("stable").pipe(
69 Effect.provideService(HostProcess.Environment, { GH_TOKEN: "gh-token" }),
70 Effect.provideService(HttpClient.HttpClient, httpClient),
71 );
72 assert.equal(version, "1.2.3");
73 assert.deepStrictEqual(pages, ["1", "2"]);
74 }),
75);
76​
77it.effect("reads the newest version from the update feed file a mirror publishes", () =>
78 Effect.gen(function* () {
79 const requested: string[] = [];
80 const mirror = {
81 organization: "Example Corp",
82 feedUrl: "https://updates.example.com/t3code/?token=abc",
83 };
84 const resolve = (
85 channel: "stable" | "nightly" | "preview",
86 platform: NodeJS.Platform,
87 arch: string,
88 feed: string,
89 ) =>
90 resolveMirrorVersion(mirror, channel).pipe(
91 Effect.map((version): string => version),
92 Effect.catchTags({ CliUpdateError: (error) => Effect.succeed(`error: ${error.reason}`) }),
93 Effect.provideService(HostProcess.Platform, platform),
94 Effect.provideService(HostProcess.Architecture, arch),
95 Effect.provideService(
96 HttpClient.HttpClient,
97 HttpClient.make((request) => {
98 requested.push(request.url);
99 return Effect.succeed(HttpClientResponse.fromWeb(request, new Response(feed)));
100 }),
101 ),
102 );
103​
104 assert.equal(
105 yield* resolve("stable", "darwin", "arm64", "version: 1.4.0\nfiles: []\n"),
106 "1.4.0",
107 );
108 assert.equal(
109 yield* resolve("nightly", "linux", "arm64", "version: '1.5.0-nightly.20261101.2'\n"),
110 "1.5.0-nightly.20261101.2",
111 );
112 assert.deepStrictEqual(requested, [
113 "https://updates.example.com/t3code/latest-mac.yml?token=abc",
114 "https://updates.example.com/t3code/nightly-linux-arm64.yml?token=abc",
115 ]);
116 // A feed for the wrong track is not trusted to mean the requested one.
117 assert.include(
118 yield* resolve("stable", "win32", "x64", "version: 1.5.0-nightly.20261101.2\n"),
119 "Could not read latest.yml from Example Corp's update mirror.",
120 );
121 assert.include(yield* resolve("preview", "win32", "x64", ""), "has no preview builds");
122 }),
123);
124​
125it.layer(NodeServices.layer)("t3 update under a managed policy", (it) => {
126 const runWithPolicy = (
127 updates: ManagedUpdatesPolicy,
128 input: { readonly channel?: "stable" | "nightly"; readonly requestedVersion?: string } = {},
129 httpClient = HttpClient.make(() => Effect.die("unexpected release lookup")),
130 ) =>
131 runUpdate({
132 baseDir: "/unused",
133 logsDir: "/unused",
134 serverRuntimeStatePath: "/unused",
135 channel: input.channel,
136 requestedVersion: input.requestedVersion,
137 allowDowngrade: false,
138 assumeYes: true,
139 }).pipe(
140 Effect.flip,
141 Effect.provide(
142 Layer.mergeAll(
143 ManagedPolicy.layerTest({
144 schemaVersion: 1,
145 organization: "Example Corp",
146 limits: { updates },
147 }),
148 // Refusals happen before the update touches the network, the service, or a process.
149 Layer.mock(BootService.BootService, {}),
150 Layer.mock(ProcessRunner.ProcessRunner, {}),
151 Layer.succeed(HttpClient.HttpClient, httpClient),
152 ),
153 ),
154 );
155​
156 it.effect("refuses to update when updates are off", () =>
157 Effect.gen(function* () {
158 const error = yield* runWithPolicy({ mode: "off" });
159 assert.equal(error.message, "Example Corp manages T3 Code updates.");
160 }),
161 );
162​
163 it.effect("refuses nightly releases when the policy disallows them", () =>
164 Effect.gen(function* () {
165 const byChannel = yield* runWithPolicy(
166 { mode: "auto", allowNightly: false },
167 { channel: "nightly" },
168 );
169 const byVersion = yield* runWithPolicy(
170 { mode: "auto", allowNightly: false },
171 { channel: "stable", requestedVersion: "1.2.0-nightly.20261028.1" },
172 );
173 for (const error of [byChannel, byVersion]) {
174 assert.equal(
175 error.message,
176 "Example Corp manages T3 Code updates. Only stable releases can be installed.",
177 );
178 }
179 }),
180 );
181​
182 it.effect("looks for updates on the policy's mirror, never the public releases", () =>
183 Effect.gen(function* () {
184 const requested: string[] = [];
185 const error = yield* runWithPolicy(
186 { mode: "auto", feedUrl: "https://updates.example.com/t3code" },
187 { channel: "stable" },
188 HttpClient.make((request) => {
189 requested.push(new URL(request.url).origin);
190 return Effect.succeed(
191 HttpClientResponse.fromWeb(request, new Response("", { status: 404 })),
192 );
193 }),
194 );
195 assert.include(error.message, "from Example Corp's update mirror");
196 assert.deepStrictEqual(requested, ["https://updates.example.com"]);
197 }),
198 );
199});
200​
201it.layer(NodeServices.layer)("t3 update launcher", (it) => {
202 it.effect("replaces a legacy symlink with a script that execs the new version", () =>
203 Effect.gen(function* () {
204 const fs = yield* FileSystem.FileSystem;
205 const path = yield* Path.Path;
206 const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-update-" });
207 const oldExe = path.join(root, "runtime/versions/1.0.0/t3");
208 const newExe = path.join(root, "runtime/versions/2.0.0/t3");
209 const launcher = path.join(root, "bin/t3");
210 for (const file of [oldExe, newExe]) {
211 yield* fs.makeDirectory(path.dirname(file), { recursive: true });
212 yield* fs.writeFileString(file, "");
213 }
214 yield* fs.makeDirectory(path.dirname(launcher), { recursive: true });
215 yield* fs.symlink(oldExe, launcher);
216​
217 const repointed = yield* repointLauncher({
218 launchedAs: launcher,
219 baseDir: root,
220 targetEntryPath: newExe,
221 });
222​
223 assert.deepStrictEqual(Option.getOrUndefined(repointed), launcher);
224 assert.equal(yield* fs.readFileString(launcher), posixLauncherScript(newExe));
225 // Replaced, not written through.
226 assert.equal(yield* fs.readFileString(oldExe), "");
227 }).pipe(Effect.scoped, Effect.provideService(HostProcess.Platform, "linux")),
228 );
229​
230 it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
231 "finds the script that ran this executable and rewrites it, keeping aliases and arguments",
232 () =>
233 Effect.gen(function* () {
234 const fs = yield* FileSystem.FileSystem;
235 const path = yield* Path.Path;
236 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
237 const root = yield* fs.realPath(
238 yield* fs.makeTempDirectoryScoped({ prefix: "t3-update-" }),
239 );
240 // A quote and a space in the home path exercise the script's quoting.
241 const home = path.join(root, "it's home");
242 const entry = (version: string) => path.join(home, "runtime/versions", version, "t3");
243 for (const version of ["1.0.0", "2.0.0"]) {
244 yield* fs.makeDirectory(path.dirname(entry(version)), { recursive: true });
245 yield* fs.writeFileString(entry(version), '#!/bin/sh\nprintf \'[%s]\\n\' "$0" "$@"\n', {
246 mode: 0o755,
247 });
248 }
249 const launcher = path.join(root, "bin/t3");
250 const alias = path.join(root, "alias/t3");
251 yield* fs.makeDirectory(path.dirname(launcher), { recursive: true });
252 yield* fs.makeDirectory(path.dirname(alias), { recursive: true });
253 yield* fs.writeFileString(launcher, posixLauncherScript(entry("1.0.0")), { mode: 0o755 });
254 yield* fs.symlink(launcher, alias);
255 const repoint = (environment: Record<string, string>) =>
256 repointLauncher({
257 // The script execs by absolute path, so argv0 names the executable.
258 launchedAs: entry("1.0.0"),
259 baseDir: home,
260 targetEntryPath: entry("2.0.0"),
261 }).pipe(Effect.provideService(HostProcess.Environment, environment));
262​
263 assert.equal(
264 Option.getOrUndefined(yield* repoint({ PATH: path.dirname(alias) })),
265 launcher,
266 );
267 assert.equal(yield* fs.readLink(alias), launcher);
268 const args = ["two words", "it's quoted", "", "*", "$HOME"];
269 assert.equal(
270 yield* spawner.string(ChildProcess.make(alias, args, { cwd: root })),
271 [entry("2.0.0"), ...args].map((arg) => `[${arg}]\n`).join(""),
272 );
273​
274 // Like the shell, a relative PATH entry resolves from the working directory, and a
275 // script nobody can execute is passed over.
276 const unrunnable = path.join(root, "unrunnable/t3");
277 yield* fs.makeDirectory(path.dirname(unrunnable), { recursive: true });
278 yield* fs.writeFileString(unrunnable, posixLauncherScript(entry("2.0.0")), { mode: 0o644 });
279 yield* fs.writeFileString(launcher, posixLauncherScript(entry("2.0.0")), { mode: 0o755 });
280 const relative = yield* repointLauncher({
281 launchedAs: entry("2.0.0"),
282 baseDir: home,
283 targetEntryPath: entry("1.0.0"),
284 }).pipe(
285 Effect.provideService(HostProcess.Environment, {
286 PATH: `${path.dirname(unrunnable)}:bin`,
287 }),
288 Effect.provideService(HostProcess.WorkingDirectory, root),
289 );
290 assert.equal(Option.getOrUndefined(relative), launcher);
291 assert.equal(yield* fs.readFileString(unrunnable), posixLauncherScript(entry("2.0.0")));
292​
293 // An edited script is the user's own wrapper now.
294 const edited = `${posixLauncherScript(entry("1.0.0"))}# mine\n`;
295 yield* fs.writeFileString(launcher, edited);
296 assert.isTrue(Option.isNone(yield* repoint({ PATH: path.dirname(launcher) })));
297 assert.equal(yield* fs.readFileString(launcher), edited);
298 }).pipe(Effect.scoped, Effect.provideService(HostProcess.Platform, "linux")),
299 );
300​
301 it.effect.each([
302 { name: "no launcher records", recorded: undefined },
303 { name: "an empty launcher record", recorded: "" },
304 { name: "whole launcher records", recorded: "/opt/t3\n" },
305 { name: "Windows launcher records", recorded: "C:\\t3.cmd\r\n" },
306 // An interrupted append cut this record short.
307 { name: "an unterminated launcher record", recorded: "/opt/t" },
308 // Another process's append is cut short after t3 update reads the records.
309 { name: "a record cut short after the read", recorded: "/opt/t3\n", cut: "/opt/t" },
310 ])("keeps the runtime a repointed launcher off PATH points at with $name", ({ recorded, cut }) =>
311 Effect.gen(function* () {
312 const fs = yield* FileSystem.FileSystem;
313 const path = yield* Path.Path;
314 const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-update-" });
315 yield* TestClock.setTime(Duration.toMillis(Duration.days(1)));
316 for (const version of ["1.0.0", "2.0.0"]) {
317 const directory = path.join(root, "runtime/versions", version);
318 yield* fs.makeDirectory(directory, { recursive: true });
319 yield* fs.writeFileString(path.join(directory, "t3"), "");
320 yield* fs.writeFileString(path.join(directory, ".install-complete"), `${version}\n`);
321 yield* fs.utimes(path.join(directory, ".install-complete"), 0, 0);
322 }
323 if (recorded !== undefined) {
324 yield* fs.writeFileString(path.join(root, "runtime/launchers"), recorded);
325 }
326 // A launcher in the T3 home itself, which neither PATH nor the default bin dir reaches.
327 const launcher = path.join(root, "t3");
328 yield* fs.symlink(path.join(root, "runtime/versions/1.0.0/t3"), launcher);
329​
330 const launchersFile = path.join(root, "runtime/launchers");
331 yield* repointLauncher({
332 launchedAs: launcher,
333 baseDir: root,
334 targetEntryPath: path.join(root, "runtime/versions/2.0.0/t3"),
335 }).pipe(
336 Effect.provideService(FileSystem.FileSystem, {
337 ...fs,
338 readFileString: (file, encoding) =>
339 fs
340 .readFileString(file, encoding)
341 .pipe(
342 Effect.tap(() =>
343 cut === undefined || file !== launchersFile
344 ? Effect.void
345 : fs.writeFileString(file, cut, { flag: "a" }),
346 ),
347 ),
348 }),
349 );
350 // A later server start prunes with no idea how this machine launches t3.
351 const removed = yield* prunePinnedRuntimes({ baseDir: root, keep: [] }).pipe(
352 Effect.provideService(ProcessRunner.ProcessRunner, unrelatedLsof),
353 Effect.provideService(HostProcess.Platform, "darwin"),
354 Effect.provideService(HostProcess.Environment, { PATH: "" }),
355 Effect.provideService(HostProcess.HomeDirectory, root),
356 );
357​
358 assert.deepStrictEqual(removed, ["1.0.0"]);
359 assert.equal(
360 yield* readPosixLauncherTarget(launcher),
361 path.join(root, "runtime/versions/2.0.0/t3"),
362 );
363 }).pipe(Effect.provideService(HostProcess.Platform, "linux")),
364 );
365​
366 const launcherFixture = Effect.fnUntraced(function* () {
367 const fs = yield* FileSystem.FileSystem;
368 const path = yield* Path.Path;
369 const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-update-" });
370 const oldExe = path.join(root, "runtime/versions/1.0.0/t3");
371 const newExe = path.join(root, "runtime/versions/2.0.0/t3");
372 const launcher = path.join(root, "t3");
373 for (const file of [oldExe, newExe]) {
374 yield* fs.makeDirectory(path.dirname(file), { recursive: true });
375 yield* fs.writeFileString(file, "");
376 }
377 yield* fs.symlink(oldExe, launcher);
378 return { root, oldExe, newExe, launcher, launchersFile: path.join(root, "runtime/launchers") };
379 });
380​
381 it.effect("records a relative launcher path as absolute", () =>
382 Effect.gen(function* () {
383 const fs = yield* FileSystem.FileSystem;
384 const path = yield* Path.Path;
385 const { root, newExe, launcher, launchersFile } = yield* launcherFixture();
386​
387 yield* repointLauncher({
388 launchedAs: path.relative(path.resolve("."), launcher),
389 baseDir: root,
390 targetEntryPath: newExe,
391 });
392​
393 assert.equal(yield* fs.readFileString(launchersFile), `\n${launcher}\n`);
394 }).pipe(Effect.provideService(HostProcess.Platform, "linux")),
395 );
396​
397 it.effect("fails instead of repointing a launcher it cannot record", () =>
398 Effect.gen(function* () {
399 const fs = yield* FileSystem.FileSystem;
400 const { root, oldExe, newExe, launcher, launchersFile } = yield* launcherFixture();
401 // Appending to a directory fails the way a read-only or full disk would.
402 yield* fs.makeDirectory(launchersFile);
403​
404 const result = yield* repointLauncher({
405 launchedAs: launcher,
406 baseDir: root,
407 targetEntryPath: newExe,
408 }).pipe(Effect.exit);
409​
410 assert.isTrue(Exit.isFailure(result));
411 assert.equal(yield* fs.readLink(launcher), oldExe);
412 }).pipe(Effect.provideService(HostProcess.Platform, "linux")),
413 );
414​
415 it.effect.each(["recording", "repointing"] as const)(
416 "never leaves a repointed launcher unrecorded when t3 update dies while %s",
417 (step) =>
418 Effect.gen(function* () {
419 const fs = yield* FileSystem.FileSystem;
420 const { root, newExe, launcher, launchersFile } = yield* launcherFixture();
421​
422 yield* repointLauncher({
423 launchedAs: launcher,
424 baseDir: root,
425 targetEntryPath: newExe,
426 }).pipe(
427 Effect.provideService(FileSystem.FileSystem, {
428 ...fs,
429 writeFileString: (path, data, options) =>
430 step === "recording" && path === launchersFile
431 ? Effect.die("t3 update was killed")
432 : fs.writeFileString(path, data, options),
433 rename: (from, to) =>
434 step === "repointing" && to === launcher
435 ? Effect.die("t3 update was killed")
436 : fs.rename(from, to),
437 }),
438 Effect.exit,
439 );
440​
441 const repointed = (yield* readPosixLauncherTarget(launcher)) === newExe;
442 const recorded = (yield* fs
443 .readFileString(launchersFile)
444 .pipe(Effect.orElseSucceed(() => "")))
445 .split("\n")
446 .includes(launcher);
447 assert.isFalse(repointed && !recorded);
448 }).pipe(Effect.provideService(HostProcess.Platform, "linux")),
449 );
450​
451 it.effect("leaves a plain copy or a foreign symlink alone", () =>
452 Effect.gen(function* () {
453 const fs = yield* FileSystem.FileSystem;
454 const path = yield* Path.Path;
455 const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-update-" });
456 const newExe = path.join(root, "runtime/versions/2.0.0/t3");
457 const copy = path.join(root, "copy/t3");
458 const foreign = path.join(root, "foreign/t3");
459 const elsewhere = path.join(root, "elsewhere/t3");
460 const otherHome = path.join(root, "other/runtime/versions/1.0.0/t3");
461 const otherLauncher = path.join(root, "other/bin/t3");
462 for (const file of [newExe, copy, elsewhere, otherHome]) {
463 yield* fs.makeDirectory(path.dirname(file), { recursive: true });
464 yield* fs.writeFileString(file, "");
465 }
466 yield* fs.makeDirectory(path.dirname(foreign), { recursive: true });
467 yield* fs.symlink(elsewhere, foreign);
468 yield* fs.makeDirectory(path.dirname(otherLauncher), { recursive: true });
469 yield* fs.symlink(otherHome, otherLauncher);
470​
471 for (const launchedAs of [copy, foreign, otherLauncher, undefined]) {
472 const repointed = yield* repointLauncher({
473 launchedAs,
474 baseDir: root,
475 targetEntryPath: newExe,
476 });
477 assert.equal(repointed._tag, "None", launchedAs ?? "undefined");
478 }
479 assert.equal(yield* fs.readLink(foreign), elsewhere);
480 assert.equal(yield* fs.readLink(otherLauncher), otherHome);
481 }).pipe(Effect.provideService(HostProcess.Platform, "linux")),
482 );
483​
484 it.effect("finds the launcher a bare command name resolved to on PATH", () =>
485 Effect.gen(function* () {
486 const fs = yield* FileSystem.FileSystem;
487 const path = yield* Path.Path;
488 const delimiter = (yield* HostProcess.Platform) === "win32" ? ";" : ":";
489 const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-update-" });
490 const launcher = path.join(root, "bin/t3");
491 yield* fs.makeDirectory(path.dirname(launcher), { recursive: true });
492 yield* fs.writeFileString(launcher, "", { mode: 0o755 });
493​
494 const bare = yield* resolveLauncherPath.pipe(
495 Effect.provideService(HostProcess.InvokedAs, "t3"),
496 Effect.provideService(HostProcess.Environment, {
497 PATH: [path.join(root, "missing"), path.join(root, "bin")].join(delimiter),
498 }),
499 Effect.provideService(HostProcess.WorkingDirectory, root),
500 );
501 const relative = yield* resolveLauncherPath.pipe(
502 Effect.provideService(HostProcess.InvokedAs, "./bin/t3"),
503 Effect.provideService(HostProcess.Environment, { PATH: "" }),
504 Effect.provideService(HostProcess.WorkingDirectory, root),
505 );
506 const absent = yield* resolveLauncherPath.pipe(
507 Effect.provideService(HostProcess.InvokedAs, "t3"),
508 Effect.provideService(HostProcess.Environment, { PATH: path.join(root, "missing") }),
509 Effect.provideService(HostProcess.WorkingDirectory, root),
510 );
511​
512 assert.equal(bare, launcher);
513 assert.equal(relative, launcher);
514 assert.equal(absent, undefined);
515 }),
516 );
517});
518​
519// Without terminals, update skips its restart prompt whatever runs the test.
520const withoutTerminals = Effect.acquireRelease(
521 Effect.sync(() =>
522 [process.stdin, process.stdout].map((stream) => {
523 const descriptor = Object.getOwnPropertyDescriptor(stream, "isTTY");
524 Object.defineProperty(stream, "isTTY", { configurable: true, value: false });
525 return { stream, descriptor };
526 }),
527 ),
528 (saved) =>
529 Effect.sync(() => {
530 for (const { stream, descriptor } of saved) {
531 if (descriptor === undefined) Reflect.deleteProperty(stream, "isTTY");
532 else Object.defineProperty(stream, "isTTY", descriptor);
533 }
534 }),
535);
536​
537it.layer(NodeServices.layer)("t3 update with a background service", (it) => {
538 it.effect("names the version a service with a pending restart still runs", () =>
539 Effect.gen(function* () {
540 yield* withoutTerminals;
541 const fs = yield* FileSystem.FileSystem;
542 const path = yield* Path.Path;
543 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-update-" });
544 const version = packageJson.version;
545 const runtime = pinnedRuntimePaths(path, baseDir, version, "linux");
546 yield* fs.makeDirectory(runtime.versionDir, { recursive: true });
547 yield* fs.writeFileString(runtime.entryPath, "");
548 yield* fs.writeFileString(runtime.sentinelPath, `${version}\n`);
549 const unitPath = path.join(baseDir, "t3code.service");
550 const logPath = path.join(baseDir, "boot-service.log");
551 // An earlier `t3 update` pointed the service at this version without restarting it.
552 const service = Layer.mock(BootService.BootService, {
553 status: Effect.succeed<BootService.BootServiceStatus>({
554 supported: true,
555 installed: true,
556 current: false,
557 installedVersion: version,
558 installedBaseDir: baseDir,
559 problems: ["restart-pending"],
560 unitPath,
561 logPath,
562 }),
563 install: () => Effect.succeed({ program: [runtime.entryPath], baseDir, unitPath, logPath }),
564 });
565 vi.spyOn(BootService, "layer").mockReturnValue(service);
566 const runner = ProcessRunner.ProcessRunner.of({
567 run: (input) =>
568 input.args?.includes("--version") === true
569 ? unrelatedLsof
570 .run(input)
571 .pipe(Effect.map((result) => ({ ...result, stdout: `t3 v${version}\n` })))
572 : unrelatedLsof.run(input),
573 });
574​
575 const output = yield* runUpdate({
576 baseDir,
577 logsDir: baseDir,
578 serverRuntimeStatePath: path.join(baseDir, "server-runtime.json"),
579 channel: undefined,
580 requestedVersion: version,
581 allowDowngrade: false,
582 assumeYes: false,
583 }).pipe(
584 Effect.andThen(TestConsole.logLines),
585 Effect.map((lines) => lines.join("\n")),
586 Effect.provide(
587 Layer.mergeAll(
588 service,
589 Layer.succeed(ProcessRunner.ProcessRunner, runner),
590 ManagedPolicy.layerTest(),
591 Layer.succeed(
592 HttpClient.HttpClient,
593 HttpClient.make(() => Effect.die("unexpected release lookup")),
594 ),
595 Layer.fresh(TestConsole.layer),
596 ),
597 ),
598 );
599​
600 assert.include(
601 output,
602 `Background service still running the version before ${version}. Run \`t3 service restart\` when you are ready to switch it to ${version}.`,
603 );
604 }).pipe(
605 Effect.provideService(HostProcess.Platform, "linux"),
606 Effect.provideService(HostProcess.Environment, { PATH: "" }),
607 ),
608 );
609});