-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti-device-test.js
74 lines (62 loc) · 3.17 KB
/
multi-device-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
const csvparse = require("csv-parse/lib/sync");
const deviceAPI = require("./device-api");
const expect = require("chai").expect;
const fs = require("fs");
const metrics = require("datadog-metrics");
const vdk = require("virtual-device-sdk");
// We use the very nice dotenv module to setup our environment variables
// It looks in the <PROJECT_DIR>/.env file for environment values so we don't have to set them up everytime
require("dotenv").config();
describe("multiple devices", function() {
// Increase the timeout for these tests to 60 seconds
this.timeout(60000);
const virtualDevice = new vdk.VirtualDevice(process.env.VIRTUAL_DEVICE_TOKEN);
// Read the device names from a CSV
const devices = csvparse(fs.readFileSync("devices.csv"));
for (const deviceArray of devices) {
const deviceName = deviceArray[0];
const deviceID = deviceName.split(" ").join("-");
describe(`turn on and off ${deviceName}`, function() {
afterEach(async function() {
// Turn off the device after each test
return virtualDevice.message(`turn off ${deviceName}`);
});
it("should toggle without error", async function() {
// To interact with a Bespoken Virtual Device, sign up and create one at:
// https://apps.bespoken.io/dashboard
// They virtual devices allow you to talk to Alexa and Google Assistant programmatically
// The token once created should be set as an environment variable named VIRTUAL_DEVICE_TOKEN
const response = await virtualDevice.message(`turn on ${deviceName}`);
const successAlexa = response.transcript.includes("ok");
// Check the device API to ensure it also worked
// In this case, we use a simple mock - but just substitute in your real API here
const state = deviceAPI.getState(process.env.DEVICE_API_KEY, deviceID)
const successDevice = state.status === "ON";
// Send the stats to Data Dog [Optional step]
// Leveraging DataDog is great for advanced reporting and notifications
sendToDataDog(deviceID, successAlexa, successDevice);
// Do our assertions
expect(successAlexa, "Call to Alexa failed: " + response.transcript).to.be.true;
expect(successDevice, "Device state was not set properly").to.be.true;
});
});
}
});
// Here we increment a counter everytime we have success interacting with the device
// Using the counter, we can see the results over time as well as trigger notifications if too many tests fail
function sendToDataDog(deviceID, successAlexa, successDevice) {
if (process.env.DATADOG_API_KEY) {
metrics.init({ host: deviceID, prefix: "device." });
if (successAlexa) {
metrics.increment("alexa.success");
} else {
metrics.increment("alexa.failure");
}
if (successDevice) {
metrics.increment("api.success");
} else {
metrics.increment("api.failure");
}
metrics.flush();
}
}