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

ElectronShell.test.ts

144 lines4.9 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 safe external URLs", () =>
28 Effect.gen(function* () {
29 openExternalMock.mockResolvedValue(undefined);
30​
31 const electronShell = yield* ElectronShell.ElectronShell;
32 const result = yield* electronShell.openExternal("https://example.com/path");
33​
34 assert.equal(result, true);
35 assert.deepEqual(openExternalMock.mock.calls, [["https://example.com/path"]]);
36 }).pipe(Effect.provide(ElectronShell.layer)),
37 );
38​
39 it.effect("copies text to the system clipboard", () =>
40 Effect.gen(function* () {
41 writeTextMock.mockResolvedValue(undefined);
42​
43 const electronShell = yield* ElectronShell.ElectronShell;
44 yield* electronShell.copyText("https://example.com/path");
45​
46 assert.deepEqual(writeTextMock.mock.calls, [["https://example.com/path"]]);
47 }).pipe(Effect.provide(ElectronShell.layer)),
48 );
49​
50 it.effect("does not fail when the clipboard write rejects", () =>
51 Effect.gen(function* () {
52 writeTextMock.mockRejectedValue(new Error("write failed"));
53​
54 const electronShell = yield* ElectronShell.ElectronShell;
55 yield* electronShell.copyText("https://example.com/path");
56​
57 assert.deepEqual(writeTextMock.mock.calls, [["https://example.com/path"]]);
58 }).pipe(Effect.provide(ElectronShell.layer)),
59 );
60​
61 it.effect("opens the Full Disk Access settings anchor", () =>
62 Effect.gen(function* () {
63 openExternalMock.mockResolvedValue(undefined);
64​
65 const electronShell = yield* ElectronShell.ElectronShell;
66 const result = yield* electronShell.openSystemSettings("full-disk-access");
67​
68 assert.equal(result, true);
69 assert.deepEqual(openExternalMock.mock.calls, [
70 ["x-apple.systempreferences:com.apple.settings.PrivacySecurity.extension?Privacy_AllFiles"],
71 ]);
72 }).pipe(Effect.provide(ElectronShell.layer)),
73 );
74​
75 it.effect("opens remote SSH editor URLs", () =>
76 Effect.gen(function* () {
77 openExternalMock.mockResolvedValue(undefined);
78​
79 const electronShell = yield* ElectronShell.ElectronShell;
80 const result = yield* electronShell.openExternal(
81 "vscode://vscode-remote/ssh-remote+example.com/home/user/project",
82 );
83​
84 assert.equal(result, true);
85 assert.deepEqual(openExternalMock.mock.calls, [
86 ["vscode://vscode-remote/ssh-remote+example.com/home/user/project"],
87 ]);
88 }).pipe(Effect.provide(ElectronShell.layer)),
89 );
90​
91 it.effect("does not open remote editor URLs with userinfo", () =>
92 Effect.gen(function* () {
93 openExternalMock.mockResolvedValue(undefined);
94​
95 const electronShell = yield* ElectronShell.ElectronShell;
96 const results = yield* Effect.all([
97 electronShell.openExternal(
98 "vscode://user@vscode-remote/ssh-remote+example.com/home/user/project",
99 ),
100 electronShell.openExternal(
101 "vscode://:secret@vscode-remote/ssh-remote+example.com/home/user/project",
102 ),
103 ]);
104​
105 assert.deepEqual(results, [false, false]);
106 assert.equal(openExternalMock.mock.calls.length, 0);
107 }).pipe(Effect.provide(ElectronShell.layer)),
108 );
109​
110 it.effect("does not open unsafe external URLs", () =>
111 Effect.gen(function* () {
112 const electronShell = yield* ElectronShell.ElectronShell;
113 const result = yield* electronShell.openExternal("file:///etc/passwd");
114​
115 assert.equal(result, false);
116 assert.equal(openExternalMock.mock.calls.length, 0);
117 }).pipe(Effect.provide(ElectronShell.layer)),
118 );
119​
120 it.effect("does not open non-remote editor URLs", () =>
121 Effect.gen(function* () {
122 openExternalMock.mockResolvedValue(undefined);
123​
124 const electronShell = yield* ElectronShell.ElectronShell;
125 const result = yield* electronShell.openExternal(
126 "vscode://ms-python.python/some-command?argument=attacker",
127 );
128​
129 assert.equal(result, false);
130 assert.equal(openExternalMock.mock.calls.length, 0);
131 }).pipe(Effect.provide(ElectronShell.layer)),
132 );
133​
134 it.effect("returns false when Electron rejects openExternal", () =>
135 Effect.gen(function* () {
136 openExternalMock.mockRejectedValue(new Error("open failed"));
137​
138 const electronShell = yield* ElectronShell.ElectronShell;
139 const result = yield* electronShell.openExternal("https://example.com/path");
140​
141 assert.equal(result, false);
142 }).pipe(Effect.provide(ElectronShell.layer)),
143 );
144});