import { strict as assert } from "assert" ; import { ContainerRuntimeFactoryWithDefaultDataStore } from "@fluidframework/aqueduct" ; import { IContainer } from "@fluidframework/container-definitions" ; import { Container , Loader } from "@fluidframework/container-loader" ; import { IFluidCodeDetails , IRequest } from "@fluidframework/core-interfaces" ; import { LocalDocumentServiceFactory , LocalResolver , } from "@fluidframework/local-driver" ; import { SharedMap } from "@fluidframework/map" ; import { requestFluidObject } from "@fluidframework/runtime-utils" ; import { ILocalDeltaConnectionServer , LocalDeltaConnectionServer } from "@fluidframework/server-local-server" ; import { createAndAttachContainer , ITestFluidObject , LoaderContainerTracker , LocalCodeLoader , TestFluidObjectFactory , } from "@fluidframework/test-utils" ; import { MockLogger } from "@fluidframework/telemetry-utils" ; import { IContainerRuntimeBase } from "@fluidframework/runtime-definitions" ; describe ( "Logging Last Connection Mode " , ( ) => { const documentId = "connectionModeTest" ; const mapId = "mapKey" ; const codeDetails = { package : "connectionModeTestPackage" , config : { } } ; let deltaConnectionServer ; let documentServiceFactory ; let loaderContainerTracker ; let container ; let dataObject ; let sharedMap ; /**
     * Waits for the "connected" event from the given container.
     */ async function waitForContainerReconnection ( c ) { assert . equal ( c . connected , false ) ; return new Promise ( ( resolve ) => c . once ( "connected" , ( ) => resolve ( ) ) ) ; } const logger = new MockLogger ( ) ; const getConnectedEvents = ( ) => logger . events . filter ( ( event ) => event . eventName === "fluid:telemetry:Container:ConnectionStateChange_Connected" ) ; const getDisconnectedEvents = ( ) => logger . events . filter ( ( event ) => event . eventName === "fluid:telemetry:Container:ConnectionStateChange_Disconnected" ) ; async function createContainer ( ) { const factory = new TestFluidObjectFactory ( [ [ mapId , SharedMap . getFactory ( ) ] , ] , "default" ) ; const innerRequestHandler = async ( request , runtime ) => runtime . IFluidHandleContext . resolveHandle ( request ) ; const runtimeFactory = new ContainerRuntimeFactoryWithDefaultDataStore ( factory , [ [ factory . type , Promise . resolve ( factory ) ] , ] , undefined , [ innerRequestHandler ] ) ; const urlResolver = new LocalResolver ( ) ; const codeLoader = new LocalCodeLoader ( [ [ codeDetails , runtimeFactory ] ] ) ; const loader = new Loader ( { urlResolver , documentServiceFactory , codeLoader , logger } ) ; loaderContainerTracker . add ( loader ) ; return createAndAttachContainer ( codeDetails , loader , urlResolver . createCreateNewRequest ( documentId ) ) ; } beforeEach ( async ( ) => { deltaConnectionServer = LocalDeltaConnectionServer . create ( ) ; documentServiceFactory = new LocalDocumentServiceFactory ( deltaConnectionServer ) ; loaderContainerTracker = new LoaderContainerTracker ( ) ; container = await createContainer ( ) as Container ; dataObject = await requestFluidObject < ITestFluidObject > ( container , "default" ) ; sharedMap = await dataObject . getSharedObject < SharedMap > ( mapId ) ; sharedMap . set ( "setup" , "done" ) ; await loaderContainerTracker . ensureSynchronized ( ) ; } ) ; afterEach ( ( ) => { loaderContainerTracker . reset ( ) ; } ) ; it ( `Logs the correct connection mode at disconnect` , async ( ) => { assert ( container . clientId ) ; documentServiceFactory . disconnectClient ( container . clientId , "Disconnected for testing" ) ; await waitForContainerReconnection ( container ) ; sharedMap . set ( "testing" , "value" ) ; await loaderContainerTracker . ensureSynchronized ( ) ; documentServiceFactory . disconnectClient ( container . clientId , "Disconnected for testing" ) ; const connectedEvents = getConnectedEvents ( ) ; const disconnectedEvents = getDisconnectedEvents ( ) ; assert ( connectedEvents !== undefined , "no connected events were logged" ) ; assert ( disconnectedEvents !== undefined , "no disconnected events were logged" ) ; assert . strictEqual ( connectedEvents [ 0 ] . connectionMode , disconnectedEvents [ 0 ] . connectionMode , "mismatch in connection mode during first disconnect" ) ; assert . strictEqual ( connectedEvents [ 1 ] . connectionMode , disconnectedEvents [ 1 ] . connectionMode , "mismatch in connection mode during second disconnect" ) ; } ) ; } ) ;