]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/videos/video-captions.ts
Merge branch 'release/3.2.0' into develop
[github/Chocobozzz/PeerTube.git] / server / tests / api / videos / video-captions.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import {
6 checkVideoFilesWereRemoved,
7 cleanupTests,
8 doubleFollow,
9 flushAndRunMultipleServers,
10 removeVideo,
11 uploadVideo,
12 wait
13 } from '../../../../shared/extra-utils'
14 import { ServerInfo, setAccessTokensToServers } from '../../../../shared/extra-utils/index'
15 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
16 import {
17 createVideoCaption,
18 deleteVideoCaption,
19 listVideoCaptions,
20 testCaptionFile
21 } from '../../../../shared/extra-utils/videos/video-captions'
22 import { VideoCaption } from '../../../../shared/models/videos/caption/video-caption.model'
23
24 const expect = chai.expect
25
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}'
28
29 let servers: ServerInfo[]
30 let videoUUID: string
31
32 before(async function () {
33 this.timeout(60000)
34
35 servers = await flushAndRunMultipleServers(2)
36
37 await setAccessTokensToServers(servers)
38 await doubleFollow(servers[0], servers[1])
39
40 await waitJobs(servers)
41
42 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'my video name' })
43 videoUUID = res.body.video.uuid
44
45 await waitJobs(servers)
46 })
47
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)
53 }
54 })
55
56 it('Should create two new captions', async function () {
57 this.timeout(30000)
58
59 await createVideoCaption({
60 url: servers[0].url,
61 accessToken: servers[0].accessToken,
62 language: 'ar',
63 videoId: videoUUID,
64 fixture: 'subtitle-good1.vtt'
65 })
66
67 await createVideoCaption({
68 url: servers[0].url,
69 accessToken: servers[0].accessToken,
70 language: 'zh',
71 videoId: videoUUID,
72 fixture: 'subtitle-good2.vtt',
73 mimeType: 'application/octet-stream'
74 })
75
76 await waitJobs(servers)
77 })
78
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)
84
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.')
90
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.')
96 }
97 })
98
99 it('Should replace an existing caption', async function () {
100 this.timeout(30000)
101
102 await createVideoCaption({
103 url: servers[0].url,
104 accessToken: servers[0].accessToken,
105 language: 'ar',
106 videoId: videoUUID,
107 fixture: 'subtitle-good2.vtt'
108 })
109
110 await waitJobs(servers)
111 })
112
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)
118
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.')
124 }
125 })
126
127 it('Should replace an existing caption with a srt file and convert it', async function () {
128 this.timeout(30000)
129
130 await createVideoCaption({
131 url: servers[0].url,
132 accessToken: servers[0].accessToken,
133 language: 'ar',
134 videoId: videoUUID,
135 fixture: 'subtitle-good.srt'
136 })
137
138 await waitJobs(servers)
139
140 // Cache invalidation
141 await wait(3000)
142 })
143
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)
149
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$'))
154
155 const expected = 'WEBVTT FILE\r\n' +
156 '\r\n' +
157 '1\r\n' +
158 '00:00:01.600 --> 00:00:04.200\r\n' +
159 'English (US)\r\n' +
160 '\r\n' +
161 '2\r\n' +
162 '00:00:05.900 --> 00:00:07.999\r\n' +
163 'This is a subtitle in American English\r\n' +
164 '\r\n' +
165 '3\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)
169 }
170 })
171
172 it('Should remove one caption', async function () {
173 this.timeout(30000)
174
175 await deleteVideoCaption(servers[0].url, servers[0].accessToken, videoUUID, 'ar')
176
177 await waitJobs(servers)
178 })
179
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)
185
186 const caption: VideoCaption = res.body.data[0]
187
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.')
192 }
193 })
194
195 it('Should remove the video, and thus all video captions', async function () {
196 await removeVideo(servers[0].url, servers[0].accessToken, videoUUID)
197
198 await checkVideoFilesWereRemoved(videoUUID, 1)
199 })
200
201 after(async function () {
202 await cleanupTests(servers)
203 })
204 })