The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { it as effectIt } from "@effect/vitest"; |
| 2 | import { |
| 3 | DEFAULT_BROWSER_PROFILE_ID, |
| 4 | INCOGNITO_BROWSER_PROFILE_ID, |
| 5 | PreviewCaptureFailure, |
| 6 | } from "@t3tools/contracts"; |
| 7 | import * as Cause from "effect/Cause"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Schema from "effect/Schema"; |
| 10 | import { beforeEach, describe, expect, it, vi } from "vite-plus/test"; |
| 11 | |
| 12 | import * as PreviewManager from "../../preview/Manager.ts"; |
| 13 | import * as PreviewIpc from "./preview.ts"; |
| 14 | import * as DesktopIpc from "../DesktopIpc.ts"; |
| 15 | import * as DesktopClientSettings from "../../settings/DesktopClientSettings.ts"; |
| 16 | |
| 17 | const { fromPartition } = vi.hoisted(() => ({ |
| 18 | fromPartition: vi.fn(() => { |
| 19 | throw new Error("Session can only be received when app is ready"); |
| 20 | }), |
| 21 | })); |
| 22 | |
| 23 | vi.mock("electron", () => ({ |
| 24 | BrowserWindow: { |
| 25 | getAllWindows: vi.fn(() => []), |
| 26 | }, |
| 27 | session: { |
| 28 | fromPartition, |
| 29 | }, |
| 30 | webContents: { |
| 31 | fromId: vi.fn(() => null), |
| 32 | }, |
| 33 | })); |
| 34 | |
| 35 | const decodeCaptureFailure = Schema.decodeUnknownEffect(PreviewCaptureFailure); |
| 36 | |
| 37 | describe("preview IPC methods", () => { |
| 38 | const invokeCapture = ( |
| 39 | method: typeof PreviewIpc.startRecording | typeof PreviewIpc.captureScreenshot, |
| 40 | manager: Partial<PreviewManager.PreviewManager["Service"]>, |
| 41 | ) => |
| 42 | Effect.scoped( |
| 43 | Effect.gen(function* () { |
| 44 | let listener: DesktopIpc.DesktopIpcHandleListener | undefined; |
| 45 | const ipc = DesktopIpc.make( |
| 46 | { |
| 47 | removeHandler: vi.fn(), |
| 48 | handle: (_channel, registered) => { |
| 49 | listener = registered; |
| 50 | }, |
| 51 | removeAllListeners: vi.fn(), |
| 52 | on: vi.fn(), |
| 53 | }, |
| 54 | () => Effect.succeed(true), |
| 55 | ); |
| 56 | yield* ipc.handle(method); |
| 57 | return yield* Effect.promise(async () => |
| 58 | structuredClone(await listener!({ sender: { id: 7 } }, { tabId: "capture-tab" })), |
| 59 | ); |
| 60 | }).pipe( |
| 61 | Effect.provideService( |
| 62 | PreviewManager.PreviewManager, |
| 63 | manager as PreviewManager.PreviewManager["Service"], |
| 64 | ), |
| 65 | Effect.provideService(DesktopClientSettings.DesktopClientSettings, { |
| 66 | get: Effect.succeedNone, |
| 67 | set: () => Effect.void, |
| 68 | }), |
| 69 | ), |
| 70 | ); |
| 71 | |
| 72 | effectIt.effect.each([ |
| 73 | { |
| 74 | cause: new Error("Current display surface not available for capture"), |
| 75 | reason: "capture-unavailable", |
| 76 | guidance: ["target tab", "unlocked", "display is on", "window is restored"], |
| 77 | }, |
| 78 | { |
| 79 | cause: new Cause.TimeoutError(), |
| 80 | reason: "capture-timeout", |
| 81 | guidance: ["timed out during capture-page"], |
| 82 | }, |
| 83 | { |
| 84 | cause: new Error("Current display surface not available for capture: private page data"), |
| 85 | reason: "capture-failed", |
| 86 | guidance: ["Preview capture failed during capture-page"], |
| 87 | }, |
| 88 | ])( |
| 89 | "transports a safe $reason screenshot diagnostic through registered IPC", |
| 90 | ({ cause, reason, guidance }) => |
| 91 | Effect.gen(function* () { |
| 92 | const result = yield* invokeCapture(PreviewIpc.captureScreenshot, { |
| 93 | captureScreenshot: (tabId) => |
| 94 | Effect.fail( |
| 95 | new PreviewManager.PreviewOperationError({ |
| 96 | operation: "captureScreenshot.capturePage", |
| 97 | tabId, |
| 98 | artifactPath: "/private/capture.png", |
| 99 | cause, |
| 100 | }), |
| 101 | ), |
| 102 | }); |
| 103 | expect(result).toEqual({ |
| 104 | _tag: "PreviewCaptureFailure", |
| 105 | reason, |
| 106 | stage: "capture-page", |
| 107 | }); |
| 108 | const failure = yield* decodeCaptureFailure(result); |
| 109 | for (const phrase of guidance) expect(failure.message).toContain(phrase); |
| 110 | }), |
| 111 | ); |
| 112 | |
| 113 | beforeEach(() => { |
| 114 | fromPartition.mockClear(); |
| 115 | }); |
| 116 | |
| 117 | it("derives distinct partition scopes when identifiers contain the delimiter", () => { |
| 118 | const first = PreviewIpc.resolvePartitionScope("a", "b::c"); |
| 119 | const second = PreviewIpc.resolvePartitionScope("a::b", "c"); |
| 120 | |
| 121 | expect(first).toEqual({ scope: '["a","b::c"]', persistent: true, namespace: "profile" }); |
| 122 | expect(second).toEqual({ scope: '["a::b","c"]', persistent: true, namespace: "profile" }); |
| 123 | expect(first.scope).not.toBe(second.scope); |
| 124 | }); |
| 125 | |
| 126 | it("keeps the legacy default partition scope and incognito persistence", () => { |
| 127 | expect(PreviewIpc.resolvePartitionScope("environment::legacy", undefined)).toEqual({ |
| 128 | scope: "environment::legacy", |
| 129 | persistent: true, |
| 130 | }); |
| 131 | expect( |
| 132 | PreviewIpc.resolvePartitionScope("environment::legacy", DEFAULT_BROWSER_PROFILE_ID), |
| 133 | ).toEqual({ scope: "environment::legacy", persistent: true }); |
| 134 | expect( |
| 135 | PreviewIpc.resolvePartitionScope("environment::legacy", INCOGNITO_BROWSER_PROFILE_ID), |
| 136 | ).toEqual({ |
| 137 | scope: '["environment::legacy","incognito"]', |
| 138 | persistent: false, |
| 139 | namespace: "profile", |
| 140 | }); |
| 141 | }); |
| 142 | }); |