-
Notifications
You must be signed in to change notification settings - Fork 10
/
packet.test.ts
87 lines (77 loc) 路 2.6 KB
/
packet.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import assert from 'assert';
import { Packet } from '../src/packet';
import { buff } from './validBuffer';
const PAYLOAD = { 1: 91.76, 2: 100, 4: 100 };
describe('Simple Packet', () => {
it('Correctly sets metadata from a given buffer', () => {
const packet = new Packet(buff, '10.0.225.7');
assert.strictEqual(packet.universe, 1);
assert.strictEqual(packet.sourceName, 'Onyx');
assert.strictEqual(packet.sequence, 172);
assert.strictEqual(packet.sourceAddress, '10.0.225.7');
assert.strictEqual(packet.priority, 100);
assert.deepStrictEqual(packet.payload, PAYLOAD);
const payloadAsArray = [...(<Buffer>packet.payloadAsBuffer)];
payloadAsArray.length = 6; // let's not check the entire thing
assert.deepStrictEqual(payloadAsArray, [234, 255, 0, 255, 0, 0]);
});
it('Returns the same buffer as supplied', () => {
const packet = new Packet(buff);
assert.deepStrictEqual(packet.buffer, buff);
});
it('throws if instantiated with no value', () => {
assert.throws(() => {
// @ts-expect-error we're testing if it throws
// eslint-disable-next-line no-new
new Packet();
});
});
it('Can create a valid buffer from options', () => {
const packet = new Packet({
sourceName: 'Onyx',
universe: 1,
sequence: 172,
priority: 100,
payload: PAYLOAD,
cid: Buffer.from([
168, 19, 166, 93, 17, 41, 81, 64, 185, 113, 133, 166, 110, 170, 141, 79,
]),
});
assert.deepStrictEqual(packet.buffer, buff);
assert.deepStrictEqual(packet.payloadAsBuffer, null); // not available when creating a packet from options
});
it('ignores invalid channels', () => {
const packet = new Packet({
universe: 1,
payload: {
513: 100,
},
sequence: 172,
});
// double wrapped so that it converts to buffer and back
assert.deepStrictEqual(new Packet(packet.buffer).payload, {});
});
it('correctly sets channel values from the [0-100] range', () => {
const packet = new Packet({
universe: 1,
payload: { 1: 91.76, 2: 100, 4: 100 },
sequence: 172,
});
assert.deepStrictEqual(
packet.buffer.slice(126, 130), // first 4 channel values
Buffer.from([0xea, 0xff, 0, 0xff]),
);
});
it('correctly sets channel values from the [0-255] range', () => {
const packet = new Packet({
universe: 1,
payload: { 1: 234, 2: 255, 4: 255 },
sequence: 172,
useRawDmxValues: true,
});
assert.deepStrictEqual(
packet.buffer.slice(126, 130), // first 4 channel values
Buffer.from([0xea, 0xff, 0, 0xff]),
);
});
});