Prettier
This commit is contained in:
@@ -1,36 +1,37 @@
|
||||
import { MessageTypes } from '../../src/common'
|
||||
import { packers, unpackers } from '../../src/mapping'
|
||||
import { MessageTypes } from "../../src/common";
|
||||
import { packers, unpackers } from "../../src/mapping";
|
||||
|
||||
const KEY = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
|
||||
const KEY = new Uint8Array([
|
||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
|
||||
]);
|
||||
|
||||
test('Create a basic message (0x0001) packet.', () => {
|
||||
test("Create a basic message (0x0001) packet.", () => {
|
||||
// Given
|
||||
const encoder = new TextEncoder()
|
||||
const message = encoder.encode('Hello, World!')
|
||||
const encoder = new TextEncoder();
|
||||
const message = encoder.encode("Hello, World!");
|
||||
|
||||
// When
|
||||
const packedPacket = packers[MessageTypes.Basic](
|
||||
message,
|
||||
KEY
|
||||
)
|
||||
const packedPacket = packers[MessageTypes.Basic](message, KEY);
|
||||
|
||||
// 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(new Uint8Array([0x00, 0x01, 0x00, 0x2D]))
|
||||
expect(packedPacket.slice(0, 4)).toMatchObject(
|
||||
new Uint8Array([0x00, 0x01, 0x00, 0x2d]),
|
||||
);
|
||||
|
||||
// Check the total length is as expected.
|
||||
expect(packedPacket.length).toBe(49)
|
||||
})
|
||||
expect(packedPacket.length).toBe(49);
|
||||
});
|
||||
|
||||
test('Parse a basic message (0x0001).', () => {
|
||||
test("Parse a basic message (0x0001).", () => {
|
||||
// Given
|
||||
const data = new Uint8Array([1, 2, 3, 4])
|
||||
const data = new Uint8Array([1, 2, 3, 4]);
|
||||
|
||||
// When
|
||||
const unpackedPacket = unpackers[MessageTypes.Basic](data)
|
||||
const unpackedPacket = unpackers[MessageTypes.Basic](data);
|
||||
|
||||
// Then
|
||||
expect(unpackedPacket)
|
||||
expect(unpackedPacket).toMatchObject(data)
|
||||
})
|
||||
expect(unpackedPacket);
|
||||
expect(unpackedPacket).toMatchObject(data);
|
||||
});
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { MessageTypes } from '../../src/common'
|
||||
import { packers } from '../../src/mapping'
|
||||
import { MessageTypes } from "../../src/common";
|
||||
import { packers } from "../../src/mapping";
|
||||
|
||||
test('Create a keepalive (0x0005) packet.', () => {
|
||||
test("Create a keepalive (0x0005) packet.", () => {
|
||||
// When
|
||||
const packedPacket = packers[MessageTypes.Keepalive]()
|
||||
const packedPacket = packers[MessageTypes.Keepalive]();
|
||||
|
||||
// Then
|
||||
const expectedResult = new Uint8Array([0x00, 0x05, 0x00, 0x00])
|
||||
expect(packedPacket).toMatchObject(expectedResult)
|
||||
})
|
||||
const expectedResult = new Uint8Array([0x00, 0x05, 0x00, 0x00]);
|
||||
expect(packedPacket).toMatchObject(expectedResult);
|
||||
});
|
||||
|
||||
@@ -1,15 +1,18 @@
|
||||
import { packOutgoingPacket, unpackIncomingPacket } from '../../src/messages/packet'
|
||||
import {
|
||||
packOutgoingPacket,
|
||||
unpackIncomingPacket,
|
||||
} from "../../src/messages/packet";
|
||||
|
||||
test('Pack an outgoing packet.', () => {
|
||||
test("Pack an outgoing packet.", () => {
|
||||
// Given
|
||||
const messageType = new Uint8Array([0x12, 0x34])
|
||||
const data = new Uint8Array([0x12, 0x34, 0x56, 0x78])
|
||||
const messageType = new Uint8Array([0x12, 0x34]);
|
||||
const data = new Uint8Array([0x12, 0x34, 0x56, 0x78]);
|
||||
|
||||
// When
|
||||
const packedPacket = packOutgoingPacket({
|
||||
messageType: messageType,
|
||||
data: data
|
||||
})
|
||||
data: data,
|
||||
});
|
||||
|
||||
// Then
|
||||
const expectedResult = new Uint8Array([
|
||||
@@ -18,12 +21,12 @@ test('Pack an outgoing packet.', () => {
|
||||
// Data length
|
||||
0x00, 0x04,
|
||||
// Data
|
||||
0x12, 0x34, 0x56, 0x78
|
||||
])
|
||||
expect(packedPacket).toMatchObject(expectedResult)
|
||||
})
|
||||
0x12, 0x34, 0x56, 0x78,
|
||||
]);
|
||||
expect(packedPacket).toMatchObject(expectedResult);
|
||||
});
|
||||
|
||||
test('Unpack an incoming packet.', () => {
|
||||
test("Unpack an incoming packet.", () => {
|
||||
// Given
|
||||
const incomingPacket = new Uint8Array([
|
||||
// Message type
|
||||
@@ -33,14 +36,16 @@ test('Unpack an incoming packet.', () => {
|
||||
// Data length
|
||||
0x00, 0x04,
|
||||
// Data
|
||||
0x12, 0x34, 0x56, 0x78
|
||||
])
|
||||
0x12, 0x34, 0x56, 0x78,
|
||||
]);
|
||||
|
||||
// When
|
||||
const unpackedResult = unpackIncomingPacket(incomingPacket)
|
||||
const unpackedResult = unpackIncomingPacket(incomingPacket);
|
||||
|
||||
// Then
|
||||
expect(unpackedResult.messageType).toBe(0x1234)
|
||||
expect(unpackedResult.senderId).toBe(0xaabbccdd)
|
||||
expect(unpackedResult.data).toMatchObject(new Uint8Array([0x12, 0x34, 0x56, 0x78]))
|
||||
})
|
||||
expect(unpackedResult.messageType).toBe(0x1234);
|
||||
expect(unpackedResult.senderId).toBe(0xaabbccdd);
|
||||
expect(unpackedResult.data).toMatchObject(
|
||||
new Uint8Array([0x12, 0x34, 0x56, 0x78]),
|
||||
);
|
||||
});
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
import { MessageTypes } from '../../src/common'
|
||||
import { packers } from '../../src/mapping'
|
||||
import { MessageTypes } from "../../src/common";
|
||||
import { packers } from "../../src/mapping";
|
||||
|
||||
test('Create a subscribe (0x0000) packet.', () => {
|
||||
test("Create a subscribe (0x0000) packet.", () => {
|
||||
// Given
|
||||
const messageType = 0xabcd
|
||||
const messageType = 0xabcd;
|
||||
|
||||
// When
|
||||
const packedPacket = packers[MessageTypes.Subscribe]({ messageType: messageType })
|
||||
const packedPacket = packers[MessageTypes.Subscribe]({
|
||||
messageType: messageType,
|
||||
});
|
||||
|
||||
// Then
|
||||
const expectedResult = new Uint8Array([0x00, 0x00, 0x00, 0x02, 0xab, 0xcd])
|
||||
expect(packedPacket).toMatchObject(expectedResult)
|
||||
})
|
||||
const expectedResult = new Uint8Array([0x00, 0x00, 0x00, 0x02, 0xab, 0xcd]);
|
||||
expect(packedPacket).toMatchObject(expectedResult);
|
||||
});
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
import { MessageTypes } from '../../src/common'
|
||||
import { packers } from '../../src/mapping'
|
||||
import { MessageTypes } from "../../src/common";
|
||||
import { packers } from "../../src/mapping";
|
||||
|
||||
test('Create an unsubscribe (0xffff) packet.', () => {
|
||||
test("Create an unsubscribe (0xffff) packet.", () => {
|
||||
// Given
|
||||
const messageType = 0xabcd
|
||||
const messageType = 0xabcd;
|
||||
|
||||
// When
|
||||
const packedPacket = packers[MessageTypes.Unsubscribe]({ messageType: messageType })
|
||||
const packedPacket = packers[MessageTypes.Unsubscribe]({
|
||||
messageType: messageType,
|
||||
});
|
||||
|
||||
// Then
|
||||
const expectedResult = new Uint8Array([0xff, 0xff, 0x00, 0x02, 0xab, 0xcd])
|
||||
expect(packedPacket).toMatchObject(expectedResult)
|
||||
})
|
||||
const expectedResult = new Uint8Array([0xff, 0xff, 0x00, 0x02, 0xab, 0xcd]);
|
||||
expect(packedPacket).toMatchObject(expectedResult);
|
||||
});
|
||||
|
||||
@@ -1,52 +1,56 @@
|
||||
import Color from 'color'
|
||||
import { MessageTypes } from '../../src/common'
|
||||
import { packers, unpackers } from '../../src/mapping'
|
||||
import Color from "color";
|
||||
import { MessageTypes } from "../../src/common";
|
||||
import { packers, unpackers } from "../../src/mapping";
|
||||
|
||||
const KEY = new Uint8Array([0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F])
|
||||
const KEY = new Uint8Array([
|
||||
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c,
|
||||
0x0d, 0x0e, 0x0f,
|
||||
]);
|
||||
|
||||
test('Create a user data request (0x0002) packet.', () => {
|
||||
test("Create a user data request (0x0002) packet.", () => {
|
||||
// Given
|
||||
const username = 'Butlersaurus'
|
||||
const colour = Color('#FF4000')
|
||||
const clientId = 'Mercury'
|
||||
const username = "Butlersaurus";
|
||||
const colour = Color("#FF4000");
|
||||
const clientId = "Mercury";
|
||||
|
||||
// When
|
||||
const packedPacket = packers[MessageTypes.UserDataRequest](
|
||||
{
|
||||
username: username,
|
||||
colour: colour,
|
||||
clientId: clientId
|
||||
clientId: clientId,
|
||||
},
|
||||
KEY
|
||||
)
|
||||
KEY,
|
||||
);
|
||||
|
||||
// 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(new Uint8Array([0x00, 0x02, 0x00, 0x67]))
|
||||
expect(packedPacket.slice(0, 4)).toMatchObject(
|
||||
new Uint8Array([0x00, 0x02, 0x00, 0x67]),
|
||||
);
|
||||
|
||||
// Check the total length is as expected.
|
||||
expect(packedPacket.length).toBe(107)
|
||||
})
|
||||
expect(packedPacket.length).toBe(107);
|
||||
});
|
||||
|
||||
test('Parse a user data request (0x0002).', () => {
|
||||
test("Parse a user data request (0x0002).", () => {
|
||||
// Given
|
||||
const data = new Uint8Array([
|
||||
0, 12,
|
||||
66, 117, 116, 108, 101, 114, 115, 97, 117, 114, 117, 115, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
255, 64, 0,
|
||||
0, 7,
|
||||
77, 101, 114, 99, 117, 114, 121, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
||||
])
|
||||
const username = 'Butlersaurus'
|
||||
const colour = Color('#FF4000')
|
||||
const clientId = 'Mercury'
|
||||
0, 12, 66, 117, 116, 108, 101, 114, 115, 97, 117, 114, 117, 115, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 64, 0, 0, 7, 77, 101,
|
||||
114, 99, 117, 114, 121, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
]);
|
||||
const username = "Butlersaurus";
|
||||
const colour = Color("#FF4000");
|
||||
const clientId = "Mercury";
|
||||
|
||||
// When
|
||||
const unpackedPacket = unpackers[MessageTypes.UserDataRequest](data)
|
||||
const unpackedPacket = unpackers[MessageTypes.UserDataRequest](data);
|
||||
|
||||
// Then
|
||||
expect(unpackedPacket.username).toBe(username)
|
||||
expect(unpackedPacket.colour).toMatchObject(colour)
|
||||
expect(unpackedPacket.clientId).toBe(clientId)
|
||||
})
|
||||
expect(unpackedPacket.username).toBe(username);
|
||||
expect(unpackedPacket.colour).toMatchObject(colour);
|
||||
expect(unpackedPacket.clientId).toBe(clientId);
|
||||
});
|
||||
|
||||
@@ -1,52 +1,56 @@
|
||||
import Color from 'color'
|
||||
import { MessageTypes } from '../../src/common'
|
||||
import { packers, unpackers } from '../../src/mapping'
|
||||
import Color from "color";
|
||||
import { MessageTypes } from "../../src/common";
|
||||
import { packers, unpackers } from "../../src/mapping";
|
||||
|
||||
const KEY = new Uint8Array([0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F])
|
||||
const KEY = new Uint8Array([
|
||||
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c,
|
||||
0x0d, 0x0e, 0x0f,
|
||||
]);
|
||||
|
||||
test('Create a user data response (0x0003) packet.', () => {
|
||||
test("Create a user data response (0x0003) packet.", () => {
|
||||
// Given
|
||||
const username = 'Butlersaurus'
|
||||
const colour = Color('#FF4000')
|
||||
const clientId = 'Mercury'
|
||||
const username = "Butlersaurus";
|
||||
const colour = Color("#FF4000");
|
||||
const clientId = "Mercury";
|
||||
|
||||
// When
|
||||
const packedPacket = packers[MessageTypes.UserDataResponse](
|
||||
{
|
||||
username: username,
|
||||
colour: colour,
|
||||
clientId: clientId
|
||||
clientId: clientId,
|
||||
},
|
||||
KEY
|
||||
)
|
||||
KEY,
|
||||
);
|
||||
|
||||
// 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(new Uint8Array([0x00, 0x03, 0x00, 0x67]))
|
||||
expect(packedPacket.slice(0, 4)).toMatchObject(
|
||||
new Uint8Array([0x00, 0x03, 0x00, 0x67]),
|
||||
);
|
||||
|
||||
// Check the total length is as expected.
|
||||
expect(packedPacket.length).toBe(107)
|
||||
})
|
||||
expect(packedPacket.length).toBe(107);
|
||||
});
|
||||
|
||||
test('Parse a user data response (0x0003).', () => {
|
||||
test("Parse a user data response (0x0003).", () => {
|
||||
// Given
|
||||
const data = new Uint8Array([
|
||||
0, 12,
|
||||
66, 117, 116, 108, 101, 114, 115, 97, 117, 114, 117, 115, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
255, 64, 0,
|
||||
0, 7,
|
||||
77, 101, 114, 99, 117, 114, 121, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
||||
])
|
||||
const username = 'Butlersaurus'
|
||||
const colour = Color('#FF4000')
|
||||
const clientId = 'Mercury'
|
||||
0, 12, 66, 117, 116, 108, 101, 114, 115, 97, 117, 114, 117, 115, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 64, 0, 0, 7, 77, 101,
|
||||
114, 99, 117, 114, 121, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
]);
|
||||
const username = "Butlersaurus";
|
||||
const colour = Color("#FF4000");
|
||||
const clientId = "Mercury";
|
||||
|
||||
// When
|
||||
const unpackedPacket = unpackers[MessageTypes.UserDataResponse](data)
|
||||
const unpackedPacket = unpackers[MessageTypes.UserDataResponse](data);
|
||||
|
||||
// Then
|
||||
expect(unpackedPacket.username).toBe(username)
|
||||
expect(unpackedPacket.colour).toMatchObject(colour)
|
||||
expect(unpackedPacket.clientId).toBe(clientId)
|
||||
})
|
||||
expect(unpackedPacket.username).toBe(username);
|
||||
expect(unpackedPacket.colour).toMatchObject(colour);
|
||||
expect(unpackedPacket.clientId).toBe(clientId);
|
||||
});
|
||||
|
||||
@@ -1,19 +1,19 @@
|
||||
import { numberToUint16BE, numberToUint32BE } from '../../src/utilities/number'
|
||||
import { numberToUint16BE, numberToUint32BE } from "../../src/utilities/number";
|
||||
|
||||
test('Test number conversion to Uint16 big endian buffer.', () => {
|
||||
test("Test number conversion to Uint16 big endian buffer.", () => {
|
||||
// When
|
||||
const result = numberToUint16BE(1234)
|
||||
const result = numberToUint16BE(1234);
|
||||
|
||||
// Then
|
||||
const expectedResult = new Uint8Array([0x04, 0xd2])
|
||||
expect(result).toMatchObject(expectedResult)
|
||||
})
|
||||
const expectedResult = new Uint8Array([0x04, 0xd2]);
|
||||
expect(result).toMatchObject(expectedResult);
|
||||
});
|
||||
|
||||
test('Test number conversion to Uint32 big endian buffer.', () => {
|
||||
test("Test number conversion to Uint32 big endian buffer.", () => {
|
||||
// When
|
||||
const result = numberToUint32BE(123456)
|
||||
const result = numberToUint32BE(123456);
|
||||
|
||||
// Then
|
||||
const expectedResult = new Uint8Array([0x00, 0x01, 0xE2, 0x40])
|
||||
expect(result).toMatchObject(expectedResult)
|
||||
})
|
||||
const expectedResult = new Uint8Array([0x00, 0x01, 0xe2, 0x40]);
|
||||
expect(result).toMatchObject(expectedResult);
|
||||
});
|
||||
|
||||
@@ -1,230 +1,240 @@
|
||||
import { SmartBuffer } from '../../src/utilities/smart-buffer'
|
||||
import { SmartBuffer } from "../../src/utilities/smart-buffer";
|
||||
|
||||
test('Read a UInt16.', () => {
|
||||
test("Read a UInt16.", () => {
|
||||
// Given
|
||||
const buffer = [0x30, 0x39]
|
||||
const buffer = [0x30, 0x39];
|
||||
|
||||
// When
|
||||
const smartBuffer = SmartBuffer.from(buffer)
|
||||
const smartBuffer = SmartBuffer.from(buffer);
|
||||
|
||||
// Then
|
||||
expect(smartBuffer.readUInt16()).toBe(12345)
|
||||
})
|
||||
expect(smartBuffer.readUInt16()).toBe(12345);
|
||||
});
|
||||
|
||||
test('Read a UInt32.', () => {
|
||||
test("Read a UInt32.", () => {
|
||||
// Given
|
||||
const buffer = [0x49, 0x96, 0x02, 0xD2]
|
||||
const buffer = [0x49, 0x96, 0x02, 0xd2];
|
||||
|
||||
// When
|
||||
const smartBuffer = SmartBuffer.from(buffer)
|
||||
const smartBuffer = SmartBuffer.from(buffer);
|
||||
|
||||
// Then
|
||||
expect(smartBuffer.readUInt32()).toBe(1234567890)
|
||||
})
|
||||
expect(smartBuffer.readUInt32()).toBe(1234567890);
|
||||
});
|
||||
|
||||
test('Read a buffer.', () => {
|
||||
test("Read a buffer.", () => {
|
||||
// Given
|
||||
const buffer = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||
const buffer = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
|
||||
|
||||
// When
|
||||
const smartBuffer = SmartBuffer.from(buffer)
|
||||
const smartBuffer = SmartBuffer.from(buffer);
|
||||
|
||||
// Then
|
||||
const result = smartBuffer.readBytes(4)
|
||||
expect(result).toMatchObject(new Uint8Array([0, 1, 2, 3]))
|
||||
})
|
||||
const result = smartBuffer.readBytes(4);
|
||||
expect(result).toMatchObject(new Uint8Array([0, 1, 2, 3]));
|
||||
});
|
||||
|
||||
test('Read a UInt16 from an offset.', () => {
|
||||
test("Read a UInt16 from an offset.", () => {
|
||||
// Given
|
||||
const buffer = [0x00, 0x00, 0x30, 0x39]
|
||||
const buffer = [0x00, 0x00, 0x30, 0x39];
|
||||
|
||||
// When
|
||||
const smartBuffer = SmartBuffer.from(buffer)
|
||||
smartBuffer.cursor = 2
|
||||
const smartBuffer = SmartBuffer.from(buffer);
|
||||
smartBuffer.cursor = 2;
|
||||
|
||||
// Then
|
||||
expect(smartBuffer.readUInt16()).toBe(12345)
|
||||
})
|
||||
expect(smartBuffer.readUInt16()).toBe(12345);
|
||||
});
|
||||
|
||||
test('Read a UInt32 from an offset.', () => {
|
||||
test("Read a UInt32 from an offset.", () => {
|
||||
// Given
|
||||
const buffer = [0x00, 0x00, 0x49, 0x96, 0x02, 0xD2]
|
||||
const buffer = [0x00, 0x00, 0x49, 0x96, 0x02, 0xd2];
|
||||
|
||||
// When
|
||||
const smartBuffer = SmartBuffer.from(buffer)
|
||||
smartBuffer.cursor = 2
|
||||
const smartBuffer = SmartBuffer.from(buffer);
|
||||
smartBuffer.cursor = 2;
|
||||
|
||||
// Then
|
||||
expect(smartBuffer.readUInt32()).toBe(1234567890)
|
||||
})
|
||||
expect(smartBuffer.readUInt32()).toBe(1234567890);
|
||||
});
|
||||
|
||||
test('Read a buffer from an offset.', () => {
|
||||
test("Read a buffer from an offset.", () => {
|
||||
// Given
|
||||
const buffer = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||
const buffer = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
|
||||
|
||||
// When
|
||||
const smartBuffer = SmartBuffer.from(buffer)
|
||||
smartBuffer.cursor = 2
|
||||
const smartBuffer = SmartBuffer.from(buffer);
|
||||
smartBuffer.cursor = 2;
|
||||
|
||||
// Then
|
||||
expect(smartBuffer.readBytes(4)).toMatchObject(new Uint8Array([2, 3, 4, 5]))
|
||||
})
|
||||
expect(smartBuffer.readBytes(4)).toMatchObject(new Uint8Array([2, 3, 4, 5]));
|
||||
});
|
||||
|
||||
test('Write a UInt16.', () => {
|
||||
test("Write a UInt16.", () => {
|
||||
// Given
|
||||
const smartBuffer = new SmartBuffer()
|
||||
const smartBuffer = new SmartBuffer();
|
||||
|
||||
// When
|
||||
smartBuffer.writeUInt16(12345)
|
||||
smartBuffer.writeUInt16(12345);
|
||||
|
||||
// Then
|
||||
expect(smartBuffer.data).toMatchObject(new Uint8Array([0x30, 0x39]))
|
||||
})
|
||||
expect(smartBuffer.data).toMatchObject(new Uint8Array([0x30, 0x39]));
|
||||
});
|
||||
|
||||
test('Write a UInt32.', () => {
|
||||
test("Write a UInt32.", () => {
|
||||
// Given
|
||||
const smartBuffer = new SmartBuffer()
|
||||
const smartBuffer = new SmartBuffer();
|
||||
|
||||
// When
|
||||
smartBuffer.writeUInt32(1234567890)
|
||||
smartBuffer.writeUInt32(1234567890);
|
||||
|
||||
// Then
|
||||
expect(smartBuffer.data).toMatchObject(new Uint8Array([0x49, 0x96, 0x02, 0xD2]))
|
||||
})
|
||||
expect(smartBuffer.data).toMatchObject(
|
||||
new Uint8Array([0x49, 0x96, 0x02, 0xd2]),
|
||||
);
|
||||
});
|
||||
|
||||
test('Write a buffer.', () => {
|
||||
test("Write a buffer.", () => {
|
||||
// Given
|
||||
const smartBuffer = new SmartBuffer()
|
||||
const smartBuffer = new SmartBuffer();
|
||||
|
||||
// When
|
||||
smartBuffer.writeBytes([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
|
||||
smartBuffer.writeBytes([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
|
||||
|
||||
// Then
|
||||
expect(smartBuffer.data).toMatchObject(new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))
|
||||
})
|
||||
expect(smartBuffer.data).toMatchObject(
|
||||
new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
|
||||
);
|
||||
});
|
||||
|
||||
test('Write a UInt16 at an offset.', () => {
|
||||
test("Write a UInt16 at an offset.", () => {
|
||||
// Given
|
||||
const smartBuffer = new SmartBuffer()
|
||||
const smartBuffer = new SmartBuffer();
|
||||
|
||||
// When
|
||||
smartBuffer.cursor = 2
|
||||
smartBuffer.writeUInt16(12345)
|
||||
smartBuffer.cursor = 2;
|
||||
smartBuffer.writeUInt16(12345);
|
||||
|
||||
// Then
|
||||
expect(smartBuffer.data).toMatchObject(new Uint8Array([0x00, 0x00, 0x30, 0x39]))
|
||||
})
|
||||
expect(smartBuffer.data).toMatchObject(
|
||||
new Uint8Array([0x00, 0x00, 0x30, 0x39]),
|
||||
);
|
||||
});
|
||||
|
||||
test('Write a UInt32 at an offset.', () => {
|
||||
test("Write a UInt32 at an offset.", () => {
|
||||
// Given
|
||||
const smartBuffer = new SmartBuffer()
|
||||
const smartBuffer = new SmartBuffer();
|
||||
|
||||
// When
|
||||
smartBuffer.cursor = 2
|
||||
smartBuffer.writeUInt32(1234567890)
|
||||
smartBuffer.cursor = 2;
|
||||
smartBuffer.writeUInt32(1234567890);
|
||||
|
||||
// Then
|
||||
expect(smartBuffer.data).toMatchObject(new Uint8Array([0x00, 0x00, 0x49, 0x96, 0x02, 0xD2]))
|
||||
})
|
||||
expect(smartBuffer.data).toMatchObject(
|
||||
new Uint8Array([0x00, 0x00, 0x49, 0x96, 0x02, 0xd2]),
|
||||
);
|
||||
});
|
||||
|
||||
test('Write a buffer at an offset.', () => {
|
||||
test("Write a buffer at an offset.", () => {
|
||||
// Given
|
||||
const smartBuffer = new SmartBuffer()
|
||||
const smartBuffer = new SmartBuffer();
|
||||
|
||||
// When
|
||||
smartBuffer.cursor = 2
|
||||
smartBuffer.writeBytes([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
|
||||
smartBuffer.cursor = 2;
|
||||
smartBuffer.writeBytes([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
|
||||
|
||||
// Then
|
||||
expect(smartBuffer.data).toMatchObject(new Uint8Array([0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))
|
||||
})
|
||||
expect(smartBuffer.data).toMatchObject(
|
||||
new Uint8Array([0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
|
||||
);
|
||||
});
|
||||
|
||||
test('Cursor is correctly incremented after reading a UInt16.', () => {
|
||||
test("Cursor is correctly incremented after reading a UInt16.", () => {
|
||||
// Given
|
||||
const buffer = new Uint8Array(4)
|
||||
const buffer = new Uint8Array(4);
|
||||
|
||||
// When
|
||||
const smartBuffer = SmartBuffer.from(buffer)
|
||||
const smartBuffer = SmartBuffer.from(buffer);
|
||||
|
||||
// Then
|
||||
smartBuffer.readUInt16()
|
||||
expect(smartBuffer.cursor).toBe(2)
|
||||
})
|
||||
smartBuffer.readUInt16();
|
||||
expect(smartBuffer.cursor).toBe(2);
|
||||
});
|
||||
|
||||
test('Cursor is correctly incremented after reading a UInt32.', () => {
|
||||
test("Cursor is correctly incremented after reading a UInt32.", () => {
|
||||
// Given
|
||||
const buffer = new Uint8Array(4)
|
||||
const buffer = new Uint8Array(4);
|
||||
|
||||
// When
|
||||
const smartBuffer = SmartBuffer.from(buffer)
|
||||
const smartBuffer = SmartBuffer.from(buffer);
|
||||
|
||||
// Then
|
||||
smartBuffer.readUInt32()
|
||||
expect(smartBuffer.cursor).toBe(4)
|
||||
})
|
||||
smartBuffer.readUInt32();
|
||||
expect(smartBuffer.cursor).toBe(4);
|
||||
});
|
||||
|
||||
test('Cursor is correctly incremented after reading a buffer.', () => {
|
||||
test("Cursor is correctly incremented after reading a buffer.", () => {
|
||||
// Given
|
||||
const buffer = new Uint8Array(8)
|
||||
const buffer = new Uint8Array(8);
|
||||
|
||||
// When
|
||||
const smartBuffer = SmartBuffer.from(buffer)
|
||||
const smartBuffer = SmartBuffer.from(buffer);
|
||||
|
||||
// Then
|
||||
smartBuffer.readBytes(4)
|
||||
expect(smartBuffer.cursor).toBe(4)
|
||||
})
|
||||
smartBuffer.readBytes(4);
|
||||
expect(smartBuffer.cursor).toBe(4);
|
||||
});
|
||||
|
||||
test('Cursor is correctly incremented after writing a UInt16.', () => {
|
||||
test("Cursor is correctly incremented after writing a UInt16.", () => {
|
||||
// Given
|
||||
const smartBuffer = new SmartBuffer()
|
||||
const smartBuffer = new SmartBuffer();
|
||||
|
||||
// When
|
||||
smartBuffer.writeUInt16(12345)
|
||||
smartBuffer.writeUInt16(12345);
|
||||
|
||||
// Then
|
||||
expect(smartBuffer.cursor).toBe(2)
|
||||
})
|
||||
expect(smartBuffer.cursor).toBe(2);
|
||||
});
|
||||
|
||||
test('Cursor is correctly incremented after writing a UInt32.', () => {
|
||||
test("Cursor is correctly incremented after writing a UInt32.", () => {
|
||||
// Given
|
||||
const smartBuffer = new SmartBuffer()
|
||||
const smartBuffer = new SmartBuffer();
|
||||
|
||||
// When
|
||||
smartBuffer.writeUInt32(1234567890)
|
||||
smartBuffer.writeUInt32(1234567890);
|
||||
|
||||
// Then
|
||||
expect(smartBuffer.cursor).toBe(4)
|
||||
})
|
||||
expect(smartBuffer.cursor).toBe(4);
|
||||
});
|
||||
|
||||
test('Cursor is correctly incremented after writing a buffer.', () => {
|
||||
test("Cursor is correctly incremented after writing a buffer.", () => {
|
||||
// Given
|
||||
const smartBuffer = new SmartBuffer()
|
||||
const smartBuffer = new SmartBuffer();
|
||||
|
||||
// When
|
||||
smartBuffer.writeBytes([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
|
||||
smartBuffer.writeBytes([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
|
||||
|
||||
// Then
|
||||
expect(smartBuffer.cursor).toBe(10)
|
||||
})
|
||||
expect(smartBuffer.cursor).toBe(10);
|
||||
});
|
||||
|
||||
test('Seek to position below 0 throws range error.', () => {
|
||||
test("Seek to position below 0 throws range error.", () => {
|
||||
// When
|
||||
const smartBuffer = new SmartBuffer()
|
||||
const smartBuffer = new SmartBuffer();
|
||||
|
||||
// Then
|
||||
expect(() => {
|
||||
smartBuffer.cursor = -1
|
||||
}).toThrow(RangeError)
|
||||
})
|
||||
smartBuffer.cursor = -1;
|
||||
}).toThrow(RangeError);
|
||||
});
|
||||
|
||||
test('Pad some data.', () => {
|
||||
test("Pad some data.", () => {
|
||||
// Given
|
||||
const smartBuffer = new SmartBuffer()
|
||||
const smartBuffer = new SmartBuffer();
|
||||
|
||||
// When
|
||||
smartBuffer.pad(10)
|
||||
smartBuffer.pad(10);
|
||||
|
||||
// Then
|
||||
expect(smartBuffer.length).toBe(10)
|
||||
})
|
||||
expect(smartBuffer.length).toBe(10);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user