Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Path from "effect/Path";
6import * as Schema from "effect/Schema";
7import * as Stream from "effect/Stream";
8import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
9
10import {
11 buildNpmPlatformPackages,
12 NpmPackagesArchivesMissingError,
13} from "./build-npm-platform-packages.ts";
14
15const VERSION = "1.2.3";
16const decodeManifest = Schema.decodeEffect(
17 Schema.fromJsonString(Schema.Record(Schema.String, Schema.Unknown)),
18);
19const KEYS = ["linux-x64", "darwin-arm64"] as const;
20
21const collect = <E>(stream: Stream.Stream<Uint8Array, E>) =>
22 stream.pipe(
23 Stream.decodeText(),
24 Stream.runFold(
25 () => "",
26 (acc, chunk) => acc + chunk,
27 ),
28 );
29
30const run = Effect.fn("test.run")(function* (
31 command: string,
32 args: ReadonlyArray<string>,
33 options: { readonly cwd: string; readonly env?: Record<string, string> },
34) {
35 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
36 const child = yield* spawner.spawn(
37 ChildProcess.make(command, args, { cwd: options.cwd, env: options.env ?? {} }),
38 );
39 const [stdout, stderr, exitCode] = yield* Effect.all(
40 [collect(child.stdout), collect(child.stderr), child.exitCode.pipe(Effect.map(Number))],
41 { concurrency: "unbounded" },
42 );
43 return { stdout, stderr, exitCode };
44});
45
46/** A tar.gz laid out like build-cli-archive.ts writes, with a stub `t3` that echoes its args. */
47const makeFakeArchives = Effect.fn("test.makeFakeArchives")(function* () {
48 const fs = yield* FileSystem.FileSystem;
49 const path = yield* Path.Path;
50 const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-npm-packages-test-" });
51 const archivesDir = path.join(root, "archives");
52 yield* fs.makeDirectory(archivesDir);
53 for (const key of KEYS) {
54 const stem = `t3-${VERSION}-${key}`;
55 const stage = path.join(root, "stage", key);
56 const contentDir = path.join(stage, stem);
57 for (const dir of ["client", "resource-monitor", "node_modules/node-pty"]) {
58 yield* fs.makeDirectory(path.join(contentDir, dir), { recursive: true });
59 }
60 yield* fs.writeFileString(path.join(contentDir, "client/index.html"), "<html></html>\n");
61 yield* fs.writeFileString(
62 path.join(contentDir, "t3"),
63 `#!/bin/sh\necho "stub ${key} $*"\nexit 7\n`,
64 );
65 yield* fs.chmod(path.join(contentDir, "t3"), 0o755);
66 const exit = yield* run("tar", ["-czf", path.join(archivesDir, `${stem}.tar.gz`), stem], {
67 cwd: stage,
68 });
69 assert.equal(exit.exitCode, 0, exit.stderr);
70 }
71 yield* fs.writeFileString(path.join(archivesDir, "SHA256SUMS"), "");
72 return { root, archivesDir, outputDir: path.join(root, "out") };
73});
74
75it.layer(NodeServices.layer)("build-npm-platform-packages", (it) => {
76 it.effect("refuses a partial release unless --allow-missing is passed", () =>
77 Effect.gen(function* () {
78 const fixture = yield* makeFakeArchives();
79 const error = yield* buildNpmPlatformPackages({
80 ...fixture,
81 version: VERSION,
82 allowMissing: false,
83 }).pipe(Effect.flip);
84 assert.instanceOf(error, NpmPackagesArchivesMissingError);
85 assert.deepStrictEqual((error as NpmPackagesArchivesMissingError).missing, [
86 "linux-arm64",
87 "win32-arm64",
88 "win32-x64",
89 ]);
90 }),
91 );
92
93 it.effect("builds platform packages and a launcher that execs the installed one", () =>
94 Effect.gen(function* () {
95 const fs = yield* FileSystem.FileSystem;
96 const path = yield* Path.Path;
97 const fixture = yield* makeFakeArchives();
98 const outputs = yield* buildNpmPlatformPackages({
99 ...fixture,
100 version: VERSION,
101 allowMissing: true,
102 });
103 // Platform packages in CLI_ARCHIVE_PLATFORM_KEYS order, launcher last.
104 assert.deepStrictEqual(
105 outputs.map((output) => output.name),
106 ["@t3code/t3-darwin-arm64", "@t3code/t3-linux-x64", "t3"],
107 );
108 for (const output of outputs) {
109 assert.isTrue(yield* fs.exists(output.tarball), output.tarball);
110 }
111
112 const linuxDir = path.join(fixture.outputDir, "@t3code/t3-linux-x64");
113 const linuxManifest = yield* decodeManifest(
114 yield* fs.readFileString(path.join(linuxDir, "package.json")),
115 );
116 assert.equal(linuxManifest.name, "@t3code/t3-linux-x64");
117 assert.equal(linuxManifest.version, VERSION);
118 assert.deepStrictEqual(linuxManifest.os, ["linux"]);
119 assert.deepStrictEqual(linuxManifest.cpu, ["x64"]);
120 assert.deepStrictEqual(linuxManifest.files, [
121 "t3",
122 "t3.exe",
123 "client",
124 "resource-monitor",
125 "node_modules",
126 ]);
127 assert.equal(linuxManifest.preferUnplugged, true);
128 assert.isUndefined(linuxManifest.bin);
129 // Archive contents sit at the package root, not under the archive stem.
130 assert.isTrue(yield* fs.exists(path.join(linuxDir, "client/index.html")));
131 // A root README, or npm would display a bundled dependency's.
132 assert.include(
133 yield* fs.readFileString(path.join(linuxDir, "README.md")),
134 "# @t3code/t3-linux-x64",
135 );
136 assert.isTrue(yield* fs.exists(path.join(linuxDir, "node_modules/node-pty")));
137 assert.equal(Number((yield* fs.stat(path.join(linuxDir, "t3"))).mode) & 0o111, 0o111);
138
139 const darwinManifest = yield* decodeManifest(
140 yield* fs.readFileString(
141 path.join(fixture.outputDir, "@t3code/t3-darwin-arm64/package.json"),
142 ),
143 );
144 assert.deepStrictEqual(darwinManifest.os, ["darwin"]);
145 assert.deepStrictEqual(darwinManifest.cpu, ["arm64"]);
146
147 const launcherDir = path.join(fixture.outputDir, "t3");
148 const launcherManifest = yield* decodeManifest(
149 yield* fs.readFileString(path.join(launcherDir, "package.json")),
150 );
151 assert.equal(launcherManifest.name, "t3");
152 assert.equal(launcherManifest.version, VERSION);
153 assert.deepStrictEqual(launcherManifest.bin, { t3: "./bin/t3.js" });
154 assert.deepStrictEqual(launcherManifest.files, ["bin"]);
155 assert.deepStrictEqual(launcherManifest.optionalDependencies, {
156 "@t3code/t3-darwin-arm64": VERSION,
157 "@t3code/t3-linux-x64": VERSION,
158 });
159 assert.isUndefined(launcherManifest.engines);
160 assert.isTrue(yield* fs.exists(path.join(launcherDir, "bin/t3.js")));
161
162 // The scratch dirs must not be left behind next to the packages.
163 const outputEntries = yield* fs.readDirectory(fixture.outputDir);
164 assert.deepStrictEqual(outputEntries.sort(), ["@t3code", "t3", "t3.tgz"]);
165
166 // The tarball is what gets published: it must carry node_modules (which
167 // `npm publish <dir>` would strip) under npm's `package/` root, with the
168 // executable bit intact.
169 const listing = yield* run(
170 "tar",
171 ["-tzvf", path.join(fixture.outputDir, "@t3code/t3-linux-x64.tgz")],
172 { cwd: fixture.outputDir },
173 );
174 assert.equal(listing.exitCode, 0, listing.stderr);
175 const lines = listing.stdout.split("\n");
176 assert.isTrue(lines.some((line) => line.endsWith(" package/node_modules/node-pty/")));
177 assert.isTrue(lines.some((line) => line.endsWith(" package/package.json")));
178 assert.isTrue(
179 lines.some((line) => /^-rwxr-xr-x .* package\/t3$/.test(line)),
180 listing.stdout,
181 );
182
183 // NODE_PATH stands in for node_modules: require.resolve finds the
184 // platform package there exactly as it would after `npm install`.
185 const env = { ...process.env, NODE_PATH: fixture.outputDir } as Record<string, string>;
186 const passthrough = yield* run(process.execPath, ["bin/t3.js", "serve", "--port", "1234"], {
187 cwd: launcherDir,
188 env,
189 });
190 assert.equal(passthrough.stdout.trim(), "stub linux-x64 serve --port 1234");
191 assert.equal(passthrough.exitCode, 7);
192
193 const unsupported = yield* run(process.execPath, ["bin/t3.js", "--version"], {
194 cwd: launcherDir,
195 env: { ...env, NODE_PATH: path.join(fixture.root, "nowhere") },
196 });
197 assert.equal(unsupported.exitCode, 1);
198 assert.include(unsupported.stderr, "linux-x64");
199 assert.include(unsupported.stderr, "win32-arm64");
200 assert.include(unsupported.stderr, "https://github.com/pingdotgg/t3code/releases");
201 }),
202 );
203});