highlightRecordingService.test.js
9.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
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
const assert = require('assert');
const fs = require('fs');
const os = require('os');
const path = require('path');
const {
HighlightRecordingService,
buildFileInfo,
buildTaskKey,
formatShanghaiDate,
getPreviousShanghaiDayRange,
md5,
normalizeFileLookup,
normalizeHighlight,
normalizeOutputNamespace
} = require('../services/highlightRecordingService');
async function run() {
assert.strictEqual(md5('14867586201784628401782'), 'a66749831542e5e090e41d72f455918d');
const shanghaiMidnight = Date.UTC(2026, 0, 1, 16, 0, 0, 0);
assert.strictEqual(formatShanghaiDate(shanghaiMidnight), '20260102');
assert.deepStrictEqual(getPreviousShanghaiDayRange(Date.UTC(2026, 7, 6, 0, 0, 0)), {
beginTime: Date.UTC(2026, 7, 4, 16, 0, 0),
endTime: Date.UTC(2026, 7, 5, 15, 59, 59, 999)
});
const item = normalizeHighlight({
id: 4,
meetingNumber: '1486758620',
siteId: 'doctest',
beginTime: 1779159793000,
endTime: 1779159893000,
type: 0,
more: 'ext'
}, 3600000);
assert.strictEqual(item.highlightId, 4);
assert.strictEqual(item.classId, '1486758620');
assert.strictEqual(buildTaskKey(item), 'doctest:4');
assert.throws(() => normalizeHighlight({
id: 4,
meetingNumber: '1486758620',
siteId: '../bad',
beginTime: 1779159793000,
endTime: 1779159893000
}, 3600000), /siteId 无效/);
assert.throws(() => normalizeHighlight({
id: 4,
meetingNumber: '1486758620',
siteId: 'doctest',
beginTime: 1779159893000,
endTime: 1779159793000
}, 3600000), /endTime 必须大于 beginTime/);
assert.deepStrictEqual(normalizeFileLookup({
highlightId: 4,
classId: '1486758620',
siteId: 'doctest',
beginTime: 1779159793000
}), {
highlightId: 4,
classId: '1486758620',
siteId: 'doctest',
beginTime: 1779159793000
});
const fileInfo = buildFileInfo(item, '/capture', 'https://xdymp4.xuedianyun.com/');
assert.strictEqual(fileInfo.fileName, '1486758620_highlight_4.mp4');
assert.strictEqual(fileInfo.localPath, path.join('/capture', 'media', 'doctest', '20260519', fileInfo.fileName));
assert.strictEqual(fileInfo.ossKey, 'oss/doctest/20260519/1486758620_highlight_4.mp4');
assert.strictEqual(fileInfo.url, `https://xdymp4.xuedianyun.com/${fileInfo.ossKey}`);
const validationFileInfo = buildFileInfo(item, '/capture', 'https://xdymp4.xuedianyun.com/', 'validation-20260805');
assert.strictEqual(
validationFileInfo.localPath,
path.join('/capture', 'media', 'validation-20260805', 'doctest', '20260519', validationFileInfo.fileName)
);
assert.strictEqual(
validationFileInfo.ossKey,
'oss/validation-20260805/doctest/20260519/1486758620_highlight_4.mp4'
);
assert.strictEqual(normalizeOutputNamespace('validation_1'), 'validation_1');
assert.throws(() => normalizeOutputNamespace('../validation'), /outputNamespace/);
const service = new HighlightRecordingService({ configPath: '/not-used-in-this-test' });
const playbackUrl = new URL(service.buildPlaybackUrl(item, {
url: 'https://pclive.xuedianyun.com/pcBase/pclive2/dev/index.html',
portalIP: 'saas.xuedianyun.com',
portalPort: 80,
channels: 2,
userRole: 'normal'
}));
assert.strictEqual(playbackUrl.searchParams.get('classId'), '1486758620');
assert.strictEqual(playbackUrl.searchParams.get('playRecord'), '1');
assert.strictEqual(playbackUrl.searchParams.get('recBeginTime'), '1779159793000');
assert.strictEqual(playbackUrl.searchParams.get('recEndTime'), '1779159893000');
const queueService = new HighlightRecordingService({ configPath: '/not-used-in-this-test' });
queueService.readConfig = () => ({
projectCatalog: '/capture',
highlight: {
siteIds: ['doctest'],
maxDurationMs: 3600000,
outputBaseUrl: 'https://xdymp4.xuedianyun.com',
taskRetentionMs: 86400000,
maxConcurrent: 1
}
});
queueService.inspectFile = async () => ({ local: false, oss: true, generated: true });
const firstResult = await queueService.enqueue([item]);
assert.strictEqual(firstResult[0].status, 'generated');
const changedResult = await queueService.enqueue([Object.assign({}, item, { beginTime: item.beginTime + 1000 })]);
assert.strictEqual(changedResult[0].status, 'invalid');
assert.match(changedResult[0].error, /时间段发生变化/);
const previewService = new HighlightRecordingService({ configPath: '/not-used-in-this-test' });
previewService.readConfig = () => ({
projectCatalog: '/capture',
playback: {
url: 'https://pclive.xuedianyun.com/pcBase/pclive2/dev/index.html',
portalIP: 'saas.xuedianyun.com',
portalPort: 80,
channels: 2
},
highlight: {
siteIds: ['doctest'],
maxDurationMs: 3600000,
outputBaseUrl: 'https://xdymp4.xuedianyun.com',
outputNamespace: 'validation-20260805'
}
});
previewService.inspectFile = async () => ({ local: false, oss: false, generated: false });
const preview = await previewService.preview([item]);
assert.strictEqual(preview[0].status, 'not_generated');
assert.strictEqual(preview[0].duration, 100000);
assert.match(preview[0].ossKey, /^oss\/validation-20260805\//);
assert.strictEqual(new URL(preview[0].playbackUrl).searchParams.get('recBeginTime'), '1779159793000');
assert.strictEqual(previewService.getQueueInfo().queued, 0);
const scheduledSiteService = new HighlightRecordingService({ configPath: '/not-used-in-this-test' });
scheduledSiteService.readConfig = () => ({
highlight: {
enabled: true,
sourceMode: 'site',
siteIds: ['xdyui2'],
maxConcurrent: 1,
taskRetentionMs: 86400000
}
});
let siteQuery;
scheduledSiteService.fetchBySite = async params => {
siteQuery = params;
return [];
};
scheduledSiteService.enqueue = async records => records;
const scheduledSite = await scheduledSiteService.runScheduledRecording(Date.UTC(2026, 7, 6, 0, 0, 0));
assert.strictEqual(scheduledSite.mode, 'site');
assert.strictEqual(siteQuery.siteId, 'xdyui2');
assert.strictEqual(scheduledSite.received, 0);
const scheduledTaskService = new HighlightRecordingService({ configPath: '/not-used-in-this-test' });
scheduledTaskService.readConfig = () => ({
highlight: {
enabled: true,
sourceMode: 'task',
siteIds: ['xdyui2'],
maxConcurrent: 1,
taskRetentionMs: 86400000
}
});
scheduledTaskService.fetchRecordingTasks = async () => [
{ meetingNumber: '1001', siteId: 'xdyui2', status: 0, onlyHighlight: 1 },
{ meetingNumber: '1002', siteId: 'xdyui2', status: 1, onlyHighlight: 1 },
{ meetingNumber: '1003', siteId: 'xdyui2', status: 0, onlyHighlight: 0 },
{ meetingNumber: '1004', siteId: 'other', status: 0, onlyHighlight: 1 }
];
const fetchedClasses = [];
scheduledTaskService.fetchByClass = async classId => {
fetchedClasses.push(classId);
return [Object.assign({}, item, { meetingNumber: classId, siteId: 'xdyui2' })];
};
scheduledTaskService.enqueue = async records => records.map(record => ({ status: 'queued', record }));
const scheduledTask = await scheduledTaskService.runScheduledRecording();
assert.deepStrictEqual(fetchedClasses, ['1001']);
assert.strictEqual(scheduledTask.sourceTasks, 4);
assert.strictEqual(scheduledTask.pendingTasks, 1);
assert.strictEqual(scheduledTask.queued, 1);
const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'webscreen-highlight-'));
try {
const captureService = new HighlightRecordingService({ configPath: '/not-used-in-this-test' });
captureService.readConfig = () => ({
projectCatalog: tempRoot,
captureCatalog: '/capture',
playback: { url: 'https://example.com/index.html' },
highlight: {
enabled: true,
siteIds: ['doctest'],
maxDurationMs: 3600000,
outputBaseUrl: 'https://xdymp4.xuedianyun.com',
outputNamespace: '',
taskRetentionMs: 86400000,
maxConcurrent: 1,
loadGraceMs: 100,
endGraceMs: 100
}
});
captureService.inspectFile = async fileInfo => ({
local: fs.existsSync(fileInfo.localPath),
oss: false,
generated: false
});
captureService.runCapture = async task => {
fs.writeFileSync(task.fileInfo.tempPath, 'test-mp4');
};
await captureService.enqueue([item]);
for (let attempt = 0; attempt < 100 && captureService.getQueueInfo().recording > 0; attempt += 1) {
await new Promise(resolve => setTimeout(resolve, 10));
}
const capturedFile = buildFileInfo(item, tempRoot, 'https://xdymp4.xuedianyun.com');
assert.strictEqual(fs.readFileSync(capturedFile.localPath, 'utf8'), 'test-mp4');
assert.ok(fs.existsSync(path.join(capturedFile.localDir, 'download.json')));
assert.strictEqual(fs.existsSync(capturedFile.tempPath), false);
} finally {
fs.rmSync(tempRoot, { recursive: true, force: true });
}
console.log('highlightRecordingService tests passed');
}
run().catch(error => {
console.error(error);
process.exitCode = 1;
});