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