import Color from 'color' import { MessageTypes } from '../../src/common' import { packers, unpackers } from '../../src/mapping' const KEY = Buffer.from([0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F]) test('Create a user data response (0x0003) packet.', () => { // Given const username = 'Butlersaurus' const colour = Color('#FF4000') const clientId = 'Mercury' // When const packedPacket = packers[MessageTypes.UserDataResponse]( { username: username, colour: colour, clientId: clientId }, KEY ) console.log(packedPacket.slice(4).toString('hex')) // Then // We can't check the contents of the data as it's encrypted with a random nonce. // Check the message type and length. expect(packedPacket.slice(0, 4)).toMatchObject(Buffer.from([0x00, 0x03, 0x00, 0x3A])) // Check the total length is as expected. expect(packedPacket.length).toBe(62) }) test('Parse a user data response (0x0003).', () => { // Given const ciphertext = Buffer.from([ 0x56, 0x71, 0x08, 0xf9, 0x2c, 0x4e, 0x41, 0x13, 0xbe, 0x44, 0xba, 0xd9, 0xe4, 0x25, 0x14, 0x60, 0x3b, 0x96, 0x7e, 0x0b, 0xbd, 0xac, 0xf0, 0xaf, 0xac, 0xd7, 0x80, 0xe5, 0x62, 0xd4, 0x33, 0x10, 0x23, 0x6d, 0x00, 0x3c, 0xae, 0x40, 0x6c, 0xe9, 0x40, 0xfc, 0x1c, 0xe0, 0xd3, 0xca, 0x65, 0xea, 0x83, 0x73, 0x5e, 0xd2, 0x67, 0xb2, 0x94, 0x58, 0x12, 0x73 ]) const username = 'Butlersaurus' const colour = Color('#FF4000') const clientId = 'Mercury' // When const unpackedPacket = unpackers[MessageTypes.UserDataResponse](ciphertext, KEY) // Then expect(unpackedPacket.success).toBe(true) expect(unpackedPacket.username).toBe(username) expect(unpackedPacket.colour).toMatchObject(colour) expect(unpackedPacket.clientId).toBe(clientId) })