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

snapShot.test.ts

358 lines13.0 KB

1import { assert, describe, it } from "@effect/vitest";
2import * as Cause from "effect/Cause";
3import * as Effect from "effect/Effect";
4import * as Exit from "effect/Exit";
5import * as Layer from "effect/Layer";
6import * as Option from "effect/Option";
7​
8import * as ElectronWindow from "../../electron/ElectronWindow.ts";
9import * as ElectronDialog from "../../electron/ElectronDialog.ts";
10import * as DesktopSnapShot from "../../snapShot/DesktopSnapShot.ts";
11import {
12 checkSnapShotShortcut,
13 requestSnapShotPermissions,
14 setupSnapShot,
15 previewSnapShotConfig,
16 applySnapShotConfig,
17 setSnapShotAnimationDestination,
18 setSnapShotShortcutSuppressed,
19 snapShotScreenFrame,
20 snapShotRelativeFrame,
21} from "./snapShot.ts";
22​
23describe("window capture IPC", () => {
24 const configPreview = {
25 id: "12345678-1234-1234-1234-123456789abc",
26 path: "/config/niri/config.kdl",
27 resolvedPath: "/config/niri/config.kdl",
28 before: "binds {}\n",
29 after: "binds {\n}\n",
30 shortcut: "Ctrl+Shift+2",
31 operation: "install" as const,
32 };
33 it.effect("requires a trusted renderer for both config read and write approval", () => {
34 const calls: string[] = [];
35 return Effect.gen(function* () {
36 const request = { operation: "install" as const, chooseFile: false };
37 const untrustedRead = yield* Effect.exit(
38 previewSnapShotConfig.handler(request, { sender: { id: 8 } }),
39 );
40 const untrustedWrite = yield* Effect.exit(
41 applySnapShotConfig.handler(configPreview.id, { sender: { id: 8 } }),
42 );
43 assert(Exit.isFailure(untrustedRead));
44 assert(Exit.isFailure(untrustedWrite));
45 assert.deepEqual(calls, []);
46 yield* previewSnapShotConfig.handler(request, { sender: { id: 7 } });
47 assert.deepEqual(calls, ["read"]);
48 yield* applySnapShotConfig.handler(configPreview.id, { sender: { id: 7 } });
49 assert.deepEqual(calls, ["read", configPreview.id]);
50 }).pipe(
51 Effect.provide(
52 Layer.mergeAll(
53 Layer.succeed(ElectronWindow.ElectronWindow, {
54 main: Effect.succeed(Option.some({ webContents: { id: 7 } })),
55 } as ElectronWindow.ElectronWindow["Service"]),
56 Layer.succeed(DesktopSnapShot.DesktopSnapShot, {
57 previewConfig: () =>
58 Effect.sync(() => {
59 calls.push("read");
60 return configPreview;
61 }),
62 applyConfig: (id: string) =>
63 Effect.sync(() => {
64 calls.push(id);
65 return { backupPath: null, warning: null };
66 }),
67 } as unknown as DesktopSnapShot.DesktopSnapShot["Service"]),
68 Layer.succeed(
69 ElectronDialog.ElectronDialog,
70 {} as ElectronDialog.ElectronDialog["Service"],
71 ),
72 ),
73 ),
74 );
75 });
76​
77 it.effect("cancelling custom file selection reads and writes nothing", () => {
78 let read = false;
79 return Effect.gen(function* () {
80 const preview = yield* previewSnapShotConfig.handler(
81 { operation: "install", chooseFile: true },
82 { sender: { id: 7 } },
83 );
84 assert.isNull(preview);
85 assert.isFalse(read);
86 }).pipe(
87 Effect.provide(
88 Layer.mergeAll(
89 Layer.succeed(ElectronWindow.ElectronWindow, {
90 main: Effect.succeed(Option.some({ webContents: { id: 7 } })),
91 } as ElectronWindow.ElectronWindow["Service"]),
92 Layer.succeed(DesktopSnapShot.DesktopSnapShot, {
93 state: Effect.succeed({
94 linuxBackend: "niri",
95 shortcutConfigPath: "/config/niri/config.kdl",
96 }),
97 previewConfig: () =>
98 Effect.sync(() => {
99 read = true;
100 return configPreview;
101 }),
102 } as unknown as DesktopSnapShot.DesktopSnapShot["Service"]),
103 Layer.succeed(ElectronDialog.ElectronDialog, {
104 pickFiles: () => Effect.succeed([]),
105 } as unknown as ElectronDialog.ElectronDialog["Service"]),
106 ),
107 ),
108 );
109 });
110​
111 it.effect("uses only the file returned by the native custom config picker", () => {
112 let path: string | undefined;
113 return Effect.gen(function* () {
114 yield* previewSnapShotConfig.handler(
115 { operation: "install", chooseFile: true },
116 { sender: { id: 7 } },
117 );
118 assert.equal(path, "/chosen/config.kdl");
119 }).pipe(
120 Effect.provide(
121 Layer.mergeAll(
122 Layer.succeed(ElectronWindow.ElectronWindow, {
123 main: Effect.succeed(Option.some({ webContents: { id: 7 } })),
124 } as ElectronWindow.ElectronWindow["Service"]),
125 Layer.succeed(DesktopSnapShot.DesktopSnapShot, {
126 state: Effect.succeed({ linuxBackend: "niri" }),
127 previewConfig: (_: unknown, selected: string) =>
128 Effect.sync(() => {
129 path = selected;
130 return configPreview;
131 }),
132 } as unknown as DesktopSnapShot.DesktopSnapShot["Service"]),
133 Layer.succeed(ElectronDialog.ElectronDialog, {
134 pickFiles: () => Effect.succeed(["/chosen/config.kdl"]),
135 } as unknown as ElectronDialog.ElectronDialog["Service"]),
136 ),
137 ),
138 );
139 });
140​
141 it("converts renderer viewport coordinates from the content origin using the window zoom", () => {
142 assert.deepEqual(
143 snapShotScreenFrame(
144 { x: 12, y: 20, width: 208, height: 112 },
145 { x: 100, y: 80, width: 1_000, height: 700 },
146 1.25,
147 ),
148 { x: 115, y: 105, width: 260, height: 140 },
149 );
150 });
151​
152 it.effect("forwards a trusted renderer animation destination in screen coordinates", () => {
153 let received: unknown;
154 const webContents = { id: 7, getZoomFactor: () => 1.25 };
155 const layer = Layer.mergeAll(
156 Layer.succeed(
157 ElectronWindow.ElectronWindow,
158 ElectronWindow.ElectronWindow.of({
159 main: Effect.succeed(
160 Option.some({
161 getBounds: () => ({ x: 100, y: 80, width: 1_000, height: 700 }),
162 getContentBounds: () => ({ x: 100, y: 118, width: 1_000, height: 662 }),
163 webContents,
164 }),
165 ),
166 } as ElectronWindow.ElectronWindow["Service"]),
167 ),
168 Layer.succeed(
169 DesktopSnapShot.DesktopSnapShot,
170 DesktopSnapShot.DesktopSnapShot.of({
171 setAnimationDestination: (id: string, destination: unknown) =>
172 Effect.sync(() => {
173 received = { id, destination };
174 }),
175 } as unknown as DesktopSnapShot.DesktopSnapShot["Service"]),
176 ),
177 );
178​
179 return Effect.gen(function* () {
180 yield* setSnapShotAnimationDestination.handler(
181 {
182 id: "12345678-1234-1234-1234-123456789abc",
183 viewportFrame: { x: 12, y: 20, width: 208, height: 112 },
184 backgroundColor: "rgb(20, 20, 20)",
185 borderColor: "rgba(80, 80, 80, 0.8)",
186 borderWidth: 1,
187 cornerRadius: 8,
188 details: {
189 appName: "T3 Code",
190 windowTitle: "Capture animation",
191 appIconDataUrl: "data:image/png;base64,aWNvbg==",
192 },
193 },
194 { sender: webContents },
195 );
196 assert.deepEqual(received, {
197 id: "12345678-1234-1234-1234-123456789abc",
198 destination: {
199 relativeFrame: { x: 15 / 1000, y: 25 / 662, width: 260 / 1000, height: 140 / 662 },
200 frame: { x: 115, y: 143, width: 260, height: 140 },
201 backgroundColor: "rgb(20, 20, 20)",
202 borderColor: "rgba(80, 80, 80, 0.8)",
203 borderWidth: 1.25,
204 cornerRadius: 10,
205 scaleFactor: 1.25,
206 details: {
207 appName: "T3 Code",
208 windowTitle: "Capture animation",
209 appIconDataUrl: "data:image/png;base64,aWNvbg==",
210 },
211 },
212 });
213 }).pipe(Effect.provide(layer));
214 });
215​
216 it.effect("forwards the accessibility permission preference from a trusted renderer", () => {
217 let includeAccessibility: boolean | undefined;
218 const webContents = { id: 7 };
219 const layer = Layer.mergeAll(
220 Layer.succeed(
221 ElectronWindow.ElectronWindow,
222 ElectronWindow.ElectronWindow.of({
223 main: Effect.succeed(Option.some({ webContents })),
224 } as ElectronWindow.ElectronWindow["Service"]),
225 ),
226 Layer.succeed(
227 DesktopSnapShot.DesktopSnapShot,
228 DesktopSnapShot.DesktopSnapShot.of({
229 requestPermissions: (include: boolean) =>
230 Effect.sync(() => {
231 includeAccessibility = include;
232 }),
233 } as unknown as DesktopSnapShot.DesktopSnapShot["Service"]),
234 ),
235 );
236​
237 return Effect.gen(function* () {
238 yield* requestSnapShotPermissions.handler(false, { sender: webContents });
239 assert.isFalse(includeAccessibility);
240 }).pipe(Effect.provide(layer));
241 });
242​
243 it.effect("rejects an untrusted renderer at the IPC boundary", () =>
244 Effect.gen(function* () {
245 const exit = yield* Effect.exit(
246 requestSnapShotPermissions.handler(false, { sender: { id: 8 } }),
247 );
248 assert(Exit.isFailure(exit));
249 const failure = Cause.findErrorOption(exit.cause);
250 assert(Option.isSome(failure));
251 const error = failure.value;
252​
253 assert.equal((error as { readonly _tag: string })._tag, "SnapShotIpcUnauthorizedSenderError");
254 assert.equal((error as Error).message, "Snapshot request was rejected.");
255 }).pipe(
256 Effect.provideService(
257 ElectronWindow.ElectronWindow,
258 ElectronWindow.ElectronWindow.of({
259 main: Effect.succeed(Option.some({ webContents: { id: 7 } })),
260 } as ElectronWindow.ElectronWindow["Service"]),
261 ),
262 Effect.provideService(DesktopSnapShot.DesktopSnapShot, null as never),
263 ),
264 );
265​
266 it.effect("allows capture setup only from the trusted main renderer", () => {
267 const actions: string[] = [];
268 return Effect.gen(function* () {
269 yield* setupSnapShot.handler("install-extension", { sender: { id: 7 } });
270 assert.deepEqual(actions, ["install-extension"]);
271 const rejected = yield* Effect.exit(
272 setupSnapShot.handler("enable-extension", { sender: { id: 8 } }),
273 );
274 assert(Exit.isFailure(rejected));
275 assert.deepEqual(actions, ["install-extension"]);
276 }).pipe(
277 Effect.provide(
278 Layer.mergeAll(
279 Layer.succeed(ElectronWindow.ElectronWindow, {
280 main: Effect.succeed(Option.some({ webContents: { id: 7 } })),
281 } as ElectronWindow.ElectronWindow["Service"]),
282 Layer.succeed(DesktopSnapShot.DesktopSnapShot, {
283 setup: (action: string) =>
284 Effect.sync(() => {
285 actions.push(action);
286 }),
287 } as unknown as DesktopSnapShot.DesktopSnapShot["Service"]),
288 ),
289 ),
290 );
291 });
292​
293 it.effect("checks shortcut availability for a trusted renderer", () => {
294 const layer = Layer.mergeAll(
295 Layer.succeed(
296 ElectronWindow.ElectronWindow,
297 ElectronWindow.ElectronWindow.of({
298 main: Effect.succeed(Option.some({ webContents: { id: 7 } })),
299 } as ElectronWindow.ElectronWindow["Service"]),
300 ),
301 Layer.succeed(
302 DesktopSnapShot.DesktopSnapShot,
303 DesktopSnapShot.DesktopSnapShot.of({
304 checkShortcut: () => Effect.succeed({ available: true, message: null }),
305 } as unknown as DesktopSnapShot.DesktopSnapShot["Service"]),
306 ),
307 );
308​
309 return Effect.gen(function* () {
310 const result = yield* checkSnapShotShortcut.handler(
311 { kind: "both-shift-keys" },
312 { sender: { id: 7 } },
313 );
314 assert.deepEqual(result, { available: true, message: null });
315 }).pipe(Effect.provide(layer));
316 });
317 it.effect("suppresses the active shortcut for a trusted renderer", () => {
318 let suppressed = false;
319 const layer = Layer.mergeAll(
320 Layer.succeed(
321 ElectronWindow.ElectronWindow,
322 ElectronWindow.ElectronWindow.of({
323 main: Effect.succeed(Option.some({ webContents: { id: 7 } })),
324 } as ElectronWindow.ElectronWindow["Service"]),
325 ),
326 Layer.succeed(
327 DesktopSnapShot.DesktopSnapShot,
328 DesktopSnapShot.DesktopSnapShot.of({
329 setShortcutSuppressed: (next: boolean) =>
330 Effect.sync(() => {
331 suppressed = next;
332 }),
333 } as unknown as DesktopSnapShot.DesktopSnapShot["Service"]),
334 ),
335 );
336​
337 return Effect.gen(function* () {
338 yield* setSnapShotShortcutSuppressed.handler(true, { sender: { id: 7 } });
339 assert.isTrue(suppressed);
340 }).pipe(Effect.provide(layer));
341 });
342});
343​
344it("normalizes Wayland attachment coordinates without trusting Electron's screen origin", () => {
345 const frame = { x: 100, y: 300, width: 200, height: 100 };
346 const bounds = { x: 0, y: 0, width: 1000, height: 600 };
347 assert.deepEqual(snapShotRelativeFrame(frame, bounds, 1.25), {
348 x: 0.125,
349 y: 0.625,
350 width: 0.25,
351 height: 125 / 600,
352 });
353 assert.deepEqual(
354 snapShotRelativeFrame(frame, { ...bounds, x: -3840, y: 900 }, 1.25),
355 snapShotRelativeFrame(frame, bounds, 1.25),
356 );
357 assert.isUndefined(snapShotRelativeFrame(frame, { ...bounds, width: 0 }, 1));
358});