import { ITelemetryBufferedLogger } from "@fluidframework/test-driver-definitions" ; import { ITelemetryBaseEvent } from "@fluidframework/common-definitions" ; import { Context } from "mocha" ; import { pkgName } from "./packageVersion" ; const testVariant = process . env . FLUID_TEST_VARIANT ; const _global = global ; class TestLogger implements ITelemetryBufferedLogger { send ( event ) { if ( event . eventName === "fluid:telemetry:RouterliciousDriver:readBlob_end" ) { return ; } event . testName = this . testName ; event . testVariant = testVariant ; event . hostName = pkgName ; this . parentLogger . send ( event ) ; } async flush ( ) { return this . parentLogger . flush ( ) ; } constructor ( private readonly parentLogger , private readonly testName ) { } } const nullLogger = { send : ( ) => { } , flush : async ( ) => { } } ; const log = console . log ; const error = console . log ; const warn = console . warn ; let currentTestLogger ; let currentTestName ; let originalLogger ; export const mochaHooks = { beforeAll ( ) { originalLogger = _global . getTestLogger ?. ( ) ?? nullLogger ; _global . getTestLogger = ( ) => { if ( ! currentTestLogger && currentTestName !== undefined ) { currentTestLogger = new TestLogger ( originalLogger , currentTestName ) ; } return currentTestLogger ?? originalLogger ; } ; } , beforeEach ( ) { if ( process . env . FLUID_TEST_VERBOSE === undefined ) { console . log = ( ) => { } ; console . error = ( ) => { } ; console . warn = ( ) => { } ; } const context = this as any as Context ; currentTestName = context . currentTest ?. fullTitle ( ) ; currentTestLogger = undefined ; originalLogger . send ( { category : "generic" , eventName : "fluid:telemetry:Test_start" , testName : currentTestName , testVariant , hostName : pkgName } ) ; } , afterEach ( ) { const context = this as any as Context ; originalLogger . send ( { category : "generic" , eventName : "fluid:telemetry:Test_end" , testName : currentTestName , state : context . currentTest ?. state , duration : context . currentTest ?. duration , timedOut : context . currentTest ?. timedOut , testVariant , hostName : pkgName } ) ; console . log = log ; console . error = error ; console . warn = warn ; currentTestLogger = undefined ; currentTestName = undefined ; } } ;