1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
6 checkVideoFilesWereRemoved,
9 flushAndRunMultipleServers,
12 setAccessTokensToServers,
17 } from '@shared/extra-utils'
19 const expect = chai.expect
21 describe('Test video captions', function () {
22 const uuidRegex = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'
24 let servers: ServerInfo[]
27 before(async function () {
30 servers = await flushAndRunMultipleServers(2)
32 await setAccessTokensToServers(servers)
33 await doubleFollow(servers[0], servers[1])
35 await waitJobs(servers)
37 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'my video name' })
38 videoUUID = res.body.video.uuid
40 await waitJobs(servers)
43 it('Should list the captions and return an empty list', async function () {
44 for (const server of servers) {
45 const body = await server.captionsCommand.listVideoCaptions({ videoId: videoUUID })
46 expect(body.total).to.equal(0)
47 expect(body.data).to.have.lengthOf(0)
51 it('Should create two new captions', async function () {
54 await servers[0].captionsCommand.createVideoCaption({
57 fixture: 'subtitle-good1.vtt'
60 await servers[0].captionsCommand.createVideoCaption({
63 fixture: 'subtitle-good2.vtt',
64 mimeType: 'application/octet-stream'
67 await waitJobs(servers)
70 it('Should list these uploaded captions', async function () {
71 for (const server of servers) {
72 const body = await server.captionsCommand.listVideoCaptions({ videoId: videoUUID })
73 expect(body.total).to.equal(2)
74 expect(body.data).to.have.lengthOf(2)
76 const caption1 = body.data[0]
77 expect(caption1.language.id).to.equal('ar')
78 expect(caption1.language.label).to.equal('Arabic')
79 expect(caption1.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-ar.vtt$'))
80 await testCaptionFile(server.url, caption1.captionPath, 'Subtitle good 1.')
82 const caption2 = body.data[1]
83 expect(caption2.language.id).to.equal('zh')
84 expect(caption2.language.label).to.equal('Chinese')
85 expect(caption2.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-zh.vtt$'))
86 await testCaptionFile(server.url, caption2.captionPath, 'Subtitle good 2.')
90 it('Should replace an existing caption', async function () {
93 await servers[0].captionsCommand.createVideoCaption({
96 fixture: 'subtitle-good2.vtt'
99 await waitJobs(servers)
102 it('Should have this caption updated', async function () {
103 for (const server of servers) {
104 const body = await server.captionsCommand.listVideoCaptions({ videoId: videoUUID })
105 expect(body.total).to.equal(2)
106 expect(body.data).to.have.lengthOf(2)
108 const caption1 = body.data[0]
109 expect(caption1.language.id).to.equal('ar')
110 expect(caption1.language.label).to.equal('Arabic')
111 expect(caption1.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-ar.vtt$'))
112 await testCaptionFile(server.url, caption1.captionPath, 'Subtitle good 2.')
116 it('Should replace an existing caption with a srt file and convert it', async function () {
119 await servers[0].captionsCommand.createVideoCaption({
122 fixture: 'subtitle-good.srt'
125 await waitJobs(servers)
127 // Cache invalidation
131 it('Should have this caption updated and converted', async function () {
132 for (const server of servers) {
133 const body = await server.captionsCommand.listVideoCaptions({ videoId: videoUUID })
134 expect(body.total).to.equal(2)
135 expect(body.data).to.have.lengthOf(2)
137 const caption1 = body.data[0]
138 expect(caption1.language.id).to.equal('ar')
139 expect(caption1.language.label).to.equal('Arabic')
140 expect(caption1.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-ar.vtt$'))
142 const expected = 'WEBVTT FILE\r\n' +
145 '00:00:01.600 --> 00:00:04.200\r\n' +
149 '00:00:05.900 --> 00:00:07.999\r\n' +
150 'This is a subtitle in American English\r\n' +
153 '00:00:10.000 --> 00:00:14.000\r\n' +
154 'Adding subtitles is very easy to do\r\n'
155 await testCaptionFile(server.url, caption1.captionPath, expected)
159 it('Should remove one caption', async function () {
162 await servers[0].captionsCommand.deleteVideoCaption({ videoId: videoUUID, language: 'ar' })
164 await waitJobs(servers)
167 it('Should only list the caption that was not deleted', async function () {
168 for (const server of servers) {
169 const body = await server.captionsCommand.listVideoCaptions({ videoId: videoUUID })
170 expect(body.total).to.equal(1)
171 expect(body.data).to.have.lengthOf(1)
173 const caption = body.data[0]
175 expect(caption.language.id).to.equal('zh')
176 expect(caption.language.label).to.equal('Chinese')
177 expect(caption.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-zh.vtt$'))
178 await testCaptionFile(server.url, caption.captionPath, 'Subtitle good 2.')
182 it('Should remove the video, and thus all video captions', async function () {
183 await removeVideo(servers[0].url, servers[0].accessToken, videoUUID)
185 await checkVideoFilesWereRemoved(videoUUID, 1)
188 after(async function () {
189 await cleanupTests(servers)