1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { checkVideoFilesWereRemoved, completeVideoCheck, testImage } from '@server/tests/shared'
6 import { wait } from '@shared/core-utils'
7 import { Video, VideoPrivacy } from '@shared/models'
8 import { cleanupTests, createSingleServer, PeerTubeServer, setAccessTokensToServers } from '@shared/server-commands'
10 const expect = chai.expect
12 describe('Test a single server', function () {
14 function runSuite (mode: 'legacy' | 'resumable') {
15 let server: PeerTubeServer = null
16 let videoId: number | string
19 let videosListBase: any[] = null
21 const getCheckAttributes = () => ({
22 name: 'my super name',
27 description: 'my super description',
28 support: 'my super support text',
31 host: 'localhost:' + server.port
35 tags: [ 'tag1', 'tag2', 'tag3' ],
36 privacy: VideoPrivacy.PUBLIC,
37 commentsEnabled: true,
38 downloadEnabled: true,
40 displayName: 'Main root channel',
45 fixture: 'video_short.webm',
54 const updateCheckAttributes = () => ({
55 name: 'my super video updated',
60 description: 'my super description updated',
61 support: 'my super support text updated',
64 host: 'localhost:' + server.port
67 tags: [ 'tagup1', 'tagup2' ],
68 privacy: VideoPrivacy.PUBLIC,
70 commentsEnabled: false,
71 downloadEnabled: false,
74 displayName: 'Main root channel',
78 fixture: 'video_short3.webm',
87 before(async function () {
90 server = await createSingleServer(1)
92 await setAccessTokensToServers([ server ])
95 it('Should list video categories', async function () {
96 const categories = await server.videos.getCategories()
97 expect(Object.keys(categories)).to.have.length.above(10)
99 expect(categories[11]).to.equal('News & Politics')
102 it('Should list video licences', async function () {
103 const licences = await server.videos.getLicences()
104 expect(Object.keys(licences)).to.have.length.above(5)
106 expect(licences[3]).to.equal('Attribution - No Derivatives')
109 it('Should list video languages', async function () {
110 const languages = await server.videos.getLanguages()
111 expect(Object.keys(languages)).to.have.length.above(5)
113 expect(languages['ru']).to.equal('Russian')
116 it('Should list video privacies', async function () {
117 const privacies = await server.videos.getPrivacies()
118 expect(Object.keys(privacies)).to.have.length.at.least(3)
120 expect(privacies[3]).to.equal('Private')
123 it('Should not have videos', async function () {
124 const { data, total } = await server.videos.list()
126 expect(total).to.equal(0)
127 expect(data).to.be.an('array')
128 expect(data.length).to.equal(0)
131 it('Should upload the video', async function () {
135 name: 'my super name',
139 tags: [ 'tag1', 'tag2', 'tag3' ]
141 const video = await server.videos.upload({ attributes, mode })
142 expect(video).to.not.be.undefined
143 expect(video.id).to.equal(1)
144 expect(video.uuid).to.have.length.above(5)
147 videoUUID = video.uuid
150 it('Should get and seed the uploaded video', async function () {
153 const { data, total } = await server.videos.list()
155 expect(total).to.equal(1)
156 expect(data).to.be.an('array')
157 expect(data.length).to.equal(1)
159 const video = data[0]
160 await completeVideoCheck(server, video, getCheckAttributes())
163 it('Should get the video by UUID', async function () {
166 const video = await server.videos.get({ id: videoUUID })
167 await completeVideoCheck(server, video, getCheckAttributes())
170 it('Should have the views updated', async function () {
173 await server.videos.view({ id: videoId })
174 await server.videos.view({ id: videoId })
175 await server.videos.view({ id: videoId })
179 await server.videos.view({ id: videoId })
180 await server.videos.view({ id: videoId })
184 await server.videos.view({ id: videoId })
185 await server.videos.view({ id: videoId })
187 // Wait the repeatable job
190 const video = await server.videos.get({ id: videoId })
191 expect(video.views).to.equal(3)
194 it('Should remove the video', async function () {
195 const video = await server.videos.get({ id: videoId })
196 await server.videos.remove({ id: videoId })
198 await checkVideoFilesWereRemoved({ video, server })
201 it('Should not have videos', async function () {
202 const { total, data } = await server.videos.list()
204 expect(total).to.equal(0)
205 expect(data).to.be.an('array')
206 expect(data).to.have.lengthOf(0)
209 it('Should upload 6 videos', async function () {
212 const videos = new Set([
213 'video_short.mp4', 'video_short.ogv', 'video_short.webm',
214 'video_short1.webm', 'video_short2.webm', 'video_short3.webm'
217 for (const video of videos) {
219 name: video + ' name',
220 description: video + ' description',
225 tags: [ 'tag1', 'tag2', 'tag3' ],
229 await server.videos.upload({ attributes, mode })
233 it('Should have the correct durations', async function () {
234 const { total, data } = await server.videos.list()
236 expect(total).to.equal(6)
237 expect(data).to.be.an('array')
238 expect(data).to.have.lengthOf(6)
240 const videosByName: { [ name: string ]: Video } = {}
241 data.forEach(v => { videosByName[v.name] = v })
243 expect(videosByName['video_short.mp4 name'].duration).to.equal(5)
244 expect(videosByName['video_short.ogv name'].duration).to.equal(5)
245 expect(videosByName['video_short.webm name'].duration).to.equal(5)
246 expect(videosByName['video_short1.webm name'].duration).to.equal(10)
247 expect(videosByName['video_short2.webm name'].duration).to.equal(5)
248 expect(videosByName['video_short3.webm name'].duration).to.equal(5)
251 it('Should have the correct thumbnails', async function () {
252 const { data } = await server.videos.list()
255 videosListBase = data
257 for (const video of data) {
258 const videoName = video.name.replace(' name', '')
259 await testImage(server.url, videoName, video.thumbnailPath)
263 it('Should list only the two first videos', async function () {
264 const { total, data } = await server.videos.list({ start: 0, count: 2, sort: 'name' })
266 expect(total).to.equal(6)
267 expect(data.length).to.equal(2)
268 expect(data[0].name).to.equal(videosListBase[0].name)
269 expect(data[1].name).to.equal(videosListBase[1].name)
272 it('Should list only the next three videos', async function () {
273 const { total, data } = await server.videos.list({ start: 2, count: 3, sort: 'name' })
275 expect(total).to.equal(6)
276 expect(data.length).to.equal(3)
277 expect(data[0].name).to.equal(videosListBase[2].name)
278 expect(data[1].name).to.equal(videosListBase[3].name)
279 expect(data[2].name).to.equal(videosListBase[4].name)
282 it('Should list the last video', async function () {
283 const { total, data } = await server.videos.list({ start: 5, count: 6, sort: 'name' })
285 expect(total).to.equal(6)
286 expect(data.length).to.equal(1)
287 expect(data[0].name).to.equal(videosListBase[5].name)
290 it('Should not have the total field', async function () {
291 const { total, data } = await server.videos.list({ start: 5, count: 6, sort: 'name', skipCount: true })
293 expect(total).to.not.exist
294 expect(data.length).to.equal(1)
295 expect(data[0].name).to.equal(videosListBase[5].name)
298 it('Should list and sort by name in descending order', async function () {
299 const { total, data } = await server.videos.list({ sort: '-name' })
301 expect(total).to.equal(6)
302 expect(data.length).to.equal(6)
303 expect(data[0].name).to.equal('video_short.webm name')
304 expect(data[1].name).to.equal('video_short.ogv name')
305 expect(data[2].name).to.equal('video_short.mp4 name')
306 expect(data[3].name).to.equal('video_short3.webm name')
307 expect(data[4].name).to.equal('video_short2.webm name')
308 expect(data[5].name).to.equal('video_short1.webm name')
310 videoId = data[3].uuid
311 videoId2 = data[5].uuid
314 it('Should list and sort by trending in descending order', async function () {
315 const { total, data } = await server.videos.list({ start: 0, count: 2, sort: '-trending' })
317 expect(total).to.equal(6)
318 expect(data.length).to.equal(2)
321 it('Should list and sort by hotness in descending order', async function () {
322 const { total, data } = await server.videos.list({ start: 0, count: 2, sort: '-hot' })
324 expect(total).to.equal(6)
325 expect(data.length).to.equal(2)
328 it('Should list and sort by best in descending order', async function () {
329 const { total, data } = await server.videos.list({ start: 0, count: 2, sort: '-best' })
331 expect(total).to.equal(6)
332 expect(data.length).to.equal(2)
335 it('Should update a video', async function () {
337 name: 'my super video updated',
342 description: 'my super description updated',
343 commentsEnabled: false,
344 downloadEnabled: false,
345 tags: [ 'tagup1', 'tagup2' ]
347 await server.videos.update({ id: videoId, attributes })
350 it('Should have the video updated', async function () {
353 const video = await server.videos.get({ id: videoId })
355 await completeVideoCheck(server, video, updateCheckAttributes())
358 it('Should update only the tags of a video', async function () {
360 tags: [ 'supertag', 'tag1', 'tag2' ]
362 await server.videos.update({ id: videoId, attributes })
364 const video = await server.videos.get({ id: videoId })
366 await completeVideoCheck(server, video, Object.assign(updateCheckAttributes(), attributes))
369 it('Should update only the description of a video', async function () {
371 description: 'hello everybody'
373 await server.videos.update({ id: videoId, attributes })
375 const video = await server.videos.get({ id: videoId })
377 const expectedAttributes = Object.assign(updateCheckAttributes(), { tags: [ 'supertag', 'tag1', 'tag2' ] }, attributes)
378 await completeVideoCheck(server, video, expectedAttributes)
381 it('Should like a video', async function () {
382 await server.videos.rate({ id: videoId, rating: 'like' })
384 const video = await server.videos.get({ id: videoId })
386 expect(video.likes).to.equal(1)
387 expect(video.dislikes).to.equal(0)
390 it('Should dislike the same video', async function () {
391 await server.videos.rate({ id: videoId, rating: 'dislike' })
393 const video = await server.videos.get({ id: videoId })
395 expect(video.likes).to.equal(0)
396 expect(video.dislikes).to.equal(1)
399 it('Should sort by originallyPublishedAt', async function () {
401 const now = new Date()
402 const attributes = { originallyPublishedAt: now.toISOString() }
403 await server.videos.update({ id: videoId, attributes })
405 const { data } = await server.videos.list({ sort: '-originallyPublishedAt' })
406 const names = data.map(v => v.name)
408 expect(names[0]).to.equal('my super video updated')
409 expect(names[1]).to.equal('video_short2.webm name')
410 expect(names[2]).to.equal('video_short1.webm name')
411 expect(names[3]).to.equal('video_short.webm name')
412 expect(names[4]).to.equal('video_short.ogv name')
413 expect(names[5]).to.equal('video_short.mp4 name')
417 const now = new Date()
418 const attributes = { originallyPublishedAt: now.toISOString() }
419 await server.videos.update({ id: videoId2, attributes })
421 const { data } = await server.videos.list({ sort: '-originallyPublishedAt' })
422 const names = data.map(v => v.name)
424 expect(names[0]).to.equal('video_short1.webm name')
425 expect(names[1]).to.equal('my super video updated')
426 expect(names[2]).to.equal('video_short2.webm name')
427 expect(names[3]).to.equal('video_short.webm name')
428 expect(names[4]).to.equal('video_short.ogv name')
429 expect(names[5]).to.equal('video_short.mp4 name')
433 after(async function () {
434 await cleanupTests([ server ])
438 describe('Legacy upload', function () {
442 describe('Resumable upload', function () {
443 runSuite('resumable')