33 DEFAULT_BROWSER_PROFILE_ID ,
44 INCOGNITO_BROWSER_PROFILE_ID ,
55 PreviewAutomationStatus ,
6+ PreviewCaptureFailure ,
67} from "@t3tools/contracts" ;
78import * as Cause from "effect/Cause" ;
89import * as Effect from "effect/Effect" ;
@@ -14,6 +15,8 @@ import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
1415import * as PreviewManager from "../../preview/Manager.ts" ;
1516import * as BrowserImport from "../../preview/BrowserImport/BrowserImport.ts" ;
1617import * as PreviewIpc from "./preview.ts" ;
18+ import * as DesktopIpc from "../DesktopIpc.ts" ;
19+ import * as DesktopClientSettings from "../../settings/DesktopClientSettings.ts" ;
1720
1821const { fromPartition } = vi . hoisted ( ( ) => ( {
1922 fromPartition : vi . fn ( ( ) => {
@@ -33,7 +36,102 @@ vi.mock("electron", () => ({
3336 } ,
3437} ) ) ;
3538
39+ const decodeCaptureFailure = Schema . decodeUnknownEffect ( PreviewCaptureFailure ) ;
40+
3641describe ( "preview IPC methods" , ( ) => {
42+ const invokeCapture = (
43+ method : typeof PreviewIpc . startRecording | typeof PreviewIpc . captureScreenshot ,
44+ manager : Partial < PreviewManager . PreviewManager [ "Service" ] > ,
45+ ) =>
46+ Effect . scoped (
47+ Effect . gen ( function * ( ) {
48+ let listener : DesktopIpc . DesktopIpcHandleListener | undefined ;
49+ const ipc = DesktopIpc . make ( {
50+ removeHandler : vi . fn ( ) ,
51+ handle : ( _channel , registered ) => {
52+ listener = registered ;
53+ } ,
54+ removeAllListeners : vi . fn ( ) ,
55+ on : vi . fn ( ) ,
56+ } ) ;
57+ yield * ipc . handle ( method ) ;
58+ return yield * Effect . promise ( async ( ) =>
59+ structuredClone ( await listener ! ( { sender : { id : 7 } } , { tabId : "capture-tab" } ) ) ,
60+ ) ;
61+ } ) . pipe (
62+ Effect . provideService (
63+ PreviewManager . PreviewManager ,
64+ manager as PreviewManager . PreviewManager [ "Service" ] ,
65+ ) ,
66+ Effect . provideService ( DesktopClientSettings . DesktopClientSettings , {
67+ get : Effect . succeedNone ,
68+ set : ( ) => Effect . void ,
69+ } ) ,
70+ ) ,
71+ ) ;
72+
73+ effectIt . effect . each ( [
74+ {
75+ cause : new Error ( "Current display surface not available for capture" ) ,
76+ reason : "capture-unavailable" ,
77+ guidance : [ "target tab" , "unlocked" , "display is on" , "window is restored" ] ,
78+ } ,
79+ {
80+ cause : new Cause . TimeoutError ( ) ,
81+ reason : "capture-timeout" ,
82+ guidance : [ "timed out during capture-page" ] ,
83+ } ,
84+ {
85+ cause : new Error ( "Current display surface not available for capture: private page data" ) ,
86+ reason : "capture-failed" ,
87+ guidance : [ "Preview capture failed during capture-page" ] ,
88+ } ,
89+ ] ) (
90+ "transports a safe $reason screenshot diagnostic through registered IPC" ,
91+ ( { cause, reason, guidance } ) =>
92+ Effect . gen ( function * ( ) {
93+ const result = yield * invokeCapture ( PreviewIpc . captureScreenshot , {
94+ captureScreenshot : ( tabId ) =>
95+ Effect . fail (
96+ new PreviewManager . PreviewOperationError ( {
97+ operation : "captureScreenshot.capturePage" ,
98+ tabId,
99+ artifactPath : "/private/capture.png" ,
100+ cause,
101+ } ) ,
102+ ) ,
103+ } ) ;
104+ expect ( result ) . toEqual ( {
105+ _tag : "PreviewCaptureFailure" ,
106+ reason,
107+ stage : "capture-page" ,
108+ } ) ;
109+ const failure = yield * decodeCaptureFailure ( result ) ;
110+ for ( const phrase of guidance ) expect ( failure . message ) . toContain ( phrase ) ;
111+ } ) ,
112+ ) ;
113+
114+ effectIt . effect ( "preserves recording arm failures without erasing successful void results" , ( ) =>
115+ Effect . gen ( function * ( ) {
116+ const result = yield * invokeCapture ( PreviewIpc . startRecording , {
117+ startRecording : ( tabId ) =>
118+ Effect . fail ( new PreviewManager . PreviewTabNotFoundError ( { tabId } ) ) ,
119+ } ) ;
120+ expect ( result ) . toEqual ( {
121+ _tag : "PreviewCaptureFailure" ,
122+ reason : "renderer-unavailable" ,
123+ stage : "recording-start" ,
124+ } ) ;
125+ const failure = yield * decodeCaptureFailure ( result ) ;
126+ expect ( failure . message ) . toContain ( "Reopen the preview tab" ) ;
127+ expect (
128+ yield * invokeCapture ( PreviewIpc . startRecording , {
129+ startRecording : ( ) => Effect . void ,
130+ } ) ,
131+ ) . toBeUndefined ( ) ;
132+ } ) ,
133+ ) ;
134+
37135 beforeEach ( ( ) => {
38136 fromPartition . mockClear ( ) ;
39137 } ) ;
0 commit comments