import { ChannelResult , Message } from '@statechannels/client-api-schema' ; import { ChannelRequest , Payload , SignedState } from '@statechannels/wire-format' ; import { Engine } from '../engine' ; import { Outgoing } from '..' ; import { Notice } from '../protocols/actions' ; export const ONE_DAY = 86400 ; export function getPayloadFor ( participantId , messagesOrNotices ) { const messages = messagesOrNotices . map ( m => { if ( 'method' in m ) { return m . params ; } else { return m ; } } ) ; const filteredOutbox = messages . filter ( outboxItem => outboxItem . recipient === participantId ) ; if ( filteredOutbox . length != 1 ) throw Error ( `Expected exactly one message in outbox: found ${ filteredOutbox . length }` ) ; return filteredOutbox [ 0 ] . data ; } export function getChannelResultFor ( channelId , channelResults ) { const filteredChannelResults = channelResults . filter ( channelResult => channelResult . channelId === channelId ) ; if ( filteredChannelResults . length != 1 ) throw Error ( `Expected exactly one channel result: found ${ filteredChannelResults . length }` ) ; return filteredChannelResults [ 0 ] ; } export function getSignedStateFor ( channelId , outbox ) { const filteredSignedStates = ( outbox [ 0 ] ! . params . data as Payload ) . signedStates ! . filter ( ss => ss . channelId === channelId ) ; if ( filteredSignedStates . length != 1 ) throw Error ( `Expected exactly one channel result: found ${ filteredSignedStates . length }` ) ; return filteredSignedStates [ 0 ] ; } export async function interParticipantChannelResultsAreEqual ( a , b ) { const { channelResults : aChannelResults } = await a . getChannels ( ) ; const { channelResults : bChannelResults , outbox : bOutbox } = await b . getChannels ( ) ; const nullifyFunding = ( cr ) => ( { ... cr , fundingStatus : null } ) ; expect ( aChannelResults . map ( nullifyFunding ) ) . toEqual ( bChannelResults . map ( nullifyFunding ) ) ; expect ( bOutbox ) . toEqual ( [ ] ) ; } export function getRequestFor ( channelId , outbox ) { const requests = ( outbox [ 0 ] ! . params . data as Payload ) . requests ! . filter ( req => req . channelId === channelId ) ; if ( requests . length != 1 ) throw Error ( `Expected exactly one request found ${ requests . length }` ) ; return requests [ 0 ] ; }