Implement captions/subtitles
[oweals/peertube.git] / server / tests / api / videos / video-captions.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import { doubleFollow, flushAndRunMultipleServers, uploadVideo } from '../../utils'
6 import { flushTests, killallServers, ServerInfo, setAccessTokensToServers } from '../../utils/index'
7 import { waitJobs } from '../../utils/server/jobs'
8 import { createVideoCaption, deleteVideoCaption, listVideoCaptions, testCaptionFile } from '../../utils/videos/video-captions'
9 import { VideoCaption } from '../../../../shared/models/videos/video-caption.model'
10
11 const expect = chai.expect
12
13 describe('Test video captions', function () {
14   let servers: ServerInfo[]
15   let videoUUID: string
16
17   before(async function () {
18     this.timeout(30000)
19
20     await flushTests()
21
22     servers = await flushAndRunMultipleServers(2)
23
24     await setAccessTokensToServers(servers)
25     await doubleFollow(servers[0], servers[1])
26
27     await waitJobs(servers)
28
29     const res = await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, { name: 'my video name' })
30     videoUUID = res.body.video.uuid
31
32     await waitJobs(servers)
33   })
34
35   it('Should list the captions and return an empty list', async function () {
36     for (const server of servers) {
37       const res = await listVideoCaptions(server.url, videoUUID)
38       expect(res.body.total).to.equal(0)
39       expect(res.body.data).to.have.lengthOf(0)
40     }
41   })
42
43   it('Should create two new captions', async function () {
44     this.timeout(30000)
45
46     await createVideoCaption({
47       url: servers[0].url,
48       accessToken: servers[0].accessToken,
49       language: 'ar',
50       videoId: videoUUID,
51       fixture: 'subtitle-good1.vtt'
52     })
53
54     await createVideoCaption({
55       url: servers[0].url,
56       accessToken: servers[0].accessToken,
57       language: 'zh',
58       videoId: videoUUID,
59       fixture: 'subtitle-good2.vtt'
60     })
61
62     await waitJobs(servers)
63   })
64
65   it('Should list these uploaded captions', async function () {
66     for (const server of servers) {
67       const res = await listVideoCaptions(server.url, videoUUID)
68       expect(res.body.total).to.equal(2)
69       expect(res.body.data).to.have.lengthOf(2)
70
71       const caption1: VideoCaption = res.body.data[0]
72       expect(caption1.language.id).to.equal('ar')
73       expect(caption1.language.label).to.equal('Arabic')
74       expect(caption1.captionPath).to.equal('/static/video-captions/' + videoUUID + '-ar.vtt')
75       await testCaptionFile(server.url, caption1.captionPath, 'Subtitle good 1.')
76
77       const caption2: VideoCaption = res.body.data[1]
78       expect(caption2.language.id).to.equal('zh')
79       expect(caption2.language.label).to.equal('Chinese')
80       expect(caption2.captionPath).to.equal('/static/video-captions/' + videoUUID + '-zh.vtt')
81       await testCaptionFile(server.url, caption2.captionPath, 'Subtitle good 2.')
82     }
83   })
84
85   it('Should replace an existing caption', async function () {
86     this.timeout(30000)
87
88     await createVideoCaption({
89       url: servers[0].url,
90       accessToken: servers[0].accessToken,
91       language: 'ar',
92       videoId: videoUUID,
93       fixture: 'subtitle-good2.vtt'
94     })
95
96     await waitJobs(servers)
97   })
98
99   it('Should have this caption updated', async function () {
100     for (const server of servers) {
101       const res = await listVideoCaptions(server.url, videoUUID)
102       expect(res.body.total).to.equal(2)
103       expect(res.body.data).to.have.lengthOf(2)
104
105       const caption1: VideoCaption = res.body.data[0]
106       expect(caption1.language.id).to.equal('ar')
107       expect(caption1.language.label).to.equal('Arabic')
108       expect(caption1.captionPath).to.equal('/static/video-captions/' + videoUUID + '-ar.vtt')
109       await testCaptionFile(server.url, caption1.captionPath, 'Subtitle good 2.')
110     }
111   })
112
113   it('Should remove one caption', async function () {
114     this.timeout(30000)
115
116     await deleteVideoCaption(servers[0].url, servers[0].accessToken, videoUUID, 'ar')
117
118     await waitJobs(servers)
119   })
120
121   it('Should only list the caption that was not deleted', async function () {
122     for (const server of servers) {
123       const res = await listVideoCaptions(server.url, videoUUID)
124       expect(res.body.total).to.equal(1)
125       expect(res.body.data).to.have.lengthOf(1)
126
127       const caption: VideoCaption = res.body.data[0]
128
129       expect(caption.language.id).to.equal('zh')
130       expect(caption.language.label).to.equal('Chinese')
131       expect(caption.captionPath).to.equal('/static/video-captions/' + videoUUID + '-zh.vtt')
132       await testCaptionFile(server.url, caption.captionPath, 'Subtitle good 2.')
133     }
134   })
135
136   after(async function () {
137     killallServers(servers)
138   })
139 })