@@ -1952,3 +1952,164 @@ it.live("profiles reported by one client survive another client's report", () =>
19521952 } ) ,
19531953 ) . pipe ( Effect . provide ( layer ) ) ,
19541954) ;
1955+
1956+ it . live . each ( [
1957+ { budget : "agent" , limit : 8 } ,
1958+ { budget : "server" , limit : 32 } ,
1959+ ] ) ( "opens and popups reserve the $budget budget before session creation" , ( { budget, limit } ) =>
1960+ Effect . scoped (
1961+ Effect . gen ( function * ( ) {
1962+ const { broker, browser, tabId } = yield * ready ;
1963+ const manager = yield * Manager . PreviewManager ;
1964+ const open = ( target = scope ) =>
1965+ broker . invoke < PreviewAutomationStatus > ( {
1966+ scope : target ,
1967+ operation : "open" ,
1968+ input : { reuseExistingTab : false , show : false } ,
1969+ } ) ;
1970+ for ( let index = 1 ; index < limit - 3 ; index ++ ) {
1971+ yield * open ( budget === "agent" ? scope : asSession ( `other-${ Math . floor ( index / 8 ) } ` ) ) ;
1972+ }
1973+
1974+ const sessionGate = Promise . withResolvers < void > ( ) ;
1975+ const launchGate = ( contextGate = Promise . withResolvers < void > ( ) ) ;
1976+ const sessionStarted = Promise . withResolvers < void > ( ) ;
1977+ const realOpen = manager . open ;
1978+ const delayedOpen = vi . spyOn ( manager , "open" ) . mockImplementation ( ( input ) =>
1979+ Effect . promise ( ( ) => {
1980+ sessionStarted . resolve ( ) ;
1981+ return sessionGate . promise ;
1982+ } ) . pipe ( Effect . andThen ( realOpen ( input ) ) ) ,
1983+ ) ;
1984+ yield * Effect . addFinalizer ( ( ) =>
1985+ Effect . sync ( ( ) => {
1986+ sessionGate . resolve ( ) ;
1987+ launchGate . resolve ( ) ;
1988+ delayedOpen . mockRestore ( ) ;
1989+ } ) ,
1990+ ) ;
1991+ const events = yield * manager . subscribeEvents ;
1992+ const first = yield * open ( ) . pipe ( Effect . result , Effect . forkScoped ) ;
1993+ yield * Effect . promise ( ( ) => sessionStarted . promise ) ;
1994+ const popup = makeContext ( ) ;
1995+ const resized = Promise . withResolvers < void > ( ) ;
1996+ const setViewportSize = popup . page . setViewportSize ;
1997+ popup . page . setViewportSize = vi . fn ( async ( size ) => {
1998+ await setViewportSize ( size ) ;
1999+ if ( size ?. width === 900 ) resized . resolve ( ) ;
2000+ } ) ;
2001+ const newCDPSession = popup . newCDPSession ;
2002+ popup . newCDPSession = async ( target ) => {
2003+ await launchGate . promise ;
2004+ return newCDPSession ( target ) ;
2005+ } ;
2006+ contexts [ 0 ] ! . page . emit ( "popup" , popup . page ) ;
2007+ const last = yield * open ( ) . pipe ( Effect . result , Effect . forkScoped ) ;
2008+ const overflow = yield * open ( ) . pipe ( Effect . result , Effect . forkScoped ) ;
2009+ // The host processes this after the opens, while manager creation is blocked.
2010+ const reused = yield * broker . invoke < PreviewAutomationStatus > ( {
2011+ scope,
2012+ tabId,
2013+ operation : "open" ,
2014+ input : { show : false } ,
2015+ } ) ;
2016+ expect ( reused . tabId ) . toBe ( tabId ) ;
2017+ expect ( ( yield * manager . list ( { threadId : scope . thread . threadId } ) ) . sessions ) . toHaveLength (
2018+ limit - 3 ,
2019+ ) ;
2020+
2021+ sessionGate . resolve ( ) ;
2022+ for ( let count = 0 ; count < 3 ; count ++ ) {
2023+ let event = yield * PubSub . take ( events ) ;
2024+ while ( event . type !== "opened" ) event = yield * PubSub . take ( events ) ;
2025+ yield * manager . resize ( {
2026+ threadId : scope . thread . threadId ,
2027+ tabId : event . tabId ,
2028+ viewport : { _tag : "freeform" , width : 900 , height : 700 } ,
2029+ } ) ;
2030+ }
2031+ // The same slots must stay reserved after publication, while pages are still opening.
2032+ const duringLaunch = yield * open ( ) . pipe ( Effect . result , Effect . forkScoped ) ;
2033+ const rejectedPopup = makeContext ( ) ;
2034+ contexts [ 0 ] ! . page . emit ( "popup" , rejectedPopup . page ) ;
2035+ yield * broker . invoke ( { scope, tabId, operation : "status" , input : { } } ) ;
2036+ launchGate . resolve ( ) ;
2037+
2038+ expect ( ( yield * Fiber . join ( first ) ) . _tag ) . toBe ( "Success" ) ;
2039+ expect ( ( yield * Fiber . join ( last ) ) . _tag ) . toBe ( "Success" ) ;
2040+ for ( const fiber of [ overflow , duringLaunch ] ) {
2041+ expect ( yield * Fiber . join ( fiber ) ) . toMatchObject ( {
2042+ _tag : "Failure" ,
2043+ failure : { _tag : "PreviewAutomationControlInterruptedError" , reason : "tabLimit" } ,
2044+ } ) ;
2045+ }
2046+ yield * Effect . promise ( ( ) => resized . promise ) ;
2047+ const { sessions } = yield * manager . list ( { threadId : scope . thread . threadId } ) ;
2048+ for ( const session of sessions )
2049+ yield * browser . attachViewer ( viewerInput ( session . tabId , false ) ) ;
2050+ expect ( sessions ) . toHaveLength ( limit ) ;
2051+ expect ( popup . page . isClosed ( ) ) . toBe ( false ) ;
2052+ expect ( rejectedPopup . page . isClosed ( ) ) . toBe ( true ) ;
2053+ const status = yield * broker . invoke < PreviewAutomationStatus > ( {
2054+ scope,
2055+ tabId,
2056+ operation : "status" ,
2057+ input : { } ,
2058+ } ) ;
2059+ expect ( status . tabs ) . toHaveLength ( limit ) ;
2060+ } ) ,
2061+ ) . pipe ( Effect . provide ( layer ) ) ,
2062+ ) ;
2063+
2064+ it . live ( "failed session and browser launches release capacity, as does closing a tab" , ( ) =>
2065+ Effect . scoped (
2066+ Effect . gen ( function * ( ) {
2067+ const { broker, tabId } = yield * ready ;
2068+ const manager = yield * Manager . PreviewManager ;
2069+ const open = broker . invoke < PreviewAutomationStatus > ( {
2070+ scope,
2071+ operation : "open" ,
2072+ input : { reuseExistingTab : false , show : false } ,
2073+ } ) ;
2074+ for ( let count = 1 ; count < 7 ; count ++ ) yield * open ;
2075+ const failSession = vi
2076+ . spyOn ( manager , "open" )
2077+ . mockImplementationOnce ( ( ) => Effect . die ( new Error ( "session creation failed" ) ) ) ;
2078+ yield * Effect . addFinalizer ( ( ) => Effect . sync ( ( ) => failSession . mockRestore ( ) ) ) ;
2079+ expect ( yield * open . pipe ( Effect . result ) ) . toMatchObject ( { _tag : "Failure" } ) ;
2080+ failSession . mockRestore ( ) ;
2081+ const events = yield * manager . subscribeEvents ;
2082+ contextFailure = new Error ( "Chromium launch failed" ) ;
2083+ expect ( yield * open . pipe ( Effect . result ) ) . toMatchObject ( { _tag : "Failure" } ) ;
2084+ let failedSession = yield * PubSub . take ( events ) ;
2085+ while ( failedSession . type !== "opened" ) failedSession = yield * PubSub . take ( events ) ;
2086+ // A failed launch must not leave a session a viewer can relaunch past the budget.
2087+ expect ( ( yield * manager . list ( { threadId : scope . thread . threadId } ) ) . sessions ) . toHaveLength ( 7 ) ;
2088+ contextFailure = null ;
2089+ expect ( ( yield * open ) . available ) . toBe ( true ) ;
2090+ expect ( yield * open . pipe ( Effect . flip ) ) . toMatchObject ( { reason : "tabLimit" } ) ;
2091+ expect (
2092+ yield * broker
2093+ . invoke < PreviewAutomationStatus > ( {
2094+ scope,
2095+ tabId : PreviewTabId . make ( failedSession . tabId ) ,
2096+ operation : "open" ,
2097+ input : { show : false } ,
2098+ } )
2099+ . pipe ( Effect . flip ) ,
2100+ ) . toMatchObject ( { _tag : "PreviewAutomationTabNotFoundError" } ) ;
2101+ const browser = yield * ServerBrowser . ServerBrowser ;
2102+ expect (
2103+ yield * browser . attachViewer ( viewerInput ( failedSession . tabId , false ) ) . pipe ( Effect . flip ) ,
2104+ ) . toMatchObject ( { _tag : "ServerBrowserTabNotFoundError" } ) ;
2105+ yield * broker . invoke ( { scope, tabId, operation : "close" , input : { } } ) ;
2106+ expect ( ( yield * open ) . available ) . toBe ( true ) ;
2107+ const status = yield * broker . invoke < PreviewAutomationStatus > ( {
2108+ scope,
2109+ operation : "status" ,
2110+ input : { } ,
2111+ } ) ;
2112+ expect ( status . tabs ) . toHaveLength ( 8 ) ;
2113+ } ) ,
2114+ ) . pipe ( Effect . provide ( layer ) ) ,
2115+ ) ;
0 commit comments