]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/videos/video-captions.ts
Introduce videos 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 ServerInfo,
11 setAccessTokensToServers,
12 testCaptionFile,
13 wait,
14 waitJobs
15 } from '@shared/extra-utils'
16
17 const expect = chai.expect
18
19 describe('Test video captions', function () {
20 const uuidRegex = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'
21
22 let servers: ServerInfo[]
23 let videoUUID: string
24
25 before(async function () {
26 this.timeout(60000)
27
28 servers = await flushAndRunMultipleServers(2)
29
30 await setAccessTokensToServers(servers)
31 await doubleFollow(servers[0], servers[1])
32
33 await waitJobs(servers)
34
35 const { uuid } = await servers[0].videosCommand.upload({ attributes: { name: 'my video name' } })
36 videoUUID = uuid
37
38 await waitJobs(servers)
39 })
40
41 it('Should list the captions and return an empty list', async function () {
42 for (const server of servers) {
43 const body = await server.captionsCommand.listVideoCaptions({ videoId: videoUUID })
44 expect(body.total).to.equal(0)
45 expect(body.data).to.have.lengthOf(0)
46 }
47 })
48
49 it('Should create two new captions', async function () {
50 this.timeout(30000)
51
52 await servers[0].captionsCommand.createVideoCaption({
53 language: 'ar',
54 videoId: videoUUID,
55 fixture: 'subtitle-good1.vtt'
56 })
57
58 await servers[0].captionsCommand.createVideoCaption({
59 language: 'zh',
60 videoId: videoUUID,
61 fixture: 'subtitle-good2.vtt',
62 mimeType: 'application/octet-stream'
63 })
64
65 await waitJobs(servers)
66 })
67
68 it('Should list these uploaded captions', async function () {
69 for (const server of servers) {
70 const body = await server.captionsCommand.listVideoCaptions({ videoId: videoUUID })
71 expect(body.total).to.equal(2)
72 expect(body.data).to.have.lengthOf(2)
73
74 const caption1 = body.data[0]
75 expect(caption1.language.id).to.equal('ar')
76 expect(caption1.language.label).to.equal('Arabic')
77 expect(caption1.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-ar.vtt$'))
78 await testCaptionFile(server.url, caption1.captionPath, 'Subtitle good 1.')
79
80 const caption2 = body.data[1]
81 expect(caption2.language.id).to.equal('zh')
82 expect(caption2.language.label).to.equal('Chinese')
83 expect(caption2.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-zh.vtt$'))
84 await testCaptionFile(server.url, caption2.captionPath, 'Subtitle good 2.')
85 }
86 })
87
88 it('Should replace an existing caption', async function () {
89 this.timeout(30000)
90
91 await servers[0].captionsCommand.createVideoCaption({
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 body = await server.captionsCommand.listVideoCaptions({ videoId: videoUUID })
103 expect(body.total).to.equal(2)
104 expect(body.data).to.have.lengthOf(2)
105
106 const caption1 = body.data[0]
107 expect(caption1.language.id).to.equal('ar')
108 expect(caption1.language.label).to.equal('Arabic')
109 expect(caption1.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-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 servers[0].captionsCommand.createVideoCaption({
118 language: 'ar',
119 videoId: videoUUID,
120 fixture: 'subtitle-good.srt'
121 })
122
123 await waitJobs(servers)
124
125 // Cache invalidation
126 await wait(3000)
127 })
128
129 it('Should have this caption updated and converted', async function () {
130 for (const server of servers) {
131 const body = await server.captionsCommand.listVideoCaptions({ videoId: videoUUID })
132 expect(body.total).to.equal(2)
133 expect(body.data).to.have.lengthOf(2)
134
135 const caption1 = body.data[0]
136 expect(caption1.language.id).to.equal('ar')
137 expect(caption1.language.label).to.equal('Arabic')
138 expect(caption1.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-ar.vtt$'))
139
140 const expected = 'WEBVTT FILE\r\n' +
141 '\r\n' +
142 '1\r\n' +
143 '00:00:01.600 --> 00:00:04.200\r\n' +
144 'English (US)\r\n' +
145 '\r\n' +
146 '2\r\n' +
147 '00:00:05.900 --> 00:00:07.999\r\n' +
148 'This is a subtitle in American English\r\n' +
149 '\r\n' +
150 '3\r\n' +
151 '00:00:10.000 --> 00:00:14.000\r\n' +
152 'Adding subtitles is very easy to do\r\n'
153 await testCaptionFile(server.url, caption1.captionPath, expected)
154 }
155 })
156
157 it('Should remove one caption', async function () {
158 this.timeout(30000)
159
160 await servers[0].captionsCommand.deleteVideoCaption({ videoId: videoUUID, language: 'ar' })
161
162 await waitJobs(servers)
163 })
164
165 it('Should only list the caption that was not deleted', async function () {
166 for (const server of servers) {
167 const body = await server.captionsCommand.listVideoCaptions({ videoId: videoUUID })
168 expect(body.total).to.equal(1)
169 expect(body.data).to.have.lengthOf(1)
170
171 const caption = body.data[0]
172
173 expect(caption.language.id).to.equal('zh')
174 expect(caption.language.label).to.equal('Chinese')
175 expect(caption.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-zh.vtt$'))
176 await testCaptionFile(server.url, caption.captionPath, 'Subtitle good 2.')
177 }
178 })
179
180 it('Should remove the video, and thus all video captions', async function () {
181 await servers[0].videosCommand.remove({ id: videoUUID })
182
183 await checkVideoFilesWereRemoved(videoUUID, servers[0])
184 })
185
186 after(async function () {
187 await cleanupTests(servers)
188 })
189 })