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

ElectronShell.test.ts

193 lines7.2 KB

1import { assert, describe, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import { beforeEach, vi } from "vite-plus/test";
4​
5const { openExternalMock, writeTextMock } = vi.hoisted(() => ({
6 openExternalMock: vi.fn(),
7 writeTextMock: vi.fn(),
8}));
9​
10vi.mock("electron", () => ({
11 shell: {
12 openExternal: openExternalMock,
13 },
14 clipboard: {
15 writeText: writeTextMock,
16 },
17}));
18​
19import * as ElectronShell from "./ElectronShell.ts";
20​
21describe("ElectronShell", () => {
22 beforeEach(() => {
23 openExternalMock.mockReset();
24 writeTextMock.mockReset();
25 });
26​
27 it.effect("opens remote SSH editor URLs", () =>
28 Effect.gen(function* () {
29 openExternalMock.mockResolvedValue(undefined);
30​
31 const electronShell = yield* ElectronShell.ElectronShell;
32 const result = yield* electronShell.openExternal(
33 "vscode://vscode-remote/ssh-remote+user%40example.com/home/user/project",
34 );
35​
36 assert.equal(result, true);
37 assert.deepEqual(openExternalMock.mock.calls, [
38 ["vscode://vscode-remote/ssh-remote+user%40example.com/home/user/project"],
39 ]);
40 }).pipe(Effect.provide(ElectronShell.layer)),
41 );
42​
43 it.effect("opens the JetBrains Toolbox ssh deep link", () =>
44 Effect.gen(function* () {
45 openExternalMock.mockResolvedValue(undefined);
46​
47 const url =
48 "jetbrains://gateway/ssh/environment?h=example.com&launchIde=true&ideHint=IU&projectHint=%2Fhome%2Fuser%2Fproject&u=user";
49 const electronShell = yield* ElectronShell.ElectronShell;
50 const result = yield* electronShell.openExternal(url);
51​
52 assert.equal(result, true);
53 assert.deepEqual(openExternalMock.mock.calls, [[url]]);
54 }).pipe(Effect.provide(ElectronShell.layer)),
55 );
56​
57 it.effect("opens only the Grok Bot prompt link", () =>
58 Effect.gen(function* () {
59 openExternalMock.mockResolvedValue(undefined);
60​
61 const prompt = "grokbot://app/v1/prompt?text=Add%20the%20MCP%20server";
62 const electronShell = yield* ElectronShell.ElectronShell;
63 const results = yield* Effect.forEach(
64 [
65 prompt,
66 "grokbot://app",
67 "grokbot://app/v1/prompt?id=bot&text=hi",
68 "grokbot://app/v1/prompt?text=a&text=b",
69 "grokbot://app/v1/other?text=hi",
70 "grokbot://app/v1/prompt?text=hi#x",
71 "grokbot://:secret@app/v1/prompt?text=hi",
72 "grokbot://user@app/v1/prompt?text=hi",
73 "grokbot://other/v1/prompt?text=hi",
74 "other://app/v1/prompt?text=hi",
75 ],
76 (url) => electronShell.openExternal(url),
77 );
78​
79 assert.deepEqual(results, [true, ...Array<boolean>(9).fill(false)]);
80 assert.deepEqual(openExternalMock.mock.calls, [[prompt]]);
81 }).pipe(Effect.provide(ElectronShell.layer)),
82 );
83​
84 it.effect("opens local editor file deep links", () =>
85 Effect.gen(function* () {
86 openExternalMock.mockResolvedValue(undefined);
87​
88 const urls = [
89 "cursor://file/Users/me/repo/file.md:186",
90 "vscode://file/Users/me/repo/file.md:3:7",
91 "vscode-insiders://file/Users/me/repo/file.md",
92 "vscodium://file/C:/Users/me/repo/file.md:3",
93 ];
94 const electronShell = yield* ElectronShell.ElectronShell;
95 const results = yield* Effect.forEach(urls, (url) => electronShell.openExternal(url));
96​
97 assert.deepEqual(results, [true, true, true, true]);
98 assert.deepEqual(
99 openExternalMock.mock.calls,
100 urls.map((url) => [url]),
101 );
102 }).pipe(Effect.provide(ElectronShell.layer)),
103 );
104​
105 it.effect("normalizes the scheme case of local editor file deep links", () =>
106 Effect.gen(function* () {
107 openExternalMock.mockResolvedValue(undefined);
108​
109 const electronShell = yield* ElectronShell.ElectronShell;
110 const result = yield* electronShell.openExternal("VSCode://file/Users/me/repo/file.md:3");
111​
112 assert.equal(result, true);
113 assert.deepEqual(openExternalMock.mock.calls, [["vscode://file/Users/me/repo/file.md:3"]]);
114 }).pipe(Effect.provide(ElectronShell.layer)),
115 );
116​
117 it.effect("does not open editor URLs that are not absolute local file links", () =>
118 Effect.gen(function* () {
119 openExternalMock.mockResolvedValue(undefined);
120​
121 const electronShell = yield* ElectronShell.ElectronShell;
122 const results = yield* Effect.forEach(
123 [
124 "cursor://user@file/Users/me/repo/file.md",
125 "cursor://:secret@file/Users/me/repo/file.md",
126 "cursor://file:8080/Users/me/repo/file.md",
127 "cursor://anysphere.cursor-deeplink/prompt?text=hi",
128 "vscode://vscode.git/clone?url=https%3A%2F%2Fexample.com%2Frepo.git",
129 "cursor://file",
130 "cursor://file/",
131 "cursor:file/Users/me/repo/file.md",
132 "foo://file/Users/me/repo/file.md",
133 "zed://file/Users/me/repo/file.md",
134 "jetbrains://file/Users/me/repo/file.md",
135 "javascript:alert(1)",
136 ],
137 (url) => electronShell.openExternal(url),
138 );
139​
140 assert.deepEqual(results, Array(12).fill(false));
141 assert.equal(openExternalMock.mock.calls.length, 0);
142 }).pipe(Effect.provide(ElectronShell.layer)),
143 );
144​
145 it.effect("does not open editor URLs that mix up link shapes", () =>
146 Effect.gen(function* () {
147 openExternalMock.mockResolvedValue(undefined);
148​
149 const electronShell = yield* ElectronShell.ElectronShell;
150 const results = yield* Effect.all([
151 electronShell.openExternal("zed://extension/attacker"),
152 electronShell.openExternal("vscode://ssh/example.com/home/user/project"),
153 electronShell.openExternal("jetbrains://idea/navigate/reference?project=x&path=y"),
154 electronShell.openExternal("jetbrains://gateway/ssh/environment?launchIde=true"),
155 electronShell.openExternal("jetbrains://gateway/com.example.provider/connect?h=x"),
156 ]);
157​
158 assert.deepEqual(results, [false, false, false, false, false]);
159 assert.equal(openExternalMock.mock.calls.length, 0);
160 }).pipe(Effect.provide(ElectronShell.layer)),
161 );
162​
163 it.effect("does not open remote editor URLs with userinfo", () =>
164 Effect.gen(function* () {
165 openExternalMock.mockResolvedValue(undefined);
166​
167 const electronShell = yield* ElectronShell.ElectronShell;
168 const results = yield* Effect.all([
169 electronShell.openExternal(
170 "vscode://user@vscode-remote/ssh-remote+example.com/home/user/project",
171 ),
172 electronShell.openExternal(
173 "vscode://:secret@vscode-remote/ssh-remote+example.com/home/user/project",
174 ),
175 electronShell.openExternal("zed://ssh/user:secret@example.com/home/user/project"),
176 electronShell.openExternal("jetbrains://user@gateway/ssh/environment?h=example.com"),
177 ]);
178​
179 assert.deepEqual(results, [false, false, false, false]);
180 assert.equal(openExternalMock.mock.calls.length, 0);
181 }).pipe(Effect.provide(ElectronShell.layer)),
182 );
183​
184 it.effect("does not open unsafe external URLs", () =>
185 Effect.gen(function* () {
186 const electronShell = yield* ElectronShell.ElectronShell;
187 const result = yield* electronShell.openExternal("file:///etc/passwd");
188​
189 assert.equal(result, false);
190 assert.equal(openExternalMock.mock.calls.length, 0);
191 }).pipe(Effect.provide(ElectronShell.layer)),
192 );
193});