@@ -306,6 +306,114 @@ beforeEach(() => {
306306 desktopConnections . length = 0 ;
307307} ) ;
308308
309+ it . live . each ( [ "desktop" as const , undefined ] ) (
310+ "explicit desktop targets ($0 runtime) fail without creating a server tab" ,
311+ ( runtime ) =>
312+ Effect . scoped (
313+ Effect . gen ( function * ( ) {
314+ const { broker, tabId } = yield * ready ;
315+ const manager = yield * Manager . PreviewManager ;
316+ const desktop = yield * manager . open ( {
317+ threadId : scope . thread . threadId ,
318+ url : "https://example.com/original" ,
319+ ...( runtime === undefined ? { } : { runtime } ) ,
320+ } ) ;
321+ const before = yield * manager . list ( { threadId : scope . thread . threadId } ) ;
322+ const error = yield * broker
323+ . invoke < PreviewAutomationStatus > ( {
324+ scope,
325+ tabId : PreviewTabId . make ( desktop . tabId ) ,
326+ operation : "open" ,
327+ input : { url : "https://example.com/replacement" , open : true } ,
328+ } )
329+ . pipe ( Effect . flip ) ;
330+ expect ( error ) . toMatchObject ( { _tag : "PreviewAutomationExecutionError" } ) ;
331+ expect ( error . message ) . toContain ( "desktop runtime" ) ;
332+ expect ( error . message ) . toContain ( "cannot control" ) ;
333+ expect ( yield * manager . list ( { threadId : scope . thread . threadId } ) ) . toEqual ( before ) ;
334+ expect ( contexts ) . toHaveLength ( 1 ) ;
335+ // A rejected explicit target must not replace this session's working tab.
336+ const reused = yield * broker . invoke < PreviewAutomationStatus > ( {
337+ scope,
338+ operation : "open" ,
339+ input : { open : false } ,
340+ } ) ;
341+ expect ( reused . tabId ) . toBe ( tabId ) ;
342+ } ) ,
343+ ) . pipe ( Effect . provide ( layer ) ) ,
344+ ) ;
345+
346+ it . live ( "an unknown explicit target fails without creating a server tab" , ( ) =>
347+ Effect . scoped (
348+ Effect . gen ( function * ( ) {
349+ const { broker } = yield * ready ;
350+ const manager = yield * Manager . PreviewManager ;
351+ const before = yield * manager . list ( { threadId : scope . thread . threadId } ) ;
352+ const error = yield * broker
353+ . invoke < PreviewAutomationStatus > ( {
354+ scope,
355+ tabId : PreviewTabId . make ( "missing-tab" ) ,
356+ operation : "open" ,
357+ input : { url : "https://example.com/replacement" , open : true } ,
358+ } )
359+ . pipe ( Effect . flip ) ;
360+ expect ( error ) . toMatchObject ( {
361+ _tag : "PreviewAutomationTabNotFoundError" ,
362+ tabId : "missing-tab" ,
363+ } ) ;
364+ expect ( yield * manager . list ( { threadId : scope . thread . threadId } ) ) . toEqual ( before ) ;
365+ expect ( contexts ) . toHaveLength ( 1 ) ;
366+ } ) ,
367+ ) . pipe ( Effect . provide ( layer ) ) ,
368+ ) ;
369+
370+ it . live ( "an explicit server target reuses its ID and navigates without creating a tab" , ( ) =>
371+ Effect . scoped (
372+ Effect . gen ( function * ( ) {
373+ const { broker, tabId } = yield * ready ;
374+ const manager = yield * Manager . PreviewManager ;
375+ const before = yield * manager . list ( { threadId : scope . thread . threadId } ) ;
376+ const opened = yield * broker . invoke < PreviewAutomationStatus > ( {
377+ scope,
378+ tabId,
379+ operation : "open" ,
380+ input : { url : "https://example.com/reused" , open : false } ,
381+ } ) ;
382+ expect ( opened ) . toMatchObject ( { tabId, url : "https://example.com/reused" } ) ;
383+ const { sessions } = yield * manager . list ( { threadId : scope . thread . threadId } ) ;
384+ expect ( sessions . map ( ( session ) => session . tabId ) ) . toEqual (
385+ before . sessions . map ( ( session ) => session . tabId ) ,
386+ ) ;
387+ expect ( contexts ) . toHaveLength ( 1 ) ;
388+ expect ( contexts [ 0 ] ! . page . goto ) . toHaveBeenCalledWith (
389+ "https://example.com/reused" ,
390+ expect . anything ( ) ,
391+ ) ;
392+ } ) ,
393+ ) . pipe ( Effect . provide ( layer ) ) ,
394+ ) ;
395+
396+ it . live ( "an unresolved implicit target still falls back to a new server tab" , ( ) =>
397+ Effect . scoped (
398+ Effect . gen ( function * ( ) {
399+ const { broker, tabId } = yield * ready ;
400+ const manager = yield * Manager . PreviewManager ;
401+ yield * manager . close ( { threadId : scope . thread . threadId , tabId } ) ;
402+ const opened = yield * broker . invoke < PreviewAutomationStatus > ( {
403+ scope,
404+ operation : "open" ,
405+ input : { url : "https://example.com/fallback" , open : false } ,
406+ } ) ;
407+ expect ( opened . tabId ) . not . toBe ( tabId ) ;
408+ expect ( opened ) . toMatchObject ( { available : true , url : "https://example.com/fallback" } ) ;
409+ const { sessions } = yield * manager . list ( { threadId : scope . thread . threadId } ) ;
410+ expect ( sessions ) . toEqual ( [
411+ expect . objectContaining ( { tabId : opened . tabId , runtime : "server" } ) ,
412+ ] ) ;
413+ } ) ,
414+ ) . pipe ( Effect . provide ( layer ) ) ,
415+ ) ;
416+
309417it . live ( "readiness none responds immediately but takeover input waits for navigation commit" , ( ) =>
310418 Effect . scoped (
311419 Effect . gen ( function * ( ) {
0 commit comments