Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

install.test.ts

197 lines8.5 KB

1// @effect-diagnostics nodeBuiltinImport:off - Drives the real shell installer against a local GitHub fixture.
2import * as HostProcess from "@t3tools/shared/HostProcess";
3import * as NodeChildProcess from "node:child_process";
4import * as NodeCrypto from "node:crypto";
5import * as NodeFSP from "node:fs/promises";
6import * as NodeHttp from "node:http";
7import * as NodeOS from "node:os";
8import * as NodePath from "node:path";
9import { describe, expect, it } from "vite-plus/test";
10​
11const nightlies = Array.from({ length: 100 }, (_, index) => ({
12 tag_name: `v1.2.4-nightly.20261020.${200 - index}`,
13}));
14​
15// regression: #22490
16describe.skipIf(HostProcess.Platform.defaultValue() !== "linux")("installer channel lookup", () => {
17 it.each([
18 ["stable", "1.2.3"],
19 ["preview", "1.2.4-preview.20261019.50"],
20 ])("finds the newest %s release behind a full page of nightlies", async (channel, version) => {
21 const root = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-install-index-"));
22 const target = NodePath.join(root, "home/runtime/versions", version);
23 const server = NodeHttp.createServer((request, response) => {
24 const url = new URL(request.url ?? "/", "http://fixture");
25 const body =
26 url.pathname === "/pingdotgg/t3code/releases/latest" &&
27 request.headers.accept === "application/json"
28 ? { tag_name: "v1.2.3" }
29 : url.pathname === "/repos/pingdotgg/t3code/releases"
30 ? { "1": nightlies, "2": [{ tag_name: `v${version}` }, { tag_name: "v1.2.3" }] }[
31 url.searchParams.get("page") ?? ""
32 ]
33 : undefined;
34 if (body === undefined) response.statusCode = 404;
35 // The API pretty-prints and the installer reads it line by line.
36 response.end(JSON.stringify(body ?? [], null, 2));
37 });
38 try {
39 // An existing completed install isolates release resolution from archive downloads.
40 await NodeFSP.mkdir(target, { recursive: true });
41 await NodeFSP.writeFile(NodePath.join(target, ".install-complete"), `${version}\n`);
42 await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
43 const address = server.address();
44 if (!address || typeof address === "string") throw new Error("Expected a TCP listener");
45 const origin = `http://127.0.0.1:${address.port}`;
46 const source = await NodeFSP.readFile(
47 NodePath.resolve(import.meta.dirname, "install.sh"),
48 "utf8",
49 );
50 const installer = NodePath.join(root, "install.sh");
51 await NodeFSP.writeFile(installer, source.replace(/https:\/\/(api\.)?github\.com/g, origin));
52 const { code, output } = await new Promise<{ code: number | null; output: string }>(
53 (resolve, reject) => {
54 const child = NodeChildProcess.spawn("sh", [installer], {
55 env: {
56 ...process.env,
57 T3CODE_VERSION: "",
58 T3CODE_CHANNEL: channel,
59 T3CODE_HOME: NodePath.join(root, "home"),
60 T3CODE_INSTALL_BIN_DIR: NodePath.join(root, "bin"),
61 // A wrong version must fail here instead of downloading a real release.
62 T3CODE_RELEASE_BASE_URL: origin,
63 },
64 stdio: ["ignore", "pipe", "pipe"],
65 });
66 let output = "";
67 child.stdout.on("data", (chunk: Buffer) => (output += chunk.toString()));
68 child.stderr.on("data", (chunk: Buffer) => (output += chunk.toString()));
69 child.on("error", reject);
70 child.on("close", (code) => resolve({ code, output }));
71 },
72 );
73 expect(code, output).toBe(0);
74 expect(output).toContain(`Installed T3 Code ${version}`);
75 } finally {
76 server.closeAllConnections();
77 await new Promise<void>((resolve) => server.close(() => resolve()));
78 await NodeFSP.rm(root, { recursive: true, force: true });
79 }
80 });
81});
82​
83describe.skipIf(HostProcess.Platform.defaultValue() !== "linux")("installer checksum", () => {
84 it.each(
85 ["home\\data", "home\ndata"].flatMap((homeName) =>
86 [true, false].map((validChecksum) => ({ homeName, validChecksum })),
87 ),
88 )(
89 "verifies home $homeName with valid checksum $validChecksum",
90 async ({ homeName, validChecksum }) => {
91 const availableTools = ["sha256sum", "shasum"].filter(
92 (tool) =>
93 NodeChildProcess.spawnSync("sh", ["-c", 'command -v "$1"', "sh", tool]).status === 0,
94 );
95 expect(availableTools.length).toBeGreaterThan(0);
96 for (const checksumTool of availableTools) {
97 const root = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-install-checksum-"));
98 const home = NodePath.join(root, homeName);
99 const version = "1.2.3";
100 const stem = `t3-${version}-linux-${HostProcess.Architecture.defaultValue()}`;
101 const archiveName = `${stem}.tar.gz`;
102 const server = NodeHttp.createServer();
103 try {
104 const tools = NodePath.join(root, "tools");
105 await NodeFSP.mkdir(tools);
106 for (const command of [
107 "sh",
108 "uname",
109 "tar",
110 "gzip",
111 "cut",
112 "curl",
113 "grep",
114 "mkdir",
115 "mktemp",
116 "rm",
117 "mv",
118 "ln",
119 checksumTool,
120 ]) {
121 const executable = NodeChildProcess.execFileSync(
122 "sh",
123 ["-c", 'command -v "$1"', "sh", command],
124 { encoding: "utf8" },
125 ).trim();
126 await NodeFSP.symlink(executable, NodePath.join(tools, command));
127 }
128 await NodeFSP.mkdir(NodePath.join(root, stem));
129 await NodeFSP.writeFile(
130 NodePath.join(root, stem, "t3"),
131 "#!/bin/sh\necho 't3 v1.2.3'\n",
132 { mode: 0o755 },
133 );
134 const archivePath = NodePath.join(root, archiveName);
135 NodeChildProcess.execFileSync("tar", ["-czf", archivePath, "-C", root, stem]);
136 const archive = await NodeFSP.readFile(archivePath);
137 const digest = NodeCrypto.createHash("sha256").update(archive).digest("hex");
138 server.on("request", (request, response) => {
139 response.end(
140 request.url?.endsWith("/SHA256SUMS")
141 ? `${validChecksum ? digest : "0".repeat(64)} ${archiveName}\n`
142 : archive,
143 );
144 });
145 await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
146 const address = server.address();
147 if (!address || typeof address === "string") throw new Error("Expected a TCP listener");
148 const { code, output } = await new Promise<{ code: number | null; output: string }>(
149 (resolve, reject) => {
150 const child = NodeChildProcess.spawn(
151 "sh",
152 [NodePath.resolve(import.meta.dirname, "install.sh")],
153 {
154 env: {
155 ...process.env,
156 PATH: tools,
157 T3CODE_VERSION: version,
158 T3CODE_HOME: home,
159 T3CODE_INSTALL_BIN_DIR: NodePath.join(root, "bin"),
160 T3CODE_RELEASE_BASE_URL: `http://127.0.0.1:${address.port}`,
161 },
162 stdio: ["ignore", "pipe", "pipe"],
163 },
164 );
165 let output = "";
166 child.stdout.on("data", (chunk: Buffer) => (output += chunk.toString()));
167 child.stderr.on("data", (chunk: Buffer) => (output += chunk.toString()));
168 child.on("error", reject);
169 child.on("close", (code) => resolve({ code, output }));
170 },
171 );
172 const versions = NodePath.join(home, "runtime/versions");
173 if (validChecksum) {
174 expect(code, `${checksumTool}: ${output}`).toBe(0);
175 expect(
176 await NodeFSP.readFile(NodePath.join(versions, version, ".install-complete"), "utf8"),
177 ).toBe("1.2.3\n");
178 expect(
179 NodeChildProcess.execFileSync(NodePath.join(root, "bin/t3"), ["--version"], {
180 encoding: "utf8",
181 }).trim(),
182 ).toBe("t3 v1.2.3");
183 } else {
184 expect(code).not.toBe(0);
185 expect(output).toContain("checksum mismatch");
186 expect(output).not.toContain("Installed T3 Code");
187 expect(await NodeFSP.readdir(versions)).toEqual([]);
188 }
189 } finally {
190 server.closeAllConnections();
191 await new Promise<void>((resolve) => server.close(() => resolve()));
192 await NodeFSP.rm(root, { recursive: true, force: true });
193 }
194 }
195 },
196 );
197});