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

externalLauncher.test.ts

1,483 lines56.3 KB

1// @effect-diagnostics nodeBuiltinImport:off - the Windows reveal smoke test drives a real PowerShell through Node process and filesystem APIs.
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";
6import * as NodeServices from "@effect/platform-node/NodeServices";
7import { assert, it } from "@effect/vitest";
8import * as ConfigProvider from "effect/ConfigProvider";
9import * as Deferred from "effect/Deferred";
10import * as Cause from "effect/Cause";
11import * as Effect from "effect/Effect";
12import * as Exit from "effect/Exit";
13import * as Fiber from "effect/Fiber";
14import * as FileSystem from "effect/FileSystem";
15import * as Layer from "effect/Layer";
16import * as Path from "effect/Path";
17import * as Sink from "effect/Sink";
18import * as Stream from "effect/Stream";
19import * as TestClock from "effect/testing/TestClock";
20import { ChildProcess, ChildProcessSpawner } from "effect/process";
21​
22import * as HostProcess from "@t3tools/shared/HostProcess";
23import { SpawnExecutableResolution } from "@t3tools/shared/shell";
24import * as ExternalLauncher from "./externalLauncher.ts";
25​
26// Tests below write `#!/bin/sh` stubs into a real temp dir and hand that
27// directory to a posix-mocked resolver as PATH. On a Windows host the temp
28// path carries a drive letter, so the posix `:` split shatters it; there is
29// no posix executable to find there anyway.
30const windowsHost = HostProcess.Platform.defaultValue() === "win32";
31​
32interface MockSpawnResult {
33 readonly exitCode?: number;
34 readonly stdout?: string;
35 /** Never deliver an exit code, like a child wedged on a broken desktop session. */
36 readonly stall?: boolean;
38​
39function makeMockDetachedHandle(input: MockSpawnResult & { readonly onUnref?: () => void } = {}) {
40 return ChildProcessSpawner.makeHandle({
41 pid: ChildProcessSpawner.ProcessId(1),
42 exitCode: input.stall
43 ? Effect.never
44 : Effect.succeed(ChildProcessSpawner.ExitCode(input.exitCode ?? 0)),
45 isRunning: Effect.succeed(true),
46 kill: () => Effect.void,
47 unref: Effect.sync(() => {
48 input.onUnref?.();
49 return Effect.void;
50 }),
51 stdin: Sink.drain,
52 stdout:
53 input.stdout === undefined
54 ? Stream.empty
55 : Stream.make(new TextEncoder().encode(input.stdout)),
56 stderr: Stream.empty,
57 all: Stream.empty,
58 getInputFd: () => Sink.drain,
59 getOutputFd: () => Stream.empty,
60 });
62​
63const layerTest = (input: {
64 readonly platform: NodeJS.Platform;
65 readonly env?: Record<string, string>;
66 readonly resolveExecutable?: (command: string) => string | undefined;
67 readonly onSpawn?: (command: ChildProcess.StandardCommand) => void;
68 readonly onUnref?: () => void;
69 readonly spawnResult?: (command: ChildProcess.StandardCommand) => MockSpawnResult | undefined;
70}) => {
71 const layerSpawner = Layer.succeed(
72 ChildProcessSpawner.ChildProcessSpawner,
73 ChildProcessSpawner.make((command) =>
74 Effect.sync(() => {
75 assert.equal(ChildProcess.isStandardCommand(command), true);
76 if (!ChildProcess.isStandardCommand(command)) {
77 throw new Error("Expected a standard command");
78 }
79 input.onSpawn?.(command);
80 return makeMockDetachedHandle({
81 ...(input.onUnref === undefined ? {} : { onUnref: input.onUnref }),
82 ...input.spawnResult?.(command),
83 });
84 }),
85 ),
86 );
87​
88 return Layer.mergeAll(
89 ExternalLauncher.layer.pipe(Layer.provide(Layer.merge(NodeServices.layer, layerSpawner))),
90 Layer.succeed(HostProcess.Platform, input.platform),
91 Layer.succeed(
92 SpawnExecutableResolution,
93 (command) => input.resolveExecutable?.(command) ?? command,
94 ),
95 ConfigProvider.layer(ConfigProvider.fromEnv({ env: input.env ?? {} })),
96 );
97};
98​
99it.effect("launches the default browser through the platform command", () => {
100 let spawned: ChildProcess.StandardCommand | undefined;
101 let didUnref = false;
102 return Effect.gen(function* () {
103 const launcher = yield* ExternalLauncher.ExternalLauncher;
104​
105 yield* launcher.launchBrowser("https://example.com/some path");
106​
107 assert.ok(spawned);
108 assert.equal(spawned.command, "xdg-open");
109 assert.deepEqual(spawned.args, ["https://example.com/some path"]);
110 assert.equal(spawned.options.detached, true);
111 assert.equal(didUnref, true);
112 }).pipe(
113 Effect.provide(
114 layerTest({
115 platform: "linux",
116 onSpawn: (command) => {
117 spawned = command;
118 },
119 onUnref: () => {
120 didUnref = true;
121 },
122 }),
123 ),
124 );
125});
126​
127it.effect("launches an installed editor with platform-safe arguments", () =>
128 Effect.gen(function* () {
129 const fileSystem = yield* FileSystem.FileSystem;
130 const path = yield* Path.Path;
131 const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });
132 yield* fileSystem.writeFileString(path.join(binDir, "code.CMD"), "@echo off\r\n");
133​
134 let spawned: ChildProcess.StandardCommand | undefined;
135 yield* Effect.gen(function* () {
136 const launcher = yield* ExternalLauncher.ExternalLauncher;
137 yield* launcher.launchEditor({
138 editor: "vscode",
139 cwd: "C:\\workspace with spaces\\src\\index.ts:12:4",
140 });
141 }).pipe(
142 Effect.provide(
143 layerTest({
144 platform: "win32",
145 env: { PATH: binDir, PATHEXT: ".COM;.EXE;.BAT;.CMD" },
146 resolveExecutable: (command) =>
147 command === "code" ? "C:\\Program Files\\Microsoft VS Code\\bin\\code.CMD" : command,
148 onSpawn: (command) => {
149 spawned = command;
150 },
151 }),
152 ),
153 );
154​
155 assert.ok(spawned);
156 assert.equal(spawned.command, '^"C:\\Program^ Files\\Microsoft^ VS^ Code\\bin\\code.CMD^"');
157 assert.deepEqual(spawned.args, [
158 '^"--goto^"',
159 '^"C:\\workspace^ with^ spaces\\src\\index.ts:12:4^"',
160 ]);
161 assert.equal(spawned.options.shell, true);
162 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
163);
164​
165for (const platform of ["darwin", "linux"] as const) {
166 it.effect.skipIf(windowsHost)(`launches Cursor in classic IDE mode on ${platform}`, () =>
167 Effect.gen(function* () {
168 const fileSystem = yield* FileSystem.FileSystem;
169 const path = yield* Path.Path;
170 const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });
171 const cursorPath = path.join(binDir, "cursor");
172 yield* fileSystem.writeFileString(cursorPath, "#!/bin/sh\n");
173 yield* fileSystem.chmod(cursorPath, 0o755);
174​
175 const spawned: ChildProcess.StandardCommand[] = [];
176 yield* Effect.gen(function* () {
177 const launcher = yield* ExternalLauncher.ExternalLauncher;
178 for (const cwd of [
179 "/workspace with spaces",
180 "/workspace with spaces/src/index.ts",
181 "/workspace with spaces/src/index.ts:12",
182 "/workspace with spaces/src/index.ts:12:4",
183 ]) {
184 yield* launcher.launchEditor({ editor: "cursor", cwd });
185 }
186 }).pipe(
187 Effect.provide(
188 layerTest({
189 platform,
190 env: { PATH: binDir },
191 onSpawn: (command) => spawned.push(command),
192 }),
193 ),
194 );
195​
196 assert.deepEqual(
197 spawned.map((command) => ({ command: command.command, args: command.args })),
198 [
199 { command: "cursor", args: ["--classic", "/workspace with spaces"] },
200 { command: "cursor", args: ["--classic", "/workspace with spaces/src/index.ts"] },
201 {
202 command: "cursor",
203 args: ["--classic", "--goto", "/workspace with spaces/src/index.ts:12"],
204 },
205 {
206 command: "cursor",
207 args: ["--classic", "--goto", "/workspace with spaces/src/index.ts:12:4"],
208 },
209 ],
210 );
211 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
212 );
214​
215it.effect("launches Cursor in classic IDE mode through the Windows command shim", () =>
216 Effect.gen(function* () {
217 const fileSystem = yield* FileSystem.FileSystem;
218 const path = yield* Path.Path;
219 const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });
220 yield* fileSystem.writeFileString(path.join(binDir, "cursor.CMD"), "@echo off\r\n");
221​
222 let spawned: ChildProcess.StandardCommand | undefined;
223 yield* Effect.gen(function* () {
224 const launcher = yield* ExternalLauncher.ExternalLauncher;
225 yield* launcher.launchEditor({
226 editor: "cursor",
227 cwd: "C:\\workspace with spaces\\src\\index.ts:12:4",
228 });
229 }).pipe(
230 Effect.provide(
231 layerTest({
232 platform: "win32",
233 env: { PATH: binDir, PATHEXT: ".COM;.EXE;.BAT;.CMD" },
234 resolveExecutable: (command) =>
235 command === "cursor" ? "C:\\Program Files\\Cursor\\bin\\cursor.CMD" : command,
236 onSpawn: (command) => {
237 spawned = command;
238 },
239 }),
240 ),
241 );
242​
243 assert.ok(spawned);
244 assert.equal(spawned.command, '^"C:\\Program^ Files\\Cursor\\bin\\cursor.CMD^"');
245 assert.deepEqual(spawned.args, [
246 '^"--classic^"',
247 '^"--goto^"',
248 '^"C:\\workspace^ with^ spaces\\src\\index.ts:12:4^"',
249 ]);
250 assert.equal(spawned.options.shell, true);
251 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
252);
253​
254it.effect.skipIf(windowsHost)("reveals a file in Finder with open -R on macOS", () =>
255 Effect.gen(function* () {
256 const fileSystem = yield* FileSystem.FileSystem;
257 const path = yield* Path.Path;
258 const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });
259 const openPath = path.join(binDir, "open");
260 yield* fileSystem.writeFileString(openPath, "#!/bin/sh\n");
261 yield* fileSystem.chmod(openPath, 0o755);
262​
263 let spawned: ChildProcess.StandardCommand | undefined;
264 yield* Effect.gen(function* () {
265 const launcher = yield* ExternalLauncher.ExternalLauncher;
266 yield* launcher.launchEditor({
267 editor: "file-manager",
268 cwd: "/workspace/media/linux-mini-v2.mp4",
269 reveal: true,
270 });
271 }).pipe(
272 Effect.provide(
273 layerTest({
274 platform: "darwin",
275 env: { PATH: binDir },
276 onSpawn: (command) => {
277 spawned = command;
278 },
279 }),
280 ),
281 );
282​
283 assert.ok(spawned);
284 assert.equal(spawned.command, "open");
285 assert.deepEqual(spawned.args, ["-R", "/workspace/media/linux-mini-v2.mp4"]);
286 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
287);
288​
289it.effect("reveals a file in File Explorer through PowerShell on Windows", () =>
290 Effect.gen(function* () {
291 const fileSystem = yield* FileSystem.FileSystem;
292 const path = yield* Path.Path;
293 const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });
294 yield* fileSystem.writeFileString(path.join(binDir, "explorer.CMD"), "@echo off\r\n");
295 // resolvePowerShellPath builds `${SYSTEMROOT}\System32\...` with Windows
296 // separators, which on the posix test filesystem is one file name.
297 const systemRoot = path.join(binDir, "system-root");
298 const powerShellPath = `${systemRoot}\\System32\\WindowsPowerShell\\v1.0\\powershell.exe`;
299 yield* fileSystem.makeDirectory(path.dirname(powerShellPath), { recursive: true });
300 yield* fileSystem.writeFileString(powerShellPath, "");
301​
302 let spawned: ChildProcess.StandardCommand | undefined;
303 const kind = yield* Effect.gen(function* () {
304 const launcher = yield* ExternalLauncher.ExternalLauncher;
305 yield* launcher.launchEditor({
306 editor: "file-manager",
307 // Web file links normalize separators even when the server runs on
308 // Windows. Explorer's `/select` switch requires Windows separators.
309 cwd: "C:/workspace with spaces/media/author's clip.mp4",
310 reveal: true,
311 });
312 return yield* launcher.resolveFileManagerRevealKind();
313 }).pipe(
314 Effect.provide(
315 layerTest({
316 platform: "win32",
317 env: { PATH: binDir, PATHEXT: ".COM;.EXE;.BAT;.CMD", SYSTEMROOT: systemRoot },
318 onSpawn: (command) => {
319 spawned = command;
320 },
321 }),
322 ),
323 );
324​
325 assert.equal(kind, "file-explorer");
326 assert.ok(spawned);
327 assert.equal(spawned.command, powerShellPath);
328 assert.deepEqual(spawned.args.slice(0, -1), [
329 "-NoProfile",
330 "-NonInteractive",
331 "-ExecutionPolicy",
332 "Bypass",
333 "-EncodedCommand",
334 ]);
335 const encodedCommand = spawned.args[spawned.args.length - 1] ?? "";
336 const decodedCommand = Buffer.from(encodedCommand, "base64").toString("utf16le");
337 // explorer.exe expects `/select,"<path>"` with only the path quoted;
338 // PowerShell 5.1's Start-Process passes the argument string verbatim.
339 assert.equal(
340 decodedCommand,
341 "$ProgressPreference = 'SilentlyContinue'; Start-Process 'explorer.exe' -ArgumentList ('/select,\"' + 'C:\\workspace with spaces\\media\\author''s clip.mp4' + '\"')",
342 );
343 assert.equal(spawned.options.shell, false);
344 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
345);
346​
347// Real-chain smoke check for the Explorer selection contract: runs the exact
348// PowerShell source the reveal launch encodes, against a stub that records
349// the raw argument tail it receives, and asserts a spaced path arrives as the
350// single `/select,"<path>"` switch. Mock argv assertions cannot prove this —
351// only Windows' own PowerShell -> CreateProcess quoting chain can, so the
352// test runs only where that chain exists.
353// oxlint-disable-next-line t3code/no-global-process-runtime -- the skip decision needs the real host platform, outside any Effect runtime.
354it.skipIf(process.platform !== "win32")(
355 "delivers the raw /select switch for spaced paths through real PowerShell",
356 { timeout: 60_000 },
357 async () => {
358 const tempDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-reveal-smoke-"));
359 try {
360 const recorderPath = NodePath.join(tempDir, "recorder.cmd");
361 const outputPath = NodePath.join(tempDir, "argv.txt");
362 NodeFS.writeFileSync(recorderPath, `@echo off\r\n>"${outputPath}" echo(%*\r\n`);
363​
364 const target = "C:/workspace with spaces/media/author's clip.mp4";
365 const explorerTarget = target.replaceAll("/", "\\");
366 const source = ExternalLauncher.buildFileExplorerRevealPowerShellSource(
367 recorderPath,
368 explorerTarget,
369 );
370 const powerShellPath = `${process.env.SYSTEMROOT ?? "C:\\Windows"}\\System32\\WindowsPowerShell\\v1.0\\powershell.exe`;
371 NodeChildProcess.execFileSync(
372 powerShellPath,
373 [
374 "-NoProfile",
375 "-NonInteractive",
376 "-ExecutionPolicy",
377 "Bypass",
378 "-EncodedCommand",
379 Buffer.from(source, "utf16le").toString("base64"),
380 ],
381 { timeout: 30_000 },
382 );
383​
384 // Start-Process returns before the recorder runs; wait for its output.
385 // The waits run outside the Effect runtime on purpose: the test
386 // exercises the real Windows process chain in real time.
387 // @effect-diagnostics-next-line globalTimers:off
388 const sleep = (millis: number) => new Promise((resolve) => setTimeout(resolve, millis));
389 // @effect-diagnostics-next-line globalDate:off
390 const deadline = Date.now() + 20_000;
391 // @effect-diagnostics-next-line globalDate:off
392 while (!NodeFS.existsSync(outputPath) && Date.now() < deadline) {
393 await sleep(100);
394 }
395 await sleep(200);
396 const recorded = NodeFS.readFileSync(outputPath, "utf8").trim();
397 assert.equal(recorded, `/select,"${explorerTarget}"`);
398 } finally {
399 NodeFS.rmSync(tempDir, { recursive: true, force: true });
400 }
401 },
402);
403​
404it.effect("does not advertise reveal on Windows when PowerShell is missing", () =>
405 Effect.gen(function* () {
406 const fileSystem = yield* FileSystem.FileSystem;
407 const path = yield* Path.Path;
408 const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });
409 yield* fileSystem.writeFileString(path.join(binDir, "explorer.CMD"), "@echo off\r\n");
410​
411 const result = yield* Effect.gen(function* () {
412 const launcher = yield* ExternalLauncher.ExternalLauncher;
413 return {
414 kind: yield* launcher.resolveFileManagerRevealKind(),
415 editors: yield* launcher.resolveAvailableEditors(),
416 };
417 }).pipe(
418 Effect.provide(
419 layerTest({
420 platform: "win32",
421 env: {
422 PATH: binDir,
423 PATHEXT: ".COM;.EXE;.BAT;.CMD",
424 SYSTEMROOT: path.join(binDir, "missing-system-root"),
425 },
426 }),
427 ),
428 );
429​
430 // Plain "open in file manager" still works through explorer; only the
431 // reveal capability, which launches PowerShell, must stay hidden.
432 assert.equal(result.editors.includes("file-manager"), true);
433 assert.isUndefined(result.kind);
434 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
435);
436​
437it.effect.skipIf(windowsHost)(
438 "reveals a WSL file in Windows File Explorer through its UNC path",
439 () =>
440 Effect.gen(function* () {
441 const fileSystem = yield* FileSystem.FileSystem;
442 const path = yield* Path.Path;
443 const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });
444 for (const name of ["explorer.exe", "powershell.exe", "xdg-open"]) {
445 const filePath = path.join(binDir, name);
446 yield* fileSystem.writeFileString(filePath, "#!/bin/sh\n");
447 yield* fileSystem.chmod(filePath, 0o755);
448 }
449​
450 let spawned: ChildProcess.StandardCommand | undefined;
451 const result = yield* Effect.gen(function* () {
452 const launcher = yield* ExternalLauncher.ExternalLauncher;
453 const kind = yield* launcher.resolveFileManagerRevealKind();
454 const editors = yield* launcher.resolveAvailableEditors();
455 yield* launcher.launchEditor({
456 editor: "file-manager",
457 cwd: "/home/t3/workspace/media/clip.mp4",
458 reveal: true,
459 });
460 return { kind, editors };
461 }).pipe(
462 Effect.provide(
463 layerTest({
464 platform: "linux",
465 env: {
466 PATH: binDir,
467 WSL_DISTRO_NAME: "Ubuntu-24.04",
468 WSL_INTEROP: "/run/WSL/1_interop",
469 },
470 onSpawn: (command) => {
471 spawned = command;
472 },
473 }),
474 ),
475 );
476​
477 assert.equal(result.kind, "file-explorer");
478 assert.equal(result.editors.includes("file-manager"), true);
479 assert.ok(spawned);
480 // The reveal routes through interop PowerShell so Explorer receives its
481 // raw `/select,"<path>"` switch even for spaced paths.
482 assert.equal(spawned.command, "powershell.exe");
483 const encodedCommand = spawned.args[spawned.args.length - 1] ?? "";
484 const decodedCommand = Buffer.from(encodedCommand, "base64").toString("utf16le");
485 assert.equal(
486 decodedCommand,
487 "$ProgressPreference = 'SilentlyContinue'; Start-Process 'explorer.exe' -ArgumentList ('/select,\"' + '\\\\wsl.localhost\\Ubuntu-24.04\\home\\t3\\workspace\\media\\clip.mp4' + '\"')",
488 );
489 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
490);
491​
492it.effect.skipIf(windowsHost)(
493 "does not advertise reveal from WSL when interop PowerShell is missing",
494 () =>
495 Effect.gen(function* () {
496 const fileSystem = yield* FileSystem.FileSystem;
497 const path = yield* Path.Path;
498 const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });
499 const explorerPath = path.join(binDir, "explorer.exe");
500 yield* fileSystem.writeFileString(explorerPath, "");
501 yield* fileSystem.chmod(explorerPath, 0o755);
502​
503 const result = yield* Effect.gen(function* () {
504 const launcher = yield* ExternalLauncher.ExternalLauncher;
505 return {
506 kind: yield* launcher.resolveFileManagerRevealKind(),
507 editors: yield* launcher.resolveAvailableEditors(),
508 };
509 }).pipe(
510 Effect.provide(
511 layerTest({
512 platform: "linux",
513 env: {
514 PATH: binDir,
515 WSL_DISTRO_NAME: "Ubuntu-24.04",
516 WSL_INTEROP: "/run/WSL/1_interop",
517 },
518 }),
519 ),
520 );
521​
522 assert.equal(result.editors.includes("file-manager"), true);
523 assert.isUndefined(result.kind);
524 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
525);
526​
527// When interop PowerShell is missing the capability advertises the Linux
528// "files" kind (or nothing), so the reveal must open the Linux file manager
529// the label promised even though plain open still prefers File Explorer.
530it.effect.skipIf(windowsHost)(
531 "reveals through the Linux file manager when WSL lacks interop PowerShell",
532 () =>
533 Effect.gen(function* () {
534 const fileSystem = yield* FileSystem.FileSystem;
535 const path = yield* Path.Path;
536 const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });
537 for (const name of ["explorer.exe", "xdg-open", "xdg-mime"]) {
538 const filePath = path.join(binDir, name);
539 yield* fileSystem.writeFileString(filePath, "#!/bin/sh\n");
540 yield* fileSystem.chmod(filePath, 0o755);
541 }
542​
543 const spawnedCommands: ChildProcess.StandardCommand[] = [];
544 const kind = yield* Effect.gen(function* () {
545 const launcher = yield* ExternalLauncher.ExternalLauncher;
546 const revealKind = yield* launcher.resolveFileManagerRevealKind();
547 yield* launcher.launchEditor({
548 editor: "file-manager",
549 cwd: "/home/t3/workspace/media/clip.mp4",
550 reveal: true,
551 });
552 return revealKind;
553 }).pipe(
554 Effect.provide(
555 layerTest({
556 platform: "linux",
557 env: {
558 PATH: binDir,
559 WSL_DISTRO_NAME: "Ubuntu-24.04",
560 WSL_INTEROP: "/run/WSL/1_interop",
561 DISPLAY: ":0",
562 },
563 onSpawn: (command) => {
564 spawnedCommands.push(command);
565 },
566 spawnResult: (command) =>
567 command.command === "xdg-mime"
568 ? { stdout: "org.gnome.Nautilus.desktop\n" }
569 : undefined,
570 }),
571 ),
572 );
573​
574 assert.equal(kind, "files");
575 const launch = spawnedCommands.find((command) => command.command === "xdg-open");
576 assert.ok(launch);
577 assert.deepEqual(launch.args, ["/home/t3/workspace/media"]);
578 assert.isUndefined(spawnedCommands.find((command) => command.command === "explorer.exe"));
579 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
580);
581​
582// Interop can exist without `explorer.exe` on PATH (appendWindowsPath=false)
583// while WSLg still provides a working Linux file manager; the host must keep
584// the Linux open/reveal path instead of losing the editor entirely.
585it.effect.skipIf(windowsHost)(
586 "falls back to the Linux file manager when WSL lacks the Explorer bridge",
587 () =>
588 Effect.gen(function* () {
589 const fileSystem = yield* FileSystem.FileSystem;
590 const path = yield* Path.Path;
591 const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });
592 for (const name of ["xdg-open", "xdg-mime"]) {
593 const filePath = path.join(binDir, name);
594 yield* fileSystem.writeFileString(filePath, "#!/bin/sh\n");
595 yield* fileSystem.chmod(filePath, 0o755);
596 }
597​
598 const spawnedCommands: ChildProcess.StandardCommand[] = [];
599 const result = yield* Effect.gen(function* () {
600 const launcher = yield* ExternalLauncher.ExternalLauncher;
601 const editors = yield* launcher.resolveAvailableEditors();
602 const kind = yield* launcher.resolveFileManagerRevealKind();
603 yield* launcher.launchEditor({
604 editor: "file-manager",
605 cwd: "/home/t3/workspace/media/clip.mp4",
606 reveal: true,
607 });
608 return { editors, kind };
609 }).pipe(
610 Effect.provide(
611 layerTest({
612 platform: "linux",
613 env: {
614 PATH: binDir,
615 WSL_DISTRO_NAME: "Ubuntu-24.04",
616 WSL_INTEROP: "/run/WSL/1_interop",
617 DISPLAY: ":0",
618 },
619 onSpawn: (command) => {
620 spawnedCommands.push(command);
621 },
622 spawnResult: (command) =>
623 command.command === "xdg-mime"
624 ? { stdout: "org.gnome.Nautilus.desktop\n" }
625 : undefined,
626 }),
627 ),
628 );
629​
630 assert.equal(result.editors.includes("file-manager"), true);
631 assert.equal(result.kind, "files");
632 const launch = spawnedCommands.find((command) => command.command === "xdg-open");
633 assert.ok(launch);
634 assert.deepEqual(launch.args, ["/home/t3/workspace/media"]);
635 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
636);
637​
638it.effect.skipIf(windowsHost)(
639 "falls back to opening the containing directory for WSL paths Explorer cannot select",
640 () =>
641 Effect.gen(function* () {
642 const fileSystem = yield* FileSystem.FileSystem;
643 const path = yield* Path.Path;
644 const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });
645 for (const name of ["explorer.exe", "powershell.exe"]) {
646 const filePath = path.join(binDir, name);
647 yield* fileSystem.writeFileString(filePath, "#!/bin/sh\n");
648 yield* fileSystem.chmod(filePath, 0o755);
649 }
650​
651 let spawned: ChildProcess.StandardCommand | undefined;
652 yield* Effect.gen(function* () {
653 const launcher = yield* ExternalLauncher.ExternalLauncher;
654 yield* launcher.launchEditor({
655 editor: "file-manager",
656 cwd: '/home/t3/work "quoted"/clip.mp4',
657 reveal: true,
658 });
659 }).pipe(
660 Effect.provide(
661 layerTest({
662 platform: "linux",
663 env: {
664 PATH: binDir,
665 WSL_DISTRO_NAME: "Ubuntu-24.04",
666 WSL_INTEROP: "/run/WSL/1_interop",
667 },
668 onSpawn: (command) => {
669 spawned = command;
670 },
671 }),
672 ),
673 );
674​
675 // Explorer's raw switch cannot express a double quote, so the launch
676 // opens the parent directory instead of misparsing a /select argument.
677 assert.ok(spawned);
678 assert.equal(spawned.command, "explorer.exe");
679 assert.deepEqual(spawned.args, ['\\\\wsl.localhost\\Ubuntu-24.04\\home\\t3\\work "quoted"']);
680 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
681);
682​
683it.effect.skipIf(windowsHost)("reveals by opening the containing directory on Linux", () =>
684 Effect.gen(function* () {
685 const fileSystem = yield* FileSystem.FileSystem;
686 const path = yield* Path.Path;
687 const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });
688 for (const name of ["xdg-open", "xdg-mime"]) {
689 const filePath = path.join(binDir, name);
690 yield* fileSystem.writeFileString(filePath, "#!/bin/sh\n");
691 yield* fileSystem.chmod(filePath, 0o755);
692 }
693​
694 const spawnedCommands: ChildProcess.StandardCommand[] = [];
695 yield* Effect.gen(function* () {
696 const launcher = yield* ExternalLauncher.ExternalLauncher;
697 yield* launcher.launchEditor({
698 editor: "file-manager",
699 cwd: "/workspace/media/linux-mini-v2.mp4",
700 reveal: true,
701 });
702 }).pipe(
703 Effect.provide(
704 layerTest({
705 platform: "linux",
706 env: { PATH: binDir, DISPLAY: ":0" },
707 onSpawn: (command) => {
708 spawnedCommands.push(command);
709 },
710 spawnResult: (command) =>
711 command.command === "xdg-mime" ? { stdout: "org.gnome.Nautilus.desktop\n" } : undefined,
712 }),
713 ),
714 );
715​
716 const spawned = spawnedCommands.find((command) => command.command === "xdg-open");
717 assert.ok(spawned);
718 assert.deepEqual(spawned.args, ["/workspace/media"]);
719 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
720);
721​
722it.effect.skipIf(windowsHost)(
723 "does not advertise a Linux file manager without a graphical session",
724 () =>
725 Effect.gen(function* () {
726 const fileSystem = yield* FileSystem.FileSystem;
727 const path = yield* Path.Path;
728 const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });
729 const xdgOpenPath = path.join(binDir, "xdg-open");
730 yield* fileSystem.writeFileString(xdgOpenPath, "#!/bin/sh\n");
731 yield* fileSystem.chmod(xdgOpenPath, 0o755);
732​
733 const editors = yield* Effect.gen(function* () {
734 const launcher = yield* ExternalLauncher.ExternalLauncher;
735 return yield* launcher.resolveAvailableEditors();
736 }).pipe(Effect.provide(layerTest({ platform: "linux", env: { PATH: binDir } })));
737​
738 assert.equal(editors.includes("file-manager"), false);
739 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
740);
741​
742it.effect.skipIf(windowsHost)(
743 "advertises a Linux file manager when a directory handler is installed",
744 () =>
745 Effect.gen(function* () {
746 const fileSystem = yield* FileSystem.FileSystem;
747 const path = yield* Path.Path;
748 const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });
749 for (const name of ["xdg-open", "xdg-mime"]) {
750 const filePath = path.join(binDir, name);
751 yield* fileSystem.writeFileString(filePath, "#!/bin/sh\n");
752 yield* fileSystem.chmod(filePath, 0o755);
753 }
754​
755 let probe: ChildProcess.StandardCommand | undefined;
756 const editors = yield* Effect.gen(function* () {
757 const launcher = yield* ExternalLauncher.ExternalLauncher;
758 return yield* launcher.resolveAvailableEditors();
759 }).pipe(
760 Effect.provide(
761 layerTest({
762 platform: "linux",
763 env: { PATH: binDir, DISPLAY: ":0" },
764 onSpawn: (command) => {
765 probe = command;
766 },
767 spawnResult: (command) =>
768 command.command === "xdg-mime"
769 ? { stdout: "org.gnome.Nautilus.desktop\n" }
770 : undefined,
771 }),
772 ),
773 );
774​
775 assert.equal(editors.includes("file-manager"), true);
776 assert.ok(probe);
777 assert.equal(probe.command, "xdg-mime");
778 assert.deepEqual(probe.args, ["query", "default", "inode/directory"]);
779 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
780);
781​
782// `xdg-open` with a display variable but no `inode/directory` handler exits
783// nonzero after the launch has already detached: without this gate the server
784// advertises a reveal that is a silent no-op.
785it.effect.skipIf(windowsHost)(
786 "does not advertise a Linux file manager without a directory handler",
787 () =>
788 Effect.gen(function* () {
789 const fileSystem = yield* FileSystem.FileSystem;
790 const path = yield* Path.Path;
791 const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });
792 for (const name of ["xdg-open", "xdg-mime"]) {
793 const filePath = path.join(binDir, name);
794 yield* fileSystem.writeFileString(filePath, "#!/bin/sh\n");
795 yield* fileSystem.chmod(filePath, 0o755);
796 }
797​
798 const editors = yield* Effect.gen(function* () {
799 const launcher = yield* ExternalLauncher.ExternalLauncher;
800 return yield* launcher.resolveAvailableEditors();
801 }).pipe(
802 Effect.provide(
803 layerTest({
804 platform: "linux",
805 env: { PATH: binDir, DISPLAY: ":0" },
806 spawnResult: (command) => (command.command === "xdg-mime" ? { stdout: "" } : undefined),
807 }),
808 ),
809 );
810​
811 assert.equal(editors.includes("file-manager"), false);
812 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
813);
814​
815it.effect.skipIf(windowsHost)(
816 "does not advertise a Linux file manager when the handler query fails",
817 () =>
818 Effect.gen(function* () {
819 const fileSystem = yield* FileSystem.FileSystem;
820 const path = yield* Path.Path;
821 const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });
822 for (const name of ["xdg-open", "xdg-mime"]) {
823 const filePath = path.join(binDir, name);
824 yield* fileSystem.writeFileString(filePath, "#!/bin/sh\n");
825 yield* fileSystem.chmod(filePath, 0o755);
826 }
827​
828 const editors = yield* Effect.gen(function* () {
829 const launcher = yield* ExternalLauncher.ExternalLauncher;
830 return yield* launcher.resolveAvailableEditors();
831 }).pipe(
832 Effect.provide(
833 layerTest({
834 platform: "linux",
835 env: { PATH: binDir, DISPLAY: ":0" },
836 spawnResult: (command) =>
837 command.command === "xdg-mime"
838 ? { exitCode: 47, stdout: "org.gnome.Nautilus.desktop\n" }
839 : undefined,
840 }),
841 ),
842 );
843​
844 assert.equal(editors.includes("file-manager"), false);
845 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
846);
847​
848// The handler probe carries its own timeout because the editor scan's outer
849// timeout in server.getConfig degrades to an EMPTY editor list: a wedged
850// xdg-mime must cost only the file manager, never the other editors. Runs on
851// the live clock so the probe's real timeout fires.
852it.live.skipIf(windowsHost)("a stalled handler probe drops only the file manager", () =>
853 Effect.gen(function* () {
854 const fileSystem = yield* FileSystem.FileSystem;
855 const path = yield* Path.Path;
856 const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });
857 for (const name of ["xdg-open", "xdg-mime", "code"]) {
858 const filePath = path.join(binDir, name);
859 yield* fileSystem.writeFileString(filePath, "#!/bin/sh\n");
860 yield* fileSystem.chmod(filePath, 0o755);
861 }
862​
863 const editors = yield* Effect.gen(function* () {
864 const launcher = yield* ExternalLauncher.ExternalLauncher;
865 return yield* launcher.resolveAvailableEditors();
866 }).pipe(
867 Effect.provide(
868 layerTest({
869 platform: "linux",
870 env: { PATH: binDir, DISPLAY: ":0" },
871 spawnResult: (command) => (command.command === "xdg-mime" ? { stall: true } : undefined),
872 }),
873 ),
874 );
875​
876 assert.equal(editors.includes("vscode"), true);
877 assert.equal(editors.includes("file-manager"), false);
878 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
879);
880​
881it.effect.skipIf(windowsHost)(
882 "does not advertise a Linux file manager when xdg-mime is missing",
883 () =>
884 Effect.gen(function* () {
885 const fileSystem = yield* FileSystem.FileSystem;
886 const path = yield* Path.Path;
887 const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });
888 const xdgOpenPath = path.join(binDir, "xdg-open");
889 yield* fileSystem.writeFileString(xdgOpenPath, "#!/bin/sh\n");
890 yield* fileSystem.chmod(xdgOpenPath, 0o755);
891​
892 const editors = yield* Effect.gen(function* () {
893 const launcher = yield* ExternalLauncher.ExternalLauncher;
894 return yield* launcher.resolveAvailableEditors();
895 }).pipe(
896 Effect.provide(layerTest({ platform: "linux", env: { PATH: binDir, DISPLAY: ":0" } })),
897 );
898​
899 assert.equal(editors.includes("file-manager"), false);
900 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
901);
902​
903it.effect("discovers editors through the service API", () =>
904 Effect.gen(function* () {
905 const fileSystem = yield* FileSystem.FileSystem;
906 const path = yield* Path.Path;
907 const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });
908 yield* fileSystem.writeFileString(path.join(binDir, "code.CMD"), "@echo off\r\n");
909 yield* fileSystem.writeFileString(path.join(binDir, "explorer.CMD"), "@echo off\r\n");
910​
911 const editors = yield* Effect.gen(function* () {
912 const launcher = yield* ExternalLauncher.ExternalLauncher;
913 return yield* launcher.resolveAvailableEditors();
914 }).pipe(
915 Effect.provide(
916 layerTest({
917 platform: "win32",
918 env: { PATH: binDir, PATHEXT: ".COM;.EXE;.BAT;.CMD" },
919 }),
920 ),
921 );
922​
923 assert.equal(editors.includes("vscode"), true);
924 assert.equal(editors.includes("file-manager"), true);
925 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
926);
927​
928for (const { platform, installPath, editor, args } of [
929 {
930 platform: "darwin",
931 installPath: "Applications/Antigravity IDE.app/Contents/Resources/app/bin/antigravity-ide",
932 editor: "antigravity",
933 args: ["--goto", "/workspace with spaces/file.ts:12:4"],
934 },
935 {
936 platform: "linux",
937 installPath: ".local/bin/antigravity-ide",
938 editor: "antigravity",
939 args: ["--goto", "/workspace with spaces/file.ts:12:4"],
940 },
941 {
942 platform: "darwin",
943 installPath: "Applications/Cursor.app/Contents/Resources/app/bin/code",
944 editor: "cursor",
945 args: ["--classic", "--goto", "/workspace with spaces/file.ts:12:4"],
946 },
947 {
948 platform: "darwin",
949 installPath: "Applications/Kiro.app/Contents/Resources/app/bin/code",
950 editor: "kiro",
951 args: ["--goto", "/workspace with spaces/file.ts:12:4"],
952 },
953 {
954 platform: "darwin",
955 installPath: "Applications/WebStorm.app/Contents/MacOS/webstorm",
956 editor: "webstorm",
957 args: ["--line", "12", "--column", "4", "/workspace with spaces/file.ts"],
958 },
959 {
960 platform: "darwin",
961 installPath: "Applications/Zed.app/Contents/MacOS/cli",
962 editor: "zed",
963 args: ["/workspace with spaces/file.ts:12:4"],
964 },
965 {
966 platform: "win32",
967 installPath: "Programs/Cursor/resources/app/bin/cursor.cmd",
968 editor: "cursor",
969 args: ['^"--classic^"', '^"--goto^"', '^"/workspace^ with^ spaces/file.ts:12:4^"'],
970 },
971 {
972 platform: "win32",
973 installPath: "Programs/Microsoft VS Code/bin/code.cmd",
974 editor: "vscode",
975 args: ['^"--goto^"', '^"/workspace^ with^ spaces/file.ts:12:4^"'],
976 },
977 {
978 platform: "win32",
979 installPath: "Programs/JetBrains/WebStorm 2026.2/bin/webstorm64.exe",
980 editor: "webstorm",
981 args: ["--line", "12", "--column", "4", "/workspace with spaces/file.ts"],
982 },
983 {
984 platform: "win32",
985 installPath: "Programs/WebStorm/bin/webstorm64.exe",
986 editor: "webstorm",
987 args: ["--line", "12", "--column", "4", "/workspace with spaces/file.ts"],
988 },
989 {
990 platform: "win32",
991 installPath: "Programs/Zed/bin/zed.exe",
992 editor: "zed",
993 args: ["/workspace with spaces/file.ts:12:4"],
994 },
995 {
996 platform: "linux",
997 installPath: ".local/share/JetBrains/Toolbox/scripts/idea",
998 editor: "idea",
999 args: ["--line", "12", "--column", "4", "/workspace with spaces/file.ts"],
1000 },
1002 platform: "linux",
1003 installPath: ".local/bin/zed",
1004 editor: "zed",
1005 args: ["/workspace with spaces/file.ts:12:4"],
1006 },
1007] as const) {
1008 it.effect.skipIf(windowsHost && platform !== "win32")(
1009 `discovers and launches ${editor} outside PATH on ${platform}`,
1010 () =>
1011 Effect.gen(function* () {
1012 const fs = yield* FileSystem.FileSystem;
1013 const path = yield* Path.Path;
1014 const home = yield* fs.makeTempDirectoryScoped({ prefix: "t3-editor installs-" });
1015 const executable = path.join(home, installPath);
1016 yield* fs.makeDirectory(path.dirname(executable), { recursive: true });
1017 yield* fs.writeFileString(executable, "#!/bin/sh\n");
1018 yield* fs.chmod(executable, 0o755);
1019 let spawned: ChildProcess.StandardCommand | undefined;
1020 yield* Effect.gen(function* () {
1021 const launcher = yield* ExternalLauncher.ExternalLauncher;
1022 assert.include(yield* launcher.resolveAvailableEditors(), editor);
1023 yield* launcher.launchEditor({ editor, cwd: "/workspace with spaces/file.ts:12:4" });
1024 }).pipe(
1025 Effect.provide(
1026 layerTest({
1027 platform,
1028 env: { HOME: home, LOCALAPPDATA: home, PATH: path.join(home, "empty") },
1029 onSpawn: (command) => {
1030 spawned = command;
1031 },
1032 }),
1033 ),
1034 );
1035 assert.ok(spawned);
1036 assert.equal(
1037 spawned.command,
1038 executable.endsWith(".cmd") ? `^"${executable.replaceAll(" ", "^ ")}^"` : executable,
1039 );
1040 assert.deepEqual(spawned.args, args);
1041 assert.equal(spawned.options.shell, executable.endsWith(".cmd"));
1042 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
1043 );
1045​
1046// JetBrains Toolbox names its bundles after the app and the installed version
1047// (`IntelliJ IDEA 2026.1.4.app`), so the darwin branch has to list the
1048// Applications directories the way the win32 branch does instead of probing
1049// exact `<name>.app` paths. The fixtures only use the mocked platform and a
1050// temp directory, so this runs on a Windows host too.
1051it.effect.each([
1052 { editor: "webstorm", installPath: "Applications/WebStorm 2026.2.app/Contents/MacOS/webstorm" },
1053 { editor: "idea", installPath: "Applications/IntelliJ IDEA 2026.1.4.app/Contents/MacOS/idea" },
1054] as const)(
1055 "discovers and launches $editor from a versioned macOS bundle",
1056 ({ editor, installPath }) =>
1057 Effect.gen(function* () {
1058 const fs = yield* FileSystem.FileSystem;
1059 const path = yield* Path.Path;
1060 const home = yield* fs.makeTempDirectoryScoped({ prefix: "t3-editor-versioned-" });
1061 const executable = path.join(home, installPath);
1062 yield* fs.makeDirectory(path.dirname(executable), { recursive: true });
1063 yield* fs.writeFileString(executable, "#!/bin/sh\n");
1064 yield* fs.chmod(executable, 0o755);
1065​
1066 let spawned: ChildProcess.StandardCommand | undefined;
1067 yield* Effect.gen(function* () {
1068 const launcher = yield* ExternalLauncher.ExternalLauncher;
1069 assert.include(yield* launcher.resolveAvailableEditors(), editor);
1070 yield* launcher.launchEditor({ editor, cwd: "/workspace with spaces/file.ts:12:4" });
1071 }).pipe(
1072 Effect.provide(
1073 layerTest({
1074 platform: "darwin",
1075 env: { HOME: home, PATH: path.join(home, "empty") },
1076 onSpawn: (command) => {
1077 spawned = command;
1078 },
1079 }),
1080 ),
1081 );
1082​
1083 assert.ok(spawned);
1084 assert.equal(spawned.command, executable);
1085 assert.deepEqual(spawned.args, [
1086 "--line",
1087 "12",
1088 "--column",
1089 "4",
1090 "/workspace with spaces/file.ts",
1091 ]);
1092 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
1094​
1095// An app whose name merely starts with the same letters is a different IDE, and
1096// an exact `<name>.app` bundle keeps resolving the way it did before.
1097it.effect("matches macOS bundles on the name boundary only", () =>
1098 Effect.gen(function* () {
1099 const fs = yield* FileSystem.FileSystem;
1100 const path = yield* Path.Path;
1101 const unrelatedHome = yield* fs.makeTempDirectoryScoped({ prefix: "t3-editor-unrelated-" });
1102 const unrelated = path.join(
1103 unrelatedHome,
1104 "Applications/WebStormHelper.app/Contents/MacOS/webstorm",
1105 );
1106 yield* fs.makeDirectory(path.dirname(unrelated), { recursive: true });
1107 yield* fs.writeFileString(unrelated, "#!/bin/sh\n");
1108 yield* fs.chmod(unrelated, 0o755);
1109​
1110 const editorsFor = (home: string) =>
1111 Effect.gen(function* () {
1112 const launcher = yield* ExternalLauncher.ExternalLauncher;
1113 return yield* launcher.resolveAvailableEditors();
1114 }).pipe(
1115 Effect.provide(
1116 layerTest({ platform: "darwin", env: { HOME: home, PATH: path.join(home, "bin") } }),
1117 ),
1118 );
1119​
1120 assert.notInclude(yield* editorsFor(unrelatedHome), "webstorm");
1121​
1122 const exactHome = yield* fs.makeTempDirectoryScoped({ prefix: "t3-editor-exact-" });
1123 const exact = path.join(exactHome, "Applications/WebStorm.app/Contents/MacOS/webstorm");
1124 yield* fs.makeDirectory(path.dirname(exact), { recursive: true });
1125 yield* fs.writeFileString(exact, "#!/bin/sh\n");
1126 yield* fs.chmod(exact, 0o755);
1127​
1128 assert.include(yield* editorsFor(exactHome), "webstorm");
1129 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
1131​
1132// `Visual Studio Code - Insiders.app` starts with the stable VS Code install
1133// name, so a plain prefix match let the stable editor resolve to the Insiders
1134// bundle. Insiders has to stay reachable under its own name, stable VS Code has
1135// to stay unresolved.
1136it.effect("keeps the macOS VS Code Insiders bundle out of stable VS Code", () =>
1137 Effect.gen(function* () {
1138 const fs = yield* FileSystem.FileSystem;
1139 const path = yield* Path.Path;
1140 const home = yield* fs.makeTempDirectoryScoped({ prefix: "t3-editor-insiders-" });
1141 // The real Insiders bundle ships both `code-insiders` and `code`.
1142 const bin = path.join(
1143 home,
1144 "Applications/Visual Studio Code - Insiders.app/Contents/Resources/app/bin",
1145 );
1146 yield* fs.makeDirectory(bin, { recursive: true });
1147 for (const name of ["code", "code-insiders"]) {
1148 yield* fs.writeFileString(path.join(bin, name), "#!/bin/sh\n");
1149 yield* fs.chmod(path.join(bin, name), 0o755);
1151​
1152 let spawned: ChildProcess.StandardCommand | undefined;
1153 const editors = yield* Effect.gen(function* () {
1154 const launcher = yield* ExternalLauncher.ExternalLauncher;
1155 const available = yield* launcher.resolveAvailableEditors();
1156 yield* launcher.launchEditor({ editor: "vscode-insiders", cwd: "/workspace/file.ts" });
1157 return available;
1158 }).pipe(
1159 Effect.provide(
1160 layerTest({
1161 platform: "darwin",
1162 env: { HOME: home, PATH: path.join(home, "empty") },
1163 onSpawn: (command) => {
1164 spawned = command;
1165 },
1166 }),
1167 ),
1168 );
1169​
1170 assert.notInclude(editors, "vscode");
1171 assert.include(editors, "vscode-insiders");
1172 assert.equal(spawned?.command, path.join(bin, "code-insiders"));
1173 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
1175​
1176// `agy` is the standalone Antigravity CLI, which installs to ~/.local/bin on
1177// macOS and Linux and to its own bin folder on Windows. It is not the IDE.
1178for (const { platform, installPath, onPath } of [
1179 { platform: "darwin", installPath: ".local/bin/agy", onPath: true },
1180 { platform: "linux", installPath: ".local/bin/agy", onPath: false },
1181 { platform: "win32", installPath: "agy/bin/agy.cmd", onPath: true },
1182] as const) {
1183 it.effect.skipIf(windowsHost && platform !== "win32")(
1184 `does not report the agy CLI as the Antigravity IDE on ${platform}`,
1185 () =>
1186 Effect.gen(function* () {
1187 const fs = yield* FileSystem.FileSystem;
1188 const path = yield* Path.Path;
1189 const home = yield* fs.makeTempDirectoryScoped({ prefix: "t3-agy-cli-" });
1190 const executable = path.join(home, installPath);
1191 yield* fs.makeDirectory(path.dirname(executable), { recursive: true });
1192 yield* fs.writeFileString(executable, "#!/bin/sh\n");
1193 yield* fs.chmod(executable, 0o755);
1194 const editors = yield* Effect.gen(function* () {
1195 const launcher = yield* ExternalLauncher.ExternalLauncher;
1196 return yield* launcher.resolveAvailableEditors();
1197 }).pipe(
1198 Effect.provide(
1199 layerTest({
1200 platform,
1201 env: {
1202 HOME: home,
1203 LOCALAPPDATA: home,
1204 PATH: onPath ? path.dirname(executable) : path.join(home, "empty"),
1205 PATHEXT: ".COM;.EXE;.BAT;.CMD",
1206 },
1207 }),
1208 ),
1209 );
1210 assert.notInclude(editors, "antigravity");
1211 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
1212 );
1214​
1215it.effect.skipIf(windowsHost)("ignores unusable app bundles and keeps PATH launchers first", () =>
1216 Effect.gen(function* () {
1217 const fs = yield* FileSystem.FileSystem;
1218 const path = yield* Path.Path;
1219 const home = yield* fs.makeTempDirectoryScoped({ prefix: "t3-editor-priority-" });
1220 const executable = path.join(home, "Applications/Cursor.app/Contents/Resources/app/bin/code");
1221 const env = { HOME: home, PATH: path.join(home, "bin") };
1222 const discover = Effect.gen(function* () {
1223 const launcher = yield* ExternalLauncher.ExternalLauncher;
1224 return yield* launcher.resolveAvailableEditors();
1225 });
1226 const before = yield* discover.pipe(Effect.provide(layerTest({ platform: "darwin", env })));
1227 yield* fs.makeDirectory(executable, { recursive: true });
1228 assert.deepEqual(
1229 yield* discover.pipe(Effect.provide(layerTest({ platform: "darwin", env }))),
1230 before,
1231 );
1232 yield* fs.remove(executable, { recursive: true });
1233 yield* fs.writeFileString(executable, "#!/bin/sh\n");
1234 yield* fs.chmod(executable, 0o644);
1235 assert.deepEqual(
1236 yield* discover.pipe(Effect.provide(layerTest({ platform: "darwin", env }))),
1237 before,
1238 );
1239 yield* fs.chmod(executable, 0o755);
1240 yield* fs.makeDirectory(env.PATH);
1241 yield* fs.writeFileString(path.join(env.PATH, "cursor"), "#!/bin/sh\n");
1242 yield* fs.chmod(path.join(env.PATH, "cursor"), 0o755);
1243 let spawned: ChildProcess.StandardCommand | undefined;
1244 yield* Effect.gen(function* () {
1245 const launcher = yield* ExternalLauncher.ExternalLauncher;
1246 yield* launcher.launchEditor({ editor: "cursor", cwd: "/workspace" });
1247 }).pipe(
1248 Effect.provide(
1249 layerTest({
1250 platform: "darwin",
1251 env: { ...env, PATH: `${env.PATH}:/usr/bin` },
1252 onSpawn: (command) => {
1253 spawned = command;
1254 },
1255 }),
1256 ),
1257 );
1258 assert.equal(spawned?.command, "cursor");
1259 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
1261​
1262it.effect("memoizes editor discovery and refreshes after the cache window", () => {
1263 let statCalls = 0;
1264 const fileInfo = { type: "File" } as FileSystem.File.Info;
1265 const layerLauncher = ExternalLauncher.layer.pipe(
1266 Layer.provide(
1267 Layer.mergeAll(
1268 FileSystem.layerNoop({
1269 stat: () =>
1270 Effect.sync(() => {
1271 statCalls += 1;
1272 return fileInfo;
1273 }),
1274 }),
1275 Path.layer,
1276 Layer.succeed(
1277 ChildProcessSpawner.ChildProcessSpawner,
1278 ChildProcessSpawner.make(() => Effect.sync(() => makeMockDetachedHandle())),
1279 ),
1280 ),
1281 ),
1282 );
1283​
1284 return Effect.gen(function* () {
1285 const launcher = yield* ExternalLauncher.ExternalLauncher;
1286​
1287 const first = yield* launcher.resolveAvailableEditors();
1288 assert.equal(first.includes("vscode"), true);
1289 const statCallsAfterFirstScan = statCalls;
1290 assert.isAbove(statCallsAfterFirstScan, 0);
1291​
1292 // Past the shared command-resolution cache TTL (30s) but within the
1293 // discovery cache window: the memoized set is reused without any scan.
1294 yield* TestClock.adjust("31 seconds");
1295 const second = yield* launcher.resolveAvailableEditors();
1296 assert.deepEqual([...second], [...first]);
1297 assert.equal(statCalls, statCallsAfterFirstScan);
1298​
1299 // Past the discovery cache window the next call rescans.
1300 yield* TestClock.adjust("30 seconds");
1301 yield* launcher.resolveAvailableEditors();
1302 assert.isAbove(statCalls, statCallsAfterFirstScan);
1303 }).pipe(
1304 Effect.provide(
1305 Layer.mergeAll(
1306 layerLauncher,
1307 Layer.succeed(HostProcess.Platform, "win32"),
1308 ConfigProvider.layer(
1309 ConfigProvider.fromEnv({
1310 env: {
1311 PATH: "C:\\t3-editor-discovery-cache-test",
1312 PATHEXT: ".COM;.EXE;.BAT;.CMD",
1313 },
1314 }),
1315 ),
1316 TestClock.layer(),
1317 ),
1318 ),
1319 );
1320});
1321​
1322// Connects run discovery under a timeout and may disconnect mid-scan. Neither
1323// may cancel the scan: on a busy host every connect would time out partway
1324// through, cache nothing, and leave every client without editors.
1325it.effect("keeps scanning after the caller is interrupted and shares that scan", () => {
1326 const fileInfo = { type: "File" } as FileSystem.File.Info;
1327 const release = Deferred.makeUnsafe<void>();
1328 let parkedStats = 0;
1329 const layerLauncher = ExternalLauncher.layer.pipe(
1330 Layer.provide(
1331 Layer.mergeAll(
1332 FileSystem.layerNoop({
1333 // Scans park inside `stat` until released, so the interrupt lands
1334 // while discovery is in flight.
1335 stat: () =>
1336 Effect.gen(function* () {
1337 if (!Deferred.isDoneUnsafe(release)) {
1338 parkedStats += 1;
1339 yield* Deferred.await(release);
1341 return fileInfo;
1342 }),
1343 }),
1344 Path.layer,
1345 Layer.succeed(
1346 ChildProcessSpawner.ChildProcessSpawner,
1347 ChildProcessSpawner.make(() => Effect.sync(() => makeMockDetachedHandle())),
1348 ),
1349 ),
1350 ),
1351 );
1352​
1353 return Effect.gen(function* () {
1354 const launcher = yield* ExternalLauncher.ExternalLauncher;
1355​
1356 const interrupted = yield* Effect.forkChild(launcher.resolveAvailableEditors());
1357 yield* Effect.yieldNow;
1358 yield* Fiber.interrupt(interrupted);
1359​
1360 // The next connect joins the running scan instead of starting its own.
1361 const next = yield* Effect.forkChild(launcher.resolveAvailableEditors());
1362 yield* Effect.yieldNow;
1363 assert.equal(parkedStats, 1);
1364​
1365 yield* Deferred.succeed(release, undefined);
1366 const editors = yield* Fiber.join(next);
1367 assert.equal(editors.includes("vscode"), true);
1368 }).pipe(
1369 Effect.provide(
1370 Layer.mergeAll(
1371 layerLauncher,
1372 Layer.succeed(HostProcess.Platform, "win32"),
1373 ConfigProvider.layer(
1374 ConfigProvider.fromEnv({
1375 env: {
1376 PATH: "C:\\t3-editor-discovery-interrupt-test",
1377 PATHEXT: ".COM;.EXE;.BAT;.CMD",
1378 },
1379 }),
1380 ),
1381 ),
1382 ),
1383 );
1384});
1385​
1386const launchWindowsShimEditor = (cwd: string) =>
1387 Effect.gen(function* () {
1388 const fileSystem = yield* FileSystem.FileSystem;
1389 const path = yield* Path.Path;
1390 const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });
1391 yield* fileSystem.writeFileString(path.join(binDir, "code.CMD"), "@echo off\r\n");
1392 let spawned: ChildProcess.StandardCommand | undefined;
1393 const exit = yield* Effect.gen(function* () {
1394 const launcher = yield* ExternalLauncher.ExternalLauncher;
1395 return yield* launcher.launchEditor({ editor: "vscode", cwd }).pipe(Effect.exit);
1396 }).pipe(
1397 Effect.provide(
1398 layerTest({
1399 platform: "win32",
1400 env: { PATH: binDir, PATHEXT: ".COM;.EXE;.BAT;.CMD" },
1401 resolveExecutable: (command) =>
1402 command === "code" ? "C:\\Program Files\\Microsoft VS Code\\bin\\code.CMD" : command,
1403 onSpawn: (command) => {
1404 spawned = command;
1405 },
1406 }),
1407 ),
1408 );
1409 return { exit, spawned };
1410 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer));
1411​
1412it.effect.each([
1413 ["CRLF", "C:\\workspace\\file.ts\r\ncalc.exe"],
1414 ["LF", "C:\\workspace\\file.ts\ncalc.exe"],
1415 ["double quote", 'C:\\workspace\\file.ts" & calc.exe & "'],
1416] as const)("does not start a Windows command shim for a path with a $0", ([, cwd]) =>
1417 Effect.gen(function* () {
1418 const { exit, spawned } = yield* launchWindowsShimEditor(cwd);
1419 assert.equal(spawned, undefined);
1420 assert.isTrue(Exit.isFailure(exit));
1421 if (Exit.isFailure(exit)) {
1422 assert.instanceOf(
1423 Cause.squash(exit.cause),
1424 ExternalLauncher.ExternalLauncherUnsupportedTargetError,
1425 );
1427 }),
1429​
1430it.effect("starts a Windows command shim for unicode paths with cmd metacharacters", () =>
1431 Effect.gen(function* () {
1432 const { exit, spawned } = yield* launchWindowsShimEditor(
1433 "C:\\Users\\jö\\R&D (100%)\\naïve ^file!.ts:3:7",
1434 );
1435 assert.isTrue(Exit.isSuccess(exit));
1436 assert.ok(spawned);
1437 assert.equal(spawned.options.shell, true);
1438 assert.deepEqual(spawned.args, [
1439 '^"--goto^"',
1440 '^"C:\\Users\\jö\\R^&D^ ^(100^%^)\\naïve^ ^^file^!.ts:3:7^"',
1441 ]);
1442 }),
1444​
1445it.effect.skipIf(windowsHost)("passes line breaks through to editors started without a shell", () =>
1446 Effect.gen(function* () {
1447 const fileSystem = yield* FileSystem.FileSystem;
1448 const path = yield* Path.Path;
1449 const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });
1450 const zed = path.join(binDir, "zed");
1451 yield* fileSystem.writeFileString(zed, "#!/bin/sh\n");
1452 yield* fileSystem.chmod(zed, 0o755);
1453 let spawned: ChildProcess.StandardCommand | undefined;
1454 yield* Effect.gen(function* () {
1455 const launcher = yield* ExternalLauncher.ExternalLauncher;
1456 yield* launcher.launchEditor({ editor: "zed", cwd: "/workspace/odd\nname.ts" });
1457 }).pipe(
1458 Effect.provide(
1459 layerTest({
1460 platform: "linux",
1461 env: { PATH: binDir, HOME: binDir },
1462 onSpawn: (command) => {
1463 spawned = command;
1464 },
1465 }),
1466 ),
1467 );
1468 assert.ok(spawned);
1469 assert.deepEqual(spawned.args, ["/workspace/odd\nname.ts"]);
1470 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
1472​
1473it.effect("rejects unknown editors through the service API", () =>
1474 Effect.gen(function* () {
1475 const launcher = yield* ExternalLauncher.ExternalLauncher;
1476 const error = yield* launcher
1477 .launchEditor({ editor: "missing-editor" as never, cwd: "/tmp/workspace" })
1478 .pipe(Effect.flip);
1479 assert.instanceOf(error, ExternalLauncher.ExternalLauncherUnknownEditorError);
1480 assert.equal(error.editor, "missing-editor");
1481 assert.equal(error.message, "Unknown editor: missing-editor");
1482 }).pipe(Effect.provide(layerTest({ platform: "linux", env: { PATH: "" } }))),