-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.js
166 lines (153 loc) · 4.01 KB
/
test.js
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
'use strict'
const { test } = require('tap')
const Packet = require('./')
test('Packet defaults - PUBLISH, QoS 0', function (t) {
const instance = new Packet({})
t.equal(instance.cmd, 'publish')
t.equal(instance.brokerId, undefined)
t.equal(instance.brokerCounter, 0)
t.equal(instance.topic, undefined)
t.same(instance.payload, Buffer.alloc(0))
t.equal(instance.qos, 0)
t.equal(instance.dup, false)
t.equal(instance.retain, false)
t.notOk(Object.prototype.hasOwnProperty.call(instance, 'messageId'))
t.end()
})
test('Packet defaults - PUBREL, QoS 0', function (t) {
const instance = new Packet({ cmd: 'pubrel' })
t.equal(instance.cmd, 'pubrel')
t.equal(instance.brokerId, undefined)
t.equal(instance.brokerCounter, 0)
t.equal(instance.topic, undefined)
t.same(instance.payload, Buffer.alloc(0))
t.equal(instance.qos, 0)
t.equal(instance.dup, false)
t.equal(instance.retain, false)
t.ok(Object.prototype.hasOwnProperty.call(instance, 'messageId'))
t.equal(instance.messageId, undefined)
t.end()
})
test('Packet defaults - PUBLISH, QoS 1', function (t) {
const instance = new Packet({ qos: 1 })
t.equal(instance.cmd, 'publish')
t.equal(instance.brokerId, undefined)
t.equal(instance.brokerCounter, 0)
t.equal(instance.topic, undefined)
t.same(instance.payload, Buffer.alloc(0))
t.equal(instance.qos, 1)
t.equal(instance.dup, false)
t.equal(instance.retain, false)
t.ok(Object.prototype.hasOwnProperty.call(instance, 'messageId'))
t.equal(instance.messageId, undefined)
t.end()
})
test('Packet defaults - PUBLISH, dup=true', function (t) {
const instance = new Packet({ dup: true })
t.equal(instance.cmd, 'publish')
t.equal(instance.brokerId, undefined)
t.equal(instance.brokerCounter, 0)
t.equal(instance.topic, undefined)
t.same(instance.payload, Buffer.alloc(0))
t.equal(instance.qos, 0)
t.equal(instance.dup, true)
t.equal(instance.retain, false)
t.equal(instance.messageId, undefined)
t.end()
})
test('Packet copies over most data', function (t) {
const original = {
cmd: 'pubrel',
brokerId: 'A56c',
brokerCounter: 42,
topic: 'hello',
payload: 'world',
qos: 2,
dup: true,
retain: true,
messageId: 24
}
const instance = new Packet(original)
const expected = {
cmd: 'pubrel',
brokerId: 'A56c',
brokerCounter: 42,
topic: 'hello',
payload: 'world',
qos: 2,
dup: true,
retain: true
}
t.ok(Object.prototype.hasOwnProperty.call(instance, 'messageId'))
t.equal(instance.messageId, undefined)
delete instance.messageId
t.same(instance, expected)
t.end()
})
test('Packet fills in broker data', function (t) {
const broker = {
id: 'A56c',
counter: 41
}
const original = {
cmd: 'pubrel',
topic: 'hello',
payload: 'world',
qos: 2,
retain: true,
messageId: 24
}
const instance = new Packet(original, broker)
const expected = {
cmd: 'pubrel',
brokerId: 'A56c',
brokerCounter: 42,
topic: 'hello',
payload: 'world',
qos: 2,
dup: false,
retain: true
}
t.ok(Object.prototype.hasOwnProperty.call(instance, 'messageId'))
t.equal(instance.messageId, undefined)
delete instance.messageId
t.same(instance, expected)
t.end()
})
test('Packet copies clientId and nl if they exist', function (t) {
const original = {
clientId: 'client-id',
nl: false
}
const instance = new Packet(original)
const expected = {
clientId: 'client-id',
nl: false,
cmd: 'publish',
brokerId: undefined,
brokerCounter: 0,
topic: undefined,
payload: Buffer.alloc(0),
qos: 0,
dup: false,
retain: false
}
t.same(instance, expected)
t.end()
})
test('Packet does not copy clientId and nl if they dont exist', function (t) {
const original = {}
const instance = new Packet(original)
const expected = {
cmd: 'publish',
brokerId: undefined,
brokerCounter: 0,
topic: undefined,
payload: Buffer.alloc(0),
qos: 0,
dup: false,
retain: false
}
t.same(instance, expected)
t.end()
})