// @effect-diagnostics nodeBuiltinImport:off - Drives the real shell installer against a local GitHub fixture.
import * as HostProcess from "@t3tools/shared/HostProcess";
import * as NodeChildProcess from "node:child_process";
import * as NodeCrypto from "node:crypto";
import * as NodeFSP from "node:fs/promises";
import * as NodeHttp from "node:http";
import * as NodeOS from "node:os";
import * as NodePath from "node:path";
import { describe, expect, it } from "vite-plus/test";
const nightlies = Array.from({ length: 100 }, (_, index) => ({
tag_name: `v1.2.4-nightly.20261020.${200 - index}`,
}));
describe.skipIf(HostProcess.Platform.defaultValue() !== "linux")("installer home", () => {
it("installs a relative home from a directory ending in a newline", async () => {
const temporaryRoot = await NodeFSP.realpath(
await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-install-home-")),
);
const root = NodePath.join(temporaryRoot, "cwd\n");
const homeName = "-custom home\\home\n";
const home = NodePath.join(root, homeName);
const version = "1.2.3";
const target = NodePath.join(home, "runtime/versions", version);
const launcher = NodePath.join(root, "bin/t3");
try {
await NodeFSP.mkdir(target, { recursive: true });
await NodeFSP.writeFile(NodePath.join(target, ".install-complete"), `${version}\n`);
await NodeFSP.writeFile(NodePath.join(target, "t3"), "#!/bin/sh\necho 't3 v1.2.3'\n", {
mode: 0o755,
});
const { status, stdout, stderr } = NodeChildProcess.spawnSync(
"sh",
[NodePath.resolve(import.meta.dirname, "install.sh")],
{
cwd: root,
env: {
...process.env,
HOME: root,
T3CODE_HOME: homeName,
T3CODE_VERSION: version,
T3CODE_INSTALL_BIN_DIR: NodePath.dirname(launcher),
T3CODE_RELEASE_BASE_URL: "http://127.0.0.1:1",
},
encoding: "utf8",
},
);
expect(status, stdout + stderr).toBe(0);
expect(stderr).toContain(`Installed T3 Code ${version}`);
expect(await NodeFSP.readlink(launcher)).toBe(NodePath.join(target, "t3"));
expect(
NodeChildProcess.execFileSync(launcher, ["--version"], {
cwd: NodeOS.tmpdir(),
encoding: "utf8",
}).trim(),
).toBe("t3 v1.2.3");
} finally {
await NodeFSP.rm(temporaryRoot, { recursive: true, force: true });
}
});
});
// regression: #22490
describe.skipIf(HostProcess.Platform.defaultValue() !== "linux")("installer channel lookup", () => {
it.each([
["stable", "1.2.3"],
["preview", "1.2.4-preview.20261019.50"],
])("finds the newest %s release behind a full page of nightlies", async (channel, version) => {
const root = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-install-index-"));
const target = NodePath.join(root, "home/runtime/versions", version);
const server = NodeHttp.createServer((request, response) => {
const url = new URL(request.url ?? "/", "http://fixture");
const body =
url.pathname === "/pingdotgg/t3code/releases/latest" &&
request.headers.accept === "application/json"
? { tag_name: "v1.2.3" }
: url.pathname === "/repos/pingdotgg/t3code/releases"
? { "1": nightlies, "2": [{ tag_name: `v${version}` }, { tag_name: "v1.2.3" }] }[
url.searchParams.get("page") ?? ""
]
: undefined;
if (body === undefined) response.statusCode = 404;
// The API pretty-prints and the installer reads it line by line.
response.end(JSON.stringify(body ?? [], null, 2));
});
try {
// An existing completed install isolates release resolution from archive downloads.
await NodeFSP.mkdir(target, { recursive: true });
await NodeFSP.writeFile(NodePath.join(target, ".install-complete"), `${version}\n`);
await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
const address = server.address();
if (!address || typeof address === "string") throw new Error("Expected a TCP listener");
const origin = `http://127.0.0.1:${address.port}`;
const source = await NodeFSP.readFile(
NodePath.resolve(import.meta.dirname, "install.sh"),
"utf8",
);
const installer = NodePath.join(root, "install.sh");
await NodeFSP.writeFile(installer, source.replace(/https:\/\/(api\.)?github\.com/g, origin));
const { code, output } = await new Promise<{ code: number | null; output: string }>(
(resolve, reject) => {
const child = NodeChildProcess.spawn("sh", [installer], {
env: {
...process.env,
T3CODE_VERSION: "",
T3CODE_CHANNEL: channel,
T3CODE_HOME: NodePath.join(root, "home"),
T3CODE_INSTALL_BIN_DIR: NodePath.join(root, "bin"),
// A wrong version must fail here instead of downloading a real release.
T3CODE_RELEASE_BASE_URL: origin,
},
stdio: ["ignore", "pipe", "pipe"],
});
let output = "";
child.stdout.on("data", (chunk: Buffer) => (output += chunk.toString()));
child.stderr.on("data", (chunk: Buffer) => (output += chunk.toString()));
child.on("error", reject);
child.on("close", (code) => resolve({ code, output }));
},
);
expect(code, output).toBe(0);
expect(output).toContain(`Installed T3 Code ${version}`);
} finally {
server.closeAllConnections();
await new Promise<void>((resolve) => server.close(() => resolve()));
await NodeFSP.rm(root, { recursive: true, force: true });
}
});
});
describe.skipIf(HostProcess.Platform.defaultValue() !== "linux")("installer checksum", () => {
it.each(
["home\\data", "home\ndata"].flatMap((homeName) =>
[true, false].map((validChecksum) => ({ homeName, validChecksum })),
),
)(
"verifies home $homeName with valid checksum $validChecksum",
async ({ homeName, validChecksum }) => {
const availableTools = ["sha256sum", "shasum"].filter(
(tool) =>
NodeChildProcess.spawnSync("sh", ["-c", 'command -v "$1"', "sh", tool]).status === 0,
);
expect(availableTools.length).toBeGreaterThan(0);
for (const checksumTool of availableTools) {
const root = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-install-checksum-"));
const home = NodePath.join(root, homeName);
const version = "1.2.3";
const stem = `t3-${version}-linux-${HostProcess.Architecture.defaultValue()}`;
const archiveName = `${stem}.tar.gz`;
const server = NodeHttp.createServer();
try {
const tools = NodePath.join(root, "tools");
await NodeFSP.mkdir(tools);
for (const command of [
"sh",
"uname",
"tar",
"gzip",
"cut",
"curl",
"grep",
"mkdir",
"mktemp",
"rm",
"mv",
"ln",
checksumTool,
]) {
const executable = NodeChildProcess.execFileSync(
"sh",
["-c", 'command -v "$1"', "sh", command],
{ encoding: "utf8" },
).trim();
await NodeFSP.symlink(executable, NodePath.join(tools, command));
}
await NodeFSP.mkdir(NodePath.join(root, stem));
await NodeFSP.writeFile(
NodePath.join(root, stem, "t3"),
"#!/bin/sh\necho 't3 v1.2.3'\n",
{ mode: 0o755 },
);
const archivePath = NodePath.join(root, archiveName);
NodeChildProcess.execFileSync("tar", ["-czf", archivePath, "-C", root, stem]);
const archive = await NodeFSP.readFile(archivePath);
const digest = NodeCrypto.createHash("sha256").update(archive).digest("hex");
server.on("request", (request, response) => {
response.end(
request.url?.endsWith("/SHA256SUMS")
? `${validChecksum ? digest : "0".repeat(64)} ${archiveName}\n`
: archive,
);
});
await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
const address = server.address();
if (!address || typeof address === "string") throw new Error("Expected a TCP listener");
const { code, output } = await new Promise<{ code: number | null; output: string }>(
(resolve, reject) => {
const child = NodeChildProcess.spawn(
"sh",
[NodePath.resolve(import.meta.dirname, "install.sh")],
{
env: {
...process.env,
PATH: tools,
T3CODE_VERSION: version,
T3CODE_HOME: home,
T3CODE_INSTALL_BIN_DIR: NodePath.join(root, "bin"),
T3CODE_RELEASE_BASE_URL: `http://127.0.0.1:${address.port}`,
},
stdio: ["ignore", "pipe", "pipe"],
},
);
let output = "";
child.stdout.on("data", (chunk: Buffer) => (output += chunk.toString()));
child.stderr.on("data", (chunk: Buffer) => (output += chunk.toString()));
child.on("error", reject);
child.on("close", (code) => resolve({ code, output }));
},
);
const versions = NodePath.join(home, "runtime/versions");
if (validChecksum) {
expect(code, `${checksumTool}: ${output}`).toBe(0);
expect(
await NodeFSP.readFile(NodePath.join(versions, version, ".install-complete"), "utf8"),
).toBe("1.2.3\n");
expect(
NodeChildProcess.execFileSync(NodePath.join(root, "bin/t3"), ["--version"], {
encoding: "utf8",
}).trim(),
).toBe("t3 v1.2.3");
} else {
expect(code).not.toBe(0);
expect(output).toContain("checksum mismatch");
expect(output).not.toContain("Installed T3 Code");
expect(await NodeFSP.readdir(versions)).toEqual([]);
}
} finally {
server.closeAllConnections();
await new Promise<void>((resolve) => server.close(() => resolve()));
await NodeFSP.rm(root, { recursive: true, force: true });
}
}
},
);
});