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