1 /* tslint:disable:no-unused-expression */
3 import * as chai from 'chai'
5 import { checkVideoFilesWereRemoved, doubleFollow, flushAndRunMultipleServers, removeVideo, uploadVideo, wait } from '../../utils'
6 import { flushTests, killallServers, ServerInfo, setAccessTokensToServers } from '../../utils/index'
7 import { waitJobs } from '../../utils/server/jobs'
8 import { createVideoCaption, deleteVideoCaption, listVideoCaptions, testCaptionFile } from '../../utils/videos/video-captions'
9 import { VideoCaption } from '../../../../shared/models/videos/video-caption.model'
11 const expect = chai.expect
13 describe('Test video captions', function () {
14 let servers: ServerInfo[]
17 before(async function () {
22 servers = await flushAndRunMultipleServers(2)
24 await setAccessTokensToServers(servers)
25 await doubleFollow(servers[0], servers[1])
27 await waitJobs(servers)
29 const res = await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, { name: 'my video name' })
30 videoUUID = res.body.video.uuid
32 await waitJobs(servers)
35 it('Should list the captions and return an empty list', async function () {
36 for (const server of servers) {
37 const res = await listVideoCaptions(server.url, videoUUID)
38 expect(res.body.total).to.equal(0)
39 expect(res.body.data).to.have.lengthOf(0)
43 it('Should create two new captions', async function () {
46 await createVideoCaption({
48 accessToken: servers[0].accessToken,
51 fixture: 'subtitle-good1.vtt'
54 await createVideoCaption({
56 accessToken: servers[0].accessToken,
59 fixture: 'subtitle-good2.vtt'
62 await waitJobs(servers)
65 it('Should list these uploaded captions', async function () {
66 for (const server of servers) {
67 const res = await listVideoCaptions(server.url, videoUUID)
68 expect(res.body.total).to.equal(2)
69 expect(res.body.data).to.have.lengthOf(2)
71 const caption1: VideoCaption = res.body.data[0]
72 expect(caption1.language.id).to.equal('ar')
73 expect(caption1.language.label).to.equal('Arabic')
74 expect(caption1.captionPath).to.equal('/static/video-captions/' + videoUUID + '-ar.vtt')
75 await testCaptionFile(server.url, caption1.captionPath, 'Subtitle good 1.')
77 const caption2: VideoCaption = res.body.data[1]
78 expect(caption2.language.id).to.equal('zh')
79 expect(caption2.language.label).to.equal('Chinese')
80 expect(caption2.captionPath).to.equal('/static/video-captions/' + videoUUID + '-zh.vtt')
81 await testCaptionFile(server.url, caption2.captionPath, 'Subtitle good 2.')
85 it('Should replace an existing caption', async function () {
88 await createVideoCaption({
90 accessToken: servers[0].accessToken,
93 fixture: 'subtitle-good2.vtt'
96 await waitJobs(servers)
99 it('Should have this caption updated', async function () {
100 for (const server of servers) {
101 const res = await listVideoCaptions(server.url, videoUUID)
102 expect(res.body.total).to.equal(2)
103 expect(res.body.data).to.have.lengthOf(2)
105 const caption1: VideoCaption = res.body.data[0]
106 expect(caption1.language.id).to.equal('ar')
107 expect(caption1.language.label).to.equal('Arabic')
108 expect(caption1.captionPath).to.equal('/static/video-captions/' + videoUUID + '-ar.vtt')
109 await testCaptionFile(server.url, caption1.captionPath, 'Subtitle good 2.')
113 it('Should replace an existing caption with a srt file and convert it', async function () {
116 await createVideoCaption({
118 accessToken: servers[0].accessToken,
121 fixture: 'subtitle-good.srt'
124 await waitJobs(servers)
126 // Cache invalidation
130 it('Should have this caption updated and converted', async function () {
131 for (const server of servers) {
132 const res = await listVideoCaptions(server.url, videoUUID)
133 expect(res.body.total).to.equal(2)
134 expect(res.body.data).to.have.lengthOf(2)
136 const caption1: VideoCaption = res.body.data[0]
137 expect(caption1.language.id).to.equal('ar')
138 expect(caption1.language.label).to.equal('Arabic')
139 expect(caption1.captionPath).to.equal('/static/video-captions/' + videoUUID + '-ar.vtt')
141 const expected = 'WEBVTT FILE\r\n' +
144 '00:00:01.600 --> 00:00:04.200\r\n' +
148 '00:00:05.900 --> 00:00:07.999\r\n' +
149 'This is a subtitle in American English\r\n' +
152 '00:00:10.000 --> 00:00:14.000\r\n' +
153 'Adding subtitles is very easy to do\r\n'
154 await testCaptionFile(server.url, caption1.captionPath, expected)
158 it('Should remove one caption', async function () {
161 await deleteVideoCaption(servers[0].url, servers[0].accessToken, videoUUID, 'ar')
163 await waitJobs(servers)
166 it('Should only list the caption that was not deleted', async function () {
167 for (const server of servers) {
168 const res = await listVideoCaptions(server.url, videoUUID)
169 expect(res.body.total).to.equal(1)
170 expect(res.body.data).to.have.lengthOf(1)
172 const caption: VideoCaption = res.body.data[0]
174 expect(caption.language.id).to.equal('zh')
175 expect(caption.language.label).to.equal('Chinese')
176 expect(caption.captionPath).to.equal('/static/video-captions/' + videoUUID + '-zh.vtt')
177 await testCaptionFile(server.url, caption.captionPath, 'Subtitle good 2.')
181 it('Should remove the video, and thus all video captions', async function () {
182 await removeVideo(servers[0].url, servers[0].accessToken, videoUUID)
184 await checkVideoFilesWereRemoved(videoUUID, 1)
187 after(async function () {
188 killallServers(servers)