-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.spec.js
177 lines (172 loc) · 6.26 KB
/
index.spec.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
import { COMMANDS, HEX_COMMANDS } from "./constants";
import * as codeGen from "./helpers/code-gen";
import url from "url";
const request = require("supertest");
const app = require("./app");
const grillPolling = require("./grill-polling");
jest.mock("./grill-polling", () => ({
pollStatus: jest.fn(),
sendOnce: jest.fn(),
latestStatus: jest.fn(() => ({
settings: "xxxxxxxxxxxx",
})),
}));
jest.spyOn(codeGen, "newCode");
describe("COMMANDS", () => {
beforeEach(() => {
codeGen.newCode.mockClear();
});
afterEach(() => {
grillPolling.sendOnce.mockClear();
});
describe("Command API", () => {
describe("Power commands", () => {
it("visit w/ no code returns link", async (done) => {
const res = await request(app).get("/command/power/off");
expect(res.statusCode).toEqual(200);
expect(res.body.code).toBeDefined();
expect(res.body.link).toBeDefined();
expect(codeGen.newCode).toHaveBeenCalledTimes(1);
done();
});
it("visit w/ correct code", async (done) => {
const res = await request(app).get("/command/power/off");
expect(res.statusCode).toEqual(200);
const code = res.body.code;
const link = res.body.link;
expect(codeGen.newCode).toHaveBeenCalledTimes(1);
expect(code).toBeDefined();
const expectedLink = url.format({
protocol: res.request.protocol,
host: res.request.host,
pathname: res.req.path,
query: {
code,
},
});
expect(link).toEqual(expectedLink);
const res2 = await request(app).get(`/command/power/off?code=${code}`);
expect(res2.statusCode).toEqual(200);
expect(grillPolling.sendOnce).toHaveBeenCalledWith(COMMANDS.powerOff, undefined);
expect(codeGen.newCode).toHaveBeenCalledTimes(2);
done();
});
it("visit w/ incorrect code", async (done) => {
const res = await request(app).get("/command/power/off?code=x123");
expect(res.statusCode).toEqual(403);
expect(res.body.message).toEqual("invalid code");
expect(codeGen.newCode).toHaveBeenCalledTimes(1);
expect(grillPolling.sendOnce).not.toHaveBeenCalled();
done();
});
describe("quick check for each", () => {
it("on", async (done) => {
const res = await request(app).get("/command/power/on");
expect(res.body.link).toBeDefined();
done();
});
it("on", async (done) => {
const res = await request(app).get("/command/power/on");
expect(res.body.link).toBeDefined();
done();
});
it("cold-smoke", async (done) => {
const res = await request(app).get("/command/power/cold-smoke");
expect(res.body.link).toBeDefined();
done();
});
});
});
describe("Temp commands", () => {
// @todo test other temps
// @todo handle out of range temps
it("grill temp 150", async (done) => {
const res = await request(app).get("/command/temp/grill?temp=150");
expect(res.statusCode).toEqual(200);
const code = res.body.code;
const link = res.body.link;
expect(codeGen.newCode).toHaveBeenCalledTimes(1);
expect(code).toBeDefined();
const expectedLink = url.format({
protocol: res.request.protocol,
host: res.request.host,
pathname: "/command/temp/grill", // @todo fix res.req.path
query: {
temp: 150,
code,
},
});
expect(link).toEqual(expectedLink);
const res2 = await request(app).get(`/command/temp/grill?temp=150&code=${code}`);
expect(res2.statusCode).toEqual(200);
expect(res2.body.message).toEqual("Sent grill grill temp 150 command");
expect(grillPolling.sendOnce).toHaveBeenCalledWith(
COMMANDS.setGrillTempF(150),
undefined
);
expect(codeGen.newCode).toHaveBeenCalledTimes(2);
done();
});
describe("quick check for each", () => {
// @todo detailed check for each
it("probe1", async (done) => {
const res = await request(app).get("/command/temp/probe1");
expect(res.body.link).toBeDefined();
done();
});
it("probe2", async (done) => {
const res = await request(app).get("/command/temp/probe2");
expect(res.body.link).toBeDefined();
done();
});
});
});
describe("Settings commands", () => {
it("set pizza mode", async (done) => {
const res = await request(app).get("/command/settings/pizza");
expect(res.statusCode).toEqual(200);
const code = res.body.code;
const link = res.body.link;
expect(codeGen.newCode).toHaveBeenCalledTimes(1);
expect(code).toBeDefined();
const expectedLink = url.format({
protocol: res.request.protocol,
host: res.request.host,
pathname: "/command/settings/pizza", // @todo fix res.req.path
query: {
code,
},
});
expect(link).toEqual(expectedLink);
const res2 = await request(app).get(`/command/settings/pizza?code=${code}`);
expect(res2.statusCode).toEqual(200);
expect(res2.body.message).toEqual("Sent grill pizza mode command");
expect(grillPolling.sendOnce).toHaveBeenCalledWith(
HEX_COMMANDS.setPizzaMode(grillPolling.latestStatus().settings),
"hex"
);
expect(codeGen.newCode).toHaveBeenCalledTimes(2);
done();
});
describe("quick check for each", () => {
// @todo detailed check for each
it("pizza", async (done) => {
const res = await request(app).get("/command/settings/pizza");
expect(res.body.link).toBeDefined();
done();
});
it("regular", async (done) => {
const res = await request(app).get("/command/settings/regular");
expect(res.body.link).toBeDefined();
done();
});
});
});
});
// it("on", async (done) => {
// const res = await request(app).get("/command/on");
// expect(res.statusCode).toEqual(200);
// expect(grillPolling.sendOnce).toHaveBeenCalledWith(COMMANDS.powerOn);
// done();
// });
});