@@ -20,6 +20,7 @@ import os from 'os';
2020import path from 'path' ;
2121import { vi } from 'vitest' ;
2222import TestDriverSDK from '../../sdk.js' ;
23+ import { events } from '../../agent/events.js' ;
2324
2425/**
2526 * Set up console spies using Vitest's vi.spyOn to intercept console logs
@@ -103,7 +104,7 @@ function setupConsoleSpy(client, taskId) {
103104 // Store spies on client for cleanup
104105 client . _consoleSpies = { logSpy, errorSpy, warnSpy, infoSpy } ;
105106
106- console . log ( `[testdriver] Console spy set up for task: ${ taskId } `) ;
107+ client . emitter . emit ( events . log . debug , ` Console spy set up for task: ${ taskId } `) ;
107108}
108109
109110/**
@@ -166,9 +167,7 @@ export function TestDriver(context, options = {}) {
166167 // Priority: test options > plugin options > environment variable > default (linux)
167168 if ( ! mergedOptions . os && process . env . TD_OS ) {
168169 mergedOptions . os = process . env . TD_OS ;
169- console . log ( `[testdriver] Set mergedOptions.os = ${ mergedOptions . os } from TD_OS environment variable` ) ;
170170 }
171- console . log ( `[testdriver] Final mergedOptions.os = ${ mergedOptions . os } ` ) ;
172171
173172 // Extract TestDriver-specific options
174173 const apiKey = mergedOptions . apiKey || process . env . TD_API_KEY ;
@@ -186,21 +185,27 @@ export function TestDriver(context, options = {}) {
186185 testdriver . __vitestContext = context . task ;
187186 testDriverInstances . set ( context . task , testdriver ) ;
188187
188+ // Log OS detection after testdriver is created
189+ if ( process . env . TD_OS ) {
190+ testdriver . emitter . emit ( events . log . debug , `Set mergedOptions.os = ${ mergedOptions . os } from TD_OS environment variable` ) ;
191+ }
192+ testdriver . emitter . emit ( events . log . debug , `Final mergedOptions.os = ${ mergedOptions . os } ` ) ;
193+
189194 // Auto-connect if enabled (default: true)
190195 const autoConnect = config . autoConnect !== undefined ? config . autoConnect : true ;
191196 const debugConsoleSpy = process . env . TD_DEBUG_CONSOLE_SPY === 'true' ;
192197
193198 if ( autoConnect ) {
194199 testdriver . __connectionPromise = ( async ( ) => {
195- console . log ( '[testdriver] Connecting to sandbox...') ;
200+ testdriver . emitter . emit ( events . log . debug , ' Connecting to sandbox...') ;
196201 if ( debugConsoleSpy ) {
197202 console . log ( '[DEBUG] Before auth - sandbox.instanceSocketConnected:' , testdriver . sandbox ?. instanceSocketConnected ) ;
198203 }
199204
200205 await testdriver . auth ( ) ;
201206 await testdriver . connect ( ) ;
202207
203- console . log ( '[testdriver] ✅ Connected to sandbox') ;
208+ testdriver . emitter . emit ( events . log . debug , ' ✅ Connected to sandbox') ;
204209
205210 if ( debugConsoleSpy ) {
206211 console . log ( '[DEBUG] After connect - sandbox.instanceSocketConnected:' , testdriver . sandbox ?. instanceSocketConnected ) ;
@@ -221,8 +226,8 @@ export function TestDriver(context, options = {}) {
221226 : `touch ${ logPath } ` ;
222227
223228 await testdriver . exec ( shell , createLogCmd , 10000 , true ) ;
224- console . log ( '[testdriver] ✅ Created log file:' , logPath ) ;
225-
229+ testdriver . emitter . emit ( events . log . debug , ` ✅ Created log file: ${ logPath } ` ) ;
230+
226231 // Add automatic log tracking when dashcam starts
227232 // Store original start method
228233
@@ -234,20 +239,20 @@ export function TestDriver(context, options = {}) {
234239 // Register cleanup handler with dashcam.stop()
235240 if ( ! lifecycleHandlers . has ( context . task ) ) {
236241 const cleanup = async ( ) => {
237- console . log ( '[testdriver] Cleaning up TestDriver client...') ;
242+ testdriver . emitter . emit ( events . log . debug , ' Cleaning up TestDriver client...') ;
238243 try {
239244 // Stop dashcam if it was started - with timeout to prevent hanging
240245 if ( testdriver . _dashcam && testdriver . _dashcam . recording ) {
241246 try {
242247 const dashcamUrl = await testdriver . dashcam . stop ( ) ;
243- console . log ( ' 🎥 Dashcam URL:' , dashcamUrl ) ;
248+ testdriver . emitter . emit ( events . log . debug , ` 🎥 Dashcam URL: ${ dashcamUrl } ` ) ;
244249
245250 // Write test result to file for the reporter (cross-process communication)
246251 // This should happen regardless of whether dashcam succeeded, to ensure platform info is available
247252 const testId = context . task . id ;
248- console . log ( `[testdriver] testdriver.os value: ${ testdriver . os } `) ;
253+ testdriver . emitter . emit ( events . log . debug , ` testdriver.os value: ${ testdriver . os } `) ;
249254 const platform = testdriver . os || 'linux' ;
250- console . log ( `[testdriver] Using platform: ${ platform } `) ;
255+ testdriver . emitter . emit ( events . log . debug , ` Using platform: ${ platform } `) ;
251256 const absolutePath = context . task . file ?. filepath || context . task . file ?. name || 'unknown' ;
252257 const projectRoot = process . cwd ( ) ;
253258 const testFile = absolutePath !== 'unknown'
@@ -271,12 +276,12 @@ export function TestDriver(context, options = {}) {
271276 } ;
272277
273278 fs . writeFileSync ( testResultFile , JSON . stringify ( testResult , null , 2 ) ) ;
274- console . log ( `[testdriver] ✅ Wrote test result to ${ testResultFile } `) ;
279+ testdriver . emitter . emit ( events . log . debug , ` ✅ Wrote test result to ${ testResultFile } `) ;
275280
276281 // Also register in memory if plugin is available
277282 if ( globalThis . __testdriverPlugin ?. registerDashcamUrl ) {
278283 globalThis . __testdriverPlugin . registerDashcamUrl ( testId , dashcamUrl , platform ) ;
279- console . log ( `[testdriver] ✅ Registered test result in memory for test ${ testId } `) ;
284+ testdriver . emitter . emit ( events . log . debug , ` ✅ Registered test result in memory for test ${ testId } `) ;
280285 }
281286 } catch ( error ) {
282287 // Log more detailed error information for debugging
@@ -306,7 +311,7 @@ export function TestDriver(context, options = {}) {
306311 testdriver . disconnect ( ) ,
307312 new Promise ( ( resolve ) => setTimeout ( resolve , 5000 ) ) // 5s timeout for disconnect
308313 ] ) ;
309- console . log ( '✅ Client disconnected' ) ;
314+ testdriver . emitter . emit ( events . log . debug , '✅ Client disconnected' ) ;
310315 } catch ( error ) {
311316 console . error ( 'Error disconnecting client:' , error ) ;
312317 }
0 commit comments