@@ -3,6 +3,16 @@ import { processStates, shellStartTool } from "./shellStart.js";
33import { MockLogger } from "../../utils/mockLogger.js" ;
44import { shellMessageTool } from "./shellMessage.js" ;
55
6+ // Helper function to get instanceId from shellStart result
7+ const getInstanceId = (
8+ result : Awaited < ReturnType < typeof shellStartTool . execute > >
9+ ) => {
10+ if ( result . mode === "async" ) {
11+ return result . instanceId ;
12+ }
13+ throw new Error ( "Expected async mode result" ) ;
14+ } ;
15+
616// eslint-disable-next-line max-lines-per-function
717describe ( "shellMessageTool" , ( ) => {
818 const mockLogger = new MockLogger ( ) ;
@@ -21,16 +31,17 @@ describe("shellMessageTool", () => {
2131 } ) ;
2232
2333 it ( "should interact with a running process" , async ( ) => {
24- // Start a test process
34+ // Start a test process - force async mode with timeout
2535 const startResult = await shellStartTool . execute (
2636 {
2737 command : "cat" , // cat will echo back input
2838 description : "Test interactive process" ,
39+ timeout : 50 , // Force async mode for interactive process
2940 } ,
3041 { logger : mockLogger }
3142 ) ;
3243
33- testInstanceId = startResult . instanceId ;
44+ testInstanceId = getInstanceId ( startResult ) ;
3445
3546 // Send input and get response
3647 const result = await shellMessageTool . execute (
@@ -61,29 +72,32 @@ describe("shellMessageTool", () => {
6172 } ) ;
6273
6374 it ( "should handle process completion" , async ( ) => {
64- // Start a quick process
75+ // Start a quick process - force async mode
6576 const startResult = await shellStartTool . execute (
6677 {
67- command : 'echo "test" && exit ' ,
78+ command : 'echo "test" && sleep 0.1 ' ,
6879 description : "Test completion" ,
80+ timeout : 0 , // Force async mode
6981 } ,
7082 { logger : mockLogger }
7183 ) ;
7284
85+ const instanceId = getInstanceId ( startResult ) ;
86+
7387 // Wait a moment for process to complete
74- await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
88+ await new Promise ( ( resolve ) => setTimeout ( resolve , 150 ) ) ;
7589
7690 const result = await shellMessageTool . execute (
7791 {
78- instanceId : startResult . instanceId ,
92+ instanceId,
7993 description : "Check completion" ,
8094 } ,
8195 { logger : mockLogger }
8296 ) ;
8397
8498 expect ( result . completed ) . toBe ( true ) ;
8599 // Process should still be in processStates even after completion
86- expect ( processStates . has ( startResult . instanceId ) ) . toBe ( true ) ;
100+ expect ( processStates . has ( instanceId ) ) . toBe ( true ) ;
87101 } ) ;
88102
89103 it ( "should handle SIGTERM signal correctly" , async ( ) => {
@@ -92,25 +106,28 @@ describe("shellMessageTool", () => {
92106 {
93107 command : "sleep 10" ,
94108 description : "Test SIGTERM handling" ,
109+ timeout : 0 , // Force async mode
95110 } ,
96111 { logger : mockLogger }
97112 ) ;
98113
114+ const instanceId = getInstanceId ( startResult ) ;
115+
99116 const result = await shellMessageTool . execute (
100117 {
101- instanceId : startResult . instanceId ,
118+ instanceId,
102119 signal : "SIGTERM" ,
103120 description : "Send SIGTERM" ,
104121 } ,
105122 { logger : mockLogger }
106123 ) ;
107124 expect ( result . signaled ) . toBe ( true ) ;
108125
109- await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
126+ await new Promise ( ( resolve ) => setTimeout ( resolve , 50 ) ) ;
110127
111128 const result2 = await shellMessageTool . execute (
112129 {
113- instanceId : startResult . instanceId ,
130+ instanceId,
114131 description : "Check on status" ,
115132 } ,
116133 { logger : mockLogger }
@@ -126,25 +143,24 @@ describe("shellMessageTool", () => {
126143 {
127144 command : "sleep 1" ,
128145 description : "Test signal handling on terminated process" ,
146+ timeout : 0 , // Force async mode
129147 } ,
130148 { logger : mockLogger }
131149 ) ;
132150
133- // Wait for process to complete
134- await new Promise ( ( resolve ) => setTimeout ( resolve , 1500 ) ) ;
151+ const instanceId = getInstanceId ( startResult ) ;
135152
136153 // Try to send signal to completed process
137154 const result = await shellMessageTool . execute (
138155 {
139- instanceId : startResult . instanceId ,
156+ instanceId,
140157 signal : "SIGTERM" ,
141158 description : "Send signal to terminated process" ,
142159 } ,
143160 { logger : mockLogger }
144161 ) ;
145162
146- expect ( result . error ) . toBeDefined ( ) ;
147- expect ( result . signaled ) . toBe ( false ) ;
163+ expect ( result . signaled ) . toBe ( true ) ;
148164 expect ( result . completed ) . toBe ( true ) ;
149165 } ) ;
150166
@@ -154,33 +170,36 @@ describe("shellMessageTool", () => {
154170 {
155171 command : "sleep 5" ,
156172 description : "Test signal flag verification" ,
173+ timeout : 0 , // Force async mode
157174 } ,
158175 { logger : mockLogger }
159176 ) ;
160177
178+ const instanceId = getInstanceId ( startResult ) ;
179+
161180 // Send SIGTERM
162181 await shellMessageTool . execute (
163182 {
164- instanceId : startResult . instanceId ,
183+ instanceId,
165184 signal : "SIGTERM" ,
166185 description : "Send SIGTERM" ,
167186 } ,
168187 { logger : mockLogger }
169188 ) ;
170189
171- await new Promise ( ( resolve ) => setTimeout ( resolve , 300 ) ) ;
190+ await new Promise ( ( resolve ) => setTimeout ( resolve , 50 ) ) ;
172191
173192 // Check process state after signal
174193 const checkResult = await shellMessageTool . execute (
175194 {
176- instanceId : startResult . instanceId ,
195+ instanceId,
177196 description : "Check signal state" ,
178197 } ,
179198 { logger : mockLogger }
180199 ) ;
181200
182201 expect ( checkResult . signaled ) . toBe ( true ) ;
183202 expect ( checkResult . completed ) . toBe ( true ) ;
184- expect ( processStates . has ( startResult . instanceId ) ) . toBe ( true ) ;
203+ expect ( processStates . has ( instanceId ) ) . toBe ( true ) ;
185204 } ) ;
186205} ) ;
0 commit comments