1+ 'use strict'
2+
3+ const path = require ( 'path' )
4+ const execa = require ( 'execa' )
5+ const pDefer = require ( 'p-defer' )
6+ const uint8ArrayToString = require ( 'uint8arrays/to-string' )
7+
8+ function startProcess ( name , args = [ ] ) {
9+ return execa ( 'node' , [ path . join ( __dirname , name ) , ...args ] , {
10+ cwd : path . resolve ( __dirname ) ,
11+ all : true
12+ } )
13+ }
14+
15+ async function test ( ) {
16+ let output1 = ''
17+ let output2 = ''
18+ let output3 = ''
19+ let relayAddr
20+ let autoRelayAddr
21+
22+ const proc1Ready = pDefer ( )
23+ const proc2Ready = pDefer ( )
24+
25+ // Step 1 process
26+ process . stdout . write ( 'relay.js\n' )
27+
28+ const proc1 = startProcess ( 'relay.js' )
29+ proc1 . all . on ( 'data' , async ( data ) => {
30+ process . stdout . write ( data )
31+
32+ output1 += uint8ArrayToString ( data )
33+
34+ if ( output1 . includes ( 'Listening on:' ) && output1 . includes ( '/p2p/' ) ) {
35+ relayAddr = output1 . trim ( ) . split ( 'Listening on:\n' ) [ 1 ] . split ( '\n' ) [ 0 ]
36+ proc1Ready . resolve ( )
37+ }
38+ } )
39+
40+ await proc1Ready . promise
41+ process . stdout . write ( '==================================================================\n' )
42+
43+ // Step 2 process
44+ process . stdout . write ( 'auto-relay.js\n' )
45+
46+ const proc2 = startProcess ( 'auto-relay.js' , [ relayAddr ] )
47+ proc2 . all . on ( 'data' , async ( data ) => {
48+ process . stdout . write ( data )
49+
50+ output2 += uint8ArrayToString ( data )
51+
52+ if ( output2 . includes ( 'Listening on:' ) && output2 . includes ( '/p2p/' ) ) {
53+ autoRelayAddr = output2 . trim ( ) . split ( 'Listening on:\n' ) [ 1 ]
54+ proc2Ready . resolve ( )
55+ }
56+ } )
57+
58+ await proc2Ready . promise
59+ process . stdout . write ( '==================================================================\n' )
60+
61+ // Step 3 process
62+ process . stdout . write ( 'other-node.js\n' )
63+
64+ const proc3 = startProcess ( 'other-node.js' , [ autoRelayAddr ] )
65+ proc3 . all . on ( 'data' , async ( data ) => {
66+ process . stdout . write ( data )
67+
68+ output3 += uint8ArrayToString ( data )
69+
70+ if ( output3 . includes ( 'Connected to the auto relay node via' ) ) {
71+ const remoteAddr = output3 . trim ( ) . split ( 'Connected to the auto relay node via ' ) [ 1 ]
72+
73+ if ( remoteAddr === autoRelayAddr ) {
74+ proc3 . kill ( )
75+ proc2 . kill ( )
76+ proc1 . kill ( )
77+ } else {
78+ throw new Error ( 'other-node did not dial through the relay' )
79+ }
80+ }
81+ } )
82+
83+ await Promise . all ( [
84+ proc1 ,
85+ proc2 ,
86+ proc3
87+ ] ) . catch ( ( err ) => {
88+ if ( err . signal !== 'SIGTERM' ) {
89+ throw err
90+ }
91+ } )
92+ }
93+
94+ module . exports = test
0 commit comments