Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off - Runs the rendered launcher in a real shell.
2import * as NodeChildProcess from "node:child_process";
3import * as NodeFS from "node:fs";
4import * as NodeOS from "node:os";
5import * as NodePath from "node:path";
6
7import { afterEach, describe, expect, it } from "vite-plus/test";
8
9import { type CliShimTarget, renderCliShim } from "./DesktopCliShim.ts";
10
11const directories: Array<string> = [];
12afterEach(() => {
13 for (const directory of directories.splice(0)) {
14 NodeFS.rmSync(directory, { recursive: true, force: true });
15 }
16});
17
18const tempRoot = () => {
19 const root = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-cli-shim-"));
20 directories.push(root);
21 return root;
22};
23
24const writeExecutable = (path: string, content: string) => {
25 NodeFS.mkdirSync(NodePath.dirname(path), { recursive: true });
26 NodeFS.writeFileSync(path, content, { mode: 0o755 });
27};
28
29const writeShim = (root: string, target: CliShimTarget) => {
30 const shim = NodePath.join(root, "bin", "t3");
31 writeExecutable(shim, renderCliShim({ target, shimPath: shim, t3Home: `${root}/home` }));
32 return shim;
33};
34
35const run = (shim: string, args: ReadonlyArray<string>, env: NodeJS.ProcessEnv = {}) =>
36 NodeChildProcess.spawnSync(shim, args, {
37 encoding: "utf8",
38 env: { PATH: process.env.PATH, ...env },
39 });
40
41const REPORTER =
42 '#!/bin/sh\necho "node=$ELECTRON_RUN_AS_NODE cli=$T3CODE_CLI_PATH home=$T3CODE_HOME"\nprintf "%s\\n" "$@"\nexit 3\n';
43
44const fakeAppImage = (canMount = true) => {
45 const root = tempRoot();
46 const image = NodePath.join(root, "image");
47 writeExecutable(NodePath.join(image, "t3code"), REPORTER);
48 const appImage = NodePath.join(root, "T3 Code.AppImage");
49 writeExecutable(
50 appImage,
51 [
52 "#!/bin/sh",
53 `if [ "$1" = --appimage-extract ]; then cp -R '${image}' squashfs-root; exit 0; fi`,
54 '[ "$1" = --appimage-mount ] || exit 9',
55 '[ -n "$APPIMAGE_EXTRACT_AND_RUN" ] && exit 8',
56 canMount ? `echo '${image}'` : 'echo "Cannot mount AppImage"; exit 1',
57 `trap 'touch "${root}/unmounted"; exit 0' TERM`,
58 "while :; do sleep 0.05; done",
59 "",
60 ].join("\n"),
61 );
62 const shim = writeShim(root, { kind: "appimage", appImage, executableName: "t3code" });
63 return { root, image, appImage, shim };
64};
65
66describe("renderCliShim", () => {
67 it("runs the AppImage's bundled CLI with every argument and unmounts after", () => {
68 const { root, image, shim } = fakeAppImage();
69 const result = run(shim, ["browser", "setup", "a b"]);
70 expect(result.status).toBe(3);
71 expect(result.stdout.split("\n")).toEqual([
72 `node=1 cli=${shim} home=${root}/home`,
73 `${image}/resources/app.asar/apps/server/dist/bin.mjs`,
74 "browser",
75 "setup",
76 "a b",
77 "",
78 ]);
79 NodeChildProcess.spawnSync("sh", [
80 "-c",
81 `i=0; while [ ! -e '${root}/unmounted' ] && [ $i -lt 40 ]; do sleep 0.05; i=$((i+1)); done`,
82 ]);
83 expect(NodeFS.existsSync(NodePath.join(root, "unmounted"))).toBe(true);
84 });
85
86 it("extracts the AppImage where it cannot be mounted", () => {
87 const { shim } = fakeAppImage(false);
88 const result = run(shim, ["--version"], { APPIMAGE_EXTRACT_AND_RUN: "1" });
89 expect(result.status).toBe(3);
90 expect(result.stdout).toMatch(
91 /squashfs-root\/resources\/app\.asar\/apps\/server\/dist\/bin\.mjs/,
92 );
93 });
94
95 it("keeps the install's T3 home under sudo, unless one is set", () => {
96 const root = tempRoot();
97 writeExecutable(NodePath.join(root, "app"), REPORTER);
98 const shim = writeShim(root, {
99 kind: "direct",
100 executable: NodePath.join(root, "app"),
101 entry: "/bin.mjs",
102 });
103 // sudo clears the environment, so the launcher supplies its own.
104 expect(run(shim, []).stdout).toContain(`home=${root}/home`);
105 expect(run(shim, [], { T3CODE_HOME: "/elsewhere" }).stdout).toContain("home=/elsewhere");
106 const relative = NodeChildProcess.spawnSync("./bin/t3", [], { cwd: root, encoding: "utf8" });
107 expect(relative.stdout).toContain(`cli=${shim}`);
108 });
109
110 it("runs an installed app's server directly", () => {
111 const root = tempRoot();
112 const executable = NodePath.join(root, "opt", "T3 Code", "t3code");
113 writeExecutable(executable, REPORTER);
114 const shim = writeShim(root, {
115 kind: "direct",
116 executable,
117 entry: "/opt/T3's/app.asar/bin.mjs",
118 });
119 expect(run(shim, ["browser", "setup"]).stdout.split("\n").slice(1)).toEqual([
120 "/opt/T3's/app.asar/bin.mjs",
121 "browser",
122 "setup",
123 "",
124 ]);
125 });
126
127 it("tells a person whose app moved to reopen it", () => {
128 const root = tempRoot();
129 const shim = writeShim(root, {
130 kind: "direct",
131 executable: NodePath.join(root, "Gone.app/Contents/MacOS/T3 Code"),
132 entry: "/bin.mjs",
133 });
134 const result = run(shim, ["--version"]);
135 expect(result.status).toBe(127);
136 expect(result.stderr).toContain("Open the app once to update this command.");
137 });
138
139 it("writes a Windows launcher that keeps cmd from reinterpreting paths", () => {
140 const executable = "C:\\Apps\\R&whoami&X 100%\\!CHANNEL!\\T3 Code.exe";
141 const script = renderCliShim({
142 target: { kind: "windows", executable, entry: "C:\\Apps\\server.asar\\bin.mjs" },
143 shimPath: "C:\\Users\\José\\.t3\\bin\\t3.cmd",
144 t3Home: "C:\\Users\\José\\.t3",
145 });
146 const lines = script.split("\r\n");
147 expect(lines).toContain("setlocal EnableExtensions DisableDelayedExpansion");
148 expect(lines).toContain("chcp 65001 >nul");
149 expect(lines).toContain(
150 'if exist "C:\\Apps\\R&whoami&X 100%%\\!CHANNEL!\\T3 Code.exe" goto run',
151 );
152 const bare = lines.filter(
153 (line) => line.includes("whoami") && !/"[^"]*whoami[^"]*"/.test(line),
154 );
155 expect(bare).toEqual([]);
156 expect(lines).toContain('set "T3CODE_CLI_PATH=C:\\Users\\José\\.t3\\bin\\t3.cmd"');
157 });
158
159 it("switches the Windows console to UTF-8 only when a path needs it", () => {
160 const ascii = renderCliShim({
161 target: { kind: "windows", executable: "C:\\T3\\T3 Code.exe", entry: "C:\\T3\\bin.mjs" },
162 shimPath: "C:\\Users\\me\\.t3\\bin\\t3.cmd",
163 t3Home: "C:\\Users\\me\\.t3",
164 });
165 expect(ascii).not.toContain("chcp");
166 expect(ascii.split("\r\n")).toContain("exit /b %t3_exit%");
167 });
168});