forked from clusterio/clusterio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
master.spec.js
184 lines (172 loc) · 6.32 KB
/
master.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
178
179
180
181
182
183
184
var app = require('./master.js'),
assert = require('assert'),
request = require('supertest');
const validateHTML = require('html5-validator');
var parallel = require('mocha.parallel');
describe('Master server endpoint testing', function() {
describe('#GET /api/getFactorioLocale', function() {
it('should get the basegame factorio locale', function(done) {
request(app).get('/api/getFactorioLocale').end(function(err, res) {
assert.equal(res.statusCode, 200);
let object = res.body;
// test that it is actual factorio locale, copied from getFactorioLocale.spec.js
assert.equal(typeof object, "object");
assert.equal(object["entity-name"]["fish"], "Fish");
assert.equal(object["entity-name"]["small-lamp"], "Lamp");
Object.keys(object).forEach(key => {
// first level of the nested object is always an object
assert.equal(typeof object[key], "object");
Object.keys(object[key]).forEach(key2 => {
// second level of the nested object is always a string, nearly always truthy
if(key2 != "so-long-and-thanks-for-all-the-fish"){
assert.ok(object[key][key2]);
assert.equal(typeof object[key][key2], "string");
}
});
});
done();
});
});
});
// describe("#GET /api/")
parallel("#GET static website data", function() {
this.timeout(6000);
it("sends some HTML when accessing /", function(done){
this.timeout(6000);
request(app).get("/").end(function(err,res){
assert.equal(res.statusCode, 200);
validateHTML(res.text).then(result => {
assert(result.messages.length === 1, "there are HTML errors on the page, please fix: "+JSON.stringify(result.messages));
done();
});
});
});
it("sends some HTML when accessing /nodes",function(done){
this.timeout(6000);
request(app).get("/nodes").end(function(err,res){
assert.equal(res.statusCode, 200);
validateHTML(res.text).then(result => {
assert(result.messages.length === 1, "there are HTML errors on the page, please fix: "+JSON.stringify(result.messages));
done();
});
});
});
it("sends some HTML when accessing /settings",function(done){
this.timeout(6000);
request(app).get("/settings").end(function(err,res){
assert.equal(res.statusCode, 200);
validateHTML(res.text).then(result => {
assert(result.messages.length === 1, "there are HTML errors on the page, please fix: "+JSON.stringify(result.messages));
done();
});
});
});
it("sends some HTML when accessing /nodeDetails",function(done){
this.timeout(6000);
request(app).get("/nodeDetails").end(function(err,res){
assert.equal(res.statusCode, 200);
validateHTML(res.text).then(result => {
assert(result.messages.length === 1, "there are HTML errors on the page, please fix: "+JSON.stringify(result.messages));
done();
});
});
});
it("sends some HTML when accessing /remoteMap",function(done){
this.timeout(6000);
request(app).get("/remoteMap").end(function(err,res){
assert.equal(res.statusCode, 200);
validateHTML(res.text).then(result => {
// there should be 1 error regarding complaining about me using ES6 modules before they are fully supported
assert(result.messages.length === 2, "there are HTML errors on the page, please fix: "+JSON.stringify(result.messages));
done();
});
});
});
});
let persistentMaster = request(app);
describe("#POST /api/place", function(){
it("adds an itemStack to the masters inventory", function(done){
persistentMaster.post("/api/place")
.send({
name:"steel-plate",
count:20,
instanceName:"unitTest"
})
.end(function(err,res){
assert(!err)
assert.equal(res.text, "success", "something went wrong with the request")
done();
});
});
});
describe("#GET /api/inventory", function(){
it("returns the masters current inventory", function(done){
persistentMaster.get("/api/inventory").end(function(err,res){
assert.equal(res.statusCode, 200);
let inventory = JSON.parse(res.text);
assert.equal(typeof inventory, "object", "Inventory should be an object");
assert(inventory.length >= 1, "There should be at least 1 entry in the inventory");
let contains20SteelPlate = false;
inventory.forEach(itemStack => {
if(itemStack.name == "steel-plate" && itemStack.count >= 20) contains20SteelPlate = true;
});
assert(contains20SteelPlate, "Please ensure there are at least 20 steel plate in the inventory")
done();
});
});
});
parallel("#POST /api/remove", function(){
it("returns an itemStack of how many items were removed", function(done){
persistentMaster.post("/api/remove")
.send({
name:"steel-plate",
count:10
})
.end(function(err,res){
assert(!err);
assert.equal(res.body.count, 10, "Something is wrong with the response, maybe the format changed slightly or you didn't have enough steel?");
done();
});
});
it("returns an empty itemStack if you try to request addItem or removeItem", function(done){
let casesRan = 0;
persistentMaster.post("/api/remove")
.send({
name:"addItem",
count:10
})
.end(function(err,res){
assert(!err);
assert.equal(res.text, '{"name":"addItem","count":0}', "When there are none of the item, you should get 0 back. addItem and removeItem should always return 0");
isDone();
});
persistentMaster.post("/api/remove")
.send({
name:"removeItem",
count:10
})
.end(function(err,res){
assert(!err);
assert.equal(res.text, '{"name":"removeItem","count":0}', "When there are none of the item, you should get 0 back. addItem and removeItem should always return 0");
isDone();
});
function isDone(){
if(++casesRan == 2) done();
}
});
it("returns an empty itemStack if you don't have any of the item you request", function(done){
persistentMaster.post("/api/remove")
.send({
name:"imaginaryItem",
count:999999
})
.end(function(err,res){
assert(!err);//{"name":"imaginaryItem","count":0}
assert.equal(res.body.name, "imaginaryItem", "Make sure body.name is the item we asked for");
assert.equal(res.body.count, 0, "Count should be 0 since we are asking for something that does not exist anywhere");
assert.equal(Object.keys(res.body).length, 2, "name and count should be the only keys on this object");
done();
});
});
});
});