1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import * as chai from 'chai'
6 checkVideoFilesWereRemoved,
9 flushAndRunMultipleServers,
13 } from '../../../../shared/extra-utils'
14 import { ServerInfo, setAccessTokensToServers } from '../../../../shared/extra-utils/index'
15 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
21 } from '../../../../shared/extra-utils/videos/video-captions'
22 import { VideoCaption } from '../../../../shared/models/videos/caption/video-caption.model'
24 const expect = chai.expect
26 describe('Test video captions', function () {
27 const uuidRegex = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'
29 let servers: ServerInfo[]
32 before(async function () {
35 servers = await flushAndRunMultipleServers(2)
37 await setAccessTokensToServers(servers)
38 await doubleFollow(servers[0], servers[1])
40 await waitJobs(servers)
42 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'my video name' })
43 videoUUID = res.body.video.uuid
45 await waitJobs(servers)
48 it('Should list the captions and return an empty list', async function () {
49 for (const server of servers) {
50 const res = await listVideoCaptions(server.url, videoUUID)
51 expect(res.body.total).to.equal(0)
52 expect(res.body.data).to.have.lengthOf(0)
56 it('Should create two new captions', async function () {
59 await createVideoCaption({
61 accessToken: servers[0].accessToken,
64 fixture: 'subtitle-good1.vtt'
67 await createVideoCaption({
69 accessToken: servers[0].accessToken,
72 fixture: 'subtitle-good2.vtt',
73 mimeType: 'application/octet-stream'
76 await waitJobs(servers)
79 it('Should list these uploaded captions', async function () {
80 for (const server of servers) {
81 const res = await listVideoCaptions(server.url, videoUUID)
82 expect(res.body.total).to.equal(2)
83 expect(res.body.data).to.have.lengthOf(2)
85 const caption1: VideoCaption = res.body.data[0]
86 expect(caption1.language.id).to.equal('ar')
87 expect(caption1.language.label).to.equal('Arabic')
88 expect(caption1.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-ar.vtt$'))
89 await testCaptionFile(server.url, caption1.captionPath, 'Subtitle good 1.')
91 const caption2: VideoCaption = res.body.data[1]
92 expect(caption2.language.id).to.equal('zh')
93 expect(caption2.language.label).to.equal('Chinese')
94 expect(caption2.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-zh.vtt$'))
95 await testCaptionFile(server.url, caption2.captionPath, 'Subtitle good 2.')
99 it('Should replace an existing caption', async function () {
102 await createVideoCaption({
104 accessToken: servers[0].accessToken,
107 fixture: 'subtitle-good2.vtt'
110 await waitJobs(servers)
113 it('Should have this caption updated', async function () {
114 for (const server of servers) {
115 const res = await listVideoCaptions(server.url, videoUUID)
116 expect(res.body.total).to.equal(2)
117 expect(res.body.data).to.have.lengthOf(2)
119 const caption1: VideoCaption = res.body.data[0]
120 expect(caption1.language.id).to.equal('ar')
121 expect(caption1.language.label).to.equal('Arabic')
122 expect(caption1.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-ar.vtt$'))
123 await testCaptionFile(server.url, caption1.captionPath, 'Subtitle good 2.')
127 it('Should replace an existing caption with a srt file and convert it', async function () {
130 await createVideoCaption({
132 accessToken: servers[0].accessToken,
135 fixture: 'subtitle-good.srt'
138 await waitJobs(servers)
140 // Cache invalidation
144 it('Should have this caption updated and converted', async function () {
145 for (const server of servers) {
146 const res = await listVideoCaptions(server.url, videoUUID)
147 expect(res.body.total).to.equal(2)
148 expect(res.body.data).to.have.lengthOf(2)
150 const caption1: VideoCaption = res.body.data[0]
151 expect(caption1.language.id).to.equal('ar')
152 expect(caption1.language.label).to.equal('Arabic')
153 expect(caption1.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-ar.vtt$'))
155 const expected = 'WEBVTT FILE\r\n' +
158 '00:00:01.600 --> 00:00:04.200\r\n' +
162 '00:00:05.900 --> 00:00:07.999\r\n' +
163 'This is a subtitle in American English\r\n' +
166 '00:00:10.000 --> 00:00:14.000\r\n' +
167 'Adding subtitles is very easy to do\r\n'
168 await testCaptionFile(server.url, caption1.captionPath, expected)
172 it('Should remove one caption', async function () {
175 await deleteVideoCaption(servers[0].url, servers[0].accessToken, videoUUID, 'ar')
177 await waitJobs(servers)
180 it('Should only list the caption that was not deleted', async function () {
181 for (const server of servers) {
182 const res = await listVideoCaptions(server.url, videoUUID)
183 expect(res.body.total).to.equal(1)
184 expect(res.body.data).to.have.lengthOf(1)
186 const caption: VideoCaption = res.body.data[0]
188 expect(caption.language.id).to.equal('zh')
189 expect(caption.language.label).to.equal('Chinese')
190 expect(caption.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-zh.vtt$'))
191 await testCaptionFile(server.url, caption.captionPath, 'Subtitle good 2.')
195 it('Should remove the video, and thus all video captions', async function () {
196 await removeVideo(servers[0].url, servers[0].accessToken, videoUUID)
198 await checkVideoFilesWereRemoved(videoUUID, 1)
201 after(async function () {
202 await cleanupTests(servers)