|
| 1 | +// @effect-diagnostics nodeBuiltinImport:off - Drives the real shell installer against a local GitHub fixture. |
| 2 | +import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 3 | +import * as NodeChildProcess from "node:child_process"; |
| 4 | +import * as NodeFSP from "node:fs/promises"; |
| 5 | +import * as NodeHttp from "node:http"; |
| 6 | +import * as NodeOS from "node:os"; |
| 7 | +import * as NodePath from "node:path"; |
| 8 | +import { describe, expect, it } from "vite-plus/test"; |
| 9 | + |
| 10 | +const nightlies = Array.from({ length: 100 }, (_, index) => ({ |
| 11 | + tag_name: `v1.2.4-nightly.20261020.${200 - index}`, |
| 12 | +})); |
| 13 | + |
| 14 | +// regression: #22490 |
| 15 | +describe.skipIf(HostProcess.Platform.defaultValue() !== "linux")("installer channel lookup", () => { |
| 16 | + it.each([ |
| 17 | + ["stable", "1.2.3"], |
| 18 | + ["preview", "1.2.4-preview.20261019.50"], |
| 19 | + ])("finds the newest %s release behind a full page of nightlies", async (channel, version) => { |
| 20 | + const root = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-install-index-")); |
| 21 | + const target = NodePath.join(root, "home/runtime/versions", version); |
| 22 | + const server = NodeHttp.createServer((request, response) => { |
| 23 | + const url = new URL(request.url ?? "/", "http://fixture"); |
| 24 | + const body = |
| 25 | + url.pathname === "/pingdotgg/t3code/releases/latest" && |
| 26 | + request.headers.accept === "application/json" |
| 27 | + ? { tag_name: "v1.2.3" } |
| 28 | + : url.pathname === "/repos/pingdotgg/t3code/releases" |
| 29 | + ? { "1": nightlies, "2": [{ tag_name: `v${version}` }, { tag_name: "v1.2.3" }] }[ |
| 30 | + url.searchParams.get("page") ?? "" |
| 31 | + ] |
| 32 | + : undefined; |
| 33 | + if (body === undefined) response.statusCode = 404; |
| 34 | + // The API pretty-prints and the installer reads it line by line. |
| 35 | + response.end(JSON.stringify(body ?? [], null, 2)); |
| 36 | + }); |
| 37 | + try { |
| 38 | + // An existing completed install isolates release resolution from archive downloads. |
| 39 | + await NodeFSP.mkdir(target, { recursive: true }); |
| 40 | + await NodeFSP.writeFile(NodePath.join(target, ".install-complete"), `${version}\n`); |
| 41 | + await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve)); |
| 42 | + const address = server.address(); |
| 43 | + if (!address || typeof address === "string") throw new Error("Expected a TCP listener"); |
| 44 | + const origin = `http://127.0.0.1:${address.port}`; |
| 45 | + const source = await NodeFSP.readFile( |
| 46 | + NodePath.resolve(import.meta.dirname, "install.sh"), |
| 47 | + "utf8", |
| 48 | + ); |
| 49 | + const installer = NodePath.join(root, "install.sh"); |
| 50 | + await NodeFSP.writeFile(installer, source.replace(/https:\/\/(api\.)?github\.com/g, origin)); |
| 51 | + const { code, output } = await new Promise<{ code: number | null; output: string }>( |
| 52 | + (resolve, reject) => { |
| 53 | + const child = NodeChildProcess.spawn("sh", [installer], { |
| 54 | + env: { |
| 55 | + ...process.env, |
| 56 | + T3CODE_VERSION: "", |
| 57 | + T3CODE_CHANNEL: channel, |
| 58 | + T3CODE_HOME: NodePath.join(root, "home"), |
| 59 | + T3CODE_INSTALL_BIN_DIR: NodePath.join(root, "bin"), |
| 60 | + // A wrong version must fail here instead of downloading a real release. |
| 61 | + T3CODE_RELEASE_BASE_URL: origin, |
| 62 | + }, |
| 63 | + stdio: ["ignore", "pipe", "pipe"], |
| 64 | + }); |
| 65 | + let output = ""; |
| 66 | + child.stdout.on("data", (chunk: Buffer) => (output += chunk.toString())); |
| 67 | + child.stderr.on("data", (chunk: Buffer) => (output += chunk.toString())); |
| 68 | + child.on("error", reject); |
| 69 | + child.on("close", (code) => resolve({ code, output })); |
| 70 | + }, |
| 71 | + ); |
| 72 | + expect(code, output).toBe(0); |
| 73 | + expect(output).toContain(`Installed T3 Code ${version}`); |
| 74 | + } finally { |
| 75 | + server.closeAllConnections(); |
| 76 | + await new Promise<void>((resolve) => server.close(() => resolve())); |
| 77 | + await NodeFSP.rm(root, { recursive: true, force: true }); |
| 78 | + } |
| 79 | + }); |
| 80 | +}); |
0 commit comments