@@ -14,6 +14,26 @@ describe('PipeTransport', () => {
1414 let transport : PipeTransport ;
1515 let myReadable : Readable ;
1616
17+ async function waitForNextMessage ( ) {
18+ return await new Promise < string > ( res => {
19+ transport . onmessage = message => {
20+ res ( message ) ;
21+ } ;
22+ } ) ;
23+ }
24+
25+ function waitForNumberOfMessages ( count : number ) : Promise < string [ ] > {
26+ const messages : string [ ] = [ ] ;
27+ return new Promise < string [ ] > ( resolve => {
28+ transport . onmessage = ( message : string ) => {
29+ messages . push ( message ) ;
30+ if ( messages . length === count ) {
31+ resolve ( messages ) ;
32+ }
33+ } ;
34+ } ) ;
35+ }
36+
1737 beforeEach ( ( ) => {
1838 const myWritable = new Writable ( {
1939 write ( _chunk : string , _encoding : string , callback ) {
@@ -63,4 +83,46 @@ describe('PipeTransport', () => {
6383 'microtask2 m2' ,
6484 ] ) ;
6585 } ) ;
86+
87+ describe ( 'message handling' , ( ) => {
88+ it ( 'should work with message with ending' , async ( ) => {
89+ let message = waitForNextMessage ( ) ;
90+ myReadable . push ( 'm1\0' ) ;
91+
92+ expect ( await message ) . toBe ( 'm1' ) ;
93+ message = waitForNextMessage ( ) ;
94+ myReadable . push ( 'm2\0' ) ;
95+ expect ( await message ) . toBe ( 'm2' ) ;
96+ } ) ;
97+
98+ it ( 'should work for messages ending in multiple lines' , async ( ) => {
99+ const message = waitForNextMessage ( ) ;
100+ myReadable . push ( 'Hello wor' ) ;
101+ myReadable . push ( 'ld!\0' ) ;
102+
103+ expect ( await message ) . toBe ( 'Hello world!' ) ;
104+ } ) ;
105+
106+ it ( 'should work with messages continuing from previous one' , async ( ) => {
107+ let message = waitForNextMessage ( ) ;
108+ myReadable . push ( 'Hello wor' ) ;
109+ myReadable . push ( 'ld!\0I started in ' ) ;
110+
111+ expect ( await message ) . toBe ( 'Hello world!' ) ;
112+ message = waitForNextMessage ( ) ;
113+ myReadable . push ( 'the previous message\0' ) ;
114+ expect ( await message ) . toBe ( 'I started in the previous message' ) ;
115+ } ) ;
116+ it ( 'should work with multiple messages in a single line' , async ( ) => {
117+ const messagesPromise = waitForNumberOfMessages ( 3 ) ;
118+ myReadable . push ( 'First\0Second\0Third\0' ) ;
119+
120+ const messages = await messagesPromise ;
121+ expect ( messages ) . toHaveLength ( 3 ) ;
122+
123+ expect ( messages [ 0 ] ) . toBe ( 'First' ) ;
124+ expect ( messages [ 1 ] ) . toBe ( 'Second' ) ;
125+ expect ( messages [ 2 ] ) . toBe ( 'Third' ) ;
126+ } ) ;
127+ } ) ;
66128} ) ;
0 commit comments