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