]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/videos/video-captions.ts
Channel sync (#5135)
[github/Chocobozzz/PeerTube.git] / server / tests / api / videos / video-captions.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
40e87e9e 2
40e87e9e 3import 'mocha'
a2470c9f 4import * as chai from 'chai'
c55e3d72
C
5import { checkVideoFilesWereRemoved, testCaptionFile } from '@server/tests/shared'
6import { wait } from '@shared/core-utils'
9639bd17 7import {
a1587156 8 cleanupTests,
254d3579 9 createMultipleServers,
4c7e60bc 10 doubleFollow,
254d3579 11 PeerTubeServer,
a2470c9f 12 setAccessTokensToServers,
a2470c9f 13 waitJobs
bf54587a 14} from '@shared/server-commands'
40e87e9e
C
15
16const expect = chai.expect
17
18describe('Test video captions', function () {
6302d599
C
19 const uuidRegex = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'
20
254d3579 21 let servers: PeerTubeServer[]
40e87e9e
C
22 let videoUUID: string
23
24 before(async function () {
59fd824c 25 this.timeout(60000)
40e87e9e 26
254d3579 27 servers = await createMultipleServers(2)
40e87e9e
C
28
29 await setAccessTokensToServers(servers)
30 await doubleFollow(servers[0], servers[1])
31
32 await waitJobs(servers)
33
89d241a7 34 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'my video name' } })
d23dd9fb 35 videoUUID = uuid
40e87e9e
C
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) {
c63830f1 42 const body = await server.captions.list({ videoId: videoUUID })
a2470c9f
C
43 expect(body.total).to.equal(0)
44 expect(body.data).to.have.lengthOf(0)
40e87e9e
C
45 }
46 })
47
48 it('Should create two new captions', async function () {
49 this.timeout(30000)
50
c63830f1 51 await servers[0].captions.add({
40e87e9e
C
52 language: 'ar',
53 videoId: videoUUID,
54 fixture: 'subtitle-good1.vtt'
55 })
56
c63830f1 57 await servers[0].captions.add({
40e87e9e
C
58 language: 'zh',
59 videoId: videoUUID,
2769e297
C
60 fixture: 'subtitle-good2.vtt',
61 mimeType: 'application/octet-stream'
40e87e9e
C
62 })
63
64 await waitJobs(servers)
65 })
66
67 it('Should list these uploaded captions', async function () {
68 for (const server of servers) {
c63830f1 69 const body = await server.captions.list({ videoId: videoUUID })
a2470c9f
C
70 expect(body.total).to.equal(2)
71 expect(body.data).to.have.lengthOf(2)
40e87e9e 72
a2470c9f 73 const caption1 = body.data[0]
40e87e9e
C
74 expect(caption1.language.id).to.equal('ar')
75 expect(caption1.language.label).to.equal('Arabic')
6302d599 76 expect(caption1.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-ar.vtt$'))
40e87e9e
C
77 await testCaptionFile(server.url, caption1.captionPath, 'Subtitle good 1.')
78
a2470c9f 79 const caption2 = body.data[1]
40e87e9e
C
80 expect(caption2.language.id).to.equal('zh')
81 expect(caption2.language.label).to.equal('Chinese')
6302d599 82 expect(caption2.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-zh.vtt$'))
40e87e9e
C
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
c63830f1 90 await servers[0].captions.add({
40e87e9e
C
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) {
c63830f1 101 const body = await server.captions.list({ videoId: videoUUID })
a2470c9f
C
102 expect(body.total).to.equal(2)
103 expect(body.data).to.have.lengthOf(2)
40e87e9e 104
a2470c9f 105 const caption1 = body.data[0]
40e87e9e
C
106 expect(caption1.language.id).to.equal('ar')
107 expect(caption1.language.label).to.equal('Arabic')
6302d599 108 expect(caption1.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-ar.vtt$'))
40e87e9e
C
109 await testCaptionFile(server.url, caption1.captionPath, 'Subtitle good 2.')
110 }
111 })
112
f4001cf4
C
113 it('Should replace an existing caption with a srt file and convert it', async function () {
114 this.timeout(30000)
115
c63830f1 116 await servers[0].captions.add({
f4001cf4
C
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) {
c63830f1 130 const body = await server.captions.list({ videoId: videoUUID })
a2470c9f
C
131 expect(body.total).to.equal(2)
132 expect(body.data).to.have.lengthOf(2)
f4001cf4 133
a2470c9f 134 const caption1 = body.data[0]
f4001cf4
C
135 expect(caption1.language.id).to.equal('ar')
136 expect(caption1.language.label).to.equal('Arabic')
6302d599 137 expect(caption1.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-ar.vtt$'))
f4001cf4
C
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
40e87e9e
C
156 it('Should remove one caption', async function () {
157 this.timeout(30000)
158
c63830f1 159 await servers[0].captions.delete({ videoId: videoUUID, language: 'ar' })
40e87e9e
C
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) {
c63830f1 166 const body = await server.captions.list({ videoId: videoUUID })
a2470c9f
C
167 expect(body.total).to.equal(1)
168 expect(body.data).to.have.lengthOf(1)
40e87e9e 169
a2470c9f 170 const caption = body.data[0]
40e87e9e
C
171
172 expect(caption.language.id).to.equal('zh')
173 expect(caption.language.label).to.equal('Chinese')
6302d599 174 expect(caption.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-zh.vtt$'))
40e87e9e
C
175 await testCaptionFile(server.url, caption.captionPath, 'Subtitle good 2.')
176 }
177 })
178
f4001cf4 179 it('Should remove the video, and thus all video captions', async function () {
83903cb6
C
180 const video = await servers[0].videos.get({ id: videoUUID })
181 const { data: captions } = await servers[0].captions.list({ videoId: videoUUID })
182
89d241a7 183 await servers[0].videos.remove({ id: videoUUID })
f4001cf4 184
83903cb6 185 await checkVideoFilesWereRemoved({ server: servers[0], video, captions })
f4001cf4
C
186 })
187
7c3b7976
C
188 after(async function () {
189 await cleanupTests(servers)
40e87e9e
C
190 })
191})