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