index.test.js
8.13 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import lib from '../../src';
import expect from 'expect.js';
import muk from 'muk';
import rewire from 'rewire';
import {
ret, orderName, setRet, setOrderName, uhf as FakeUhf
}
from '../mock/fakeUhf';
import FakeConnector from '../mock/fakeConnector';
import FakeConfig from '../mock/fakeConfig';
describe('index.js', function() {
let index;
before(function() {
index = muk('../../src', {
'./lib/connector': FakeConnector,
'./lib/directive/uhf': FakeUhf
});
});
describe('open UHF', function() {
it('open expect return', function() {
expect(function() {
lib.open(null, null, null);
}).to.throwException('错误:ip,端口,回调必须要传');
expect(function() {
lib.open('1.1.1.1', null, null);
}).to.throwException('错误:ip,端口,回调必须要传');
expect(function() {
lib.open('1.1.1.1', '123', null);
}).to.throwException('错误:ip,端口,回调必须要传');
});
it('open expect success', function(done) {
index.open('1.1.1.1', '123', function(err, data) {
expect(data).to.be.a(FakeConnector);
done();
});
});
it('open expect error', function(done) {
let val = {
code: 0,
message: 'fake error'
};
setRet(val);
index.open('1.1.1.1', '123', function(err, data) {
expect(err.message).to.be('fake error');
done();
});
});
});
describe('start inventory', function() {
it('start expect error', function() {
expect(function() {
index.start();
}).to.throwException('错误:ip,端口,回调必须要传');
expect(function() {
index.start({
'port': '123'
});
}).to.throwException('错误:ip,端口,回调必须要传');
});
it('start expect asyn error', function(done) {
index.start({
'port': '123',
'host': '1.1.1.1'
}, function(err, data) {
expect(err.message).to.be('Error:灵敏度,定频,盘点时长,功率必须要传!')
done();
});
});
it('start epxect ok', function(done) {
let val = {
code: 1
};
setRet(val);
let index = rewire("../../src");
index.__set__('setFilter', function() {});
index.__set__('setAntStayTime', function(connector, param) {
process.nextTick(function() {
connector.done('setAntStayTime', ret);
});
});
index.__set__('setFrequencyRegion', function(connector, param) {
process.nextTick(function() {
connector.done('setFrequencyRegion', ret);
});
});
index.__set__('setSpeedMode', function(connector, param) {
process.nextTick(function() {
connector.done('setSpeedMode', ret);
});
});
index.__set__('setTagGroup', function(connector, param) {
process.nextTick(function() {
connector.done('setTagGroup', ret);
});
});
index.__set__('setFrequency', function(connector, param) {
process.nextTick(function() {
connector.done('setFrequency', ret);
});
});
index.__set__('setInventoryTime', function(connector, param) {
process.nextTick(function() {
connector.done('setInventoryTime', ret);
});
});
index.__set__('setPower', function(connector, param) {
process.nextTick(function() {
connector.done('setPower', ret);
});
});
let startIndex = 0;
index.__set__('startInventory', function(connector, param) {
process.nextTick(function() {
if (startIndex > 2) {
return;
}
connector.done('startInventory', {
code: 2,
data: [{
epc: '123'
}]
});
startIndex++;
});
process.nextTick(function() {
if (startIndex > 2) {
return;
}
connector.done('startInventory', {
code: 1
});
startIndex++;
});
});
index.__set__('Connector', FakeConnector);
index.__set__('uhf', new FakeUhf());
index.start({
'port': '123',
'host': '1.1.1.1',
filter: { //设置灵敏度的过滤
'on': '1',
rssi: -60
},
frequency: 10,
antStayTime: 100,
inventoryTime: 400, //设置盘点时长
power: [15, 0, 0, 0] //功率设置
},
function(err, connected, data) {
expect(data).to.have.length(1);
done();
})
});
});
describe('stop inventory', function() {
it('stop ok', function() {
expect(index.stop).to.be.ok();
});
});
describe('procTagInfo', function() {
it('procTagInfo exit proc', function() {
let proc = rewire("../../src").__get__('procTagInfo');
let countMap = {
'1': 1
};
let rets = [{
epc: '1',
rssi: '2',
updateTime: 1 * new Date(),
state: 'on',
ant: 1
}];
let data = [{
epc: '1',
rssi: '3',
ant: 2
}];
rets = proc(rets, data, 1, '1.1.1.1');
expect(rets[0].ant).to.be(2);
expect(rets[0].rssi).to.be('3');
expect(rets[0].epc).to.be('1');
expect(rets).to.have.length(1);
});
it('procTagInfo noExist proc', function() {
let proc = rewire("../../src").__get__('procTagInfo');
let rets = proc([], [{
epc: '1',
rssi: '3',
ant: 2
}], 1, '1.1.1.1');
expect(rets[0].speed).to.be(1);
expect(rets[0].epc).to.be('1');
expect(rets[0].rssi).to.be('3');
expect(rets[0].ant).to.be(2);
expect(rets).to.have.length(1);
});
});
describe('compTags', function() {
it('lastRets empty rets a record expect lastRets a record', function() {
let compTags = rewire("../../src").__get__('compTags');
let ret = compTags([], [{
epc: '1',
rssi: '3',
ant: 2
}], []);
expect(ret.last).to.eql([{
epc: '1',
rssi: '3',
ant: 2
}]);
expect(ret.gone).to.eql([]);
});
it('lastRets a record rets empty expect gone a record', function() {
let compTags = rewire("../../src").__get__('compTags');
let ret = compTags([{
epc: '1',
rssi: '3',
speed: 2,
ant: 2
}], [], []);
expect(ret.last).to.eql([{
epc: '1',
rssi: '3',
speed: 2,
ant: 2
}]);
expect(ret.gone).to.eql([{
epc: '1',
rssi: '3',
speed: 2,
ant: 2
}]);
});
});
describe('receive', function() {
it('receive expect uhf and config and inventory ret', function() {
let indexM = rewire("../../src");
let expectClassNames = ['uhf', 'config', 'inventory'];
for (let i = 0; i < expectClassNames.length; i++) {
indexM.__set__('locateOrder', function() {
return {
val: {
directive: expectClassNames[i]
}
}
});
indexM.__set__(expectClassNames[i], {
receive: function() {
return expectClassNames[i];
}
});
let receive = indexM.__get__('receive');
let retData = receive(true, {});
expect(retData).to.be(expectClassNames[i]);
}
});
it('receive param data null expect null', function() {
let indexM = rewire("../../src");
let retData = indexM.__get__('receive')(true, null);
expect(retData).to.be(null);
});
it('receive param err expect error', function() {
let indexM = rewire("../../src");
expect(function() {
indexM.__get__('receive')(true, {}, new Error('mock error'));
}).to.throwException(/mock error/);
});
});
after(function() {
muk.restore();
});
});