import { MessageHeader } from '../../lib/message' ; describe ( 'Message' , ( ) => { describe ( '#parse' , ( ) => { it ( 'parses a messsage header, with checksum enabled' , ( ) => { const data = new DataView ( new ArrayBuffer ( 24 ) ) ; data . setUint32 ( 0 , 0x4e584e43 , true ) ; data . setUint32 ( 4 , 0x01000000 , true ) ; data . setUint32 ( 8 , 256 * 1024 , true ) ; data . setUint32 ( 12 , 16 , true ) ; data . setUint32 ( 16 , 0 , true ) ; data . setUint32 ( 20 , 0x4e584e43 ^ 0xffffffff , true ) ; const header = MessageHeader . parse ( data , true ) ; expect ( header . cmd ) . toBe ( 'CNXN' ) ; expect ( header . arg0 ) . toBe ( 0x01000000 ) ; expect ( header . arg1 ) . toBe ( 256 * 1024 ) ; expect ( header . length ) . toBe ( 16 ) ; expect ( header . checksum ) . toBe ( 0 ) ; } ) ; it ( 'Fails to parse a header with an invalid magic / checksum enabled' , ( ) => { const data = new DataView ( new ArrayBuffer ( 24 ) ) ; data . setUint32 ( 0 , 0x4e584e43 , true ) ; data . setUint32 ( 4 , 0x01000000 , true ) ; data . setUint32 ( 8 , 256 * 1024 , true ) ; data . setUint32 ( 12 , 16 , true ) ; data . setUint32 ( 16 , 0 , true ) ; data . setUint32 ( 20 , 0 , true ) ; expect ( ( ( ) => MessageHeader . parse ( data , true ) ) ) . toThrowError ( 'magic mismatch' ) ; } ) ; } ) ; describe ( '#toDataView' , ( ) => { it ( 'converts a header to a DataView' , ( ) => { const data = new MessageHeader ( 'CNXN' , 0x01000000 , 256 * 1024 , 16 , 0 ) . toDataView ( ) ; expect ( data . getUint32 ( 0 , true ) ) . toBe ( 0x4e584e43 ) ; expect ( data . getUint32 ( 4 , true ) ) . toBe ( 0x01000000 ) ; expect ( data . getUint32 ( 8 , true ) ) . toBe ( 256 * 1024 ) ; expect ( data . getUint32 ( 12 , true ) ) . toBe ( 16 ) ; expect ( data . getUint32 ( 16 , true ) ) . toBe ( 0 ) ; expect ( data . getInt32 ( 20 , true ) ) . toBe ( ( 0x4e584e43 ^ 0xffffffff ) ) ; } ) ; } ) ; } ) ;