Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, expect, it } from "@effect/vitest";
3import * as HostProcess from "@t3tools/shared/HostProcess";
4import * as NetService from "@t3tools/shared/Net";
5import * as ConfigProvider from "effect/ConfigProvider";
6import * as Console from "effect/Console";
7import * as Effect from "effect/Effect";
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 { Command } from "effect/cli";
13import { afterEach, vi } from "vite-plus/test";
14
15import packageJson from "../../package.json" with { type: "json" };
16import * as BootService from "../cloud/bootService.ts";
17import { pinnedRuntimePaths } from "../cloud/pinnedRuntime.ts";
18import { repointLauncher, resolveLauncherPath, updateCommand } from "./update.ts";
19
20afterEach(() => vi.restoreAllMocks());
21
22it.layer(NodeServices.layer)("t3 update launcher", (it) => {
23 it.effect("repoints a symlink that lives in a runtime versions tree", () =>
24 Effect.gen(function* () {
25 const fs = yield* FileSystem.FileSystem;
26 const path = yield* Path.Path;
27 const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-update-" });
28 const oldExe = path.join(root, "runtime/versions/1.0.0/t3");
29 const newExe = path.join(root, "runtime/versions/2.0.0/t3");
30 const launcher = path.join(root, "bin/t3");
31 for (const file of [oldExe, newExe]) {
32 yield* fs.makeDirectory(path.dirname(file), { recursive: true });
33 yield* fs.writeFileString(file, "");
34 }
35 yield* fs.makeDirectory(path.dirname(launcher), { recursive: true });
36 yield* fs.symlink(oldExe, launcher);
37
38 const repointed = yield* repointLauncher({
39 launchedAs: launcher,
40 versionsDir: path.join(root, "runtime/versions"),
41 targetEntryPath: newExe,
42 });
43
44 assert.deepStrictEqual(Option.getOrUndefined(repointed), launcher);
45 assert.equal(yield* fs.readLink(launcher), newExe);
46 }).pipe(Effect.scoped, Effect.provideService(HostProcess.Platform, "linux")),
47 );
48
49 it.effect("leaves a plain copy or a foreign symlink alone", () =>
50 Effect.gen(function* () {
51 const fs = yield* FileSystem.FileSystem;
52 const path = yield* Path.Path;
53 const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-update-" });
54 const newExe = path.join(root, "runtime/versions/2.0.0/t3");
55 const copy = path.join(root, "copy/t3");
56 const foreign = path.join(root, "foreign/t3");
57 const elsewhere = path.join(root, "elsewhere/t3");
58 const otherHome = path.join(root, "other/runtime/versions/1.0.0/t3");
59 const otherLauncher = path.join(root, "other/bin/t3");
60 for (const file of [newExe, copy, elsewhere, otherHome]) {
61 yield* fs.makeDirectory(path.dirname(file), { recursive: true });
62 yield* fs.writeFileString(file, "");
63 }
64 yield* fs.makeDirectory(path.dirname(foreign), { recursive: true });
65 yield* fs.symlink(elsewhere, foreign);
66 yield* fs.makeDirectory(path.dirname(otherLauncher), { recursive: true });
67 yield* fs.symlink(otherHome, otherLauncher);
68
69 for (const launchedAs of [copy, foreign, otherLauncher, undefined]) {
70 const repointed = yield* repointLauncher({
71 launchedAs,
72 versionsDir: path.join(root, "runtime/versions"),
73 targetEntryPath: newExe,
74 });
75 assert.equal(repointed._tag, "None", launchedAs ?? "undefined");
76 }
77 assert.equal(yield* fs.readLink(foreign), elsewhere);
78 assert.equal(yield* fs.readLink(otherLauncher), otherHome);
79 }).pipe(Effect.scoped, Effect.provideService(HostProcess.Platform, "linux")),
80 );
81
82 it.effect("finds the launcher a bare command name resolved to on PATH", () =>
83 Effect.gen(function* () {
84 const fs = yield* FileSystem.FileSystem;
85 const path = yield* Path.Path;
86 const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-update-" });
87 const launcher = path.join(root, "bin/t3");
88 yield* fs.makeDirectory(path.dirname(launcher), { recursive: true });
89 yield* fs.writeFileString(launcher, "");
90
91 const bare = yield* resolveLauncherPath.pipe(
92 Effect.provideService(HostProcess.InvokedAs, "t3"),
93 Effect.provideService(HostProcess.Environment, {
94 PATH: `${path.join(root, "missing")}:${path.join(root, "bin")}`,
95 }),
96 Effect.provideService(HostProcess.WorkingDirectory, root),
97 );
98 const relative = yield* resolveLauncherPath.pipe(
99 Effect.provideService(HostProcess.InvokedAs, "./bin/t3"),
100 Effect.provideService(HostProcess.Environment, { PATH: "" }),
101 Effect.provideService(HostProcess.WorkingDirectory, root),
102 );
103 const absent = yield* resolveLauncherPath.pipe(
104 Effect.provideService(HostProcess.InvokedAs, "t3"),
105 Effect.provideService(HostProcess.Environment, { PATH: path.join(root, "missing") }),
106 Effect.provideService(HostProcess.WorkingDirectory, root),
107 );
108
109 assert.equal(bare, launcher);
110 assert.equal(relative, launcher);
111 assert.equal(absent, undefined);
112 }).pipe(Effect.scoped, Effect.provideService(HostProcess.Platform, "linux")),
113 );
114});
115
116it.layer(Layer.mergeAll(NodeServices.layer, NetService.layer))("t3 update service", (it) => {
117 it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
118 "reports the service switch as pending after installing the target runtime",
119 () =>
120 Effect.gen(function* () {
121 const fs = yield* FileSystem.FileSystem;
122 const path = yield* Path.Path;
123 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-update-service-" });
124 const targetVersion = "999.0.0";
125 const runtime = pinnedRuntimePaths(path, baseDir, targetVersion, "linux");
126 yield* fs.makeDirectory(runtime.versionDir, { recursive: true });
127 yield* fs.writeFileString(
128 runtime.entryPath,
129 `#!/bin/sh\nprintf 't3 v${targetVersion}\\n'\n`,
130 );
131 yield* fs.chmod(runtime.entryPath, 0o755);
132 yield* fs.writeFileString(runtime.sentinelPath, `${targetVersion}\n`);
133 const logPath = path.join(baseDir, "userdata/logs/boot-service.log");
134 const unitPath = path.join(baseDir, "t3code.service");
135 const installs: Array<Parameters<BootService.BootService["Service"]["install"]>[0]> = [];
136 vi.spyOn(BootService, "layer").mockReturnValue(
137 Layer.succeed(BootService.BootService, {
138 status: Effect.succeed({
139 supported: true,
140 installed: true,
141 current: true,
142 installedVersion: packageJson.version,
143 installedBaseDir: baseDir,
144 logPath,
145 unitPath,
146 }),
147 install: (options) =>
148 Effect.sync(() => {
149 installs.push(options);
150 return {
151 program: [runtime.entryPath],
152 baseDir,
153 logPath,
154 unitPath,
155 handedOff: true,
156 };
157 }),
158 restart: Effect.die("unexpected restart"),
159 uninstall: Effect.die("unexpected uninstall"),
160 withStopped: () => Effect.die("unexpected maintenance"),
161 }),
162 );
163 const lines: string[] = [];
164 yield* Command.runWith(updateCommand, { version: packageJson.version })([
165 targetVersion,
166 "--yes",
167 "--base-dir",
168 baseDir,
169 ]).pipe(
170 Effect.provideService(HostProcess.Platform, "linux"),
171 Effect.provideService(HostProcess.IsExecutable, false),
172 Effect.provideService(HostProcess.Environment, {}),
173 Effect.provideService(Console.Console, {
174 ...globalThis.console,
175 log: (...args) => {
176 lines.push(args.map(String).join(" "));
177 },
178 }),
179 Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} }))),
180 );
181
182 expect(installs).toEqual([{ allowDowngrade: false, start: true }]);
183 const output = lines.join("\n");
184 expect(output).not.toContain("Background service restarted");
185 expect(output).toContain(`Background service switching to ${targetVersion}`);
186 expect(output).toContain(logPath);
187 }),
188 );
189});