@@ -8,6 +8,8 @@ import React from "react";
88import { SessionManager , type SessionMessage } from "@vegamo/deepcode-core" ;
99import App from "../ui/views/App" ;
1010import { terminal } from "./terminal-helper" ;
11+ import { McpClient } from "../../../core/src/mcp/mcp-client" ;
12+ import { MCP_TEST_SERVER } from "../../../core/src/tests/mcp-test-server" ;
1113
1214function deferred ( ) {
1315 let resolve ! : ( ) => void ;
@@ -27,6 +29,7 @@ async function harness(
2729 fs . mkdirSync ( path . join ( root , ".deepcode" ) ) ;
2830 fs . writeFileSync ( path . join ( root , ".deepcode" , "settings.json" ) , JSON . stringify ( { statusline : { enabled : false } } ) ) ;
2931 const ref : { current ?: SessionManager } = { } ;
32+ const initMcpServers = SessionManager . prototype . initMcpServers ;
3033 t . mock . method ( SessionManager . prototype , "initMcpServers" , async function ( this : SessionManager ) {
3134 ref . current = this ;
3235 Object . assign ( this , {
@@ -59,6 +62,7 @@ async function harness(
5962 return {
6063 ...h ,
6164 manager,
65+ initMcpServers : initMcpServers . bind ( manager ) ,
6266 async submit ( text : string ) {
6367 await h . send ( text ) ;
6468 await h . send ( "\r" ) ;
@@ -153,11 +157,132 @@ for (const action of ["/exit", "Ctrl+D twice"] as const) {
153157 assert . equal ( requestSignal . aborted , true ) ;
154158 assert . equal ( h . manager . isSessionRunning ( id ) , true , "execution still owns the lock during cleanup" ) ;
155159 assert . deepEqual ( disposedWhileRunning , [ ] ) ;
160+ assert . match ( h . frame ( ) , / E x i t i n g … / ) ;
156161 cleanup . resolve ( ) ;
157162 await h . waitUntilExit ( ) ;
158163 assert . ok ( disposedWhileRunning . length > 0 ) ;
159164 assert . ok ( disposedWhileRunning . every ( ( running ) => ! running ) ) ;
160165 assert . equal ( requests , 1 , "exiting must not start queued work" ) ;
161166 assert . equal ( h . manager . getSession ( id ) ?. status , "interrupted" ) ;
167+ assert . equal ( h . rawMode ( ) , false , "exit must restore terminal input mode" ) ;
168+ } ) ;
169+
170+ test ( `${ action } exits promptly during an unresponsive MCP call` , { timeout : 5000 } , async ( t ) => {
171+ const entered = deferred ( ) ;
172+ const callTool = McpClient . prototype . callTool ;
173+ let requestSignal : AbortSignal | undefined ;
174+ t . mock . method (
175+ McpClient . prototype ,
176+ "callTool" ,
177+ function ( this : McpClient , ...args : Parameters < McpClient [ "callTool" ] > ) {
178+ requestSignal = args [ 3 ] ;
179+ const result = callTool . apply ( this , args ) ;
180+ entered . resolve ( ) ;
181+ return result ;
182+ }
183+ ) ;
184+ let requests = 0 ;
185+ const h = await harness ( t , ( ) => {
186+ requests ++ ;
187+ return {
188+ choices : [
189+ {
190+ message : {
191+ content : "" ,
192+ tool_calls : [ { id : "hang" , type : "function" , function : { name : "mcp__probe__hang" , arguments : "{}" } } ] ,
193+ } ,
194+ } ,
195+ ] ,
196+ } ;
197+ } ) ;
198+ await h . initMcpServers ( { probe : { command : process . execPath , args : [ "-e" , MCP_TEST_SERVER ] } } ) ;
199+ await h . submit ( "call the MCP tool" ) ;
200+ await entered . promise ;
201+ const id = h . manager . getActiveSessionId ( ) ! ;
202+ h . manager . enqueuePrompt ( id , { text : "queued input" } ) ;
203+ if ( action === "/exit" ) await h . submit ( "/exit" ) ;
204+ else {
205+ await h . send ( "\x04" ) ;
206+ await h . send ( "\x04" ) ;
207+ }
208+ await h . waitUntilExit ( ) ;
209+ assert . equal ( requestSignal ?. aborted , true ) ;
210+ assert . equal ( h . manager . isSessionRunning ( id ) , false ) ;
211+ assert . equal ( h . manager . getSession ( id ) ?. status , "interrupted" ) ;
212+ assert . equal ( requests , 1 ) ;
213+ assert . equal ( h . rawMode ( ) , false ) ;
214+ } ) ;
215+
216+ test ( `${ action } exits after five seconds even if execution ignores cancellation` , { timeout : 12000 } , async ( t ) => {
217+ const entered = deferred ( ) ;
218+ const cleanup = deferred ( ) ;
219+ t . after ( ( ) => cleanup . resolve ( ) ) ;
220+ let requests = 0 ;
221+ const h = await harness ( t , async ( _messages , signal ) => {
222+ requests ++ ;
223+ entered . resolve ( ) ;
224+ await cleanup . promise ;
225+ signal . throwIfAborted ( ) ;
226+ } ) ;
227+ await h . submit ( "keep working" ) ;
228+ await entered . promise ;
229+ const id = h . manager . getActiveSessionId ( ) ! ;
230+ h . manager . enqueuePrompt ( id , { text : "queued input" } ) ;
231+ const disposedWhileRunning : boolean [ ] = [ ] ;
232+ const dispose = h . manager . dispose . bind ( h . manager ) ;
233+ t . mock . method ( h . manager , "dispose" , ( ) => {
234+ disposedWhileRunning . push ( h . manager . isSessionRunning ( id ) ) ;
235+ dispose ( ) ;
236+ } ) ;
237+ const started = performance . now ( ) ;
238+ if ( action === "/exit" ) await h . submit ( "/exit" ) ;
239+ else {
240+ await h . send ( "\x04" ) ;
241+ await h . send ( "\x04" ) ;
242+ }
243+ assert . match ( h . frame ( ) , / E x i t i n g … / ) ;
244+ assert . deepEqual ( disposedWhileRunning , [ ] ) ;
245+ await h . waitUntilExit ( ) ;
246+ assert . ok ( performance . now ( ) - started >= 5_000 , "allow five seconds for cleanup" ) ;
247+ assert . ok ( performance . now ( ) - started < 8_000 , "do not wait indefinitely for execution" ) ;
248+ assert . equal ( disposedWhileRunning [ 0 ] , true ) ;
249+ assert . equal ( requests , 1 ) ;
250+ assert . equal ( h . rawMode ( ) , false ) ;
162251 } ) ;
163252}
253+
254+ test ( "exit restores the terminal even if resource cleanup throws" , { timeout : 5000 } , async ( t ) => {
255+ const h = await harness ( t , ( ) => ( { choices : [ { message : { content : "done" } } ] } ) ) ;
256+ assert . equal ( h . rawMode ( ) , true ) ;
257+ const mocked = t . mock . method ( h . manager , "dispose" , ( ) => {
258+ throw new Error ( "cleanup failed" ) ;
259+ } ) ;
260+ try {
261+ await h . submit ( "/exit" ) ;
262+ await h . waitUntilExit ( ) ;
263+ assert . equal ( h . rawMode ( ) , false ) ;
264+ } finally {
265+ mocked . mock . restore ( ) ;
266+ }
267+ } ) ;
268+
269+ test ( "exit restores the terminal even if printing the summary throws" , { timeout : 5000 } , async ( t ) => {
270+ const h = await harness ( t , ( ) => ( { choices : [ { message : { content : "done" } } ] } ) ) ;
271+ const write = process . stdout . write ;
272+ let failed = false ;
273+ const mocked = t . mock . method ( process . stdout , "write" , function ( ...args : Parameters < typeof write > ) {
274+ if ( typeof args [ 0 ] === "string" && args [ 0 ] . includes ( "Goodbye!" ) ) {
275+ failed = true ;
276+ throw new Error ( "output failed" ) ;
277+ }
278+ return write . apply ( process . stdout , args ) ;
279+ } ) ;
280+ try {
281+ await h . submit ( "/exit" ) ;
282+ await h . waitUntilExit ( ) ;
283+ assert . equal ( failed , true ) ;
284+ assert . equal ( h . rawMode ( ) , false ) ;
285+ } finally {
286+ mocked . mock . restore ( ) ;
287+ }
288+ } ) ;
0 commit comments