diff options
Diffstat (limited to 'packages/tests/src/api/videos/video-captions.ts')
-rw-r--r-- | packages/tests/src/api/videos/video-captions.ts | 189 |
1 files changed, 189 insertions, 0 deletions
diff --git a/packages/tests/src/api/videos/video-captions.ts b/packages/tests/src/api/videos/video-captions.ts new file mode 100644 index 000000000..027022549 --- /dev/null +++ b/packages/tests/src/api/videos/video-captions.ts | |||
@@ -0,0 +1,189 @@ | |||
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ | ||
2 | |||
3 | import { expect } from 'chai' | ||
4 | import { wait } from '@peertube/peertube-core-utils' | ||
5 | import { | ||
6 | cleanupTests, | ||
7 | createMultipleServers, | ||
8 | doubleFollow, | ||
9 | PeerTubeServer, | ||
10 | setAccessTokensToServers, | ||
11 | waitJobs | ||
12 | } from '@peertube/peertube-server-commands' | ||
13 | import { testCaptionFile } from '@tests/shared/captions.js' | ||
14 | import { checkVideoFilesWereRemoved } from '@tests/shared/videos.js' | ||
15 | |||
16 | describe('Test video captions', function () { | ||
17 | const uuidRegex = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}' | ||
18 | |||
19 | let servers: PeerTubeServer[] | ||
20 | let videoUUID: string | ||
21 | |||
22 | before(async function () { | ||
23 | this.timeout(60000) | ||
24 | |||
25 | servers = await createMultipleServers(2) | ||
26 | |||
27 | await setAccessTokensToServers(servers) | ||
28 | await doubleFollow(servers[0], servers[1]) | ||
29 | |||
30 | await waitJobs(servers) | ||
31 | |||
32 | const { uuid } = await servers[0].videos.upload({ attributes: { name: 'my video name' } }) | ||
33 | videoUUID = uuid | ||
34 | |||
35 | await waitJobs(servers) | ||
36 | }) | ||
37 | |||
38 | it('Should list the captions and return an empty list', async function () { | ||
39 | for (const server of servers) { | ||
40 | const body = await server.captions.list({ videoId: videoUUID }) | ||
41 | expect(body.total).to.equal(0) | ||
42 | expect(body.data).to.have.lengthOf(0) | ||
43 | } | ||
44 | }) | ||
45 | |||
46 | it('Should create two new captions', async function () { | ||
47 | this.timeout(30000) | ||
48 | |||
49 | await servers[0].captions.add({ | ||
50 | language: 'ar', | ||
51 | videoId: videoUUID, | ||
52 | fixture: 'subtitle-good1.vtt' | ||
53 | }) | ||
54 | |||
55 | await servers[0].captions.add({ | ||
56 | language: 'zh', | ||
57 | videoId: videoUUID, | ||
58 | fixture: 'subtitle-good2.vtt', | ||
59 | mimeType: 'application/octet-stream' | ||
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 body = await server.captions.list({ videoId: videoUUID }) | ||
68 | expect(body.total).to.equal(2) | ||
69 | expect(body.data).to.have.lengthOf(2) | ||
70 | |||
71 | const caption1 = body.data[0] | ||
72 | expect(caption1.language.id).to.equal('ar') | ||
73 | expect(caption1.language.label).to.equal('Arabic') | ||
74 | expect(caption1.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-ar.vtt$')) | ||
75 | await testCaptionFile(server.url, caption1.captionPath, 'Subtitle good 1.') | ||
76 | |||
77 | const caption2 = body.data[1] | ||
78 | expect(caption2.language.id).to.equal('zh') | ||
79 | expect(caption2.language.label).to.equal('Chinese') | ||
80 | expect(caption2.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-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 servers[0].captions.add({ | ||
89 | language: 'ar', | ||
90 | videoId: videoUUID, | ||
91 | fixture: 'subtitle-good2.vtt' | ||
92 | }) | ||
93 | |||
94 | await waitJobs(servers) | ||
95 | }) | ||
96 | |||
97 | it('Should have this caption updated', async function () { | ||
98 | for (const server of servers) { | ||
99 | const body = await server.captions.list({ videoId: videoUUID }) | ||
100 | expect(body.total).to.equal(2) | ||
101 | expect(body.data).to.have.lengthOf(2) | ||
102 | |||
103 | const caption1 = body.data[0] | ||
104 | expect(caption1.language.id).to.equal('ar') | ||
105 | expect(caption1.language.label).to.equal('Arabic') | ||
106 | expect(caption1.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-ar.vtt$')) | ||
107 | await testCaptionFile(server.url, caption1.captionPath, 'Subtitle good 2.') | ||
108 | } | ||
109 | }) | ||
110 | |||
111 | it('Should replace an existing caption with a srt file and convert it', async function () { | ||
112 | this.timeout(30000) | ||
113 | |||
114 | await servers[0].captions.add({ | ||
115 | language: 'ar', | ||
116 | videoId: videoUUID, | ||
117 | fixture: 'subtitle-good.srt' | ||
118 | }) | ||
119 | |||
120 | await waitJobs(servers) | ||
121 | |||
122 | // Cache invalidation | ||
123 | await wait(3000) | ||
124 | }) | ||
125 | |||
126 | it('Should have this caption updated and converted', async function () { | ||
127 | for (const server of servers) { | ||
128 | const body = await server.captions.list({ videoId: videoUUID }) | ||
129 | expect(body.total).to.equal(2) | ||
130 | expect(body.data).to.have.lengthOf(2) | ||
131 | |||
132 | const caption1 = body.data[0] | ||
133 | expect(caption1.language.id).to.equal('ar') | ||
134 | expect(caption1.language.label).to.equal('Arabic') | ||
135 | expect(caption1.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-ar.vtt$')) | ||
136 | |||
137 | const expected = 'WEBVTT FILE\r\n' + | ||
138 | '\r\n' + | ||
139 | '1\r\n' + | ||
140 | '00:00:01.600 --> 00:00:04.200\r\n' + | ||
141 | 'English (US)\r\n' + | ||
142 | '\r\n' + | ||
143 | '2\r\n' + | ||
144 | '00:00:05.900 --> 00:00:07.999\r\n' + | ||
145 | 'This is a subtitle in American English\r\n' + | ||
146 | '\r\n' + | ||
147 | '3\r\n' + | ||
148 | '00:00:10.000 --> 00:00:14.000\r\n' + | ||
149 | 'Adding subtitles is very easy to do\r\n' | ||
150 | await testCaptionFile(server.url, caption1.captionPath, expected) | ||
151 | } | ||
152 | }) | ||
153 | |||
154 | it('Should remove one caption', async function () { | ||
155 | this.timeout(30000) | ||
156 | |||
157 | await servers[0].captions.delete({ videoId: videoUUID, language: 'ar' }) | ||
158 | |||
159 | await waitJobs(servers) | ||
160 | }) | ||
161 | |||
162 | it('Should only list the caption that was not deleted', async function () { | ||
163 | for (const server of servers) { | ||
164 | const body = await server.captions.list({ videoId: videoUUID }) | ||
165 | expect(body.total).to.equal(1) | ||
166 | expect(body.data).to.have.lengthOf(1) | ||
167 | |||
168 | const caption = body.data[0] | ||
169 | |||
170 | expect(caption.language.id).to.equal('zh') | ||
171 | expect(caption.language.label).to.equal('Chinese') | ||
172 | expect(caption.captionPath).to.match(new RegExp('^/lazy-static/video-captions/' + uuidRegex + '-zh.vtt$')) | ||
173 | await testCaptionFile(server.url, caption.captionPath, 'Subtitle good 2.') | ||
174 | } | ||
175 | }) | ||
176 | |||
177 | it('Should remove the video, and thus all video captions', async function () { | ||
178 | const video = await servers[0].videos.get({ id: videoUUID }) | ||
179 | const { data: captions } = await servers[0].captions.list({ videoId: videoUUID }) | ||
180 | |||
181 | await servers[0].videos.remove({ id: videoUUID }) | ||
182 | |||
183 | await checkVideoFilesWereRemoved({ server: servers[0], video, captions }) | ||
184 | }) | ||
185 | |||
186 | after(async function () { | ||
187 | await cleanupTests(servers) | ||
188 | }) | ||
189 | }) | ||