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

open.test.ts

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