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

externalLauncher.test.ts

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