-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
tester.js
360 lines (276 loc) · 8.66 KB
/
tester.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
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
require('total4');
const Path = require('path');
const Fs = require('fs');
const TestAll = process.mainModule.exports === exports;
// Loggers
const logSuccess = function(test) {
const tester = test.tester || test.__tester__;
tester.stats.total++;
tester.stats.ok++;
const name = test.__description__;
const duration = '(' + (new Date() - test.beg) + ' ms)';
console.log('[OK]', test.__name__, (name ? '- ' + name : ''), duration);
delete test.__tester__;
delete test.__name__;
};
const logFailed = function(test) {
const tester = test.tester || test.__tester__;
tester.stats.total++;
tester.stats.failed++;
const name = test.__description__;
const duration = '(' + (new Date() - test.beg) + ' ms)';
console.log('[FAILED]', test.__name__, (name ? '- ' + name : ''), duration);
delete test.__tester__;
delete test.__name__;
};
// Tester
const tester = {};
tester.path = './components';
tester.timeout = 5 * 1000;
tester.inputTests = {};
tester.tests = {};
tester.stats = { total: 0, ok: 0, failed: 0 };
tester.output = function(msg) {
const test = this.tests[msg.fromid];
const inputTest = tester.inputTests[msg.fromid + '_' + msg.__input__];
// Global message catch
test.output && test.output(msg);
test.message && test.message(msg);
// Input with callback
if (inputTest) {
inputTest.output && inputTest.output(msg);
inputTest.message && inputTest.message(msg);
inputTest.resolve(msg);
inputTest.handler && inputTest.handler(msg);
}
};
tester.finish = tester.done = function(message) {
setTimeout(() => {
const duration = new Date() - tester.beg;
var div = '|--------------------------------------|';
if (message) {
console.log(div);
console.log(message);
}
console.log(div);
console.log('| Total |', tester.stats.total.padLeft(15, ' '), '|');
console.log('| Success |', tester.stats.ok.padLeft(15, ' '), '|');
console.log('| Failed |', tester.stats.failed.padLeft(15, ' '), '|');
console.log('| Duration |', (duration + ' ms').padLeft(15, ' '), '|');
console.log(div);
tester.stop();
}, 5);
};
tester.end = tester.throw = tester.fail = function(message) {
console.log('[NO]' + (message ? ' - ' + message : ''));
this.stop();
};
tester.stop = function() {
this.timeoutid && clearTimeout(this.timeoutid);
this.flowstream && this.flowstream.destroy();
this.flowstream = null;
this.timeoutid = null;
process.exit(this.stats.failed > 0);
};
// this is the 'describe' function in require('../tester')(async function(describe, done) {
tester.test = function(name, callback) {
let testName = name;
// Optional name parameter
if (typeof (name) === 'function') {
testName = U.getName(process.mainModule.filename).replace(/\.js$/, '');
callback = name;
}
const componentPath = Path.join(__dirname, tester.path, testName + '.html');
let testPromiseResolver;
const testPromise = new Promise(resolve => testPromiseResolver = resolve);
Fs.readFile(componentPath, 'utf8', (err, data) => {
if (err) {
console.error(err);
return;
}
const flow = tester.flowstream;
// Connection Ids
const componentConnectionId = 'com_' + testName + '_' + UID();
const dummyConnectionId = 'com_DUMMY_' + UID();
tester.dummyConnection = [{ id: dummyConnectionId, index: 'input' }];
// Register DUMMY component
flow.register('DUMMY', instance => {
instance.inputs = [{ id: 'input', name: 'Input' }];
instance.message = function(msg) {
msg.__tester__ = tester;
msg.__input__ = instance.__input__;
tester.output(msg);
};
});
// Add DUMMY component to flowstream
flow.use(`{
"${dummyConnectionId}": {
"component": "DUMMY",
"connections": {}
}
}`, function() {
// Add component to flowstream
var comp = flow.add(testName, data);
let conns = {};
let is;
// add connection from every output to DUMMY compponent
(comp.outputs || []).forEach(function(c){
is = true;
return conns[c.id] = tester.dummyConnection;
});
if (!is)
conns = { output: tester.dummyConnection };
const componentConnection = { [componentConnectionId]: { component: testName, connections: conns } };
// Connect component to DUMMY component
flow.insert(componentConnection, function() {
if (callback) {
const id = componentConnectionId;
const instance = flow.meta.flow[id];
const testInstance = flow.meta.flow[dummyConnectionId];
const test = {};
test.input = function(inputIndex, data, handler) {
// Optional input index
if (typeof (inputIndex) !== 'string') {
inputIndex = data;
data = handler;
}
// Tell DUMMY component, that next incoming message is from "inputIndex"
testInstance.__input__ = inputIndex;
const inputTest = {};
inputTest.handler = handler;
const promise = new Promise(resolve => {
inputTest.resolve = resolve;
});
tester.inputTests[id + '_' + inputIndex] = inputTest;
// Send data to component
flow.trigger(id + '__' + inputIndex, data);
return promise;
};
// Handlers
test.ok = function(value, description) {
if (typeof value === 'string')
description = value;
this.__name__ = instance.module.name;
this.__description__ = description;
if (typeof (value) === 'undefined') {
logSuccess(this);
return;
}
if (value)
logSuccess(this);
else
logFailed(this);
delete this.__description__;
};
test.fail = function(value, description) {
if (typeof value === 'string')
description = value;
this.__name__ = instance.module.name;
this.__description__ = description;
if (typeof (value) === 'undefined') {
logFailed(this);
return;
}
if (value)
logFailed(this);
else
logSuccess(this);
delete this.__description__;
};
test.tester = tester; // Reference to main tester instance
test.default_config = instance.config;
test.config = instance.config;
test.instance = instance;
test.beg = new Date();
// Change config of component
test.configure = test.reconfigure = function(properties, withoutConfigure) {
instance.config = test.default_config;
for (let key in properties)
instance.config[key] = properties[key];
!withoutConfigure && instance.configure && instance.configure();
};
// Trigger
test.trigger = function(data) {
instance.trigger(data);
};
tester.tests[id] = test;
callback(test, testPromiseResolver);
instance.onerror = test.onerror;
instance.onError = test.onError;
}
});
});
});
return testPromise;
};
module.exports = function(callback) {
const flow = tester.flowstream = FLOWSTREAM('Test');
flow.onstatus = function(status) {
const test = tester.tests[this.id];
if (!test)
return;
test.status && test.status(status);
test.current_status = status;
};
flow.ondashboard = function(status) {
const test = tester.tests[this.id];
test.dashboard && test.dashboard(status);
test.current_dashboard = status;
};
flow.onerror = function(a, b, c, d) {
const test = tester.tests[this.id];
test.onError && test.onError(a, b, c, d);
test.onerror && test.onerror(a, b, c, d);
};
flow.onconnect = function(instance) {
instance.httproute = NOOP;
instance.save = function() {
if (instance.outputs && instance.outputs.length) {
instance.connections = {};
instance.outputs.forEach(function(out){
instance.connections[out.id] = tester.dummyConnection;
});
}
};
instance.io = NOOP;
instance.toinput = NOOP;
instance.output = NOOP;
instance.newvariables = function(vars) {
instance.variables && instance.variables(vars);
};
instance.reconfigure = function(conf){
for (let key in conf)
instance.config[key] = conf[key];
instance.configure && instance.configure();
};
};
tester.beg = new Date();
callback.call(tester, tester.test, tester.done);
// Start "Timeout" timer
tester.timeoutid = setTimeout(() => {
tester.done();
}, tester.timeout);
};
if (TestAll) {
// Run all test scripts
F.Fs.readdir('tests', function(err, files) {
tester.beg = Date.now();
files.wait(function(file, next) {
if (U.getExtension(file) === 'js') {
tester.stats.total++;
var now = Date.now();
var child = F.Child.fork('tests/' + file, { detached: true, silent: true });
child.on('exit', function(code) {
if (code)
tester.stats.failed++;
else
tester.stats.ok++;
var ms = Date.now() - now;
console.log('[{0}] '.format(code ? 'FAILED' : 'OK') + file + ' ({0} s)'.format((ms / 1000).toFixed(2)));
next();
});
} else
next();
}, tester.done);
});
}