The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { assert, describe, it } from "@effect/vitest"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Exit from "effect/Exit"; |
| 4 | import * as Layer from "effect/Layer"; |
| 5 | |
| 6 | import * as ElectronWindow from "../../electron/ElectronWindow.ts"; |
| 7 | import * as ElectronDialog from "../../electron/ElectronDialog.ts"; |
| 8 | import * as DesktopSnapShot from "../../snapShot/DesktopSnapShot.ts"; |
| 9 | import { |
| 10 | setupSnapShot, |
| 11 | previewSnapShotConfig, |
| 12 | applySnapShotConfig, |
| 13 | snapShotRelativeFrame, |
| 14 | } from "./snapShot.ts"; |
| 15 | |
| 16 | describe("window capture IPC", () => { |
| 17 | const configPreview = { |
| 18 | id: "12345678-1234-1234-1234-123456789abc", |
| 19 | path: "/config/niri/config.kdl", |
| 20 | resolvedPath: "/config/niri/config.kdl", |
| 21 | before: "binds {}\n", |
| 22 | after: "binds {\n}\n", |
| 23 | shortcut: "Ctrl+Shift+2", |
| 24 | operation: "install" as const, |
| 25 | }; |
| 26 | it.effect("requires a trusted renderer for both config read and write approval", () => { |
| 27 | const calls: string[] = []; |
| 28 | return Effect.gen(function* () { |
| 29 | const request = { operation: "install" as const, chooseFile: false }; |
| 30 | const untrustedRead = yield* Effect.exit( |
| 31 | previewSnapShotConfig.handler(request, { sender: { id: 8 } }), |
| 32 | ); |
| 33 | const untrustedWrite = yield* Effect.exit( |
| 34 | applySnapShotConfig.handler(configPreview.id, { sender: { id: 8 } }), |
| 35 | ); |
| 36 | assert(Exit.isFailure(untrustedRead)); |
| 37 | assert(Exit.isFailure(untrustedWrite)); |
| 38 | assert.deepEqual(calls, []); |
| 39 | yield* previewSnapShotConfig.handler(request, { sender: { id: 7 } }); |
| 40 | assert.deepEqual(calls, ["read"]); |
| 41 | yield* applySnapShotConfig.handler(configPreview.id, { sender: { id: 7 } }); |
| 42 | assert.deepEqual(calls, ["read", configPreview.id]); |
| 43 | }).pipe( |
| 44 | Effect.provide( |
| 45 | Layer.mergeAll( |
| 46 | Layer.succeed(ElectronWindow.ElectronWindow, { |
| 47 | main: Effect.succeedSome({ webContents: { id: 7 } }), |
| 48 | } as ElectronWindow.ElectronWindow["Service"]), |
| 49 | Layer.succeed(DesktopSnapShot.DesktopSnapShot, { |
| 50 | previewConfig: () => |
| 51 | Effect.sync(() => { |
| 52 | calls.push("read"); |
| 53 | return configPreview; |
| 54 | }), |
| 55 | applyConfig: (id: string) => |
| 56 | Effect.sync(() => { |
| 57 | calls.push(id); |
| 58 | return { backupPath: null, warning: null }; |
| 59 | }), |
| 60 | } as unknown as DesktopSnapShot.DesktopSnapShot["Service"]), |
| 61 | Layer.succeed( |
| 62 | ElectronDialog.ElectronDialog, |
| 63 | {} as ElectronDialog.ElectronDialog["Service"], |
| 64 | ), |
| 65 | ), |
| 66 | ), |
| 67 | ); |
| 68 | }); |
| 69 | |
| 70 | it.effect("allows capture setup only from the trusted main renderer", () => { |
| 71 | const actions: string[] = []; |
| 72 | return Effect.gen(function* () { |
| 73 | yield* setupSnapShot.handler("install-extension", { sender: { id: 7 } }); |
| 74 | assert.deepEqual(actions, ["install-extension"]); |
| 75 | const rejected = yield* Effect.exit( |
| 76 | setupSnapShot.handler("enable-extension", { sender: { id: 8 } }), |
| 77 | ); |
| 78 | assert(Exit.isFailure(rejected)); |
| 79 | assert.deepEqual(actions, ["install-extension"]); |
| 80 | }).pipe( |
| 81 | Effect.provide( |
| 82 | Layer.mergeAll( |
| 83 | Layer.succeed(ElectronWindow.ElectronWindow, { |
| 84 | main: Effect.succeedSome({ webContents: { id: 7 } }), |
| 85 | } as ElectronWindow.ElectronWindow["Service"]), |
| 86 | Layer.succeed(DesktopSnapShot.DesktopSnapShot, { |
| 87 | setup: (action: string) => |
| 88 | Effect.sync(() => { |
| 89 | actions.push(action); |
| 90 | }), |
| 91 | } as unknown as DesktopSnapShot.DesktopSnapShot["Service"]), |
| 92 | ), |
| 93 | ), |
| 94 | ); |
| 95 | }); |
| 96 | }); |
| 97 | |
| 98 | it("normalizes Wayland attachment coordinates without trusting Electron's screen origin", () => { |
| 99 | const frame = { x: 100, y: 300, width: 200, height: 100 }; |
| 100 | const bounds = { x: 0, y: 0, width: 1000, height: 600 }; |
| 101 | assert.deepEqual(snapShotRelativeFrame(frame, bounds, 1.25), { |
| 102 | x: 0.125, |
| 103 | y: 0.625, |
| 104 | width: 0.25, |
| 105 | height: 125 / 600, |
| 106 | }); |
| 107 | assert.deepEqual( |
| 108 | snapShotRelativeFrame(frame, { ...bounds, x: -3840, y: 900 }, 1.25), |
| 109 | snapShotRelativeFrame(frame, bounds, 1.25), |
| 110 | ); |
| 111 | assert.isUndefined(snapShotRelativeFrame(frame, { ...bounds, width: 0 }, 1)); |
| 112 | }); |