Open History to see the last commit
1#!/usr/bin/env node
2import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
3import * as NodeServices from "@effect/platform-node/NodeServices";
4import * as Duration from "effect/Duration";
5import * as Effect from "effect/Effect";
6import * as Fiber from "effect/Fiber";
7import * as FileSystem from "effect/FileSystem";
8import * as Layer from "effect/Layer";
9import * as Logger from "effect/Logger";
10import * as Path from "effect/Path";
11import * as Schedule from "effect/Schedule";
12import * as Schema from "effect/Schema";
13import * as Stream from "effect/Stream";
14import { Command, Flag } from "effect/cli";
15import { ChildProcess, ChildProcessSpawner } from "effect/process";
16
17import * as NetService from "@t3tools/shared/Net";
18import * as HostProcess from "@t3tools/shared/HostProcess";
19import { windowsSystemTar } from "./lib/windows-tar.ts";
20import { FetchHttpClient, HttpClient, HttpClientRequest } from "effect/http";
21
22export class CliArchiveSmokeError extends Schema.TaggedError<CliArchiveSmokeError>()(
23 "CliArchiveSmokeError",
24 { step: Schema.String, detail: Schema.String },
25) {
26 override get message(): string {
27 return `CLI archive smoke test failed while ${this.step}: ${this.detail}`;
28 }
29}
30
31const collect = <E>(stream: Stream.Stream<Uint8Array, E>) =>
32 stream.pipe(
33 Stream.decodeText(),
34 Stream.runFold(
35 () => "",
36 (acc, chunk) => acc + chunk,
37 ),
38 );
39
40const runExecutable = Effect.fn("runExecutable")(function* (
41 executable: string,
42 args: ReadonlyArray<string>,
43 cwd: string,
44) {
45 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
46 const child = yield* spawner.spawn(
47 ChildProcess.make(executable, args, {
48 cwd,
49 // Isolate system Node and inherited launcher context from the archive smoke check.
50 env: { PATH: "", HOME: cwd, USERPROFILE: cwd, TMPDIR: cwd, TEMP: cwd },
51 extendEnv: false,
52 }),
53 );
54 const [stdout, stderr, exitCode] = yield* Effect.all(
55 [collect(child.stdout), collect(child.stderr), child.exitCode.pipe(Effect.map(Number))],
56 { concurrency: "unbounded" },
57 );
58 return { stdout, stderr, exitCode };
59});
60
61const smokeCliArchive = Effect.fn("smokeCliArchive")(function* (input: {
62 readonly archive: string;
63 readonly expectVersion: string;
64}) {
65 const fs = yield* FileSystem.FileSystem;
66 const path = yield* Path.Path;
67 const platform = yield* HostProcess.Platform;
68 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
69 const scratch = yield* fs.makeTempDirectory({ prefix: "t3-cli-smoke-" });
70 // Windows can briefly retain executable locks; cleanup retries but must not fail a successful smoke check.
71 yield* Effect.addFinalizer(() =>
72 fs.remove(scratch, { recursive: true }).pipe(
73 Effect.retry({ times: 10, schedule: Schedule.spaced("500 millis") }),
74 Effect.catch((error) =>
75 Effect.logWarning(`[cli-smoke] could not remove ${scratch}: ${error.message}`),
76 ),
77 ),
78 );
79
80 // Windows bsdtar reads zip; Git Bash's GNU tar does not.
81 const tar = platform === "win32" ? windowsSystemTar() : "tar";
82 const extract = yield* spawner
83 .spawn(ChildProcess.make(tar, ["-xf", input.archive, "-C", scratch]))
84 .pipe(Effect.flatMap((child) => child.exitCode));
85 if (Number(extract) !== 0) {
86 return yield* new CliArchiveSmokeError({
87 step: "extracting the archive",
88 detail: `tar exited with ${String(extract)}`,
89 });
90 }
91 const [root] = yield* fs.readDirectory(scratch);
92 if (root === undefined) {
93 return yield* new CliArchiveSmokeError({
94 step: "extracting the archive",
95 detail: "the archive was empty",
96 });
97 }
98 const contentDir = path.join(scratch, root);
99 const executable = path.join(contentDir, platform === "win32" ? "t3.exe" : "t3");
100 const bundledLibatomic = path.join(contentDir, "lib/libatomic.so.1");
101 for (const required of [
102 executable,
103 path.join(contentDir, "client/index.html"),
104 path.join(contentDir, "client/.vite/manifest.json"),
105 ...(platform === "linux" ? [bundledLibatomic, `${bundledLibatomic}.LICENSE`] : []),
106 ]) {
107 if (!(yield* fs.exists(required))) {
108 return yield* new CliArchiveSmokeError({
109 step: "checking the archive layout",
110 detail: `missing ${path.relative(contentDir, required)}`,
111 });
112 }
113 }
114
115 // The runner has libatomic installed; fresh Ubuntu servers do not.
116 if (platform === "linux") {
117 const ldd = yield* spawner.string(ChildProcess.make("ldd", [executable]));
118 // Older glibc prints the path as given, newer prints it resolved; compare both resolved.
119 const loaded = /libatomic\.so\.1 => (\/.*) \(0x[0-9a-f]+\)/.exec(ldd)?.[1];
120 if (
121 loaded === undefined ||
122 (yield* fs.realPath(loaded)) !== (yield* fs.realPath(bundledLibatomic))
123 ) {
124 return yield* new CliArchiveSmokeError({
125 step: "checking that t3 loads the bundled libatomic",
126 detail: ldd,
127 });
128 }
129 }
130
131 const version = yield* runExecutable(executable, ["--version"], contentDir);
132 if (version.exitCode !== 0 || !version.stdout.includes(input.expectVersion)) {
133 return yield* new CliArchiveSmokeError({
134 step: "running --version",
135 detail: `exit ${String(version.exitCode)}\n${version.stdout}${version.stderr}`,
136 });
137 }
138
139 // The service launcher loads outside the embedded CLI; without T3CODE_HOME it exits right away.
140 const launcher = yield* runExecutable(executable, ["__service-launcher"], contentDir);
141 if (launcher.exitCode !== 1 || !launcher.stderr.includes("T3CODE_HOME is required")) {
142 return yield* new CliArchiveSmokeError({
143 step: "running __service-launcher",
144 detail: `exit ${String(launcher.exitCode)}\n${launcher.stdout}${launcher.stderr}`,
145 });
146 }
147
148 const net = yield* NetService.NetService;
149 const port = yield* net.findAvailablePort(47700);
150 const home = path.join(scratch, "home");
151 const server = yield* spawner.spawn(
152 ChildProcess.make(
153 executable,
154 ["serve", "--host", "127.0.0.1", "--port", String(port), "--no-browser"],
155 {
156 cwd: contentDir,
157 env: {
158 PATH: "",
159 HOME: home,
160 USERPROFILE: home,
161 TMPDIR: scratch,
162 TEMP: scratch,
163 T3CODE_HOME: home,
164 // The archive has a release channel built in, so its events would
165 // look like a real new install.
166 T3CODE_TELEMETRY_ENABLED: "false",
167 },
168 extendEnv: false,
169 },
170 ),
171 );
172 const output = yield* Effect.forkScoped(
173 Effect.all([collect(server.stdout), collect(server.stderr)]),
174 );
175 const httpClient = yield* HttpClient.HttpClient;
176 // Probes can hang during server initialization; bound each request as SSH readiness does.
177 const probe = httpClient.execute(HttpClientRequest.get(`http://127.0.0.1:${String(port)}/`)).pipe(
178 Effect.map((response) => response.status === 200),
179 Effect.timeout(Duration.seconds(2)),
180 Effect.orElseSucceed(() => false),
181 );
182 const pollUntilReady = Effect.gen(function* () {
183 while (!(yield* probe)) {
184 yield* Effect.sleep(Duration.millis(250));
185 }
186 return true;
187 });
188 const ready = yield* pollUntilReady.pipe(
189 Effect.timeout(Duration.seconds(30)),
190 Effect.orElseSucceed(() => false),
191 );
192 yield* server.kill({ killSignal: "SIGTERM" }).pipe(Effect.ignore);
193 yield* server.exitCode.pipe(Effect.timeout(Duration.seconds(10)), Effect.ignore);
194 const [stdout, stderr] = yield* Fiber.join(output).pipe(
195 Effect.timeout(Duration.seconds(5)),
196 Effect.orElseSucceed(() => ["", ""] as const),
197 );
198 if (!ready) {
199 return yield* new CliArchiveSmokeError({
200 step: "serving from the extracted archive",
201 detail: `no 200 from / within 30s\n${stdout}${stderr}`,
202 });
203 }
204 yield* Effect.log(
205 `[cli-smoke] ${root}: --version and __service-launcher passed, and serve answered on ${String(port)}.`,
206 );
207});
208
209const command = Command.make(
210 "smoke-cli-archive",
211 {
212 archive: Flag.String("archive"),
213 expectVersion: Flag.String("expect-version"),
214 },
215 (input) => smokeCliArchive(input).pipe(Effect.scoped),
216).pipe(Command.withDescription("Extract a CLI archive and run its executable."));
217
218if (import.meta.main) {
219 Command.run(command, { version: "0.0.0" }).pipe(
220 Effect.provide(
221 Layer.mergeAll(
222 Logger.layer([Logger.consolePretty()]),
223 NodeServices.layer,
224 NetService.layer,
225 FetchHttpClient.layer,
226 ),
227 ),
228 NodeRuntime.runMain,
229 );
230}