Schedules.test.js
12.8 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
'use strict';
const expect = require('chai').expect
, v3Api = require('../v3').api
, discovery = require('../v3').discovery
, model = require('@peter-murray/hue-bridge-model').model
, timePatterns = require('@peter-murray/hue-bridge-model').timePatterns
, Schedule = model.Schedule
, testValues = require('../../test/support/testValues.js')
, ApiError = require('../ApiError')
;
describe('Hue API #schedule', () => {
let hue;
before(() => {
return discovery.nupnpSearch()
.then(searchResults => {
const localApi = v3Api.createLocal(searchResults[0].ipaddress);
return localApi.connect(testValues.username)
.then(api => {
hue = api;
});
});
});
describe('#getAll()', () => {
it('should find some', async () => {
const results = await hue.schedules.getAll();
expect(results).to.be.instanceOf(Array);
expect(results).to.have.length.to.be.at.least(1);
const schedule = results[0];
expect(schedule instanceof Schedule).to.be.true;
});
});
describe('#get()', () => {
let targetSchedule;
before(async () => {
const schedules = await hue.schedules.getAll();
targetSchedule = schedules[0];
});
describe('using id', () => {
it('should get a specific schedule', async () => {
const schedule = await hue.schedules.getSchedule(targetSchedule.id);
expect(schedule instanceof Schedule).to.be.true;
expect(schedule).to.have.property('id').to.equal(targetSchedule.id);
expect(schedule).to.have.property('description').to.equal(targetSchedule.description);
expect(schedule).to.have.property('localtime').to.equal(targetSchedule.localtime);
expect(schedule).to.have.property('status').to.equal(targetSchedule.status);
expect(schedule).to.have.property('recycle').to.equal(targetSchedule.recycle);
});
it('should fail for invalid schedule id', async () => {
try {
await hue.schedules.getSchedule('65535');
expect.fail('should not get here');
} catch (err) {
expect(err).to.be.instanceof(ApiError);
expect(err.getHueErrorType()).to.equal(3);
expect(err.message).to.contain('not available');
}
});
});
describe('using Schedule Object', () => {
it('should get a specific schedule', async () => {
const schedule = await hue.schedules.getSchedule(targetSchedule);
expect(schedule instanceof Schedule).to.be.true;
expect(schedule).to.have.property('id').to.equal(targetSchedule.id);
expect(schedule).to.have.property('description').to.equal(targetSchedule.description);
expect(schedule).to.have.property('localtime').to.equal(targetSchedule.localtime);
expect(schedule).to.have.property('status').to.equal(targetSchedule.status);
expect(schedule).to.have.property('recycle').to.equal(targetSchedule.recycle);
});
});
});
describe('#createSchedule()', function () {
// We need a longer wait time here as we have to pause for the schedules to trigger and remove themselves from the bridge
this.timeout(30 * 1000);
let created;
beforeEach(() => {
created = null;
});
afterEach(async () => {
if (created) {
try {
await hue.schedules.deleteSchedule(created);
} catch (err) {
console.log(`Failed to delete created schedule: ${created.id}, ${err.message}`);
}
}
});
it('should create a schedule', async () => {
const schedule = new Schedule();
schedule.name = 'Test Schedule Recurring';
schedule.description = 'A node-hue-api test schedule that can be removed';
schedule.localtime = timePatterns.createRecurringTime('W124/T12:00:00');
schedule.recycle = true;
schedule.command = model.actions.light(0).withState({on: true});
const result = await hue.schedules.createSchedule(schedule);
created = result;
expect(result).to.have.property('name').to.equal(schedule.name);
expect(result).to.have.property('description').to.equal(schedule.description);
expect(result).to.have.property('command');
expect(result.command).to.have.property('method').to.equal('PUT');
expect(result.command).to.have.property('address').to.contain('/lights/0/state');
expect(result.command).to.have.property('body').to.deep.equal({on: true});
});
//TODO should try all the different time patterns for schedules
it('should create a schedule that will autodelete', async () => {
const schedule = new Schedule();
schedule.name = 'Test Schedule AutoDelete';
schedule.description = 'A node-hue-api test schedule should autodelete itself';
schedule.localtime = timePatterns.createTimer().seconds(2);
schedule.recycle = true;
schedule.autodelete = true;
schedule.command = model.actions.light(40).withState(new model.lightStates.LightState().alertShort());
const result = await hue.schedules.createSchedule(schedule);
expect(result).to.have.property('name').to.equal(schedule.name);
expect(result).to.have.property('autodelete').to.be.true;
// Verify it has auto deleted
await waitFor(8 * 1000);
try {
await hue.schedules.getSchedule(result);
expect.fail('should have auto deleted from schedules');
} catch (err) {
// console.error(err);
expect(err.getHueErrorType()).to.equal(3);
}
});
it('should create a schedule with a repeat timer that repeats 2 times', async () => {
const schedule = new Schedule();
schedule.name = 'Reoccurring Schedule from Timer';
schedule.description = 'A node-hue-api test schedule should autodelete itself';
schedule.localtime = timePatterns.createRecurringTimer().seconds(5).reoccurs(2);
schedule.recycle = true;
schedule.command = model.actions.light(40).withState(new model.lightStates.LightState().alertShort());
const result = await hue.schedules.createSchedule(schedule);
await waitFor(15 * 1000);
try {
await hue.schedules.getSchedule(result);
expect.fail('should have auto deleted from schedules');
} catch (err) {
expect(err.getHueErrorType()).to.equal(3);
}
});
it('should create a schedule with a repeats forever', async () => {
const schedule = new Schedule();
schedule.name = 'Reoccurring Schedule from Timer';
schedule.description = 'A node-hue-api test schedule should autodelete itself';
schedule.localtime = timePatterns.createRecurringTimer().seconds(3);
schedule.recycle = true;
schedule.command = model.actions.light(40).withState(new model.lightStates.LightState().alertShort());
const result = await hue.schedules.createSchedule(schedule);
created = result;
// Let the schedule run at least three times
await waitFor(12 * 1000);
// Stop it as it is annoying
const runningSchedule = await hue.schedules.getSchedule(result);
runningSchedule.status = 'disabled';
await hue.schedules.updateSchedule(runningSchedule);
});
});
describe('#updateSchedule()', () => {
let schedule;
beforeEach(async () => {
const createSchedule = new Schedule();
createSchedule.name = 'Test Schedule For Updates';
createSchedule.description = 'A node-hue-api test schedule that can be removed';
createSchedule.localtime = timePatterns.createAbsoluteTime(new Date(Date.now() + (1000 * 60 * 60)));
createSchedule.recycle = true;
createSchedule.command = model.actions.light(0).withState({on: true});
schedule = await hue.schedules.createSchedule(createSchedule);
});
afterEach(async () => {
if (schedule) {
try {
await hue.schedules.deleteSchedule(schedule);
} catch (err) {
console.log(`Failed to delete created schedule: ${schedule.id}, ${err.message}`);
}
}
});
it('should update the name', async () => {
const newName = 'Updated Schedule Name';
schedule.name = newName;
const result = await hue.schedules.updateSchedule(schedule)
, updatedSchedule = await hue.schedules.getSchedule(schedule)
;
expect(result).to.have.property('name').to.be.true;
expect(updatedSchedule).to.have.property('name').to.equal(newName);
});
it('should update the description', async () => {
const original = schedule.getHuePayload()
, newDescription = original.description + Date.now();
schedule.description = newDescription;
const result = await hue.schedules.updateSchedule(schedule)
, updatedSchedule = await hue.schedules.getSchedule(schedule)
;
expect(result).to.have.property('description').to.be.true;
expect(updatedSchedule).to.have.property('description').to.equal(newDescription);
});
it('should update the command', async () => {
const newCommand = model.actions.light(1).withState(new model.lightStates.LightState().off());
schedule.command = newCommand;
const result = await hue.schedules.updateSchedule(schedule)
, updatedSchedule = await hue.schedules.getSchedule(schedule)
;
expect(result).to.have.property('command').to.be.true;
expect(updatedSchedule).to.have.property('command').to.have.property('address').to.contain('/lights/1');
expect(updatedSchedule).to.have.property('command').to.have.property('body').to.deep.equal({on: false});
});
it('should update the localtime', async () => {
const newTime = timePatterns.createAbsoluteTime(new Date(Date.now() + (10 * 1000 * 60 * 60)));
schedule.localtime = newTime;
const result = await hue.schedules.updateSchedule(schedule)
, updatedSchedule = await hue.schedules.getSchedule(schedule)
;
expect(result).to.have.property('localtime').to.be.true;
expect(updatedSchedule).to.have.property('localtime').to.equal(newTime.toString());
});
it('should update the status', async () => {
expect(schedule.status).to.equal('enabled');
schedule.status = 'disabled';
const result = await hue.schedules.updateSchedule(schedule)
, updatedSchedule = await hue.schedules.getSchedule(schedule)
;
expect(result).to.have.property('status').to.be.true;
expect(updatedSchedule).to.have.property('status').to.equal('disabled');
});
it('should update autodelete', async () => {
expect(schedule).to.have.property('autodelete').to.be.true;
schedule.autodelete = false;
const result = await hue.schedules.updateSchedule(schedule)
, updatedSchedule = await hue.schedules.getSchedule(schedule)
;
expect(result).to.have.property('autodelete').to.be.true;
expect(updatedSchedule).to.have.property('autodelete').to.be.false;
});
});
describe('#deleteSchedule()', () => {
let schedule;
beforeEach(async () => {
const createSchedule = new Schedule();
createSchedule.name = 'Test Schedule For Deletes';
createSchedule.description = 'A node-hue-api test schedule that can be removed';
createSchedule.localtime = timePatterns.createAbsoluteTime(new Date(Date.now() + (1000 * 60 * 60)));
createSchedule.recycle = true;
createSchedule.command = model.actions.light(0).withState({on: false});
schedule = await hue.schedules.createSchedule(createSchedule);
});
afterEach(async () => {
if (schedule) {
try {
const exists = await hue.schedules.getSchedule(schedule);
await hue.schedules.deleteSchedule(exists);
} catch (err) {
if (err.getHueErrorType() !== 3) {
console.log(`Failed to delete created schedule: ${schedule.id}, ${err.message}`);
}
}
}
});
it('should delete an existing schedule', async () => {
const result = await hue.schedules.deleteSchedule(schedule);
expect(result).to.be.true;
});
it('should error when deleting a schedule that does not exist', async () => {
const allSchedules = await hue.schedules.getAll()
, nextId = getNextScheduleId(allSchedules)
;
try {
await hue.schedules.deleteSchedule(nextId);
expect.fail('Should not get here');
} catch (err) {
expect(err.message).to.contain('not available');
expect(err.getHueErrorType()).to.equal(3);
}
});
});
});
//TODO make part of utils
function waitFor(milliseconds) {
return new Promise(resolve => {
setTimeout(() => {
resolve(true)
}, milliseconds);
})
}
// Common with Rules tests
function getNextScheduleId(allSchedules) {
const ids = allSchedules.map(schedule => schedule.id);
let id = 1
, nextId = null
;
while (!nextId) {
id++;
if (ids.indexOf(id) === -1) {
nextId = id;
}
}
return nextId;
}