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

open.test.ts

288 lines9.4 KB

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 traeLaunch = yield* resolveEditorLaunch(
35 { cwd: "/tmp/workspace", editor: "trae" },
36 "darwin",
37 );
38 assert.deepEqual(traeLaunch, {
39 command: "trae",
40 args: ["/tmp/workspace"],
41 });
42​
43 const vscodeLaunch = yield* resolveEditorLaunch(
44 { cwd: "/tmp/workspace", editor: "vscode" },
45 "darwin",
46 );
47 assert.deepEqual(vscodeLaunch, {
48 command: "code",
49 args: ["/tmp/workspace"],
50 });
51​
52 const vscodeInsidersLaunch = yield* resolveEditorLaunch(
53 { cwd: "/tmp/workspace", editor: "vscode-insiders" },
54 "darwin",
55 );
56 assert.deepEqual(vscodeInsidersLaunch, {
57 command: "code-insiders",
58 args: ["/tmp/workspace"],
59 });
60​
61 const vscodiumLaunch = yield* resolveEditorLaunch(
62 { cwd: "/tmp/workspace", editor: "vscodium" },
63 "darwin",
64 );
65 assert.deepEqual(vscodiumLaunch, {
66 command: "codium",
67 args: ["/tmp/workspace"],
68 });
69​
70 const zedLaunch = yield* resolveEditorLaunch(
71 { cwd: "/tmp/workspace", editor: "zed" },
72 "darwin",
73 );
74 assert.deepEqual(zedLaunch, {
75 command: "zed",
76 args: ["/tmp/workspace"],
77 });
78 }),
79 );
80​
81 it.effect("uses --goto when editor supports line/column suffixes", () =>
82 Effect.gen(function* () {
83 const lineOnly = yield* resolveEditorLaunch(
84 { cwd: "/tmp/workspace/AGENTS.md:48", editor: "cursor" },
85 "darwin",
86 );
87 assert.deepEqual(lineOnly, {
88 command: "cursor",
89 args: ["--goto", "/tmp/workspace/AGENTS.md:48"],
90 });
91​
92 const lineAndColumn = yield* resolveEditorLaunch(
93 { cwd: "/tmp/workspace/src/open.ts:71:5", editor: "cursor" },
94 "darwin",
95 );
96 assert.deepEqual(lineAndColumn, {
97 command: "cursor",
98 args: ["--goto", "/tmp/workspace/src/open.ts:71:5"],
99 });
100​
101 const traeLineAndColumn = yield* resolveEditorLaunch(
102 { cwd: "/tmp/workspace/src/open.ts:71:5", editor: "trae" },
103 "darwin",
104 );
105 assert.deepEqual(traeLineAndColumn, {
106 command: "trae",
107 args: ["--goto", "/tmp/workspace/src/open.ts:71:5"],
108 });
109​
110 const vscodeLineAndColumn = yield* resolveEditorLaunch(
111 { cwd: "/tmp/workspace/src/open.ts:71:5", editor: "vscode" },
112 "darwin",
113 );
114 assert.deepEqual(vscodeLineAndColumn, {
115 command: "code",
116 args: ["--goto", "/tmp/workspace/src/open.ts:71:5"],
117 });
118​
119 const vscodeInsidersLineAndColumn = yield* resolveEditorLaunch(
120 { cwd: "/tmp/workspace/src/open.ts:71:5", editor: "vscode-insiders" },
121 "darwin",
122 );
123 assert.deepEqual(vscodeInsidersLineAndColumn, {
124 command: "code-insiders",
125 args: ["--goto", "/tmp/workspace/src/open.ts:71:5"],
126 });
127​
128 const vscodiumLineAndColumn = yield* resolveEditorLaunch(
129 { cwd: "/tmp/workspace/src/open.ts:71:5", editor: "vscodium" },
130 "darwin",
131 );
132 assert.deepEqual(vscodiumLineAndColumn, {
133 command: "codium",
134 args: ["--goto", "/tmp/workspace/src/open.ts:71:5"],
135 });
136​
137 const zedLineAndColumn = yield* resolveEditorLaunch(
138 { cwd: "/tmp/workspace/src/open.ts:71:5", editor: "zed" },
139 "darwin",
140 );
141 assert.deepEqual(zedLineAndColumn, {
142 command: "zed",
143 args: ["/tmp/workspace/src/open.ts:71:5"],
144 });
145 }),
146 );
147​
148 it.effect("maps file-manager editor to OS open commands", () =>
149 Effect.gen(function* () {
150 const launch1 = yield* resolveEditorLaunch(
151 { cwd: "/tmp/workspace", editor: "file-manager" },
152 "darwin",
153 );
154 assert.deepEqual(launch1, {
155 command: "open",
156 args: ["/tmp/workspace"],
157 });
158​
159 const launch2 = yield* resolveEditorLaunch(
160 { cwd: "C:\\workspace", editor: "file-manager" },
161 "win32",
162 );
163 assert.deepEqual(launch2, {
164 command: "explorer",
165 args: ["C:\\workspace"],
166 });
167​
168 const launch3 = yield* resolveEditorLaunch(
169 { cwd: "/tmp/workspace", editor: "file-manager" },
170 "linux",
171 );
172 assert.deepEqual(launch3, {
173 command: "xdg-open",
174 args: ["/tmp/workspace"],
175 });
176 }),
177 );
178});
179​
180it.layer(NodeServices.layer)("launchDetached", (it) => {
181 it.effect("resolves when command can be spawned", () =>
182 Effect.gen(function* () {
183 const result = yield* launchDetached({
184 command: process.execPath,
185 args: ["-e", "process.exit(0)"],
186 }).pipe(Effect.result);
187 assertSuccess(result, undefined);
188 }),
189 );
190​
191 it.effect("rejects when command does not exist", () =>
192 Effect.gen(function* () {
193 const result = yield* launchDetached({
194 command: `t3code-no-such-command-${Date.now()}`,
195 args: [],
196 }).pipe(Effect.result);
197 assert.equal(result._tag, "Failure");
198 }),
199 );
200});
201​
202it.layer(NodeServices.layer)("isCommandAvailable", (it) => {
203 it.effect("resolves win32 commands with PATHEXT", () =>
204 Effect.gen(function* () {
205 const fs = yield* FileSystem.FileSystem;
206 const path = yield* Path.Path;
207 const dir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-open-test-" });
208 yield* fs.writeFileString(path.join(dir, "code.CMD"), "@echo off\r\n");
209 const env = {
210 PATH: dir,
211 PATHEXT: ".COM;.EXE;.BAT;.CMD",
212 } satisfies NodeJS.ProcessEnv;
213 assert.equal(isCommandAvailable("code", { platform: "win32", env }), true);
214 }),
215 );
216​
217 it("returns false when a command is not on PATH", () => {
218 const env = {
219 PATH: "",
220 PATHEXT: ".COM;.EXE;.BAT;.CMD",
221 } satisfies NodeJS.ProcessEnv;
222 assert.equal(isCommandAvailable("definitely-not-installed", { platform: "win32", env }), false);
223 });
224​
225 it.effect("does not treat bare files without executable extension as available on win32", () =>
226 Effect.gen(function* () {
227 const fs = yield* FileSystem.FileSystem;
228 const path = yield* Path.Path;
229 const dir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-open-test-" });
230 yield* fs.writeFileString(path.join(dir, "npm"), "echo nope\r\n");
231 const env = {
232 PATH: dir,
233 PATHEXT: ".COM;.EXE;.BAT;.CMD",
234 } satisfies NodeJS.ProcessEnv;
235 assert.equal(isCommandAvailable("npm", { platform: "win32", env }), false);
236 }),
237 );
238​
239 it.effect("appends PATHEXT for commands with non-executable extensions on win32", () =>
240 Effect.gen(function* () {
241 const fs = yield* FileSystem.FileSystem;
242 const path = yield* Path.Path;
243 const dir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-open-test-" });
244 yield* fs.writeFileString(path.join(dir, "my.tool.CMD"), "@echo off\r\n");
245 const env = {
246 PATH: dir,
247 PATHEXT: ".COM;.EXE;.BAT;.CMD",
248 } satisfies NodeJS.ProcessEnv;
249 assert.equal(isCommandAvailable("my.tool", { platform: "win32", env }), true);
250 }),
251 );
252​
253 it.effect("uses platform-specific PATH delimiter for platform overrides", () =>
254 Effect.gen(function* () {
255 const fs = yield* FileSystem.FileSystem;
256 const path = yield* Path.Path;
257 const firstDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-open-test-" });
258 const secondDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-open-test-" });
259 yield* fs.writeFileString(path.join(firstDir, "code.CMD"), "@echo off\r\n");
260 yield* fs.writeFileString(path.join(secondDir, "code.CMD"), "MZ");
261 const env = {
262 PATH: `${firstDir};${secondDir}`,
263 PATHEXT: ".COM;.EXE;.BAT;.CMD",
264 } satisfies NodeJS.ProcessEnv;
265 assert.equal(isCommandAvailable("code", { platform: "win32", env }), true);
266 }),
267 );
268});
269​
270it.layer(NodeServices.layer)("resolveAvailableEditors", (it) => {
271 it.effect("returns installed editors for command launches", () =>
272 Effect.gen(function* () {
273 const fs = yield* FileSystem.FileSystem;
274 const path = yield* Path.Path;
275 const dir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-editors-" });
276​
277 yield* fs.writeFileString(path.join(dir, "trae.CMD"), "@echo off\r\n");
278 yield* fs.writeFileString(path.join(dir, "code-insiders.CMD"), "@echo off\r\n");
279 yield* fs.writeFileString(path.join(dir, "codium.CMD"), "@echo off\r\n");
280 yield* fs.writeFileString(path.join(dir, "explorer.CMD"), "MZ");
281 const editors = resolveAvailableEditors("win32", {
282 PATH: dir,
283 PATHEXT: ".COM;.EXE;.BAT;.CMD",
284 });
285 assert.deepEqual(editors, ["trae", "vscode-insiders", "vscodium", "file-manager"]);
286 }),
287 );
288});