1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { checkVideoFilesWereRemoved, testCaptionFile } from '@server/tests/shared'
6 import { wait } from '@shared/core-utils'
12 setAccessTokensToServers,
14 } from '@shared/server-commands'
16 const expect = chai.expect
18 describe('Test video captions', function () {
19 const uuidRegex = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'
21 let servers: PeerTubeServer[]
24 before(async function () {
27 servers = await createMultipleServers(2)
29 await setAccessTokensToServers(servers)
30 await doubleFollow(servers[0], servers[1])
32 await waitJobs(servers)
34 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'my video name' } })
37 await waitJobs(servers)
40 it('Should list the captions and return an empty list', async function () {
41 for (const server of servers) {
42 const body = await server.captions.list({ videoId: videoUUID })
43 expect(body.total).to.equal(0)
44 expect(body.data).to.have.lengthOf(0)
48 it('Should create two new captions', async function () {
51 await servers[0].captions.add({
54 fixture: 'subtitle-good1.vtt'
57 await servers[0].captions.add({
60 fixture: 'subtitle-good2.vtt',
61 mimeType: 'application/octet-stream'
64 await waitJobs(servers)
67 it('Should list these uploaded captions', async function () {
68 for (const server of servers) {
69 const body = await server.captions.list({ videoId: videoUUID })
70 expect(body.total).to.equal(2)
71 expect(body.data).to.have.lengthOf(2)
73 const caption1 = body.data[0]
74 expect(caption1.language.id).to.equal('ar')
75 expect(caption1.language.label).to.equal('Arabic')
76 expect(caption1.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-ar.vtt$'))
77 await testCaptionFile(server.url, caption1.captionPath, 'Subtitle good 1.')
79 const caption2 = body.data[1]
80 expect(caption2.language.id).to.equal('zh')
81 expect(caption2.language.label).to.equal('Chinese')
82 expect(caption2.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-zh.vtt$'))
83 await testCaptionFile(server.url, caption2.captionPath, 'Subtitle good 2.')
87 it('Should replace an existing caption', async function () {
90 await servers[0].captions.add({
93 fixture: 'subtitle-good2.vtt'
96 await waitJobs(servers)
99 it('Should have this caption updated', async function () {
100 for (const server of servers) {
101 const body = await server.captions.list({ videoId: videoUUID })
102 expect(body.total).to.equal(2)
103 expect(body.data).to.have.lengthOf(2)
105 const caption1 = body.data[0]
106 expect(caption1.language.id).to.equal('ar')
107 expect(caption1.language.label).to.equal('Arabic')
108 expect(caption1.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-ar.vtt$'))
109 await testCaptionFile(server.url, caption1.captionPath, 'Subtitle good 2.')
113 it('Should replace an existing caption with a srt file and convert it', async function () {
116 await servers[0].captions.add({
119 fixture: 'subtitle-good.srt'
122 await waitJobs(servers)
124 // Cache invalidation
128 it('Should have this caption updated and converted', async function () {
129 for (const server of servers) {
130 const body = await server.captions.list({ videoId: videoUUID })
131 expect(body.total).to.equal(2)
132 expect(body.data).to.have.lengthOf(2)
134 const caption1 = body.data[0]
135 expect(caption1.language.id).to.equal('ar')
136 expect(caption1.language.label).to.equal('Arabic')
137 expect(caption1.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-ar.vtt$'))
139 const expected = 'WEBVTT FILE\r\n' +
142 '00:00:01.600 --> 00:00:04.200\r\n' +
146 '00:00:05.900 --> 00:00:07.999\r\n' +
147 'This is a subtitle in American English\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)
156 it('Should remove one caption', async function () {
159 await servers[0].captions.delete({ videoId: videoUUID, language: 'ar' })
161 await waitJobs(servers)
164 it('Should only list the caption that was not deleted', async function () {
165 for (const server of servers) {
166 const body = await server.captions.list({ videoId: videoUUID })
167 expect(body.total).to.equal(1)
168 expect(body.data).to.have.lengthOf(1)
170 const caption = body.data[0]
172 expect(caption.language.id).to.equal('zh')
173 expect(caption.language.label).to.equal('Chinese')
174 expect(caption.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-zh.vtt$'))
175 await testCaptionFile(server.url, caption.captionPath, 'Subtitle good 2.')
179 it('Should remove the video, and thus all video captions', async function () {
180 const video = await servers[0].videos.get({ id: videoUUID })
181 const { data: captions } = await servers[0].captions.list({ videoId: videoUUID })
183 await servers[0].videos.remove({ id: videoUUID })
185 await checkVideoFilesWereRemoved({ server: servers[0], video, captions })
188 after(async function () {
189 await cleanupTests(servers)