]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/videos/video-captions.ts
Introduce captions command
[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 'mocha'
4 import * as chai from 'chai'
5 import {
6 checkVideoFilesWereRemoved,
7 cleanupTests,
8 doubleFollow,
9 flushAndRunMultipleServers,
10 removeVideo,
11 ServerInfo,
12 setAccessTokensToServers,
13 testCaptionFile,
14 uploadVideo,
15 wait,
16 waitJobs
17 } from '@shared/extra-utils'
18
19 const expect = chai.expect
20
21 describe('Test video captions', function () {
22 const uuidRegex = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'
23
24 let servers: ServerInfo[]
25 let videoUUID: string
26
27 before(async function () {
28 this.timeout(60000)
29
30 servers = await flushAndRunMultipleServers(2)
31
32 await setAccessTokensToServers(servers)
33 await doubleFollow(servers[0], servers[1])
34
35 await waitJobs(servers)
36
37 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'my video name' })
38 videoUUID = res.body.video.uuid
39
40 await waitJobs(servers)
41 })
42
43 it('Should list the captions and return an empty list', async function () {
44 for (const server of servers) {
45 const body = await server.captionsCommand.listVideoCaptions({ videoId: videoUUID })
46 expect(body.total).to.equal(0)
47 expect(body.data).to.have.lengthOf(0)
48 }
49 })
50
51 it('Should create two new captions', async function () {
52 this.timeout(30000)
53
54 await servers[0].captionsCommand.createVideoCaption({
55 language: 'ar',
56 videoId: videoUUID,
57 fixture: 'subtitle-good1.vtt'
58 })
59
60 await servers[0].captionsCommand.createVideoCaption({
61 language: 'zh',
62 videoId: videoUUID,
63 fixture: 'subtitle-good2.vtt',
64 mimeType: 'application/octet-stream'
65 })
66
67 await waitJobs(servers)
68 })
69
70 it('Should list these uploaded captions', async function () {
71 for (const server of servers) {
72 const body = await server.captionsCommand.listVideoCaptions({ videoId: videoUUID })
73 expect(body.total).to.equal(2)
74 expect(body.data).to.have.lengthOf(2)
75
76 const caption1 = body.data[0]
77 expect(caption1.language.id).to.equal('ar')
78 expect(caption1.language.label).to.equal('Arabic')
79 expect(caption1.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-ar.vtt$'))
80 await testCaptionFile(server.url, caption1.captionPath, 'Subtitle good 1.')
81
82 const caption2 = body.data[1]
83 expect(caption2.language.id).to.equal('zh')
84 expect(caption2.language.label).to.equal('Chinese')
85 expect(caption2.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-zh.vtt$'))
86 await testCaptionFile(server.url, caption2.captionPath, 'Subtitle good 2.')
87 }
88 })
89
90 it('Should replace an existing caption', async function () {
91 this.timeout(30000)
92
93 await servers[0].captionsCommand.createVideoCaption({
94 language: 'ar',
95 videoId: videoUUID,
96 fixture: 'subtitle-good2.vtt'
97 })
98
99 await waitJobs(servers)
100 })
101
102 it('Should have this caption updated', async function () {
103 for (const server of servers) {
104 const body = await server.captionsCommand.listVideoCaptions({ videoId: videoUUID })
105 expect(body.total).to.equal(2)
106 expect(body.data).to.have.lengthOf(2)
107
108 const caption1 = body.data[0]
109 expect(caption1.language.id).to.equal('ar')
110 expect(caption1.language.label).to.equal('Arabic')
111 expect(caption1.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-ar.vtt$'))
112 await testCaptionFile(server.url, caption1.captionPath, 'Subtitle good 2.')
113 }
114 })
115
116 it('Should replace an existing caption with a srt file and convert it', async function () {
117 this.timeout(30000)
118
119 await servers[0].captionsCommand.createVideoCaption({
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 body = await server.captionsCommand.listVideoCaptions({ videoId: videoUUID })
134 expect(body.total).to.equal(2)
135 expect(body.data).to.have.lengthOf(2)
136
137 const caption1 = body.data[0]
138 expect(caption1.language.id).to.equal('ar')
139 expect(caption1.language.label).to.equal('Arabic')
140 expect(caption1.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-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 servers[0].captionsCommand.deleteVideoCaption({ videoId: videoUUID, language: '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 body = await server.captionsCommand.listVideoCaptions({ videoId: videoUUID })
170 expect(body.total).to.equal(1)
171 expect(body.data).to.have.lengthOf(1)
172
173 const caption = 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.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-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 await cleanupTests(servers)
190 })
191 })