Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, it } from "@effect/vitest";
3import { assertSuccess } from "@effect/vitest/utils";
4import { FileSystem, Path, Effect } from "effect";
5
6import {
7 isCommandAvailable,
8 launchDetached,
9 resolveAvailableEditors,
10 resolveEditorLaunch,
11} from "./open";
12
13it.layer(NodeServices.layer)("resolveEditorLaunch", (it) => {
14 it.effect("returns commands for command-based editors", () =>
15 Effect.gen(function* () {
16 const antigravityLaunch = yield* resolveEditorLaunch(
17 { cwd: "/tmp/workspace", editor: "antigravity" },
18 "darwin",
19 );
20 assert.deepEqual(antigravityLaunch, {
21 command: "agy",
22 args: ["/tmp/workspace"],
23 });
24
25 const cursorLaunch = yield* resolveEditorLaunch(
26 { cwd: "/tmp/workspace", editor: "cursor" },
27 "darwin",
28 );
29 assert.deepEqual(cursorLaunch, {
30 command: "cursor",
31 args: ["/tmp/workspace"],
32 });
33
34 const vscodeLaunch = yield* resolveEditorLaunch(
35 { cwd: "/tmp/workspace", editor: "vscode" },
36 "darwin",
37 );
38 assert.deepEqual(vscodeLaunch, {
39 command: "code",
40 args: ["/tmp/workspace"],
41 });
42
43 const zedLaunch = yield* resolveEditorLaunch(
44 { cwd: "/tmp/workspace", editor: "zed" },
45 "darwin",
46 );
47 assert.deepEqual(zedLaunch, {
48 command: "zed",
49 args: ["/tmp/workspace"],
50 });
51 }),
52 );
53
54 it.effect("uses --goto when editor supports line/column suffixes", () =>
55 Effect.gen(function* () {
56 const lineOnly = yield* resolveEditorLaunch(
57 { cwd: "/tmp/workspace/AGENTS.md:48", editor: "cursor" },
58 "darwin",
59 );
60 assert.deepEqual(lineOnly, {
61 command: "cursor",
62 args: ["--goto", "/tmp/workspace/AGENTS.md:48"],
63 });
64
65 const lineAndColumn = yield* resolveEditorLaunch(
66 { cwd: "/tmp/workspace/src/open.ts:71:5", editor: "cursor" },
67 "darwin",
68 );
69 assert.deepEqual(lineAndColumn, {
70 command: "cursor",
71 args: ["--goto", "/tmp/workspace/src/open.ts:71:5"],
72 });
73
74 const vscodeLineAndColumn = yield* resolveEditorLaunch(
75 { cwd: "/tmp/workspace/src/open.ts:71:5", editor: "vscode" },
76 "darwin",
77 );
78 assert.deepEqual(vscodeLineAndColumn, {
79 command: "code",
80 args: ["--goto", "/tmp/workspace/src/open.ts:71:5"],
81 });
82
83 const zedLineAndColumn = yield* resolveEditorLaunch(
84 { cwd: "/tmp/workspace/src/open.ts:71:5", editor: "zed" },
85 "darwin",
86 );
87 assert.deepEqual(zedLineAndColumn, {
88 command: "zed",
89 args: ["/tmp/workspace/src/open.ts:71:5"],
90 });
91 }),
92 );
93
94 it.effect("maps file-manager editor to OS open commands", () =>
95 Effect.gen(function* () {
96 const launch1 = yield* resolveEditorLaunch(
97 { cwd: "/tmp/workspace", editor: "file-manager" },
98 "darwin",
99 );
100 assert.deepEqual(launch1, {
101 command: "open",
102 args: ["/tmp/workspace"],
103 });
104
105 const launch2 = yield* resolveEditorLaunch(
106 { cwd: "C:\\workspace", editor: "file-manager" },
107 "win32",
108 );
109 assert.deepEqual(launch2, {
110 command: "explorer",
111 args: ["C:\\workspace"],
112 });
113
114 const launch3 = yield* resolveEditorLaunch(
115 { cwd: "/tmp/workspace", editor: "file-manager" },
116 "linux",
117 );
118 assert.deepEqual(launch3, {
119 command: "xdg-open",
120 args: ["/tmp/workspace"],
121 });
122 }),
123 );
124});
125
126it.layer(NodeServices.layer)("launchDetached", (it) => {
127 it.effect("resolves when command can be spawned", () =>
128 Effect.gen(function* () {
129 const result = yield* launchDetached({
130 command: process.execPath,
131 args: ["-e", "process.exit(0)"],
132 }).pipe(Effect.result);
133 assertSuccess(result, undefined);
134 }),
135 );
136
137 it.effect("rejects when command does not exist", () =>
138 Effect.gen(function* () {
139 const result = yield* launchDetached({
140 command: `t3code-no-such-command-${Date.now()}`,
141 args: [],
142 }).pipe(Effect.result);
143 assert.equal(result._tag, "Failure");
144 }),
145 );
146});
147
148it.layer(NodeServices.layer)("isCommandAvailable", (it) => {
149 it.effect("resolves win32 commands with PATHEXT", () =>
150 Effect.gen(function* () {
151 const fs = yield* FileSystem.FileSystem;
152 const path = yield* Path.Path;
153 const dir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-open-test-" });
154 yield* fs.writeFileString(path.join(dir, "code.CMD"), "@echo off\r\n");
155 const env = {
156 PATH: dir,
157 PATHEXT: ".COM;.EXE;.BAT;.CMD",
158 } satisfies NodeJS.ProcessEnv;
159 assert.equal(isCommandAvailable("code", { platform: "win32", env }), true);
160 }),
161 );
162
163 it("returns false when a command is not on PATH", () => {
164 const env = {
165 PATH: "",
166 PATHEXT: ".COM;.EXE;.BAT;.CMD",
167 } satisfies NodeJS.ProcessEnv;
168 assert.equal(isCommandAvailable("definitely-not-installed", { platform: "win32", env }), false);
169 });
170
171 it.effect("does not treat bare files without executable extension as available on win32", () =>
172 Effect.gen(function* () {
173 const fs = yield* FileSystem.FileSystem;
174 const path = yield* Path.Path;
175 const dir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-open-test-" });
176 yield* fs.writeFileString(path.join(dir, "npm"), "echo nope\r\n");
177 const env = {
178 PATH: dir,
179 PATHEXT: ".COM;.EXE;.BAT;.CMD",
180 } satisfies NodeJS.ProcessEnv;
181 assert.equal(isCommandAvailable("npm", { platform: "win32", env }), false);
182 }),
183 );
184
185 it.effect("appends PATHEXT for commands with non-executable extensions on win32", () =>
186 Effect.gen(function* () {
187 const fs = yield* FileSystem.FileSystem;
188 const path = yield* Path.Path;
189 const dir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-open-test-" });
190 yield* fs.writeFileString(path.join(dir, "my.tool.CMD"), "@echo off\r\n");
191 const env = {
192 PATH: dir,
193 PATHEXT: ".COM;.EXE;.BAT;.CMD",
194 } satisfies NodeJS.ProcessEnv;
195 assert.equal(isCommandAvailable("my.tool", { platform: "win32", env }), true);
196 }),
197 );
198
199 it.effect("uses platform-specific PATH delimiter for platform overrides", () =>
200 Effect.gen(function* () {
201 const fs = yield* FileSystem.FileSystem;
202 const path = yield* Path.Path;
203 const firstDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-open-test-" });
204 const secondDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-open-test-" });
205 yield* fs.writeFileString(path.join(firstDir, "code.CMD"), "@echo off\r\n");
206 yield* fs.writeFileString(path.join(secondDir, "code.CMD"), "MZ");
207 const env = {
208 PATH: `${firstDir};${secondDir}`,
209 PATHEXT: ".COM;.EXE;.BAT;.CMD",
210 } satisfies NodeJS.ProcessEnv;
211 assert.equal(isCommandAvailable("code", { platform: "win32", env }), true);
212 }),
213 );
214});
215
216it.layer(NodeServices.layer)("resolveAvailableEditors", (it) => {
217 it.effect("returns installed editors for command launches", () =>
218 Effect.gen(function* () {
219 const fs = yield* FileSystem.FileSystem;
220 const path = yield* Path.Path;
221 const dir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-editors-" });
222
223 yield* fs.writeFileString(path.join(dir, "cursor.CMD"), "@echo off\r\n");
224 yield* fs.writeFileString(path.join(dir, "explorer.CMD"), "MZ");
225 const editors = resolveAvailableEditors("win32", {
226 PATH: dir,
227 PATHEXT: ".COM;.EXE;.BAT;.CMD",
228 });
229 assert.deepEqual(editors, ["cursor", "file-manager"]);
230 }),
231 );
232});