import { filesystem } from '@sidetree/test-vectors' ; import AWS from 'aws-sdk/global' ; import QLDBLedger from '../QLDBLedger' ; import MockQLDBLedger from '../MockQLDBLedger' ; jest . setTimeout ( 10 * 1000 ) ; describe ( 'QLDB tests' , ( ) => { const forceMock = true ; const config = new AWS . Config ( ) ; if ( process . env . NODE_ENV !== 'test' ) { if ( forceMock ) { console . warn ( 'Using mock QLDB interface for QLDB tests' ) ; } else if ( ! config . credentials ) { console . warn ( 'No AWS credentials found in ~/.aws/credentials, using mock interace' ) ; } } const ledger = config . credentials && ! forceMock ? new QLDBLedger ( 'photon-test' , 'Test' ) : new MockQLDBLedger ( 'Test' ) ; const { anchorString , anchorString2 , anchorString3 } = filesystem . anchorFile ; let blockTime1 ; let blockTimeHash1 ; beforeAll ( async ( ) => { await ledger . reset ( ) ; await ledger . initialize ( ) ; } ) ; it ( 'constructs object' , async ( ) => { const tableNames = await ledger . qldbDriver . getTableNames ( ) ; expect ( ledger . transactionTable ) . toBe ( 'Test' ) ; expect ( tableNames . includes ( ledger . transactionTable ) ) . toBeTruthy ( ) ; expect ( ledger . approximateTime ) . toEqual ( { time : 0 , hash : '' } ) ; } ) ; it ( 'gets service version' , async ( ) => { const serviceVersion = await ledger . getServiceVersion ( ) ; expect ( serviceVersion ) . toBeDefined ( ) ; expect ( [ 'qldb' , 'mock-qldb' ] . includes ( serviceVersion . name ) ) . toBeTruthy ( ) ; expect ( serviceVersion . version ) . toBeDefined ( ) ; } ) ; it ( 'writes to the ledger' , async ( ) => { const realTime = await ledger . getLatestTime ( ) ; const cachedTime = await ledger . approximateTime ; expect ( realTime . time ) . toBeDefined ( ) ; expect ( realTime . hash ) . toBeDefined ( ) ; expect ( cachedTime . time ) . toBe ( realTime . time ) ; expect ( cachedTime . hash ) . toBe ( realTime . hash ) ; const data = anchorString ; await ledger . write ( data ) ; const realTime2 = await ledger . getLatestTime ( ) ; const cachedTime2 = await ledger . approximateTime ; blockTime1 = realTime2 . time ; blockTimeHash1 = realTime2 . hash ; expect ( realTime2 . time ) . toBeDefined ( ) ; expect ( realTime2 . hash ) . toBeDefined ( ) ; expect ( cachedTime2 . time ) . toBe ( realTime2 . time ) ; expect ( cachedTime2 . hash ) . toBe ( realTime2 . hash ) ; } ) ; it ( 'reads from ledger' , async ( ) => { const { moreTransactions , transactions } = await ledger . read ( - 1 , blockTimeHash1 ) ; expect ( moreTransactions ) . toBeFalsy ( ) ; expect ( transactions ) . toHaveLength ( 1 ) ; const [ transaction ] = transactions ; expect ( transaction ) . toEqual ( { anchorString , normalizedTransactionFee : 0 , transactionFeePaid : 0 , transactionNumber : blockTime1 , transactionTime : blockTime1 , transactionTimeHash : blockTimeHash1 , transactionTimestamp : transaction . transactionTimestamp , writer : 'writer' , } ) ; } ) ; it ( 'reads next transaction that got wrote' , async ( ) => { await ledger . write ( anchorString2 ) ; const realTime = await ledger . getLatestTime ( ) ; const { moreTransactions , transactions } = await ledger . read ( blockTime1 , realTime . hash ) ; expect ( moreTransactions ) . toBeFalsy ( ) ; expect ( transactions ) . toHaveLength ( 1 ) ; const [ t1 ] = transactions ; expect ( t1 ) . toEqual ( { anchorString : anchorString2 , normalizedTransactionFee : 0 , transactionFeePaid : 0 , transactionNumber : realTime . time , transactionTime : realTime . time , transactionTimeHash : realTime . hash , transactionTimestamp : t1 . transactionTimestamp , writer : 'writer' , } ) ; } ) ; it ( 'gets multiple transactions' , async ( ) => { await ledger . write ( anchorString3 ) ; const { moreTransactions , transactions } = await ledger . read ( blockTime1 + 1 , undefined ) ; expect ( moreTransactions ) . toBeFalsy ( ) ; expect ( transactions ) . toHaveLength ( 2 ) ; } ) ; it ( 'should return no transaction if the requested transactionNumber has not been reached' , async ( ) => { const readResult = await ledger . read ( Number . MAX_SAFE_INTEGER - 1 , blockTimeHash1 ) ; expect ( readResult . moreTransactions ) . toBeFalsy ( ) ; expect ( readResult . transactions ) . toHaveLength ( 0 ) ; } ) ; it ( 'should return no transaction if the requested transactionTimeHash doesnt exist' , async ( ) => { const readResult = await ledger . read ( - 1 , '0x123' ) ; expect ( readResult . moreTransactions ) . toBeFalsy ( ) ; expect ( readResult . transactions ) . toHaveLength ( 0 ) ; } ) ; } ) ;