Open History to see the last commit
1import * as NodeChildProcess from "node:child_process";
2import * as NodeEvents from "node:events";
3import * as NodeFS from "node:fs";
4import * as NodeOS from "node:os";
5import * as NodePath from "node:path";
6import * as NodeVM from "node:vm";
7
8import { assert, describe, it } from "vite-plus/test";
9
10import {
11 makeDevelopmentEnvironmentScript,
12 makeDevelopmentLauncherScript,
13 resolveElectronBinaryPath,
14 resolveMacLauncherPaths,
15 writeDevelopmentLauncherScript,
16} from "./electron-launcher.mjs";
17
18describe("electron development launcher", () => {
19 it("uses captured values only as fallbacks for a live runner environment", () => {
20 const environmentScript = makeDevelopmentEnvironmentScript({
21 VITE_DEV_SERVER_URL: "http://127.0.0.1:8526",
22 T3CODE_PORT: "16566",
23 T3CODE_HOME: "/tmp/t3",
24 T3CODE_OTLP_PROTOCOL: "http/protobuf",
25 });
26
27 assert.include(
28 environmentScript,
29 "if [ -z \"${VITE_DEV_SERVER_URL:-}\" ]; then export VITE_DEV_SERVER_URL='http://127.0.0.1:8526'; fi",
30 );
31 assert.include(
32 environmentScript,
33 "if [ -z \"${T3CODE_OTLP_PROTOCOL:-}\" ]; then export T3CODE_OTLP_PROTOCOL='http/protobuf'; fi",
34 );
35 assert.notInclude(environmentScript, "\nexport VITE_DEV_SERVER_URL=");
36 });
37
38 it("keeps the launcher script free of volatile environment values", () => {
39 const script = makeDevelopmentLauncherScript({
40 electronBinaryPath: "/repo/node_modules/electron/Electron",
41 mainEntryPath: "/repo/apps/desktop/dist-electron/main.cjs",
42 desktopRoot: "/repo/apps/desktop",
43 environmentFilePath: "/repo/apps/desktop/.electron-runtime/dev-environment.sh",
44 });
45
46 assert.include(
47 script,
48 "if [ -f '/repo/apps/desktop/.electron-runtime/dev-environment.sh' ]; then . '/repo/apps/desktop/.electron-runtime/dev-environment.sh'; fi",
49 );
50 assert.notInclude(script, "VITE_DEV_SERVER_URL");
51 assert.include(
52 script,
53 "exec '/repo/node_modules/electron/Electron' --t3code-dev-root='/repo/apps/desktop' '/repo/apps/desktop/dist-electron/main.cjs' \"$@\"",
54 );
55 });
56
57 it("repairs Electron before loading the package entrypoint", () => {
58 const calls = [];
59 const electronPath = resolveElectronBinaryPath({
60 ensureRuntime: () => {
61 calls.push("ensure");
62 },
63 createRequire: () => (specifier) => {
64 calls.push(`require:${specifier}`);
65 return "/repo/node_modules/electron/dist/Electron.app/Contents/MacOS/Electron";
66 },
67 moduleUrl: import.meta.url,
68 });
69
70 assert.equal(
71 electronPath,
72 "/repo/node_modules/electron/dist/Electron.app/Contents/MacOS/Electron",
73 );
74 assert.deepEqual(calls, ["ensure", "require:electron"]);
75 });
76
77 it("keeps the native Electron executable name inside the branded macOS bundle", () => {
78 const paths = resolveMacLauncherPaths(
79 "/repo/apps/desktop/.electron-runtime/T3 Code (Dev).app",
80 "T3 Code (Dev)",
81 );
82
83 assert.equal(paths.launcherExecutableName, "T3 Code (Dev) Launcher");
84 assert.equal(
85 paths.launcherBinaryPath,
86 "/repo/apps/desktop/.electron-runtime/T3 Code (Dev).app/Contents/MacOS/T3 Code (Dev) Launcher",
87 );
88 assert.equal(
89 paths.runtimeElectronBinaryPath,
90 "/repo/apps/desktop/.electron-runtime/T3 Code (Dev).app/Contents/MacOS/Electron",
91 );
92
93 const script = makeDevelopmentLauncherScript({
94 electronBinaryPath: paths.runtimeElectronBinaryPath,
95 mainEntryPath: "/repo/apps/desktop/dist-electron/main.cjs",
96 desktopRoot: "/repo/apps/desktop",
97 environmentFilePath: "/repo/apps/desktop/.electron-runtime/dev-environment.sh",
98 });
99 assert.include(
100 script,
101 "exec '/repo/apps/desktop/.electron-runtime/T3 Code (Dev).app/Contents/MacOS/Electron'",
102 );
103 assert.notInclude(script, "node_modules/electron");
104 });
105
106 it("restores execute permissions on an unchanged launcher", () => {
107 const directory = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-launcher-"));
108 const launcherPath = NodePath.join(directory, "launcher");
109 try {
110 writeDevelopmentLauncherScript(launcherPath, "/runtime/Electron");
111 NodeFS.chmodSync(launcherPath, 0o644);
112
113 assert.isFalse(writeDevelopmentLauncherScript(launcherPath, "/runtime/Electron"));
114 assert.equal(NodeFS.statSync(launcherPath).mode & 0o777, 0o755);
115 } finally {
116 NodeFS.rmSync(directory, { recursive: true, force: true });
117 }
118 });
119});
120
121async function startupCleanupPattern(desktopDir) {
122 const source = NodeFS.readFileSync(new URL("./dev-electron.mjs", import.meta.url), "utf8");
123 let pattern;
124 // Supply inert imports so the real startup runs without launching Electron or signalling processes.
125 await NodeVM.runInNewContext(
126 `(async () => { ${source.replace(/^import\s[\s\S]*?;\n/gm, "")} })()`,
127 {
128 NodeChildProcess: {
129 execFileSync: () => undefined,
130 spawnSync: (command, args) => {
131 if (command === "pkill" && args.includes("-f")) pattern = args.at(-1);
132 },
133 spawn: () => new NodeEvents.EventEmitter(),
134 },
135 NodeFS: { watch: () => ({ close: () => undefined }) },
136 NodeOS: { platform: () => "linux" },
137 NodePath,
138 desktopDir,
139 resolveDevProtocolClient: () => null,
140 resolveElectronLaunchCommand: (args) => ({ electronPath: "electron", args }),
141 waitForResources: async () => undefined,
142 URL,
143 process: {
144 env: { VITE_DEV_SERVER_URL: "http://localhost:1234" },
145 execPath: "node",
146 once: () => undefined,
147 },
148 },
149 );
150 assert.isString(pattern);
151 return pattern;
152}
153
154it
155 // oxlint-disable-next-line t3code/no-global-process-runtime -- Native pgrep is unavailable on Windows.
156 .skipIf(NodeOS.platform() === "win32")
157 .each([
158 "/tmp/t3-feature.one/apps/desktop",
159 "/tmp/t3-feature.one [two](three)+?^${four}|back\\slash*/apps/desktop",
160 ])("dev cleanup matches only the literal root argument: %s", async (desktopDir) => {
161 const pattern = await startupCleanupPattern(desktopDir);
162 const flag = `--t3code-dev-root=${desktopDir}`;
163 const argvCases = [
164 [flag],
165 [flag, "main.cjs"],
166 [flag.replace(".one", "Xone")],
167 [`${flag}-two`],
168 [`prefix${flag}`],
169 [`--t3code-dev-root=/other${desktopDir}`],
170 ];
171 const children = [];
172 try {
173 for (const args of argvCases) {
174 const child = NodeChildProcess.spawn(
175 process.execPath,
176 ["-e", "process.send('ready'); setInterval(() => {}, 1000)", "--", ...args],
177 { stdio: ["ignore", "ignore", "inherit", "ipc"] },
178 );
179 children.push(child);
180 await Promise.race([
181 NodeEvents.once(child, "message"),
182 NodeEvents.once(child, "exit").then(() => {
183 throw new Error("Fixture exited before readiness");
184 }),
185 ]);
186 }
187 // Use the native ERE engine, but never execute the launcher's signal-producing command.
188 const result = NodeChildProcess.spawnSync(
189 "pgrep",
190 ["-P", String(process.pid), "-f", "--", pattern],
191 { encoding: "utf8" },
192 );
193 assert.equal(result.status, 0, result.stderr);
194 const matches = result.stdout.trim().split(/\s+/).map(Number);
195 assert.sameMembers(
196 matches,
197 children.slice(0, 2).map((child) => child.pid),
198 );
199 } finally {
200 await Promise.all(
201 children.map(async (child) => {
202 if (child.exitCode !== null || child.signalCode !== null) return;
203 const exited = NodeEvents.once(child, "exit");
204 child.kill("SIGTERM");
205 await exited;
206 }),
207 );
208 }
209});