import assert from "assert" ; import { ITelemetryBaseEvent } from "@fluidframework/common-definitions" ; import { ITelemetryLoggerPropertyBags , ITelemetryLoggerPropertyBag , TelemetryLogger } from "../logger" ; class TestTelemetryLogger extends TelemetryLogger { public events : ITelemetryBaseEvent [ ] = [ ] ; public send ( event ) { this . events . push ( this . prepareEvent ( event ) ) ; } } const allCases = [ { } , { allProp : 1 } , { allGetter : ( ) => 1 } , { allProp : 1 , allGetter : ( ) => 1 } ] ; const errorCases = [ { } , { errorProp : 2 } , { errorGetter : ( ) => 2 } , { errorProp : 2 , errorGetter : ( ) => 2 } ] ; const propertyCases = allCases . reduce < ITelemetryLoggerPropertyBags [ ] > ( ( pv , all ) => { pv . push ( ... errorCases . map ( ( error ) => ( { all , error } ) ) ) ; return pv ; } , [ ] ) ; propertyCases . push ( ... allCases . map ( ( all ) => ( { all , error : all } ) ) ) ; propertyCases . push ( ... allCases ) ; propertyCases . push ( ... errorCases ) ; propertyCases . push ( undefined ) ; describe ( "TelemetryLogger" , ( ) => { describe ( "Properties" , ( ) => { it ( "send" , ( ) => { for ( const props of propertyCases ) { const logger = new TestTelemetryLogger ( "namespace" , props ) ; logger . send ( { category : "anything" , eventName : "whatever" } ) ; assert . strictEqual ( logger . events . length , 1 ) ; const event = logger . events [ 0 ] ; assert . strictEqual ( event . category , "anything" ) ; assert . strictEqual ( event . eventName , "namespace:whatever" ) ; const eventKeys = Object . keys ( event ) ; const propsKeys = Object . keys ( props ?. all ?? { } ) ; assert . strictEqual ( eventKeys . length , propsKeys . length + 2 , `actual:\n${ JSON . stringify ( event ) }\nexpected:${ props ? JSON . stringify ( props ) : "undefined" }` ) ; } } ) ; it ( "sendErrorEvent" , ( ) => { for ( const props of propertyCases ) { const logger = new TestTelemetryLogger ( "namespace" , props ) ; logger . sendErrorEvent ( { eventName : "whatever" } ) ; assert . strictEqual ( logger . events . length , 1 ) ; const event = logger . events [ 0 ] ; assert . strictEqual ( event . category , "error" ) ; assert . strictEqual ( event . eventName , "namespace:whatever" ) ; const eventKeys = Object . keys ( event ) ; const propsKeys = Object . keys ( { ... props ?. all , ... props ?. error } ) ; assert . strictEqual ( eventKeys . length , propsKeys . length + 2 , `actual:\n${ JSON . stringify ( event ) }\nexpected:${ props ? JSON . stringify ( props ) : "undefined" }` ) ; } } ) ; it ( "sendTelemetryEvent" , ( ) => { for ( const props of propertyCases ) { const logger = new TestTelemetryLogger ( "namespace" , props ) ; logger . sendTelemetryEvent ( { eventName : "whatever" } ) ; assert . strictEqual ( logger . events . length , 1 ) ; const event = logger . events [ 0 ] ; assert . strictEqual ( event . category , "generic" ) ; assert . strictEqual ( event . eventName , "namespace:whatever" ) ; const eventKeys = Object . keys ( event ) ; const propsKeys = Object . keys ( props ?. all ?? { } ) ; assert . strictEqual ( eventKeys . length , propsKeys . length + 2 , `actual:\n${ JSON . stringify ( event ) }\nexpected:${ props ? JSON . stringify ( props ) : "undefined" }` ) ; } } ) ; } ) ; } ) ;