]>
Commit | Line | Data |
---|---|---|
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ | |
2 | ||
3 | import 'mocha' | |
4 | import * as chai from 'chai' | |
5 | import { | |
6 | checkVideoFilesWereRemoved, | |
7 | cleanupTests, | |
8 | createMultipleServers, | |
9 | doubleFollow, | |
10 | PeerTubeServer, | |
11 | setAccessTokensToServers, | |
12 | testCaptionFile, | |
13 | wait, | |
14 | waitJobs | |
15 | } from '@shared/extra-utils' | |
16 | ||
17 | const expect = chai.expect | |
18 | ||
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}' | |
21 | ||
22 | let servers: PeerTubeServer[] | |
23 | let videoUUID: string | |
24 | ||
25 | before(async function () { | |
26 | this.timeout(60000) | |
27 | ||
28 | servers = await createMultipleServers(2) | |
29 | ||
30 | await setAccessTokensToServers(servers) | |
31 | await doubleFollow(servers[0], servers[1]) | |
32 | ||
33 | await waitJobs(servers) | |
34 | ||
35 | const { uuid } = await servers[0].videos.upload({ attributes: { name: 'my video name' } }) | |
36 | videoUUID = uuid | |
37 | ||
38 | await waitJobs(servers) | |
39 | }) | |
40 | ||
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) | |
46 | } | |
47 | }) | |
48 | ||
49 | it('Should create two new captions', async function () { | |
50 | this.timeout(30000) | |
51 | ||
52 | await servers[0].captions.add({ | |
53 | language: 'ar', | |
54 | videoId: videoUUID, | |
55 | fixture: 'subtitle-good1.vtt' | |
56 | }) | |
57 | ||
58 | await servers[0].captions.add({ | |
59 | language: 'zh', | |
60 | videoId: videoUUID, | |
61 | fixture: 'subtitle-good2.vtt', | |
62 | mimeType: 'application/octet-stream' | |
63 | }) | |
64 | ||
65 | await waitJobs(servers) | |
66 | }) | |
67 | ||
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) | |
73 | ||
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.') | |
79 | ||
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.') | |
85 | } | |
86 | }) | |
87 | ||
88 | it('Should replace an existing caption', async function () { | |
89 | this.timeout(30000) | |
90 | ||
91 | await servers[0].captions.add({ | |
92 | language: 'ar', | |
93 | videoId: videoUUID, | |
94 | fixture: 'subtitle-good2.vtt' | |
95 | }) | |
96 | ||
97 | await waitJobs(servers) | |
98 | }) | |
99 | ||
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) | |
105 | ||
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.') | |
111 | } | |
112 | }) | |
113 | ||
114 | it('Should replace an existing caption with a srt file and convert it', async function () { | |
115 | this.timeout(30000) | |
116 | ||
117 | await servers[0].captions.add({ | |
118 | language: 'ar', | |
119 | videoId: videoUUID, | |
120 | fixture: 'subtitle-good.srt' | |
121 | }) | |
122 | ||
123 | await waitJobs(servers) | |
124 | ||
125 | // Cache invalidation | |
126 | await wait(3000) | |
127 | }) | |
128 | ||
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) | |
134 | ||
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$')) | |
139 | ||
140 | const expected = 'WEBVTT FILE\r\n' + | |
141 | '\r\n' + | |
142 | '1\r\n' + | |
143 | '00:00:01.600 --> 00:00:04.200\r\n' + | |
144 | 'English (US)\r\n' + | |
145 | '\r\n' + | |
146 | '2\r\n' + | |
147 | '00:00:05.900 --> 00:00:07.999\r\n' + | |
148 | 'This is a subtitle in American English\r\n' + | |
149 | '\r\n' + | |
150 | '3\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) | |
154 | } | |
155 | }) | |
156 | ||
157 | it('Should remove one caption', async function () { | |
158 | this.timeout(30000) | |
159 | ||
160 | await servers[0].captions.delete({ videoId: videoUUID, language: 'ar' }) | |
161 | ||
162 | await waitJobs(servers) | |
163 | }) | |
164 | ||
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) | |
170 | ||
171 | const caption = body.data[0] | |
172 | ||
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.') | |
177 | } | |
178 | }) | |
179 | ||
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 }) | |
183 | ||
184 | await servers[0].videos.remove({ id: videoUUID }) | |
185 | ||
186 | await checkVideoFilesWereRemoved({ server: servers[0], video, captions }) | |
187 | }) | |
188 | ||
189 | after(async function () { | |
190 | await cleanupTests(servers) | |
191 | }) | |
192 | }) |