@@ -60,23 +60,41 @@ vi.mock("./ServerBrowserContexts.ts", () => ({
6060 } ,
6161} ) ) ;
6262
63- function makeSession ( ) {
63+ /** A frame in its own process. Only its own session reaches the copy it holds. */
64+ function makeFrame ( ) {
65+ const frame = {
66+ copy : "" ,
67+ detached : false ,
68+ isDetached : ( ) => frame . detached ,
69+ recording : Promise . withResolvers < void > ( ) ,
70+ } ;
71+ return frame ;
72+ }
73+
74+ function makeSession ( frame ?: ReturnType < typeof makeFrame > ) {
75+ const events = new NodeEvents . EventEmitter ( ) ;
6476 return {
65- on : vi . fn ( ) ,
77+ on : vi . fn ( ( name : string , listener : ( ) => void ) => events . on ( name , listener ) ) ,
78+ emit : ( name : string ) => events . emit ( name ) ,
6679 detach : vi . fn ( async ( ) => { } ) ,
6780 send : vi . fn ( async ( method : string , input ?: unknown ) : Promise < Record < string , unknown > > => {
6881 if ( method === "Page.getNavigationHistory" ) return { currentIndex : 0 , entries : [ { } ] } ;
6982 if ( method === "Page.getLayoutMetrics" ) return { cssVisualViewport : { pageX : 0 , pageY : 0 } } ;
7083 if ( method === "Page.captureScreenshot" ) return { data : "ZnJhbWU=" } ;
7184 if ( method === "Page.getFrameTree" ) return { frameTree : { frame : { id : "main" } } } ;
7285 if ( method === "Page.createIsolatedWorld" ) return { executionContextId : 7 } ;
73- // A copy or cut key copies keyCopy; one that copies nothing leaves the frames as they were.
74- if ( method === "Input.dispatchKeyEvent" && ( input as { commands ?: unknown } ) . commands )
75- pageCopy = keyCopy || pageCopy ;
76- // The clipboard world hands over what the page's frames copied, once.
86+ if ( method === "Page.addScriptToEvaluateOnNewDocument" ) frame ?. recording . resolve ( ) ;
87+ // A copy or cut key copies keyCopy in the focused frame, or the page's process if none;
88+ // one that copies nothing leaves the frames as they were.
89+ if ( method === "Input.dispatchKeyEvent" && ( input as { commands ?: unknown } ) . commands ) {
90+ if ( focusedFrame ) focusedFrame . copy = keyCopy || focusedFrame . copy ;
91+ else pageCopy = keyCopy || pageCopy ;
92+ }
93+ // The clipboard world hands over what the session's frames copied, once.
7794 if ( method === "Runtime.evaluate" && ( input as { contextId ?: number } ) . contextId === 7 ) {
78- const value = pageCopy ;
79- pageCopy = "" ;
95+ const value = frame ? frame . copy : pageCopy ;
96+ if ( frame ) frame . copy = "" ;
97+ else pageCopy = "" ;
8098 return { result : { value } } ;
8199 }
82100 return { result : { value : "evaluated" } } ;
@@ -102,6 +120,7 @@ function makeContext(onClose?: (context: BrowserContext) => void) {
102120 setDefaultNavigationTimeout : vi . fn ( ) ,
103121 context : ( ) => context ,
104122 mainFrame : ( ) => page ,
123+ frames : ( ) => [ page ] ,
105124 url : ( ) => url ,
106125 title : vi . fn ( async ( ) => "test page" ) ,
107126 viewportSize : ( ) => viewport ,
@@ -141,8 +160,10 @@ function makeContext(onClose?: (context: BrowserContext) => void) {
141160 sessions,
142161 newPage : async ( ) => page as unknown as Page ,
143162 grantPermissions : vi . fn ( async ( ) => { } ) ,
144- newCDPSession : async ( ) => {
145- const session = makeSession ( ) ;
163+ newCDPSession : async ( target : unknown ) => {
164+ const session = makeSession (
165+ target === page ? undefined : ( target as ReturnType < typeof makeFrame > ) ,
166+ ) ;
146167 sessions . push ( session ) ;
147168 return session ;
148169 } ,
@@ -164,6 +185,8 @@ let contextGate: PromiseWithResolvers<void> | null = null;
164185let pageCopy = "" ;
165186/** The text the next copy or cut key copies. */
166187let keyCopy = "" ;
188+ /** The frame in its own process that has focus; otherwise keys copy in the page's process. */
189+ let focusedFrame : ReturnType < typeof makeFrame > | null = null ;
167190let contextFailure : Error | null = null ;
168191/** Server tabs the fake desktop renders, and the endpoints the server connected to. */
169192let desktopRendersNext = false ;
@@ -283,6 +306,7 @@ beforeEach(() => {
283306 contextFailure = null ;
284307 pageCopy = "" ;
285308 keyCopy = "" ;
309+ focusedFrame = null ;
286310 desktopTabs . clear ( ) ;
287311 desktopRendersNext = false ;
288312 releasedDesktopTabs . length = 0 ;
@@ -1057,6 +1081,40 @@ it.live("only the controlling viewer's copy and cut keys reach its clipboard", (
10571081 ) . pipe ( Effect . provide ( layer ) ) ,
10581082) ;
10591083
1084+ it . live ( "copy keys reach the viewer from a frame in its own process" , ( ) =>
1085+ Effect . scoped (
1086+ Effect . gen ( function * ( ) {
1087+ const { browser, tabId } = yield * ready ;
1088+ const viewer = yield * browser . attachViewer ( viewerInput ( tabId , true ) ) ;
1089+ yield * viewer . input ( { type : "takeControl" } ) ;
1090+ const copy = ( ) =>
1091+ viewer . input ( { type : "key" , action : "down" , key : "c" , code : "KeyC" , modifiers : 4 } ) . pipe (
1092+ Effect . andThen ( Queue . clear ( viewer . output ) ) ,
1093+ Effect . map ( ( items ) => items . filter ( ( item ) => item . _tag === "clipboard" ) ) ,
1094+ ) ;
1095+ const frame = makeFrame ( ) ;
1096+ contexts [ 0 ] ! . page . emit ( "framenavigated" , frame ) ;
1097+ yield * Effect . promise ( ( ) => frame . recording . promise ) ;
1098+ const frameSession = contexts [ 0 ] ! . sessions . at ( - 1 ) ! ;
1099+ focusedFrame = frame ;
1100+ keyCopy = "framed" ;
1101+ expect ( yield * copy ( ) ) . toEqual ( [ { _tag : "clipboard" , text : "framed" } ] ) ;
1102+ // What the frame kept from before is cleared, not sent by a key elsewhere.
1103+ frame . copy = "left over" ;
1104+ focusedFrame = null ;
1105+ keyCopy = "" ;
1106+ expect ( yield * copy ( ) ) . toEqual ( [ ] ) ;
1107+ expect ( frame . copy ) . toBe ( "" ) ;
1108+ // A frame that went away is no longer read.
1109+ frame . detached = true ;
1110+ frame . copy = "gone" ;
1111+ frameSession . emit ( "close" ) ;
1112+ expect ( yield * copy ( ) ) . toEqual ( [ ] ) ;
1113+ expect ( frame . copy ) . toBe ( "gone" ) ;
1114+ } ) ,
1115+ ) . pipe ( Effect . provide ( layer ) ) ,
1116+ ) ;
1117+
10601118it . live ( "a page download is saved, offered to the controller, and listed for the agent" , ( ) =>
10611119 Effect . scoped (
10621120 Effect . gen ( function * ( ) {
0 commit comments