@@ -29,13 +29,19 @@ import TestDriverSDK from '../../sdk.js';
2929 * @param {string } taskId - Unique task identifier for this test
3030 */
3131function setupConsoleSpy ( client , taskId ) {
32- // Determine log file path based on OS
33- const logPath = client . os === "windows"
34- ? "C:\\Users\\testdriver\\Documents\\testdriver.log"
35- : "/tmp/testdriver.log" ;
3632
37- // Store log path on client for later use
38- client . _testLogPath = logPath ;
33+ // Debug logging for console spy setup
34+ const debugConsoleSpy = process . env . TD_DEBUG_CONSOLE_SPY === 'true' ;
35+ if ( debugConsoleSpy ) {
36+ process . stdout . write ( `[DEBUG setupConsoleSpy] taskId: ${ taskId } \n` ) ;
37+ process . stdout . write ( `[DEBUG setupConsoleSpy] client.sandbox exists: ${ ! ! client . sandbox } \n` ) ;
38+ process . stdout . write ( `[DEBUG setupConsoleSpy] client.sandbox?.instanceSocketConnected: ${ client . sandbox ?. instanceSocketConnected } \n` ) ;
39+ process . stdout . write ( `[DEBUG setupConsoleSpy] client.sandbox?.send: ${ typeof client . sandbox ?. send } \n` ) ;
40+ }
41+
42+ // Track forwarding stats
43+ let forwardedCount = 0 ;
44+ let skippedCount = 0 ;
3945
4046 // Helper to forward logs to sandbox
4147 const forwardToSandbox = ( args ) => {
@@ -49,10 +55,25 @@ function setupConsoleSpy(client, taskId) {
4955
5056 // Send to sandbox for immediate visibility in dashcam
5157 if ( client . sandbox && client . sandbox . instanceSocketConnected ) {
52- client . sandbox . send ( {
53- type : "output" ,
54- output : Buffer . from ( message , "utf8" ) . toString ( "base64" ) ,
55- } ) ;
58+ try {
59+ client . sandbox . send ( {
60+ type : "output" ,
61+ output : Buffer . from ( message , "utf8" ) . toString ( "base64" ) ,
62+ } ) ;
63+ forwardedCount ++ ;
64+ if ( debugConsoleSpy && forwardedCount <= 3 ) {
65+ process . stdout . write ( `[DEBUG forwardToSandbox] Forwarded message #${ forwardedCount } : "${ message . substring ( 0 , 50 ) } ..."\n` ) ;
66+ }
67+ } catch ( err ) {
68+ if ( debugConsoleSpy ) {
69+ process . stdout . write ( `[DEBUG forwardToSandbox] Error sending: ${ err . message } \n` ) ;
70+ }
71+ }
72+ } else {
73+ skippedCount ++ ;
74+ if ( debugConsoleSpy && skippedCount <= 3 ) {
75+ process . stdout . write ( `[DEBUG forwardToSandbox] SKIPPED (sandbox not connected): "${ message . substring ( 0 , 50 ) } ..."\n` ) ;
76+ }
5677 }
5778 } ;
5879
@@ -159,14 +180,25 @@ export function TestDriver(context, options = {}) {
159180
160181 // Auto-connect if enabled (default: true)
161182 const autoConnect = config . autoConnect !== undefined ? config . autoConnect : true ;
183+ const debugConsoleSpy = process . env . TD_DEBUG_CONSOLE_SPY === 'true' ;
184+
162185 if ( autoConnect ) {
163186 testdriver . __connectionPromise = ( async ( ) => {
164187 try {
165188 console . log ( '[testdriver] Connecting to sandbox...' ) ;
189+ if ( debugConsoleSpy ) {
190+ console . log ( '[DEBUG] Before auth - sandbox.instanceSocketConnected:' , testdriver . sandbox ?. instanceSocketConnected ) ;
191+ }
192+
166193 await testdriver . auth ( ) ;
167194 await testdriver . connect ( ) ;
168195 console . log ( '[testdriver] ✅ Connected to sandbox' ) ;
169196
197+ if ( debugConsoleSpy ) {
198+ console . log ( '[DEBUG] After connect - sandbox.instanceSocketConnected:' , testdriver . sandbox ?. instanceSocketConnected ) ;
199+ console . log ( '[DEBUG] After connect - sandbox.send:' , typeof testdriver . sandbox ?. send ) ;
200+ }
201+
170202 // Set up console spy using vi.spyOn (test-isolated)
171203 setupConsoleSpy ( testdriver , context . task . id ) ;
172204
@@ -185,19 +217,9 @@ export function TestDriver(context, options = {}) {
185217
186218 // Add automatic log tracking when dashcam starts
187219 // Store original start method
188- const originalDashcamStart = testdriver . dashcam . start . bind ( testdriver . dashcam ) ;
189- testdriver . dashcam . start = async function ( ) {
190- // Call original start (which handles auth)
191- await originalDashcamStart ( ) ;
192-
193- // Add log file tracking after dashcam starts
194- try {
195- await testdriver . dashcam . addFileLog ( logPath , "TestDriver Log" ) ;
196- console . log ( '[testdriver] ✅ Added log file to dashcam tracking' ) ;
197- } catch ( error ) {
198- console . warn ( '[testdriver] ⚠️ Failed to add log tracking:' , error . message ) ;
199- }
200- } ;
220+
221+ await testdriver . dashcam . addFileLog ( logPath , "TestDriver Log" ) ;
222+
201223 } catch ( error ) {
202224 console . error ( '[testdriver] Error during setup:' , error ) ;
203225 throw error ;
0 commit comments