@@ -4,8 +4,10 @@ import { expect, it } from "@effect/vitest";
44import {
55 DEFAULT_SERVER_SETTINGS ,
66 EnvironmentId ,
7+ PreviewAutomationControlInterruptedError ,
78 ProjectId ,
89 ProviderInstanceId ,
10+ SERVER_BROWSER_AUTOMATION_CLIENT_ID ,
911 ThreadId ,
1012} from "@t3tools/contracts" ;
1113import { resolveProjectSettings } from "@t3tools/shared/projectSettings" ;
@@ -101,3 +103,86 @@ it.effect.each([
101103 } ) ,
102104 ) ,
103105) ;
106+
107+ it . effect ( "close reports why the server browser refused to close a tab" , ( ) =>
108+ Effect . scoped (
109+ Effect . gen ( function * ( ) {
110+ const threadId = ThreadId . make ( "preview-controls-thread" ) ;
111+ const scope : McpInvocationContext . McpInvocationScope = {
112+ environmentId : EnvironmentId . make ( "preview-controls-environment" ) ,
113+ requestNamespace : "preview-controls-provider-session" ,
114+ thread : {
115+ threadId,
116+ providerSessionId : "preview-controls-provider-session" ,
117+ providerInstanceId : ProviderInstanceId . make ( "codex" ) ,
118+ } ,
119+ client : undefined ,
120+ capabilities : new Set ( [ "preview" ] ) ,
121+ issuedAt : 0 ,
122+ } ;
123+ const manager = yield * Preview . make . pipe (
124+ Effect . provide (
125+ Layer . merge (
126+ ServerConfig . layerTest ( process . cwd ( ) , { prefix : "t3-preview-controls-" } ) . pipe (
127+ Layer . provide ( NodeServices . layer ) ,
128+ ) ,
129+ NodeCrypto . layer ,
130+ ) ,
131+ ) ,
132+ ) ;
133+ const tab = yield * manager . open ( {
134+ threadId,
135+ url : "http://localhost:3000" ,
136+ runtime : "server" ,
137+ } ) ;
138+ const layerDependencies = Layer . mergeAll (
139+ Layer . mock ( PreviewAutomationBroker . PreviewAutomationBroker ) ( {
140+ invoke : ( ) =>
141+ Effect . fail (
142+ new PreviewAutomationControlInterruptedError ( {
143+ operation : "close" ,
144+ environmentId : scope . environmentId ,
145+ threadId,
146+ providerSessionId : "preview-controls-provider-session" ,
147+ providerInstanceId : ProviderInstanceId . make ( "codex" ) ,
148+ clientId : SERVER_BROWSER_AUTOMATION_CLIENT_ID ,
149+ connectionId : "server-browser-connection" ,
150+ requestId : "close-request" ,
151+ tabId : tab . tabId ,
152+ timeoutMs : 1_000 ,
153+ remoteTag : "PreviewAutomationControlInterruptedError" ,
154+ remoteMessageLength : 0 ,
155+ cause : null ,
156+ reason : "agentMismatch" ,
157+ } ) ,
158+ ) ,
159+ } ) ,
160+ Layer . succeed ( Preview . PreviewManager , manager ) ,
161+ Layer . succeed ( McpInvocationContext . McpInvocationContext , scope ) ,
162+ McpToolAccessTestkit . liveThreadsLayer ,
163+ Layer . mock ( ServerSettings . ServerSettingsService ) ( {
164+ getSettings : Effect . succeed ( {
165+ ...DEFAULT_SERVER_SETTINGS ,
166+ enableAgentBrowserAccess : true ,
167+ } ) ,
168+ } ) ,
169+ ) ;
170+ const toolkit = yield * PreviewControlsToolkit . pipe (
171+ Effect . provide (
172+ McpToolAccess . HandlersLayer . layer ( PreviewControlsHandlers . layer ) . pipe (
173+ Layer . provide ( layerDependencies ) ,
174+ ) ,
175+ ) ,
176+ ) ;
177+
178+ const closed = yield * toolkit
179+ . handle ( "t3_preview_close" , { tabId : tab . tabId } )
180+ . pipe ( Stream . unwrap , Stream . runCollect , Effect . provide ( layerDependencies ) ) ;
181+
182+ expect ( closed . at ( - 1 ) ?. result ) . toMatchObject ( {
183+ _tag : "PreviewAutomationControlInterruptedError" ,
184+ reason : "agentMismatch" ,
185+ } ) ;
186+ } ) ,
187+ ) ,
188+ ) ;
0 commit comments