import { expect } from "chai" ; import { readdirSync } from "fs" ; import { ChangesetGenerationConfig , ChangesetGenerationHarness , HubUtility , TestChangesetSequence } from "imodel-changeset-test-utility" ; import * as path from "path" ; import { Logger , LogLevel } from "@bentley/bentleyjs-core" ; import { AuthorizedBackendRequestContext , IModelHost } from "@bentley/imodeljs-backend" ; import { AccessToken , AuthorizedClientRequestContext } from "@bentley/itwin-client" ; import { ChangeSummaryExtractor } from "../ChangeSummaryExtractor" ; import { QueryAgent } from "../QueryAgent" ; import { QueryAgentConfig } from "../QueryAgentConfig" ; import { TestMockObjects } from "./TestMockObjects" ; describe ( "QueryAgent" , ( ) => { let agent ; before ( ( ) => { TestMockObjects . setupMockAppConfig ( ) ; } ) ; after ( ( ) => { TestMockObjects . clearMockAppConfig ( ) ; IModelHost . authorizationClient = undefined ; } ) ; it ( "Extracts changeset information published to an iModel" , async ( ) => { IModelHost . authorizationClient = TestMockObjects . getMockAgentAuthorizationClient ( ) ; agent = new QueryAgent ( TestMockObjects . getMockHubClient ( ) , TestMockObjects . getMockContextRegistryClient ( ) , TestMockObjects . getMockBriefcaseProvider ( ) , TestMockObjects . getMockChangeSummaryExtractor ( ) ) ; await agent . listenForAndHandleChangesets ( 10 ) ; await agent . listenForAndHandleChangesets ( 10 ) ; } ) ; it ( "Throws error when async initialization fails" , async ( ) => { const throwError = true ; IModelHost . authorizationClient = TestMockObjects . getMockAgentAuthorizationClient ( throwError ) ; agent = new QueryAgent ( TestMockObjects . getMockHubClient ( ) , TestMockObjects . getMockContextRegistryClient ( ) , TestMockObjects . getMockBriefcaseProvider ( ) , TestMockObjects . getMockChangeSummaryExtractor ( ) ) ; try { await agent . listenForAndHandleChangesets ( 10 ) ; } catch ( error ) { expect ( error !== undefined ) . equals ( true ) ; } } ) ; } ) ; describe ( "QueryAgentConfig" , ( ) => { before ( ( ) => { TestMockObjects . setupMockAppConfig ( ) ; } ) ; after ( ( ) => { TestMockObjects . clearMockAppConfig ( ) ; } ) ; it ( "Uses the project name if the iModel name is not defined" , ( ) => { TestMockObjects . clearMockAppConfigProjectName ( ) ; expect ( QueryAgentConfig . projectName ) . equals ( QueryAgentConfig . iModelName ) ; TestMockObjects . setupMockAppConfig ( ) ; } ) ; it ( "Uses __dirname/output as default output directory" , ( ) => { expect ( QueryAgentConfig . outputDir ) . equals ( path . join ( __dirname , "../" , "output" ) ) ; } ) ; } ) ; describe ( "QueryAgent" , ( ) => { let agent ; before ( async ( ) => { TestMockObjects . setupMockAppConfig ( ) ; IModelHost . authorizationClient = TestMockObjects . getMockAgentAuthorizationClient ( ) ; agent = new QueryAgent ( TestMockObjects . getMockHubClient ( ) , TestMockObjects . getMockContextRegistryClient ( ) , TestMockObjects . getMockBriefcaseProvider ( ) , TestMockObjects . getMockChangeSummaryExtractor ( ) ) ; await agent . initialize ( ) ; } ) ; after ( ( ) => { TestMockObjects . clearMockAppConfig ( ) ; IModelHost . authorizationClient = undefined ; } ) ; it ( "Extracts changeset information published to an iModel" , async ( ) => { await agent . run ( 10 ) ; } ) ; it ( "Runs when listen for changesets routine does not throw error" , async ( ) => { const throwError = false ; IModelHost . authorizationClient = TestMockObjects . getMockAgentAuthorizationClient ( throwError ) ; const localAgent = new QueryAgent ( TestMockObjects . getMockHubClient ( ) , TestMockObjects . getMockContextRegistryClient ( ) , TestMockObjects . getMockBriefcaseProvider ( ) , TestMockObjects . getMockChangeSummaryExtractor ( ) ) ; await localAgent . initialize ( ) ; await localAgent . run ( 10 ) ; } ) ; } ) ; describe ( "ChangeSummaryExtractor" , ( ) => { let changeSummaryExtractor ; before ( ( ) => { TestMockObjects . setupMockAppConfig ( ) ; changeSummaryExtractor = new ChangeSummaryExtractor ( ) ; } ) ; after ( ( ) => { TestMockObjects . clearMockAppConfig ( ) ; } ) ; it ( "Catches errors in its method" , async ( ) => { IModelHost . authorizationClient = TestMockObjects . getMockAgentAuthorizationClient ( ) ; const requestContext = await AuthorizedBackendRequestContext . create ( ) ; const ret = await changeSummaryExtractor . extractChangeSummary ( requestContext , TestMockObjects . getMockIModelDb ( ) , "FAKE_CHANGESET_ID" ) ; expect ( ret ) . equals ( undefined ) ; } ) ; } ) ; describe ( "IModelQueryAgent Running with Changesets (#integration)" , ( ) => { let changesetHarness ; let requestContext ; let agent ; const hubUtility = new HubUtility ( ) ; let accessToken ; before ( async ( ) => { QueryAgentConfig . setupConfig ( ) ; Logger . initializeToConsole ( ) ; Logger . setLevelDefault ( LogLevel . Trace ) ; Logger . setLevel ( QueryAgentConfig . loggingCategory , LogLevel . Trace ) ; accessToken = await hubUtility . login ( ) ; requestContext = new AuthorizedClientRequestContext ( accessToken ! ) ; agent = new QueryAgent ( ) ; await agent . initialize ( ) ; changesetHarness = new ChangesetGenerationHarness ( undefined , undefined , QueryAgentConfig . outputDir ) ; await changesetHarness . initialize ( ) ; } ) ; after ( async ( ) => { Logger . logTrace ( QueryAgentConfig . loggingCategory , "Cleaning up test resources, may take some time..." ) ; if ( requestContext ) { await hubUtility . purgeAcquiredBriefcases ( requestContext , ChangesetGenerationConfig . projectName , ChangesetGenerationConfig . iModelName ) ; } } ) ; it ( "Extracts changeset information published to an iModel" , async ( ) => { const numChangeSets = 4 ; const changesetSequence = new TestChangesetSequence ( numChangeSets , 5 , 2000 ) ; const promise = agent . run ( 25000 ) ; const changesetGenerated = await changesetHarness . generateChangesets ( changesetSequence ) ; expect ( changesetGenerated ) . to . be . true ; await promise ; const items = readdirSync ( path . join ( QueryAgentConfig . outputDir , "changeSummaries" ) ) ; expect ( items . length ) . to . equal ( numChangeSets ) ; } ) ; } ) ;