1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } 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 describe('Test video captions', function () {
16 const uuidRegex = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'
18 let servers: PeerTubeServer[]
21 before(async function () {
24 servers = await createMultipleServers(2)
26 await setAccessTokensToServers(servers)
27 await doubleFollow(servers[0], servers[1])
29 await waitJobs(servers)
31 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'my video name' } })
34 await waitJobs(servers)
37 it('Should list the captions and return an empty list', async function () {
38 for (const server of servers) {
39 const body = await server.captions.list({ videoId: videoUUID })
40 expect(body.total).to.equal(0)
41 expect(body.data).to.have.lengthOf(0)
45 it('Should create two new captions', async function () {
48 await servers[0].captions.add({
51 fixture: 'subtitle-good1.vtt'
54 await servers[0].captions.add({
57 fixture: 'subtitle-good2.vtt',
58 mimeType: 'application/octet-stream'
61 await waitJobs(servers)
64 it('Should list these uploaded captions', async function () {
65 for (const server of servers) {
66 const body = await server.captions.list({ videoId: videoUUID })
67 expect(body.total).to.equal(2)
68 expect(body.data).to.have.lengthOf(2)
70 const caption1 = body.data[0]
71 expect(caption1.language.id).to.equal('ar')
72 expect(caption1.language.label).to.equal('Arabic')
73 expect(caption1.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-ar.vtt$'))
74 await testCaptionFile(server.url, caption1.captionPath, 'Subtitle good 1.')
76 const caption2 = body.data[1]
77 expect(caption2.language.id).to.equal('zh')
78 expect(caption2.language.label).to.equal('Chinese')
79 expect(caption2.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-zh.vtt$'))
80 await testCaptionFile(server.url, caption2.captionPath, 'Subtitle good 2.')
84 it('Should replace an existing caption', async function () {
87 await servers[0].captions.add({
90 fixture: 'subtitle-good2.vtt'
93 await waitJobs(servers)
96 it('Should have this caption updated', async function () {
97 for (const server of servers) {
98 const body = await server.captions.list({ videoId: videoUUID })
99 expect(body.total).to.equal(2)
100 expect(body.data).to.have.lengthOf(2)
102 const caption1 = body.data[0]
103 expect(caption1.language.id).to.equal('ar')
104 expect(caption1.language.label).to.equal('Arabic')
105 expect(caption1.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-ar.vtt$'))
106 await testCaptionFile(server.url, caption1.captionPath, 'Subtitle good 2.')
110 it('Should replace an existing caption with a srt file and convert it', async function () {
113 await servers[0].captions.add({
116 fixture: 'subtitle-good.srt'
119 await waitJobs(servers)
121 // Cache invalidation
125 it('Should have this caption updated and converted', async function () {
126 for (const server of servers) {
127 const body = await server.captions.list({ videoId: videoUUID })
128 expect(body.total).to.equal(2)
129 expect(body.data).to.have.lengthOf(2)
131 const caption1 = body.data[0]
132 expect(caption1.language.id).to.equal('ar')
133 expect(caption1.language.label).to.equal('Arabic')
134 expect(caption1.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-ar.vtt$'))
136 const expected = 'WEBVTT FILE\r\n' +
139 '00:00:01.600 --> 00:00:04.200\r\n' +
143 '00:00:05.900 --> 00:00:07.999\r\n' +
144 'This is a subtitle in American English\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)
153 it('Should remove one caption', async function () {
156 await servers[0].captions.delete({ videoId: videoUUID, language: 'ar' })
158 await waitJobs(servers)
161 it('Should only list the caption that was not deleted', async function () {
162 for (const server of servers) {
163 const body = await server.captions.list({ videoId: videoUUID })
164 expect(body.total).to.equal(1)
165 expect(body.data).to.have.lengthOf(1)
167 const caption = body.data[0]
169 expect(caption.language.id).to.equal('zh')
170 expect(caption.language.label).to.equal('Chinese')
171 expect(caption.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-zh.vtt$'))
172 await testCaptionFile(server.url, caption.captionPath, 'Subtitle good 2.')
176 it('Should remove the video, and thus all video captions', async function () {
177 const video = await servers[0].videos.get({ id: videoUUID })
178 const { data: captions } = await servers[0].captions.list({ videoId: videoUUID })
180 await servers[0].videos.remove({ id: videoUUID })
182 await checkVideoFilesWereRemoved({ server: servers[0], video, captions })
185 after(async function () {
186 await cleanupTests(servers)