@@ -8,7 +8,7 @@ import { formatResponse } from "../../prompts/responses"
88import { ToolUse , AskApproval , HandleError , PushToolResult } from "../../../shared/tools"
99import { unescapeHtmlEntities } from "../../../utils/text-normalization"
1010import { Terminal } from "../../../integrations/terminal/Terminal"
11- import type { RooTerminalCallbacks , RooTerminalProcess } from "../../../integrations/terminal/types"
11+ import type { RooTerminal , RooTerminalCallbacks , RooTerminalProcess } from "../../../integrations/terminal/types"
1212
1313// Mock dependencies
1414vitest . mock ( "execa" , ( ) => ( {
@@ -27,18 +27,86 @@ vitest.mock("vscode", () => ({
2727 } ,
2828} ) )
2929
30+ // Mock CommandScheduler to avoid singleton state leakage between tests.
31+ vitest . mock ( "../../../integrations/terminal/CommandScheduler" , ( ) => ( {
32+ CommandScheduler : {
33+ getInstance : vitest . fn ( ) . mockReturnValue ( {
34+ enqueue : vitest . fn ( ) . mockResolvedValue ( undefined ) ,
35+ release : vitest . fn ( ) ,
36+ } ) ,
37+ } ,
38+ } ) )
39+
40+ // Mock getTaskDirectoryPath to prevent real filesystem operations when
41+ // executeCommandInTerminal creates an OutputInterceptor.
42+ vitest . mock ( "../../../utils/storage" , ( ) => ( {
43+ getTaskDirectoryPath : vitest . fn ( ) . mockResolvedValue ( "/test/storage/task-dir" ) ,
44+ } ) )
45+
46+ // Mock OutputInterceptor to prevent real filesystem operations.
47+ // Use a class-based mock so `new OutputInterceptor(...)` works correctly.
48+ vitest . mock ( "../../../integrations/terminal/OutputInterceptor" , ( ) => {
49+ class MockOutputInterceptor {
50+ write ( _chunk : string ) : void { }
51+ async finalize ( ) : Promise < undefined > {
52+ return undefined
53+ }
54+ }
55+ return { OutputInterceptor : MockOutputInterceptor }
56+ } )
57+
58+
59+ /**
60+ * Build a mock terminal that satisfies the RooTerminal interface contract
61+ * without resorting to `as any`. Only the properties accessed by
62+ * ExecuteCommandTool are populated; the rest default to no-op stubs.
63+ */
64+ function createMockTerminal ( overrides : Partial < RooTerminal > = { } ) : RooTerminal {
65+ const runCommandMock = vitest . fn ( ) . mockImplementation ( ( _cmd : string , callbacks : RooTerminalCallbacks ) => {
66+ // Invoke onCompleted so onCompletedPromise resolves and the tool returns.
67+ void callbacks ?. onCompleted ?.( "" , undefined as unknown as RooTerminalProcess )
68+ const p = Promise . resolve ( )
69+ // Attach promise-like properties so mergePromise callers don't throw.
70+ return Object . assign ( p , {
71+ continue : ( ) => { } ,
72+ abort : ( ) => { } ,
73+ } ) as unknown as ReturnType < typeof runCommandMock >
74+ } )
75+
76+ const lifecycle = {
77+ state : "idle" as const ,
78+ incrementRecovery : ( ) => { } ,
79+ markBroken : ( ) => { } ,
80+ transition : ( ) => { } ,
81+ }
82+
83+ const base : RooTerminal = {
84+ provider : "execa" ,
85+ id : 0 ,
86+ reuseKey : "test" ,
87+ busy : false ,
88+ running : false ,
89+ lifecycle : lifecycle as unknown as RooTerminal [ "lifecycle" ] ,
90+ getCurrentWorkingDirectory : ( ) => "/test/workspace" ,
91+ isClosed : ( ) => false ,
92+ runCommand : runCommandMock ,
93+ setActiveStream : ( ) => { } ,
94+ shellExecutionComplete : ( ) => { } ,
95+ getProcessesWithOutput : ( ) => [ ] ,
96+ getUnretrievedOutput : ( ) => "" ,
97+ getLastCommand : ( ) => "" ,
98+ cleanCompletedProcessQueue : ( ) => { } ,
99+ canReuse : ( ) => false ,
100+ }
101+
102+ return { ...base , ...overrides }
103+ }
104+
30105vitest . mock ( "../../../integrations/terminal/TerminalRegistry" , ( ) => ( {
31106 TerminalRegistry : {
32- getOrCreateTerminal : vitest . fn ( ) . mockResolvedValue ( {
33- runCommand : vitest . fn ( ) . mockImplementation ( ( _cmd : string , callbacks : any ) => {
34- // Invoke onCompleted so onCompletedPromise resolves and the tool returns.
35- callbacks ?. onCompleted ?.( "" )
36- const p = Promise . resolve ( )
37- // Attach promise-like properties so mergePromise callers don't throw.
38- return Object . assign ( p , { continue : ( ) => { } , abort : ( ) => { } } )
39- } ) ,
40- getCurrentWorkingDirectory : vitest . fn ( ) . mockReturnValue ( "/test/workspace" ) ,
41- } ) ,
107+ getOrCreateTerminal : vitest . fn ( ) . mockResolvedValue ( createMockTerminal ( ) ) ,
108+ setExecaShellFamily : vitest . fn ( ) ,
109+ prepareProviderSwitch : vitest . fn ( ) ,
42110 } ,
43111} ) )
44112
@@ -59,34 +127,53 @@ const { executeCommandTool } = executeCommandModule
59127
60128describe ( "executeCommandTool" , ( ) => {
61129 // Setup common test variables
62- let mockCline : any & { consecutiveMistakeCount : number ; didRejectTool : boolean }
63- let mockAskApproval : any
64- let mockHandleError : any
65- let mockPushToolResult : any
130+ let mockCline : Record < string , unknown > & {
131+ consecutiveMistakeCount : number
132+ didRejectTool : boolean
133+ }
134+ let mockAskApproval : ReturnType < typeof vitest . fn >
135+ let mockHandleError : ReturnType < typeof vitest . fn >
136+ let mockPushToolResult : ReturnType < typeof vitest . fn >
66137 let mockToolUse : ToolUse < "execute_command" >
67138 const originalCliRuntime = process . env . ROO_CLI_RUNTIME
68139
69- beforeEach ( ( ) => {
70- // Reset mocks
140+ beforeEach ( async ( ) => {
141+ // Reset mock call history (does NOT reset factory mock implementations).
71142 vitest . clearAllMocks ( )
72143 vitest . useRealTimers ( )
73144
145+ // Reset TerminalRegistry.getOrCreateTerminal to the default mock terminal,
146+ // since foreground tests override it with mockImplementation which
147+ // clearAllMocks does NOT reset.
148+ const { TerminalRegistry } = await import ( "../../../integrations/terminal/TerminalRegistry" )
149+ ; ( TerminalRegistry . getOrCreateTerminal as ReturnType < typeof vitest . fn > ) . mockResolvedValue ( createMockTerminal ( ) )
150+
74151 // Spy on executeCommandInTerminal and mock its return value
75152 vitest . spyOn ( executeCommandModule , "executeCommandInTerminal" ) . mockResolvedValue ( [ false , "Command executed" ] )
76153
154+ // formatResponse is auto-mocked; provide a passthrough for toolError so
155+ // cwd validation tests can assert on the returned error message.
156+ vitest . spyOn ( formatResponse , "toolError" ) . mockImplementation ( ( msg : string ) => msg )
157+ vitest . spyOn ( formatResponse , "rooIgnoreError" ) . mockReturnValue ( "RooIgnore error" )
158+
77159 // Create mock implementations with eslint directives to handle the type issues
78160 mockCline = {
79161 ask : vitest . fn ( ) . mockResolvedValue ( undefined ) ,
80162 say : vitest . fn ( ) . mockResolvedValue ( undefined ) ,
81163 sayAndCreateMissingParamError : vitest . fn ( ) . mockResolvedValue ( "Missing parameter error" ) ,
82164 consecutiveMistakeCount : 0 ,
83165 didRejectTool : false ,
166+ didToolFailInCurrentTurn : false ,
84167 rooIgnoreController : {
85168 validateCommand : vitest . fn ( ) . mockReturnValue ( null ) ,
86169 } ,
87170 recordToolUsage : vitest . fn ( ) . mockReturnValue ( { } as ToolUsage ) ,
88171 recordToolError : vitest . fn ( ) ,
89172 supersedePendingAsk : vitest . fn ( ) ,
173+ taskId : "test-task-id" ,
174+ apiConfiguration : { } ,
175+ terminalProcess : undefined ,
176+ getResolvedCommandEnvironment : vitest . fn ( ) . mockReturnValue ( undefined ) ,
90177 providerRef : {
91178 deref : vitest . fn ( ) . mockResolvedValue ( {
92179 contextProxy : {
@@ -112,11 +199,11 @@ describe("executeCommandTool", () => {
112199
113200 // Setup vscode config mock
114201 const mockConfig = {
115- get : vitest . fn ( ) . mockImplementation ( ( key : string , defaultValue : any ) => {
202+ get : vitest . fn ( ) . mockImplementation ( ( _key : string , defaultValue : unknown ) => {
116203 return defaultValue
117204 } ) ,
118205 }
119- ; ( vscode . workspace . getConfiguration as any ) . mockReturnValue ( mockConfig )
206+ ; ( vscode . workspace . getConfiguration as ReturnType < typeof vitest . fn > ) . mockReturnValue ( mockConfig )
120207
121208 // Create a mock tool use object
122209 mockToolUse = {
@@ -392,7 +479,7 @@ describe("executeCommandTool", () => {
392479 }
393480
394481 const mockRooIgnoreError = "RooIgnore error"
395- ; ( formatResponse . rooIgnoreError as any ) . mockReturnValue ( mockRooIgnoreError )
482+ ; ( formatResponse . rooIgnoreError as ReturnType < typeof vitest . fn > ) . mockReturnValue ( mockRooIgnoreError )
396483
397484 // Execute
398485 await executeCommandTool . handle ( mockCline as unknown as Task , mockToolUse , {
@@ -509,15 +596,15 @@ describe("executeCommandTool", () => {
509596 abort : vitest . fn ( ) ,
510597 } )
511598 ; ( TerminalRegistry . getOrCreateTerminal as ReturnType < typeof vitest . fn > ) . mockImplementation (
512- async ( _cwd : string , _taskId : string , provider : string ) => {
599+ async ( _cwd : string , _taskId : string , _executionId : string , provider ? : string ) => {
513600 state . provider = provider
514- return {
601+ return createMockTerminal ( {
602+ provider : ( provider ?? "execa" ) as RooTerminal [ "provider" ] ,
515603 runCommand : vitest . fn ( ( _cmd : string , callbacks : RooTerminalCallbacks ) => {
516604 state . callbacks = callbacks
517605 return state . proc
518606 } ) ,
519- getCurrentWorkingDirectory : vitest . fn ( ) . mockReturnValue ( "/test/workspace" ) ,
520- }
607+ } )
521608 } ,
522609 )
523610 return state
@@ -647,8 +734,9 @@ describe("executeCommandTool", () => {
647734 invalidCwdCases . forEach ( ( { label, value } ) => {
648735 it ( `rejects ${ label } cwd` , async ( ) => {
649736 mockToolUse . params . command = "echo test"
650- mockToolUse . params . cwd = value as any
651- mockToolUse . nativeArgs = { command : "echo test" , cwd : value as any }
737+ // Intentionally pass invalid types to test runtime validation.
738+ mockToolUse . params . cwd = value as unknown as string
739+ mockToolUse . nativeArgs = { command : "echo test" , cwd : value as unknown as string }
652740
653741 await executeCommandTool . handle ( mockCline as unknown as Task , mockToolUse , {
654742 askApproval : mockAskApproval as unknown as AskApproval ,
@@ -714,8 +802,9 @@ describe("executeCommandTool", () => {
714802
715803 it ( "does not acquire a terminal for malformed cwd" , async ( ) => {
716804 mockToolUse . params . command = "echo test"
717- mockToolUse . params . cwd = 12345 as any
718- mockToolUse . nativeArgs = { command : "echo test" , cwd : 12345 as any }
805+ // Intentionally pass a number to test runtime validation.
806+ mockToolUse . params . cwd = 12345 as unknown as string
807+ mockToolUse . nativeArgs = { command : "echo test" , cwd : 12345 as unknown as string }
719808
720809 await executeCommandTool . handle ( mockCline as unknown as Task , mockToolUse , {
721810 askApproval : mockAskApproval as unknown as AskApproval ,
0 commit comments