|
| 1 | +import { describe, expect, it } from "@effect/vitest"; |
| 2 | +import { spawn } from "node:child_process"; |
| 3 | +import { createServer } from "node:http"; |
| 4 | +import { mkdtemp, readFile, readdir, rm, cp, writeFile } from "node:fs/promises"; |
| 5 | +import { tmpdir } from "node:os"; |
| 6 | +import { basename, join, resolve } from "node:path"; |
| 7 | +import { fileURLToPath } from "node:url"; |
| 8 | + |
| 9 | +type CommandResult = { |
| 10 | + readonly exitCode: number; |
| 11 | + readonly stdout: string; |
| 12 | + readonly stderr: string; |
| 13 | +}; |
| 14 | + |
| 15 | +const repoRoot = resolve(dirnameOf(import.meta.url), ".."); |
| 16 | +const cliRoot = join(repoRoot, "apps/cli"); |
| 17 | +const distDir = join(cliRoot, "dist"); |
| 18 | + |
| 19 | +function dirnameOf(url: string): string { |
| 20 | + return resolve(fileURLToPath(new URL(".", url))); |
| 21 | +} |
| 22 | + |
| 23 | +const runCommand = async ( |
| 24 | + command: string, |
| 25 | + args: ReadonlyArray<string>, |
| 26 | + cwd: string, |
| 27 | + env: NodeJS.ProcessEnv = process.env, |
| 28 | +): Promise<CommandResult> => { |
| 29 | + const child = spawn(command, [...args], { |
| 30 | + cwd, |
| 31 | + env, |
| 32 | + stdio: ["ignore", "pipe", "pipe"], |
| 33 | + }); |
| 34 | + |
| 35 | + let stdout = ""; |
| 36 | + let stderr = ""; |
| 37 | + |
| 38 | + child.stdout.setEncoding("utf8"); |
| 39 | + child.stdout.on("data", (chunk) => { |
| 40 | + stdout += chunk; |
| 41 | + }); |
| 42 | + |
| 43 | + child.stderr.setEncoding("utf8"); |
| 44 | + child.stderr.on("data", (chunk) => { |
| 45 | + stderr += chunk; |
| 46 | + }); |
| 47 | + |
| 48 | + const exitCode = await new Promise<number>((resolveExitCode, reject) => { |
| 49 | + child.once("error", reject); |
| 50 | + child.once("close", (code) => { |
| 51 | + resolveExitCode(code ?? -1); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + return { exitCode, stdout, stderr }; |
| 56 | +}; |
| 57 | + |
| 58 | +const listen = async (server: ReturnType<typeof createServer>): Promise<number> => |
| 59 | + new Promise((resolvePort, reject) => { |
| 60 | + server.once("error", reject); |
| 61 | + server.listen(0, "127.0.0.1", () => { |
| 62 | + const address = server.address(); |
| 63 | + if (!address || typeof address === "string") { |
| 64 | + reject(new Error("Failed to resolve local release server address")); |
| 65 | + return; |
| 66 | + } |
| 67 | + resolvePort(address.port); |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | +const closeServer = async (server: ReturnType<typeof createServer>): Promise<void> => |
| 72 | + new Promise((resolveClose, reject) => { |
| 73 | + server.close((error) => { |
| 74 | + if (error) { |
| 75 | + reject(error); |
| 76 | + return; |
| 77 | + } |
| 78 | + resolveClose(); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | +const currentRuntimeBinaryName = process.platform === "win32" ? "executor.exe" : "executor"; |
| 83 | +const isSupportedPlatform = ["darwin", "linux", "win32"].includes(process.platform) && |
| 84 | + ["x64", "arm64"].includes(process.arch); |
| 85 | + |
| 86 | +describe("release bootstrap smoke", () => { |
| 87 | + it( |
| 88 | + "fresh wrapper install bootstraps locally hosted release assets and stays runnable", |
| 89 | + async () => { |
| 90 | + if (!isSupportedPlatform) { |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + const build = await runCommand("bun", ["run", "src/build.ts", "binary", "--single"], cliRoot); |
| 95 | + expect(build.exitCode, build.stderr || build.stdout).toBe(0); |
| 96 | + |
| 97 | + const assets = await runCommand("bun", ["run", "src/build.ts", "release-assets"], cliRoot); |
| 98 | + expect(assets.exitCode, assets.stderr || assets.stdout).toBe(0); |
| 99 | + |
| 100 | + const wrapperDir = join(distDir, "executor"); |
| 101 | + const assetNames = (await readdir(distDir)) |
| 102 | + .filter((entry) => /^executor-.*\.(?:tar\.gz|zip)$/.test(entry)) |
| 103 | + .sort(); |
| 104 | + |
| 105 | + expect(assetNames, `expected one current-platform asset in ${distDir}`).toHaveLength(1); |
| 106 | + |
| 107 | + const assetName = assetNames[0]!; |
| 108 | + const assetPath = join(distDir, assetName); |
| 109 | + const tempRoot = await mkdtemp(join(tmpdir(), "executor-release-bootstrap-")); |
| 110 | + const installedPackageDir = join(tempRoot, "executor"); |
| 111 | + |
| 112 | + await cp(wrapperDir, installedPackageDir, { recursive: true }); |
| 113 | + |
| 114 | + const originalPackageJson = JSON.parse( |
| 115 | + await readFile(join(installedPackageDir, "package.json"), "utf8"), |
| 116 | + ) as { version: string; homepage?: string }; |
| 117 | + |
| 118 | + const assetRoute = `/releases/download/v${originalPackageJson.version}/${assetName}`; |
| 119 | + const server = createServer(async (request, response) => { |
| 120 | + if (request.url !== assetRoute) { |
| 121 | + response.statusCode = 404; |
| 122 | + response.end("not found"); |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + const body = await readFile(assetPath); |
| 127 | + response.statusCode = 200; |
| 128 | + response.setHeader("content-length", String(body.byteLength)); |
| 129 | + response.end(body); |
| 130 | + }); |
| 131 | + |
| 132 | + try { |
| 133 | + const port = await listen(server); |
| 134 | + const packageJsonPath = join(installedPackageDir, "package.json"); |
| 135 | + await writeFile( |
| 136 | + packageJsonPath, |
| 137 | + JSON.stringify( |
| 138 | + { |
| 139 | + ...originalPackageJson, |
| 140 | + homepage: `http://127.0.0.1:${port}`, |
| 141 | + }, |
| 142 | + null, |
| 143 | + 2, |
| 144 | + ) + "\n", |
| 145 | + ); |
| 146 | + |
| 147 | + const firstRun = await runCommand( |
| 148 | + process.execPath, |
| 149 | + [join(installedPackageDir, "bin", "executor"), "--help"], |
| 150 | + installedPackageDir, |
| 151 | + ); |
| 152 | + const combinedOutput = `${firstRun.stdout}\n${firstRun.stderr}`; |
| 153 | + |
| 154 | + expect(firstRun.exitCode, combinedOutput).toBe(0); |
| 155 | + expect(combinedOutput).toContain("downloading release asset"); |
| 156 | + expect(combinedOutput).toContain(`installed ${basename(assetName, ".zip").replace(/\.tar\.gz$/, "")}`); |
| 157 | + expect(combinedOutput).not.toContain("core_bg.wasm"); |
| 158 | + expect(combinedOutput).not.toContain("ENOENT"); |
| 159 | + |
| 160 | + const installedBinaryPath = join(installedPackageDir, "bin", "runtime", currentRuntimeBinaryName); |
| 161 | + const installedBinaryStat = await readFile(installedBinaryPath); |
| 162 | + expect(installedBinaryStat.byteLength).toBeGreaterThan(0); |
| 163 | + |
| 164 | + const secondRun = await runCommand( |
| 165 | + process.execPath, |
| 166 | + [join(installedPackageDir, "bin", "executor"), "--help"], |
| 167 | + installedPackageDir, |
| 168 | + ); |
| 169 | + const secondCombinedOutput = `${secondRun.stdout}\n${secondRun.stderr}`; |
| 170 | + |
| 171 | + expect(secondRun.exitCode, secondCombinedOutput).toBe(0); |
| 172 | + expect(secondCombinedOutput).not.toContain("downloading release asset"); |
| 173 | + } finally { |
| 174 | + await closeServer(server); |
| 175 | + await rm(tempRoot, { recursive: true, force: true }); |
| 176 | + } |
| 177 | + }, |
| 178 | + 180_000, |
| 179 | + ); |
| 180 | +}); |
0 commit comments