1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import * as chai from 'chai'
4 import { checkVideoFilesWereRemoved, testCaptionFile } from '@server/tests/shared'
5 import { wait } from '@shared/core-utils'
11 setAccessTokensToServers,
13 } from '@shared/server-commands'
15 const expect = chai.expect
17 describe('Test video captions', function () {
18 const uuidRegex = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'
20 let servers: PeerTubeServer[]
23 before(async function () {
26 servers = await createMultipleServers(2)
28 await setAccessTokensToServers(servers)
29 await doubleFollow(servers[0], servers[1])
31 await waitJobs(servers)
33 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'my video name' } })
36 await waitJobs(servers)
39 it('Should list the captions and return an empty list', async function () {
40 for (const server of servers) {
41 const body = await server.captions.list({ videoId: videoUUID })
42 expect(body.total).to.equal(0)
43 expect(body.data).to.have.lengthOf(0)
47 it('Should create two new captions', async function () {
50 await servers[0].captions.add({
53 fixture: 'subtitle-good1.vtt'
56 await servers[0].captions.add({
59 fixture: 'subtitle-good2.vtt',
60 mimeType: 'application/octet-stream'
63 await waitJobs(servers)
66 it('Should list these uploaded captions', async function () {
67 for (const server of servers) {
68 const body = await server.captions.list({ videoId: videoUUID })
69 expect(body.total).to.equal(2)
70 expect(body.data).to.have.lengthOf(2)
72 const caption1 = body.data[0]
73 expect(caption1.language.id).to.equal('ar')
74 expect(caption1.language.label).to.equal('Arabic')
75 expect(caption1.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-ar.vtt$'))
76 await testCaptionFile(server.url, caption1.captionPath, 'Subtitle good 1.')
78 const caption2 = body.data[1]
79 expect(caption2.language.id).to.equal('zh')
80 expect(caption2.language.label).to.equal('Chinese')
81 expect(caption2.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-zh.vtt$'))
82 await testCaptionFile(server.url, caption2.captionPath, 'Subtitle good 2.')
86 it('Should replace an existing caption', async function () {
89 await servers[0].captions.add({
92 fixture: 'subtitle-good2.vtt'
95 await waitJobs(servers)
98 it('Should have this caption updated', async function () {
99 for (const server of servers) {
100 const body = await server.captions.list({ videoId: videoUUID })
101 expect(body.total).to.equal(2)
102 expect(body.data).to.have.lengthOf(2)
104 const caption1 = body.data[0]
105 expect(caption1.language.id).to.equal('ar')
106 expect(caption1.language.label).to.equal('Arabic')
107 expect(caption1.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-ar.vtt$'))
108 await testCaptionFile(server.url, caption1.captionPath, 'Subtitle good 2.')
112 it('Should replace an existing caption with a srt file and convert it', async function () {
115 await servers[0].captions.add({
118 fixture: 'subtitle-good.srt'
121 await waitJobs(servers)
123 // Cache invalidation
127 it('Should have this caption updated and converted', async function () {
128 for (const server of servers) {
129 const body = await server.captions.list({ videoId: videoUUID })
130 expect(body.total).to.equal(2)
131 expect(body.data).to.have.lengthOf(2)
133 const caption1 = body.data[0]
134 expect(caption1.language.id).to.equal('ar')
135 expect(caption1.language.label).to.equal('Arabic')
136 expect(caption1.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-ar.vtt$'))
138 const expected = 'WEBVTT FILE\r\n' +
141 '00:00:01.600 --> 00:00:04.200\r\n' +
145 '00:00:05.900 --> 00:00:07.999\r\n' +
146 'This is a subtitle in American English\r\n' +
149 '00:00:10.000 --> 00:00:14.000\r\n' +
150 'Adding subtitles is very easy to do\r\n'
151 await testCaptionFile(server.url, caption1.captionPath, expected)
155 it('Should remove one caption', async function () {
158 await servers[0].captions.delete({ videoId: videoUUID, language: 'ar' })
160 await waitJobs(servers)
163 it('Should only list the caption that was not deleted', async function () {
164 for (const server of servers) {
165 const body = await server.captions.list({ videoId: videoUUID })
166 expect(body.total).to.equal(1)
167 expect(body.data).to.have.lengthOf(1)
169 const caption = body.data[0]
171 expect(caption.language.id).to.equal('zh')
172 expect(caption.language.label).to.equal('Chinese')
173 expect(caption.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-zh.vtt$'))
174 await testCaptionFile(server.url, caption.captionPath, 'Subtitle good 2.')
178 it('Should remove the video, and thus all video captions', async function () {
179 const video = await servers[0].videos.get({ id: videoUUID })
180 const { data: captions } = await servers[0].captions.list({ videoId: videoUUID })
182 await servers[0].videos.remove({ id: videoUUID })
184 await checkVideoFilesWereRemoved({ server: servers[0], video, captions })
187 after(async function () {
188 await cleanupTests(servers)