import _ from 'lodash' ; import { defaultTestEngineConfig , Engine } from '..' ; import { testKnex } from '../../../jest/knex-setup-teardown' ; import { defaultTestWalletConfig } from '../../config' ; import { seedAlicesSigningWallet } from '../../db/seeds/1_signing_wallet_seeds' ; import { createLogger } from '../../logger' ; import { Channel } from '../../models/channel' ; import { ObjectiveModel } from '../../models/objective' ; import { channel } from '../../models/__test__/fixtures/channel' ; import { openChannelObjective } from './fixtures/open-channel-objective' ; import { alice } from './fixtures/signing-wallets' ; import { stateWithHashSignedBy } from './fixtures/states' ; import { TestChannel } from './fixtures/test-channel' ; import { TestLedgerChannel } from './fixtures/test-ledger-channel' ; let engine ; beforeAll ( async ( ) => { await seedAlicesSigningWallet ( testKnex ) ; const logger = createLogger ( defaultTestWalletConfig ( ) ) ; engine = await Engine . create ( defaultTestEngineConfig ( ) , logger ) ; } ) ; afterAll ( async ( ) => { await engine . destroy ( ) ; } ) ; describe ( 'SyncObjective' , ( ) => { it ( 'throws an error if an objectiveId does not exist' , async ( ) => { await expect ( engine . syncObjectives ( [ 'FAKE' ] ) ) . rejects . toThrow ( 'Could not find all objectives' ) ; } ) ; it ( 'handles being called with no objective ids' , async ( ) => { const syncResults = await engine . syncObjectives ( [ ] ) ; expect ( syncResults ) . toHaveLength ( 0 ) ; } ) ; it ( 'generates a message containing specified objectives and channels' , async ( ) => { const targetChannel = await Channel . query ( testKnex ) . insert ( channel ( { vars : [ stateWithHashSignedBy ( [ alice ( ) ] ) ( ) ] } ) ) . withGraphFetched ( 'signingWallet' ) . withGraphFetched ( 'funding' ) ; const objective = await ObjectiveModel . insert ( openChannelObjective ( ) , testKnex ) ; const { channelId } = targetChannel ; const result = await engine . syncObjectives ( [ objective . objectiveId ] ) ; expect ( result ) . toHaveLength ( 1 ) ; const expectedPayload = { sender : 'alice' , recipient : 'bob' , data : { signedStates : [ expect . objectContaining ( { turnNum : 0 , channelId } ) ] , requests : [ expect . objectContaining ( { channelId , type : 'GetChannel' } ) ] , objectives : [ { type : 'OpenChannel' , data : { targetChannelId : channelId } } ] , } , } ; expect ( result [ 0 ] ) . toMatchObject ( expectedPayload ) ; } ) ; it ( 'includes the ledger channel when syncing a ledger funded open channel objective' , async ( ) => { TestChannel . maximumNonce = 100 ; const ledger = TestLedgerChannel . create ( { } ) ; const app = TestChannel . create ( { fundingStrategy : 'Ledger' , fundingLedgerChannelId : ledger . channelId , } ) ; await ledger . insertInto ( engine . store ) ; await app . insertInto ( engine . store ) ; const objective = await ObjectiveModel . insert ( openChannelObjective ( { data : { targetChannelId : app . channelId , fundingStrategy : 'Ledger' , fundingLedgerChannelId : ledger . channelId , } , } ) , testKnex ) ; const result = await engine . syncObjectives ( [ objective . objectiveId ] ) ; expect ( result ) . toHaveLength ( 1 ) ; const expectedPayload = { sender : 'alice' , recipient : 'bob' , data : { signedStates : [ expect . objectContaining ( { channelId : ledger . channelId } ) , expect . objectContaining ( { channelId : app . channelId } ) , ] , requests : [ expect . objectContaining ( { channelId : ledger . channelId , type : 'GetChannel' } ) , expect . objectContaining ( { channelId : app . channelId , type : 'GetChannel' } ) , ] , } , } ; expect ( result [ 0 ] ) . toMatchObject ( expectedPayload ) ; } ) ; } ) ;