highlightRoutes.test.js
6.6 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
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
const assert = require('assert');
const http = require('http');
const {
HighlightValidationError,
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 originalGetFileStatusesForDisabled = highlightRecordingService.getFileStatuses;
try {
highlightRecordingService.getFileStatuses = async () => {
throw new HighlightValidationError('高光录制功能未启用');
};
const disabledFiles = await request(server, 'POST', '/highlight/files', {
items: [{ highlightId: 5, classId: '1001', siteId: 'xdyui2', beginTime: 1785895298000 }]
});
assert.strictEqual(disabledFiles.status, 400);
assert.strictEqual(disabledFiles.body.code, 10);
assert.strictEqual(disabledFiles.body.message, '高光录制功能未启用');
} finally {
highlightRecordingService.getFileStatuses = originalGetFileStatusesForDisabled;
}
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 originalRunScheduledRecording = highlightRecordingService.runScheduledRecording;
try {
highlightRecordingService.runScheduledRecording = async () => {
throw new HighlightValidationError('高光录制功能未启用');
};
const disabledScheduled = await request(server, 'POST', '/highlight/recording/scheduled');
assert.strictEqual(disabledScheduled.status, 400);
assert.strictEqual(disabledScheduled.body.code, 10);
assert.strictEqual(disabledScheduled.body.message, '高光录制功能未启用');
highlightRecordingService.runScheduledRecording = async () => ({
mode: 'site',
beginTime: 1785945600000,
endTime: 1786031999999,
received: 2,
queued: 2,
recording: 0,
uploading: 0,
generated: 0,
invalid: 0,
failed: 0
});
const scheduled = await request(server, 'POST', '/highlight/recording/scheduled');
assert.strictEqual(scheduled.status, 200);
assert.strictEqual(scheduled.body.code, 0);
assert.strictEqual(scheduled.body.message, 'success');
assert.strictEqual(scheduled.body.data.mode, 'site');
assert.strictEqual(scheduled.body.data.received, 2);
highlightRecordingService.runScheduledRecording = async () => ({
mode: 'site',
beginTime: 1785945600000,
endTime: 1786031999999,
received: 0,
queued: 0,
recording: 0,
uploading: 0,
generated: 0,
invalid: 0,
failed: 0
});
const scheduledEmpty = await request(server, 'POST', '/highlight/recording/scheduled');
assert.strictEqual(scheduledEmpty.body.code, 0);
assert.strictEqual(scheduledEmpty.body.message, '无高光数据');
} finally {
highlightRecordingService.runScheduledRecording = originalRunScheduledRecording;
}
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;
});