import assert from "assert" ; import { ContainerRuntimeFactoryWithDefaultDataStore , DataObject , DataObjectFactory , } from "@fluidframework/aqueduct" ; import { ISummaryConfiguration } from "@fluidframework/protocol-definitions" ; import { ITestObjectProvider } from "@fluidframework/test-utils" ; import { requestFluidObject } from "@fluidframework/runtime-utils" ; import { describeNoCompat } from "@fluidframework/test-version-utils" ; import { IContainerRuntimeOptions } from "@fluidframework/container-runtime" ; import { AttachState } from "@fluidframework/container-definitions" ; import { MockLogger } from "@fluidframework/telemetry-utils" ; class TestDataObject extends DataObject { public get _root ( ) { return this . root ; } public get _context ( ) { return this . context ; } } describeNoCompat ( "Cache CreateNewSummary" , ( getTestObjectProvider ) => { let provider ; const dataObjectFactory = new DataObjectFactory ( "TestDataObject" , TestDataObject , [ ] , [ ] ) ; const IdleDetectionTime = 100 ; const summaryConfigOverrides = { idleTime : IdleDetectionTime , maxTime : IdleDetectionTime * 12 } ; const runtimeOptions = { summaryOptions : { initialSummarizerDelayMs : 10 , summaryConfigOverrides } , gcOptions : { gcAllowed : true } } ; const runtimeFactory = new ContainerRuntimeFactoryWithDefaultDataStore ( dataObjectFactory , [ [ dataObjectFactory . type , Promise . resolve ( dataObjectFactory ) ] , ] , undefined , undefined , runtimeOptions ) ; let mockLogger ; beforeEach ( function ( ) { provider = getTestObjectProvider ( ) ; if ( provider . driver . type !== "odsp" ) { this . skip ( ) ; } } ) ; it ( "should fetch from cache when second client loads the container" , async ( ) => { mockLogger = new MockLogger ( ) ; const mainContainer = await provider . createContainer ( runtimeFactory , { logger : mockLogger } ) ; assert . strictEqual ( mainContainer . attachState , AttachState . Attached , "container was not attached" ) ; const mainDataStore = await requestFluidObject < TestDataObject > ( mainContainer , "default" ) ; const dataStore2 = await dataObjectFactory . createInstance ( mainDataStore . _context . containerRuntime ) ; mainDataStore . _root . set ( "dataStore2" , dataStore2 . handle ) ; const container2 = await provider . loadContainer ( runtimeFactory , { logger : mockLogger } ) ; const defaultDataStore = await requestFluidObject < TestDataObject > ( container2 , "default" ) ; await provider . ensureSynchronized ( ) ; const handle2 = defaultDataStore . _root . get ( "dataStore2" ) ; const testDataStore = await handle2 . get ( ) ; assert ( testDataStore !== undefined , "2nd data store within loaded container is not loaded" ) ; const fetchEvent = mockLogger . events . find ( ( event ) => event . eventName === "fluid:telemetry:OdspDriver:ObtainSnapshot_end" ) ; assert ( fetchEvent !== undefined , "odsp obtain snapshot event does not exist " ) ; assert . strictEqual ( fetchEvent . method , "cache" , `second client fetched snapshot with ${ fetchEvent . method } method instead of from cache` ) ; } ) ; it ( "should fetch from cache when second client loads the container in offline mode" , async ( ) => { mockLogger = new MockLogger ( ) ; const mainContainer = await provider . createContainer ( runtimeFactory , { logger : mockLogger } ) ; assert . strictEqual ( mainContainer . attachState , AttachState . Attached , "container was not attached" ) ; const mainDataStore = await requestFluidObject < TestDataObject > ( mainContainer , "default" ) ; const dataStore2 = await dataObjectFactory . createInstance ( mainDataStore . _context . containerRuntime ) ; mainDataStore . _root . set ( "dataStore2" , dataStore2 . handle ) ; const mockDocumentServiceFactory = Object . create ( provider . documentServiceFactory ) ; mockDocumentServiceFactory . getStorageToken = ( options ) => { throw new Error ( "TokenFail" ) ; } ; provider . documentServiceFactory = mockDocumentServiceFactory ; const container2 = await provider . loadContainer ( runtimeFactory , { logger : mockLogger } ) ; const defaultDataStore = await requestFluidObject < TestDataObject > ( container2 , "default" ) ; await provider . ensureSynchronized ( ) ; const handle2 = defaultDataStore . _root . get ( "dataStore2" ) ; const testDataStore = await handle2 . get ( ) ; assert ( testDataStore !== undefined , "2nd data store within loaded container is not loaded" ) ; const fetchEvent = mockLogger . events . find ( ( event ) => event . eventName === "fluid:telemetry:OdspDriver:ObtainSnapshot_end" ) ; assert ( fetchEvent !== undefined , "odsp obtain snapshot event does not exist " ) ; assert . strictEqual ( fetchEvent . method , "cache" , `second client fetched snapshot with ${ fetchEvent . method } method instead of from cache` ) ; } ) ; } ) ;