1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
6 checkVideoFilesWereRemoved,
11 setAccessTokensToServers,
15 } from '@shared/extra-utils'
17 const expect = chai.expect
19 describe('Test video captions', function () {
20 const uuidRegex = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'
22 let servers: PeerTubeServer[]
25 before(async function () {
28 servers = await createMultipleServers(2)
30 await setAccessTokensToServers(servers)
31 await doubleFollow(servers[0], servers[1])
33 await waitJobs(servers)
35 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'my video name' } })
38 await waitJobs(servers)
41 it('Should list the captions and return an empty list', async function () {
42 for (const server of servers) {
43 const body = await server.captions.list({ videoId: videoUUID })
44 expect(body.total).to.equal(0)
45 expect(body.data).to.have.lengthOf(0)
49 it('Should create two new captions', async function () {
52 await servers[0].captions.add({
55 fixture: 'subtitle-good1.vtt'
58 await servers[0].captions.add({
61 fixture: 'subtitle-good2.vtt',
62 mimeType: 'application/octet-stream'
65 await waitJobs(servers)
68 it('Should list these uploaded captions', async function () {
69 for (const server of servers) {
70 const body = await server.captions.list({ videoId: videoUUID })
71 expect(body.total).to.equal(2)
72 expect(body.data).to.have.lengthOf(2)
74 const caption1 = body.data[0]
75 expect(caption1.language.id).to.equal('ar')
76 expect(caption1.language.label).to.equal('Arabic')
77 expect(caption1.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-ar.vtt$'))
78 await testCaptionFile(server.url, caption1.captionPath, 'Subtitle good 1.')
80 const caption2 = body.data[1]
81 expect(caption2.language.id).to.equal('zh')
82 expect(caption2.language.label).to.equal('Chinese')
83 expect(caption2.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-zh.vtt$'))
84 await testCaptionFile(server.url, caption2.captionPath, 'Subtitle good 2.')
88 it('Should replace an existing caption', async function () {
91 await servers[0].captions.add({
94 fixture: 'subtitle-good2.vtt'
97 await waitJobs(servers)
100 it('Should have this caption updated', async function () {
101 for (const server of servers) {
102 const body = await server.captions.list({ videoId: videoUUID })
103 expect(body.total).to.equal(2)
104 expect(body.data).to.have.lengthOf(2)
106 const caption1 = body.data[0]
107 expect(caption1.language.id).to.equal('ar')
108 expect(caption1.language.label).to.equal('Arabic')
109 expect(caption1.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-ar.vtt$'))
110 await testCaptionFile(server.url, caption1.captionPath, 'Subtitle good 2.')
114 it('Should replace an existing caption with a srt file and convert it', async function () {
117 await servers[0].captions.add({
120 fixture: 'subtitle-good.srt'
123 await waitJobs(servers)
125 // Cache invalidation
129 it('Should have this caption updated and converted', async function () {
130 for (const server of servers) {
131 const body = await server.captions.list({ videoId: videoUUID })
132 expect(body.total).to.equal(2)
133 expect(body.data).to.have.lengthOf(2)
135 const caption1 = body.data[0]
136 expect(caption1.language.id).to.equal('ar')
137 expect(caption1.language.label).to.equal('Arabic')
138 expect(caption1.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-ar.vtt$'))
140 const expected = 'WEBVTT FILE\r\n' +
143 '00:00:01.600 --> 00:00:04.200\r\n' +
147 '00:00:05.900 --> 00:00:07.999\r\n' +
148 'This is a subtitle in American English\r\n' +
151 '00:00:10.000 --> 00:00:14.000\r\n' +
152 'Adding subtitles is very easy to do\r\n'
153 await testCaptionFile(server.url, caption1.captionPath, expected)
157 it('Should remove one caption', async function () {
160 await servers[0].captions.delete({ videoId: videoUUID, language: 'ar' })
162 await waitJobs(servers)
165 it('Should only list the caption that was not deleted', async function () {
166 for (const server of servers) {
167 const body = await server.captions.list({ videoId: videoUUID })
168 expect(body.total).to.equal(1)
169 expect(body.data).to.have.lengthOf(1)
171 const caption = body.data[0]
173 expect(caption.language.id).to.equal('zh')
174 expect(caption.language.label).to.equal('Chinese')
175 expect(caption.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-zh.vtt$'))
176 await testCaptionFile(server.url, caption.captionPath, 'Subtitle good 2.')
180 it('Should remove the video, and thus all video captions', async function () {
181 const video = await servers[0].videos.get({ id: videoUUID })
182 const { data: captions } = await servers[0].captions.list({ videoId: videoUUID })
184 await servers[0].videos.remove({ id: videoUUID })
186 await checkVideoFilesWereRemoved({ server: servers[0], video, captions })
189 after(async function () {
190 await cleanupTests(servers)