-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.test.js
More file actions
95 lines (79 loc) · 2.9 KB
/
index.test.js
File metadata and controls
95 lines (79 loc) · 2.9 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
import sinon from "sinon";
import assert from "assert";
import { describe, it, before } from "mocha";
import { unzipSync } from "zlib";
import { getFunction } from "@google-cloud/functions-framework/testing";
describe("functions_solve_http", () => {
const getMocks = () => {
const req = { body: {}, query: {} };
return {
req: req,
res: {
set: () => {},
status: sinon.stub().returnsThis(),
send: sinon.stub().returnsThis(),
sendStatus: sinon.stub().returnsThis(),
},
};
};
before(async () => {
await import("./index.js");
});
it("solve: should return status 400 if no query input", () => {
const mocks = getMocks();
const solve = getFunction("solveHttp");
solve(mocks.req, mocks.res);
assert.equal(mocks.res.status.calledOnceWith(400), true);
});
it("solve: should return status 400 if query input is not an array", () => {
const mocks = getMocks();
mocks.req.query.input = "";
const solve = getFunction("solveHttp");
solve(mocks.req, mocks.res);
assert.equal(mocks.res.status.calledOnceWith(400), true);
});
it("solve: should return longest solving step if no solution is found", () => {
const mocks = getMocks();
mocks.req.query.input = ["ABC", "DEF", "GHI", "JKL"];
const solve = getFunction("solveHttp");
solve(mocks.req, mocks.res);
const res = parseBuffer(mocks.res.send.getCall(0).args[0]);
sinon.assert.match(res.slice(-1), [
["LILA", "ALIKE", "ELI", "ILIAD", "DIE"],
]);
}).timeout(30000);
it("solve: should return a nested array of the first solution found with viable query input", () => {
const mocks = getMocks();
mocks.req.query.input = ["SRG", "MDH", "IOL", "ENP"];
const solve = getFunction("solveHttp");
solve(mocks.req, mocks.res);
const res = parseBuffer(mocks.res.send.getCall(0).args[0]);
sinon.assert.match(res.slice(-1), [["MORPHS", "SHIELDING"]]);
});
it("findBest: should return array of best solution with viable query input and length", () => {
const mocks = getMocks();
mocks.req.query.input = ["SRG", "MDH", "IOL", "ENP"];
mocks.req.query.length = 2;
const solve = getFunction("solveHttp");
solve(mocks.req, mocks.res);
const res = parseBuffer(mocks.res.send.getCall(0).args[0]);
sinon.assert.match(res, ["MORPHS", "SINGLED"]);
}).timeout(1000);
it("findBest: should return empty array if no best solution found", () => {
const mocks = getMocks();
mocks.req.query.input = ["SRG", "MDH", "IOL", "ENP"];
mocks.req.query.length = 1;
const solve = getFunction("solveHttp");
solve(mocks.req, mocks.res);
const res = parseBuffer(mocks.res.send.getCall(0).args[0]);
sinon.assert.match(res, []);
});
});
/**
* Unzip and parse data from buffer
* @param {Buffer} buffer
* @returns {Array}
*/
const parseBuffer = (buffer) => {
return JSON.parse(unzipSync(buffer).toString())["data"];
};