import * as assert from 'assert' ; import * as path from 'path' ; import * as puppeteer from 'puppeteer' ; import * as webpack from 'webpack' ; import PluginMiniAccounts from 'ilp-plugin-mini-accounts' ; import { createServer , Connection } from '../src' ; const BTP_SERVER_OPTS = { port : 9000 , allowedOrigins : [ '.*' ] , debugHostIldcpInfo : { clientAddress : 'test.example' , assetScale : 9 , assetCode : '___' } } ; describe ( 'Puppeteer' , function ( ) { before ( async function ( ) { this . timeout ( 1e4 ) ; await buildClientBundle ( ) ; this . browser = await puppeteer . launch ( ) ; } ) ; after ( async function ( ) { await this . browser . close ( ) ; } ) ; beforeEach ( 'Set up server' , async function ( ) { this . serverPlugin = new PluginMiniAccounts ( BTP_SERVER_OPTS ) ; this . server = await createServer ( { plugin : this . serverPlugin } ) ; this . server . on ( 'connection' , ( connection ) => { this . serverConnection = connection ; connection . on ( 'stream' , ( stream ) => { stream . setReceiveMax ( 10000 ) ; } ) ; } ) ; } ) ; beforeEach ( 'Set up client' , async function ( ) { this . timeout ( 1e4 ) ; this . page = await this . browser . newPage ( ) ; this . page . on ( 'error' , ( err ) => { console . log ( 'puppeteer_error:' , err . stack ) ; } ) ; this . page . on ( 'console' , ( message ) => { console . log ( 'puppeteer:' , message . text ( ) ) ; } ) ; this . page . on ( 'close' , ( ) => console . log ( 'puppeteer_close' ) ) ; this . page . on ( 'dialog' , ( dialog ) => console . log ( 'puppeteer_dialog' , dialog ) ) ; this . page . on ( 'pageerror' , ( err ) => console . log ( 'puppeteer_pageerror' , err ) ) ; await this . page . goto ( 'file:///dev/null' ) ; await this . page . addScriptTag ( { path : path . resolve ( __dirname , '../dist/test/browser/bundle.js' ) } ) ; const { destinationAccount , sharedSecret } = this . server . generateAddressAndSecret ( ) ; await this . page . evaluate ( async ( opts ) => { try { window [ 'streamClient' ] = await window [ 'makeStreamClient' ] ( { server : 'btp+ws://127.0.0.1:' + opts . port , btpToken : 'secret' } , opts ) ; } catch ( err ) { console . error ( 'uncaught error:' , err . stack ) ; throw err ; } } , { port : BTP_SERVER_OPTS . port , destinationAccount , sharedSecret : sharedSecret . toString ( 'base64' ) } ) ; } ) ; afterEach ( 'Tear down client & server' , async function ( ) { await this . page . evaluate ( async function ( ) { await window [ 'streamClient' ] . end ( ) ; } ) ; await this . serverConnection . destroy ( ) ; await this . server . close ( ) ; } ) ; describe ( 'stream money' , function ( ) { it ( 'sends money' , async function ( ) { await this . page . evaluate ( async function ( ) { const stream = window [ 'streamClient' ] . createStream ( ) ; await stream . sendTotal ( 100 ) ; } ) ; assert . equal ( this . serverConnection . totalReceived , '100' ) ; } ) ; } ) ; describe ( 'crypto helpers (browser)' , function ( ) { it ( 'passes' , async function ( ) { await this . page . evaluate ( async function ( ) { const tests = [ ] ; window [ 'runCryptoTests' ] ( { describe : function ( label , run ) { run ( ) ; } , it : function ( label , run ) { tests . push ( run ( ) ) ; } } ) ; await Promise . all ( tests ) ; } ) ; } ) ; } ) ; } ) ; function buildClientBundle ( ) { return new Promise ( ( resolve , reject ) => { webpack ( require ( './browser/webpack.config.js' ) , ( err , stats ) => { if ( err ) { reject ( err ) ; } else if ( stats . hasErrors ( ) ) { reject ( stats . compilation . errors [ 0 ] ) ; } else { resolve ( ) ; } } ) ; } ) ; }