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