-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfcctesting.cjs
103 lines (96 loc) · 2.61 KB
/
fcctesting.cjs
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
/*
*
*
*
*
*
*
*
*
*
*
*
* DO NOT EDIT THIS FILE
* For FCC testing purposes!
*
*
*
*
*
*
*
*
*
*
*
*/
'use strict';
const cors = require('cors');
const fs = require('fs');
module.exports = async function (app) {
const run = await import('./test-runner.js')
const runner = run.default
app.route('/_api/server.js')
.get(function (req, res, next) {
console.log('requested');
fs.readFile(__dirname + '/server.js', function (err, data) {
if (err) return next(err);
res.send(data.toString());
});
});
app.route('/_api/routes/api.js')
.get(function (req, res, next) {
console.log('requested');
fs.readFile(__dirname + '/routes/api.js', function (err, data) {
if (err) return next(err);
res.type('txt').send(data.toString());
});
});
app.route('/_api/controllers/convertHandler.js')
.get(function (req, res, next) {
console.log('requested');
fs.readFile(__dirname + '/controllers/convertHandler.js', function (err, data) {
if (err) return next(err);
res.type('txt').send(data.toString());
});
});
app.get('/_api/get-tests', cors(), function (req, res, next) {
console.log('requested');
if (process.env.NODE_ENV === 'test') return next();
res.json({ status: 'unavailable' });
},
function (req, res, next) {
if (!runner.report) return next();
res.json(testFilter(runner.report, req.query.type, req.query.n));
},
function (req, res) {
runner.on('done', function (report) {
process.nextTick(() => res.json(testFilter(runner.report, req.query.type, req.query.n)));
});
});
app.get('/_api/app-info', function (req, res) {
let hs = Object.keys(res._headers)
.filter(h => !h.match(/^access-control-\w+/));
let hObj = {};
hs.forEach(h => { hObj[h] = res._headers[h] });
delete res._headers['strict-transport-security'];
res.json({ headers: hObj });
});
};
function testFilter(tests, type, n) {
let out;
switch (type) {
case 'unit':
out = tests.filter(t => t.context.match('Unit Tests'));
break;
case 'functional':
out = tests.filter(t => t.context.match('Functional Tests') && !t.title.match('#example'));
break;
default:
out = tests;
}
if (n !== undefined) {
return out[n] || out;
}
return out;
}