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