Shared utils -> extra-utils
[oweals/peertube.git] / server / tests / api / videos / video-captions.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import {
6   checkVideoFilesWereRemoved,
7   doubleFollow,
8   flushAndRunMultipleServers,
9   removeVideo,
10   uploadVideo,
11   wait
12 } from '../../../../shared/extra-utils'
13 import { flushTests, killallServers, ServerInfo, setAccessTokensToServers } from '../../../../shared/extra-utils/index'
14 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
15 import {
16   createVideoCaption,
17   deleteVideoCaption,
18   listVideoCaptions,
19   testCaptionFile
20 } from '../../../../shared/extra-utils/videos/video-captions'
21 import { VideoCaption } from '../../../../shared/models/videos/caption/video-caption.model'
22
23 const expect = chai.expect
24
25 describe('Test video captions', function () {
26   let servers: ServerInfo[]
27   let videoUUID: string
28
29   before(async function () {
30     this.timeout(30000)
31
32     await flushTests()
33
34     servers = await flushAndRunMultipleServers(2)
35
36     await setAccessTokensToServers(servers)
37     await doubleFollow(servers[0], servers[1])
38
39     await waitJobs(servers)
40
41     const res = await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, { name: 'my video name' })
42     videoUUID = res.body.video.uuid
43
44     await waitJobs(servers)
45   })
46
47   it('Should list the captions and return an empty list', async function () {
48     for (const server of servers) {
49       const res = await listVideoCaptions(server.url, videoUUID)
50       expect(res.body.total).to.equal(0)
51       expect(res.body.data).to.have.lengthOf(0)
52     }
53   })
54
55   it('Should create two new captions', async function () {
56     this.timeout(30000)
57
58     await createVideoCaption({
59       url: servers[0].url,
60       accessToken: servers[0].accessToken,
61       language: 'ar',
62       videoId: videoUUID,
63       fixture: 'subtitle-good1.vtt'
64     })
65
66     await createVideoCaption({
67       url: servers[0].url,
68       accessToken: servers[0].accessToken,
69       language: 'zh',
70       videoId: videoUUID,
71       fixture: 'subtitle-good2.vtt',
72       mimeType: 'application/octet-stream'
73     })
74
75     await waitJobs(servers)
76   })
77
78   it('Should list these uploaded captions', async function () {
79     for (const server of servers) {
80       const res = await listVideoCaptions(server.url, videoUUID)
81       expect(res.body.total).to.equal(2)
82       expect(res.body.data).to.have.lengthOf(2)
83
84       const caption1: VideoCaption = res.body.data[0]
85       expect(caption1.language.id).to.equal('ar')
86       expect(caption1.language.label).to.equal('Arabic')
87       expect(caption1.captionPath).to.equal('/static/video-captions/' + videoUUID + '-ar.vtt')
88       await testCaptionFile(server.url, caption1.captionPath, 'Subtitle good 1.')
89
90       const caption2: VideoCaption = res.body.data[1]
91       expect(caption2.language.id).to.equal('zh')
92       expect(caption2.language.label).to.equal('Chinese')
93       expect(caption2.captionPath).to.equal('/static/video-captions/' + videoUUID + '-zh.vtt')
94       await testCaptionFile(server.url, caption2.captionPath, 'Subtitle good 2.')
95     }
96   })
97
98   it('Should replace an existing caption', async function () {
99     this.timeout(30000)
100
101     await createVideoCaption({
102       url: servers[0].url,
103       accessToken: servers[0].accessToken,
104       language: 'ar',
105       videoId: videoUUID,
106       fixture: 'subtitle-good2.vtt'
107     })
108
109     await waitJobs(servers)
110   })
111
112   it('Should have this caption updated', async function () {
113     for (const server of servers) {
114       const res = await listVideoCaptions(server.url, videoUUID)
115       expect(res.body.total).to.equal(2)
116       expect(res.body.data).to.have.lengthOf(2)
117
118       const caption1: VideoCaption = res.body.data[0]
119       expect(caption1.language.id).to.equal('ar')
120       expect(caption1.language.label).to.equal('Arabic')
121       expect(caption1.captionPath).to.equal('/static/video-captions/' + videoUUID + '-ar.vtt')
122       await testCaptionFile(server.url, caption1.captionPath, 'Subtitle good 2.')
123     }
124   })
125
126   it('Should replace an existing caption with a srt file and convert it', async function () {
127     this.timeout(30000)
128
129     await createVideoCaption({
130       url: servers[0].url,
131       accessToken: servers[0].accessToken,
132       language: 'ar',
133       videoId: videoUUID,
134       fixture: 'subtitle-good.srt'
135     })
136
137     await waitJobs(servers)
138
139     // Cache invalidation
140     await wait(3000)
141   })
142
143   it('Should have this caption updated and converted', async function () {
144     for (const server of servers) {
145       const res = await listVideoCaptions(server.url, videoUUID)
146       expect(res.body.total).to.equal(2)
147       expect(res.body.data).to.have.lengthOf(2)
148
149       const caption1: VideoCaption = res.body.data[0]
150       expect(caption1.language.id).to.equal('ar')
151       expect(caption1.language.label).to.equal('Arabic')
152       expect(caption1.captionPath).to.equal('/static/video-captions/' + videoUUID + '-ar.vtt')
153
154       const expected = 'WEBVTT FILE\r\n' +
155         '\r\n' +
156         '1\r\n' +
157         '00:00:01.600 --> 00:00:04.200\r\n' +
158         'English (US)\r\n' +
159         '\r\n' +
160         '2\r\n' +
161         '00:00:05.900 --> 00:00:07.999\r\n' +
162         'This is a subtitle in American English\r\n' +
163         '\r\n' +
164         '3\r\n' +
165         '00:00:10.000 --> 00:00:14.000\r\n' +
166         'Adding subtitles is very easy to do\r\n'
167       await testCaptionFile(server.url, caption1.captionPath, expected)
168     }
169   })
170
171   it('Should remove one caption', async function () {
172     this.timeout(30000)
173
174     await deleteVideoCaption(servers[0].url, servers[0].accessToken, videoUUID, 'ar')
175
176     await waitJobs(servers)
177   })
178
179   it('Should only list the caption that was not deleted', async function () {
180     for (const server of servers) {
181       const res = await listVideoCaptions(server.url, videoUUID)
182       expect(res.body.total).to.equal(1)
183       expect(res.body.data).to.have.lengthOf(1)
184
185       const caption: VideoCaption = res.body.data[0]
186
187       expect(caption.language.id).to.equal('zh')
188       expect(caption.language.label).to.equal('Chinese')
189       expect(caption.captionPath).to.equal('/static/video-captions/' + videoUUID + '-zh.vtt')
190       await testCaptionFile(server.url, caption.captionPath, 'Subtitle good 2.')
191     }
192   })
193
194   it('Should remove the video, and thus all video captions', async function () {
195     await removeVideo(servers[0].url, servers[0].accessToken, videoUUID)
196
197     await checkVideoFilesWereRemoved(videoUUID, 1)
198   })
199
200   after(async function () {
201     killallServers(servers)
202   })
203 })