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

DesktopCliShim.test.ts

126 lines4.6 KB

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("extracts the AppImage where it cannot be mounted", () => {
68 const { shim } = fakeAppImage(false);
69 const result = run(shim, ["--version"], { APPIMAGE_EXTRACT_AND_RUN: "1" });
70 expect(result.status).toBe(3);
71 expect(result.stdout).toMatch(
72 /squashfs-root\/resources\/app\.asar\/apps\/server\/dist\/bin\.mjs/,
73 );
74 });
75​
76 it("keeps the install's T3 home under sudo, unless one is set", () => {
77 const root = tempRoot();
78 writeExecutable(NodePath.join(root, "app"), REPORTER);
79 const shim = writeShim(root, {
80 kind: "direct",
81 executable: NodePath.join(root, "app"),
82 entry: "/bin.mjs",
83 });
84 expect(run(shim, []).stdout).toContain(`home=${root}/home`);
85 expect(run(shim, [], { T3CODE_HOME: "/elsewhere" }).stdout).toContain("home=/elsewhere");
86 const relative = NodeChildProcess.spawnSync("./bin/t3", [], { cwd: root, encoding: "utf8" });
87 expect(relative.stdout).toContain(`cli=${shim}`);
88 });
89​
90 it("runs an installed app's server directly", () => {
91 const root = tempRoot();
92 const executable = NodePath.join(root, "opt", "T3 Code", "t3code");
93 writeExecutable(executable, REPORTER);
94 const shim = writeShim(root, {
95 kind: "direct",
96 executable,
97 entry: "/opt/T3's/app.asar/bin.mjs",
98 });
99 expect(run(shim, ["browser", "setup"]).stdout.split("\n").slice(1)).toEqual([
100 "/opt/T3's/app.asar/bin.mjs",
101 "browser",
102 "setup",
103 "",
104 ]);
105 });
106​
107 it("writes a Windows launcher that keeps cmd from reinterpreting paths", () => {
108 const executable = "C:\\Apps\\R&whoami&X 100%\\!CHANNEL!\\T3 Code.exe";
109 const script = renderCliShim({
110 target: { kind: "windows", executable, entry: "C:\\Apps\\server.asar\\bin.mjs" },
111 shimPath: "C:\\Users\\José\\.t3\\bin\\t3.cmd",
112 t3Home: "C:\\Users\\José\\.t3",
113 });
114 const lines = script.split("\r\n");
115 expect(lines).toContain("setlocal EnableExtensions DisableDelayedExpansion");
116 expect(lines).toContain("chcp 65001 >nul");
117 expect(lines).toContain(
118 'if exist "C:\\Apps\\R&whoami&X 100%%\\!CHANNEL!\\T3 Code.exe" goto run',
119 );
120 const bare = lines.filter(
121 (line) => line.includes("whoami") && !/"[^"]*whoami[^"]*"/.test(line),
122 );
123 expect(bare).toEqual([]);
124 expect(lines).toContain('set "T3CODE_CLI_PATH=C:\\Users\\José\\.t3\\bin\\t3.cmd"');
125 });
126});