@@ -2,6 +2,7 @@ import { it as effectIt } from "@effect/vitest";
22import * as NodeCrypto from "@effect/platform-node/NodeCrypto" ;
33import * as NodeServices from "@effect/platform-node/NodeServices" ;
44import type {
5+ DesktopPreviewFindResultEvent ,
56 DesktopPreviewRecordingInputEvent ,
67 PreviewAnnotationPayload ,
78 PreviewAnnotationRect ,
@@ -812,6 +813,135 @@ describe("PreviewManager", () => {
812813 ) ,
813814 ) ;
814815
816+ effectIt . effect . each ( [
817+ [ "darwin" , true ] ,
818+ [ "linux" , false ] ,
819+ [ "win32" , false ] ,
820+ ] as const ) (
821+ "lets the page and its iframes claim the find shortcut before the host opens find on %s" ,
822+ ( [ platform , usesMeta ] ) =>
823+ withManager (
824+ ( manager ) =>
825+ Effect . gen ( function * ( ) {
826+ const preview = makeFaviconWebContents ( ) ;
827+ const send = vi . fn ( ) ;
828+ const mainFrame = { processId : 1 , routingId : 1 } ;
829+ Object . assign ( preview . webContents , {
830+ hostWebContents : { isDestroyed : ( ) => false , send } ,
831+ mainFrame,
832+ focusedFrame : mainFrame ,
833+ } ) ;
834+ fromId . mockReturnValue ( preview . webContents ) ;
835+ yield * manager . createTab ( "tab_page_find" ) ;
836+ yield * manager . registerWebview ( "tab_page_find" , 42 ) ;
837+ yield * manager . setForwardedShortcuts ( [
838+ { command : "preview.find" , shortcut : parseKeybindingShortcut ( "mod+f" ) ! } ,
839+ ] ) ;
840+ const beforeInput = preview . listeners . get ( "before-input-event" ) ! ;
841+ const findKey = {
842+ type : "keyDown" ,
843+ key : "f" ,
844+ code : "KeyF" ,
845+ meta : usesMeta ,
846+ control : ! usesMeta ,
847+ } ;
848+ const preventDefault = vi . fn ( ) ;
849+
850+ beforeInput (
851+ { preventDefault } as never ,
852+ { ...findKey , shift : false , alt : false } as never ,
853+ ) ;
854+ expect ( preventDefault ) . not . toHaveBeenCalled ( ) ;
855+ expect ( send ) . not . toHaveBeenCalled ( ) ;
856+
857+ const ipcOn = ( preview . webContents as Electron . WebContents ) . ipc . on as ReturnType <
858+ typeof vi . fn
859+ > ;
860+ const pageUnhandledShortcut = ipcOn . mock . calls . find (
861+ ( [ channel ] ) => channel === "preview:page-unhandled-shortcut" ,
862+ ) ! [ 1 ] ;
863+ pageUnhandledShortcut (
864+ { } ,
865+ {
866+ key : "f" ,
867+ code : "KeyF" ,
868+ metaKey : usesMeta ,
869+ ctrlKey : ! usesMeta ,
870+ shiftKey : false ,
871+ altKey : false ,
872+ } ,
873+ ) ;
874+ expect ( send ) . toHaveBeenCalledExactlyOnceWith ( "desktop:menu-action" , "preview.find" ) ;
875+
876+ Object . assign ( preview . webContents , { focusedFrame : { processId : 1 , routingId : 2 } } ) ;
877+ beforeInput (
878+ { preventDefault } as never ,
879+ { ...findKey , shift : false , alt : false } as never ,
880+ ) ;
881+ expect ( preventDefault ) . not . toHaveBeenCalled ( ) ;
882+ expect ( send ) . toHaveBeenCalledOnce ( ) ;
883+
884+ Object . assign ( preview . webContents , { focusedFrame : mainFrame } ) ;
885+ yield * manager . setForwardedShortcuts ( [
886+ { command : "preview.find" , shortcut : parseKeybindingShortcut ( "ctrl+alt+8" ) ! } ,
887+ ] ) ;
888+ pageUnhandledShortcut (
889+ { } ,
890+ {
891+ key : "[" ,
892+ code : "Digit8" ,
893+ metaKey : false ,
894+ ctrlKey : true ,
895+ shiftKey : false ,
896+ altKey : true ,
897+ } ,
898+ ) ;
899+ expect ( send ) . toHaveBeenCalledTimes ( usesMeta ? 2 : 1 ) ;
900+ } ) ,
901+ platform ,
902+ ) ,
903+ ) ;
904+
905+ effectIt . effect ( "searches the guest page and reports its match counts" , ( ) =>
906+ withManager ( ( manager ) =>
907+ Effect . gen ( function * ( ) {
908+ const preview = makeFaviconWebContents ( ) ;
909+ const findInPage = vi . fn ( ) ;
910+ const stopFindInPage = vi . fn ( ) ;
911+ Object . assign ( preview . webContents , { findInPage, stopFindInPage } ) ;
912+ fromId . mockReturnValue ( preview . webContents ) ;
913+ yield * manager . createTab ( "tab_find" ) ;
914+ yield * manager . registerWebview ( "tab_find" , 42 ) ;
915+ const reported = yield * Deferred . make < DesktopPreviewFindResultEvent > ( ) ;
916+ yield * manager . subscribeFindResults ( ( event ) => Deferred . succeed ( reported , event ) ) ;
917+
918+ yield * manager . findInPage ( "tab_find" , "needle" , { forward : true , newSession : true } ) ;
919+ expect ( findInPage ) . toHaveBeenCalledExactlyOnceWith ( "needle" , {
920+ forward : true ,
921+ findNext : true ,
922+ } ) ;
923+
924+ preview . listeners . get ( "found-in-page" ) ! (
925+ { } as never ,
926+ {
927+ requestId : 1 ,
928+ activeMatchOrdinal : 2 ,
929+ matches : 5 ,
930+ finalUpdate : true ,
931+ } as never ,
932+ ) ;
933+ expect ( yield * Deferred . await ( reported ) ) . toEqual ( {
934+ tabId : "tab_find" ,
935+ activeMatchOrdinal : 2 ,
936+ matches : 5 ,
937+ } ) ;
938+
939+ yield * manager . stopFindInPage ( "tab_find" ) ;
940+ expect ( stopFindInPage ) . toHaveBeenCalledExactlyOnceWith ( "clearSelection" ) ;
941+ } ) ,
942+ ) ,
943+ ) ;
944+
815945 effectIt . effect ( "isolates failed state listeners and continues delivery" , ( ) => {
816946 const loggedErrors : Array < unknown > = [ ] ;
817947 const logger = Logger . make ( ( { message } ) => {
0 commit comments