highlightRoutes.test.js
3.7 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
const assert = require('assert');
const http = require('http');
const { highlightRecordingService } = require('../services/highlightRecordingService');
const app = require('../app');
function request(server, method, route, body) {
return new Promise((resolve, reject) => {
const address = server.address();
const payload = body == null ? '' : JSON.stringify(body);
const req = http.request({
host: '127.0.0.1',
port: address.port,
path: route,
method,
headers: payload ? {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(payload)
} : {}
}, response => {
let data = '';
response.setEncoding('utf8');
response.on('data', chunk => { data += chunk; });
response.on('end', () => {
resolve({ status: response.statusCode, body: JSON.parse(data) });
});
});
req.on('error', reject);
if (payload) req.write(payload);
req.end();
});
}
async function run() {
const server = http.createServer(app);
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
try {
const status = await request(server, 'GET', '/highlight/status');
assert.strictEqual(status.status, 200);
assert.deepStrictEqual(status.body.data, { queued: 0, recording: 0, knownTasks: 0 });
const invalidFiles = await request(server, 'POST', '/highlight/files', { items: [] });
assert.strictEqual(invalidFiles.status, 400);
assert.strictEqual(invalidFiles.body.code, 10);
const invalidPreview = await request(server, 'POST', '/highlight/preview/by-class', {});
assert.strictEqual(invalidPreview.status, 400);
assert.strictEqual(invalidPreview.body.code, 10);
const invalidClassFiles = await request(server, 'POST', '/highlight/fileExists', {});
assert.strictEqual(invalidClassFiles.status, 400);
assert.strictEqual(invalidClassFiles.body.code, 10);
const originalGetClassFileStatuses = highlightRecordingService.getClassFileStatuses;
highlightRecordingService.getClassFileStatuses = async () => [
{ highlightId: 4, classId: '1001', siteId: 'xdyui2', generated: true, status: 'generated', url: 'https://example/4.mp4' },
{ highlightId: 5, classId: '1001', siteId: 'xdyui2', generated: true, status: 'generated', url: 'https://example/5.mp4' }
];
const generatedFiles = await request(server, 'POST', '/highlight/fileExists', { siteId: 'xdyui2', classId: '1001' });
assert.strictEqual(generatedFiles.body.code, 0);
assert.strictEqual(generatedFiles.body.onlyHighlight, 1);
assert.strictEqual(generatedFiles.body.classUrlList.length, 2);
highlightRecordingService.getClassFileStatuses = async () => [
{ highlightId: 4, classId: '1001', siteId: 'xdyui2', generated: true, status: 'generated', url: 'https://example/4.mp4' },
{ highlightId: 5, classId: '1001', siteId: 'xdyui2', generated: false, status: 'uploading', url: null }
];
const partialFiles = await request(server, 'POST', '/highlight/fileExists', { siteId: 'xdyui2', classId: '1001' });
assert.strictEqual(partialFiles.body.code, 1);
assert.strictEqual(partialFiles.body.message, '部分文件未生成');
highlightRecordingService.getClassFileStatuses = originalGetClassFileStatuses;
} finally {
await new Promise(resolve => server.close(resolve));
}
console.log('highlight routes tests passed');
}
run().catch(error => {
console.error(error);
process.exitCode = 1;
});