-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtest.js
executable file
·266 lines (246 loc) · 8.34 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
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
require('should');
const cp = require('child_process');
const request = require('request');
const path = require('path');
const streamSplitter = require('stream-splitter');
const Mqtt = require('mqtt');
mqtt = Mqtt.connect('mqtt://127.0.0.1');
const simCmd = path.join(__dirname, '/node_modules/.bin/hue-simulator');
const simArgs = ['--hostname=127.0.0.1'];
let sim;
let simPipeOut;
let simPipeErr;
const simSubscriptions = {};
const simBuffer = [];
const hueCmd = path.join(__dirname, '/index.js');
const hueArgs = ['-u mqtt://127.0.0.1:1883', '--bridge', '127.0.0.1', '-v', 'debug', '--publish-distinct'];
let hue;
let huePipeOut;
let huePipeErr;
const hueSubscriptions = {};
const hueBuffer = [];
let subIndex = 0;
function subscribe(type, rx, cb) {
subIndex += 1;
if (type === 'sim') {
simSubscriptions[subIndex] = {rx, cb};
} else if (type === 'hue') {
hueSubscriptions[subIndex] = {rx, cb};
}
matchSubscriptions(type);
return subIndex;
}
function unsubscribe(type, subIndex) {
if (type === 'sim') {
delete simSubscriptions[subIndex];
} else if (type === 'hue') {
delete hueSubscriptions[subIndex];
}
}
function matchSubscriptions(type, data) {
let subs;
let buf;
if (type === 'sim') {
subs = simSubscriptions;
buf = simBuffer;
} else if (type === 'hue') {
subs = hueSubscriptions;
buf = hueBuffer;
}
if (data) {
buf.push(data);
}
buf.forEach((line, index) => {
Object.keys(subs).forEach(key => {
const sub = subs[key];
if (line.match(sub.rx)) {
sub.cb(line);
delete subs[key];
buf.splice(index, 1);
}
});
});
}
function startHue() {
hue = cp.spawn(hueCmd, hueArgs);
huePipeOut = hue.stdout.pipe(streamSplitter('\n'));
huePipeErr = hue.stderr.pipe(streamSplitter('\n'));
huePipeOut.on('token', data => {
console.log('hue', data.toString())
matchSubscriptions('hue', data.toString());
});
huePipeErr.on('token', data => {
console.log('hue', data.toString())
matchSubscriptions('hue', data.toString());
});
}
function startSim() {
sim = cp.spawn(simCmd, simArgs);
simPipeOut = sim.stdout.pipe(streamSplitter('\n'));
simPipeErr = sim.stderr.pipe(streamSplitter('\n'));
simPipeOut.on('token', data => {
console.log('sim', data.toString());
matchSubscriptions('sim', data.toString());
});
simPipeErr.on('token', data => {
console.log('sim', data.toString());
matchSubscriptions('sim', data.toString());
});
}
function end(code) {
if (hue.kill) {
hue.kill();
}
if (sim.kill) {
sim.kill();
}
if (typeof code !== 'undefined') {
process.exit(code);
}
}
process.on('SIGINT', () => {
end(1);
});
process.on('exit', () => {
end();
});
describe('start daemons', () => {
it('hue-simulator should start without error', function (done) {
this.timeout(20000);
subscribe('sim', /hue simulator listening/, data => {
done();
});
startSim();
});
it('hue2mqtt should start without error', function (done) {
this.timeout(20000);
subscribe('hue', /hue2mqtt [0-9.]+ starting/, data => {
done();
});
startHue();
});
});
describe('hue2mqtt - mqtt connection', () => {
it('hue2mqtt should connect to the mqtt broker', function (done) {
this.timeout(12000);
subscribe('hue', /mqtt connected/, data => {
done();
});
});
});
describe('hue2mqtt - hue-simulator connection', () => {
it('hue2mqtt should output "please press the link button"', function (done) {
this.timeout(12000);
subscribe('hue', /please press the link button/, data => {
done();
});
});
it('hue2mqtt should obtain a username after the link button was pressed', function (done) {
this.timeout(42000);
request({
url: 'http://127.0.0.1:80/linkbutton',
method: 'GET'
});
subscribe('hue', /got username/, data => {
done();
});
});
it('hue2mqtt should should be connected to hue-simulator', function (done) {
this.timeout(42000);
subscribe('hue', /bridge connected/, data => {
done();
});
});
});
describe('hue2mqtt - hue-simulator api', () => {
it('hue2mqtt should receive 2 lights from hue-simulator', function (done) {
this.timeout(10000);
subscribe('hue', /got 2 lights/, data => {
done();
});
});
it('hue2mqtt should receive 2 groups from hue-simulator', function (done) {
this.timeout(10000);
subscribe('hue', /got 2 groups/, data => {
done();
});
});
});
describe('mqtt - hue2mqtt - hue-simulator', () => {
it('hue-simulator should receive a put after publishing on hue/set/lights/+', function (done) {
subscribe('sim', /PUT \/api\/letmegeneratethatforyou\/lights\/1\/state/, data => {
done();
});
mqtt.publish('hue/set/lights/Hue Lamp 1', JSON.stringify({on: false, bri: 254}));
});
it('hue-simulator should receive a put after publishing on hue/set/groups/+', function (done) {
subscribe('sim', /PUT \/api\/letmegeneratethatforyou\/groups\/1\/action/, data => {
done();
});
mqtt.publish('hue/set/groups/Group 1', JSON.stringify({on: true}));
});
it('hue2mqtt should react on mqtt message and publish changed state on mqtt', function (done) {
mqtt.subscribe('hue/status/lights/Hue Lamp 2/on');
mqtt.on('message', (topic, payload, options) => {
if (options.retain === false && payload.toString() === '{"val":false}') {
mqtt.removeAllListeners('message');
mqtt.unsubscribe('hue/status/lights/Hue Lamp 2/on');
done();
}
});
mqtt.publish('hue/set/groups/Group 1', JSON.stringify({on: false}));
});
it('hue2mqtt should react on mqtt message and publish changed state on mqtt', function (done) {
mqtt.subscribe('hue/status/lights/Hue Lamp 2/on');
mqtt.on('message', (topic, payload, options) => {
if (!options.retain && payload.toString() === '{"val":true}') {
mqtt.removeAllListeners('message');
mqtt.unsubscribe('hue/status/lights/Hue Lamp 2/on');
done();
}
});
mqtt.publish('hue/set/lights/Hue Lamp 2', '254');
});
it('hue2mqtt should react on mqtt message and publish changed state on mqtt', function (done) {
mqtt.subscribe('hue/status/lights/Hue Lamp 2/bri');
mqtt.on('message', (topic, payload, options) => {
if (!options.retain && payload.toString() === '{"val":127}') {
mqtt.removeAllListeners('message');
mqtt.unsubscribe('hue/status/lights/Hue Lamp 2/bri');
done();
}
});
mqtt.publish('hue/set/lights/Hue Lamp 2', '127');
});
it('hue2mqtt should react on mqtt message and publish changed state on mqtt', function (done) {
mqtt.subscribe('hue/status/lights/Hue Lamp 2');
mqtt.on('message', (topic, payload, options) => {
if (!options.retain && payload.toString() === '{"val":0,"hue_state":{"on":false,"bri":127,"hue":33536,"sat":144,"xy":[0.346,0.3568],"ct":201,"alert":"none","effect":"none","colormode":"hs","reachable":true}}') {
mqtt.removeAllListeners('message');
mqtt.unsubscribe('hue/status/lights/Hue Lamp 2');
done();
}
});
mqtt.publish('hue/set/lights/Hue Lamp 2', 'false');
});
});
describe('hue2mqtt - hue-simulator - disconnect', () => {
it('hue2mqtt should log "bridge-disconnected', function (done) {
this.timeout(12000);
subscribe('hue', /bridge disconnected/, data => {
done();
});
sim.kill();
});
});
/* TODO fixme
describe('hue2mqtt - hue-simulator - reconnect', () => {
it('hue2mqtt should reconnect when the hue-simulator is running again', function (done) {
this.timeout(60000);
subscribe('hue', /bridge connected/, data => {
done();
});
setTimeout(startSim, 3000);
});
});
*/