import assert from "assert" ; import { ITelemetryBaseEvent } from "@fluidframework/common-definitions" ; import { IContainerContext } from "@fluidframework/container-definitions" ; import { MockDeltaManager , MockQuorum } from "@fluidframework/test-runtime-utils" ; import { MockLogger } from "@fluidframework/telemetry-utils" ; import { ContainerRuntime , getDeviceSpec } from "../containerRuntime" ; function setNavigator ( navigator ) { global . navigator = navigator as Navigator ; } describe ( "Hardware Stats" , ( ) => { let mockLogger = new MockLogger ( ) ; let mockContext = { deltaManager : new MockDeltaManager ( ) , quorum : new MockQuorum ( ) , logger : mockLogger , clientDetails : { capabilities : { interactive : true } } } ; const getDeviceSpecEvents = ( ) : ITelemetryBaseEvent [ ] => mockLogger . events . filter ( ( event ) => event . eventName === "DeviceSpec" ) ; const loadContainer = async ( ) => ContainerRuntime . load ( mockContext as IContainerContext , [ ] , undefined , { summaryOptions : { disableSummaries : true } } ) ; beforeEach ( async ( ) => { mockLogger = new MockLogger ( ) ; mockContext = { deltaManager : new MockDeltaManager ( ) , quorum : new MockQuorum ( ) , logger : mockLogger , clientDetails : { capabilities : { interactive : true } } } ; } ) ; it ( "should generate correct hardware stats with regular navigator" , async ( ) => { const navigator = { deviceMemory : 10 , hardwareConcurrency : 8 } ; setNavigator ( navigator ) ; const { deviceMemory , hardwareConcurrency } = getDeviceSpec ( ) ; assert . strictEqual ( deviceMemory , 10 , "incorrect deviceMemory value" ) ; assert . strictEqual ( hardwareConcurrency , 8 , "incorrect hardwareConcurrency value" ) ; await loadContainer ( ) ; const events = getDeviceSpecEvents ( ) ; assert ( events !== undefined , "No deviceSpec event found" ) ; assert . strictEqual ( events [ 0 ] . deviceMemory , 10 , "incorrect deviceMemory logged" ) ; assert . strictEqual ( events [ 0 ] . hardwareConcurrency , 8 , "incorrect hardwareConcurrency logged" ) ; } ) ; it ( "should generate correct hardware stats with null navigator" , async ( ) => { const navigator = null ; setNavigator ( navigator ) ; const { deviceMemory , hardwareConcurrency } = getDeviceSpec ( ) ; assert . strictEqual ( deviceMemory , undefined , "incorrect deviceMemory value" ) ; assert . strictEqual ( hardwareConcurrency , undefined , "incorrect hardwareConcurrency value" ) ; await loadContainer ( ) ; const events = getDeviceSpecEvents ( ) ; assert ( events !== undefined , "No deviceSpec event found" ) ; assert . strictEqual ( events [ 0 ] . deviceMemory , undefined , "incorrect deviceMemory logged" ) ; assert . strictEqual ( events [ 0 ] . hardwareConcurrency , undefined , "incorrect hardwareConcurrency logged" ) ; } ) ; it ( "should generate correct hardware stats with undefined navigator" , async ( ) => { const navigator = undefined ; setNavigator ( navigator ) ; const { deviceMemory , hardwareConcurrency } = getDeviceSpec ( ) ; assert . strictEqual ( deviceMemory , undefined , "incorrect deviceMemory value" ) ; assert . strictEqual ( hardwareConcurrency , undefined , "incorrect hardwareConcurrency value" ) ; await loadContainer ( ) ; const events = getDeviceSpecEvents ( ) ; assert ( events !== undefined , "No deviceSpec event found" ) ; assert . strictEqual ( events [ 0 ] . deviceMemory , undefined , "incorrect deviceMemory logged" ) ; assert . strictEqual ( events [ 0 ] . hardwareConcurrency , undefined , "incorrect hardwareConcurrency logged" ) ; } ) ; } ) ;