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'
12 setAccessTokensToServers,
13 setDefaultAccountAvatar,
14 setDefaultChannelAvatar
15 } from '@shared/server-commands'
17 const expect = chai.expect
19 describe('Test a single server', function () {
21 function runSuite (mode: 'legacy' | 'resumable') {
22 let server: PeerTubeServer = null
23 let videoId: number | string
26 let videosListBase: any[] = null
28 const getCheckAttributes = () => ({
29 name: 'my super name',
34 description: 'my super description',
35 support: 'my super support text',
38 host: 'localhost:' + server.port
42 tags: [ 'tag1', 'tag2', 'tag3' ],
43 privacy: VideoPrivacy.PUBLIC,
44 commentsEnabled: true,
45 downloadEnabled: true,
47 displayName: 'Main root channel',
52 fixture: 'video_short.webm',
61 const updateCheckAttributes = () => ({
62 name: 'my super video updated',
67 description: 'my super description updated',
68 support: 'my super support text updated',
71 host: 'localhost:' + server.port
74 tags: [ 'tagup1', 'tagup2' ],
75 privacy: VideoPrivacy.PUBLIC,
77 commentsEnabled: false,
78 downloadEnabled: false,
81 displayName: 'Main root channel',
85 fixture: 'video_short3.webm',
94 before(async function () {
97 server = await createSingleServer(1)
99 await setAccessTokensToServers([ server ])
100 await setDefaultChannelAvatar(server)
101 await setDefaultAccountAvatar(server)
104 it('Should list video categories', async function () {
105 const categories = await server.videos.getCategories()
106 expect(Object.keys(categories)).to.have.length.above(10)
108 expect(categories[11]).to.equal('News & Politics')
111 it('Should list video licences', async function () {
112 const licences = await server.videos.getLicences()
113 expect(Object.keys(licences)).to.have.length.above(5)
115 expect(licences[3]).to.equal('Attribution - No Derivatives')
118 it('Should list video languages', async function () {
119 const languages = await server.videos.getLanguages()
120 expect(Object.keys(languages)).to.have.length.above(5)
122 expect(languages['ru']).to.equal('Russian')
125 it('Should list video privacies', async function () {
126 const privacies = await server.videos.getPrivacies()
127 expect(Object.keys(privacies)).to.have.length.at.least(3)
129 expect(privacies[3]).to.equal('Private')
132 it('Should not have videos', async function () {
133 const { data, total } = await server.videos.list()
135 expect(total).to.equal(0)
136 expect(data).to.be.an('array')
137 expect(data.length).to.equal(0)
140 it('Should upload the video', async function () {
144 name: 'my super name',
148 tags: [ 'tag1', 'tag2', 'tag3' ]
150 const video = await server.videos.upload({ attributes, mode })
151 expect(video).to.not.be.undefined
152 expect(video.id).to.equal(1)
153 expect(video.uuid).to.have.length.above(5)
156 videoUUID = video.uuid
159 it('Should get and seed the uploaded video', async function () {
162 const { data, total } = await server.videos.list()
164 expect(total).to.equal(1)
165 expect(data).to.be.an('array')
166 expect(data.length).to.equal(1)
168 const video = data[0]
169 await completeVideoCheck(server, video, getCheckAttributes())
172 it('Should get the video by UUID', async function () {
175 const video = await server.videos.get({ id: videoUUID })
176 await completeVideoCheck(server, video, getCheckAttributes())
179 it('Should have the views updated', async function () {
182 await server.views.simulateView({ id: videoId })
183 await server.views.simulateView({ id: videoId })
184 await server.views.simulateView({ id: videoId })
188 await server.views.simulateView({ id: videoId })
189 await server.views.simulateView({ id: videoId })
193 await server.views.simulateView({ id: videoId })
194 await server.views.simulateView({ id: videoId })
196 await server.debug.sendCommand({ body: { command: 'process-video-views-buffer' } })
198 const video = await server.videos.get({ id: videoId })
199 expect(video.views).to.equal(3)
202 it('Should remove the video', async function () {
203 const video = await server.videos.get({ id: videoId })
204 await server.videos.remove({ id: videoId })
206 await checkVideoFilesWereRemoved({ video, server })
209 it('Should not have videos', async function () {
210 const { total, data } = await server.videos.list()
212 expect(total).to.equal(0)
213 expect(data).to.be.an('array')
214 expect(data).to.have.lengthOf(0)
217 it('Should upload 6 videos', async function () {
220 const videos = new Set([
221 'video_short.mp4', 'video_short.ogv', 'video_short.webm',
222 'video_short1.webm', 'video_short2.webm', 'video_short3.webm'
225 for (const video of videos) {
227 name: video + ' name',
228 description: video + ' description',
233 tags: [ 'tag1', 'tag2', 'tag3' ],
237 await server.videos.upload({ attributes, mode })
241 it('Should have the correct durations', async function () {
242 const { total, data } = await server.videos.list()
244 expect(total).to.equal(6)
245 expect(data).to.be.an('array')
246 expect(data).to.have.lengthOf(6)
248 const videosByName: { [ name: string ]: Video } = {}
249 data.forEach(v => { videosByName[v.name] = v })
251 expect(videosByName['video_short.mp4 name'].duration).to.equal(5)
252 expect(videosByName['video_short.ogv name'].duration).to.equal(5)
253 expect(videosByName['video_short.webm name'].duration).to.equal(5)
254 expect(videosByName['video_short1.webm name'].duration).to.equal(10)
255 expect(videosByName['video_short2.webm name'].duration).to.equal(5)
256 expect(videosByName['video_short3.webm name'].duration).to.equal(5)
259 it('Should have the correct thumbnails', async function () {
260 const { data } = await server.videos.list()
263 videosListBase = data
265 for (const video of data) {
266 const videoName = video.name.replace(' name', '')
267 await testImage(server.url, videoName, video.thumbnailPath)
271 it('Should list only the two first videos', async function () {
272 const { total, data } = await server.videos.list({ start: 0, count: 2, sort: 'name' })
274 expect(total).to.equal(6)
275 expect(data.length).to.equal(2)
276 expect(data[0].name).to.equal(videosListBase[0].name)
277 expect(data[1].name).to.equal(videosListBase[1].name)
280 it('Should list only the next three videos', async function () {
281 const { total, data } = await server.videos.list({ start: 2, count: 3, sort: 'name' })
283 expect(total).to.equal(6)
284 expect(data.length).to.equal(3)
285 expect(data[0].name).to.equal(videosListBase[2].name)
286 expect(data[1].name).to.equal(videosListBase[3].name)
287 expect(data[2].name).to.equal(videosListBase[4].name)
290 it('Should list the last video', async function () {
291 const { total, data } = await server.videos.list({ start: 5, count: 6, sort: 'name' })
293 expect(total).to.equal(6)
294 expect(data.length).to.equal(1)
295 expect(data[0].name).to.equal(videosListBase[5].name)
298 it('Should not have the total field', async function () {
299 const { total, data } = await server.videos.list({ start: 5, count: 6, sort: 'name', skipCount: true })
301 expect(total).to.not.exist
302 expect(data.length).to.equal(1)
303 expect(data[0].name).to.equal(videosListBase[5].name)
306 it('Should list and sort by name in descending order', async function () {
307 const { total, data } = await server.videos.list({ sort: '-name' })
309 expect(total).to.equal(6)
310 expect(data.length).to.equal(6)
311 expect(data[0].name).to.equal('video_short.webm name')
312 expect(data[1].name).to.equal('video_short.ogv name')
313 expect(data[2].name).to.equal('video_short.mp4 name')
314 expect(data[3].name).to.equal('video_short3.webm name')
315 expect(data[4].name).to.equal('video_short2.webm name')
316 expect(data[5].name).to.equal('video_short1.webm name')
318 videoId = data[3].uuid
319 videoId2 = data[5].uuid
322 it('Should list and sort by trending in descending order', async function () {
323 const { total, data } = await server.videos.list({ start: 0, count: 2, sort: '-trending' })
325 expect(total).to.equal(6)
326 expect(data.length).to.equal(2)
329 it('Should list and sort by hotness in descending order', async function () {
330 const { total, data } = await server.videos.list({ start: 0, count: 2, sort: '-hot' })
332 expect(total).to.equal(6)
333 expect(data.length).to.equal(2)
336 it('Should list and sort by best in descending order', async function () {
337 const { total, data } = await server.videos.list({ start: 0, count: 2, sort: '-best' })
339 expect(total).to.equal(6)
340 expect(data.length).to.equal(2)
343 it('Should update a video', async function () {
345 name: 'my super video updated',
350 description: 'my super description updated',
351 commentsEnabled: false,
352 downloadEnabled: false,
353 tags: [ 'tagup1', 'tagup2' ]
355 await server.videos.update({ id: videoId, attributes })
358 it('Should have the video updated', async function () {
361 const video = await server.videos.get({ id: videoId })
363 await completeVideoCheck(server, video, updateCheckAttributes())
366 it('Should update only the tags of a video', async function () {
368 tags: [ 'supertag', 'tag1', 'tag2' ]
370 await server.videos.update({ id: videoId, attributes })
372 const video = await server.videos.get({ id: videoId })
374 await completeVideoCheck(server, video, Object.assign(updateCheckAttributes(), attributes))
377 it('Should update only the description of a video', async function () {
379 description: 'hello everybody'
381 await server.videos.update({ id: videoId, attributes })
383 const video = await server.videos.get({ id: videoId })
385 const expectedAttributes = Object.assign(updateCheckAttributes(), { tags: [ 'supertag', 'tag1', 'tag2' ] }, attributes)
386 await completeVideoCheck(server, video, expectedAttributes)
389 it('Should like a video', async function () {
390 await server.videos.rate({ id: videoId, rating: 'like' })
392 const video = await server.videos.get({ id: videoId })
394 expect(video.likes).to.equal(1)
395 expect(video.dislikes).to.equal(0)
398 it('Should dislike the same video', async function () {
399 await server.videos.rate({ id: videoId, rating: 'dislike' })
401 const video = await server.videos.get({ id: videoId })
403 expect(video.likes).to.equal(0)
404 expect(video.dislikes).to.equal(1)
407 it('Should sort by originallyPublishedAt', async function () {
409 const now = new Date()
410 const attributes = { originallyPublishedAt: now.toISOString() }
411 await server.videos.update({ id: videoId, attributes })
413 const { data } = await server.videos.list({ sort: '-originallyPublishedAt' })
414 const names = data.map(v => v.name)
416 expect(names[0]).to.equal('my super video updated')
417 expect(names[1]).to.equal('video_short2.webm name')
418 expect(names[2]).to.equal('video_short1.webm name')
419 expect(names[3]).to.equal('video_short.webm name')
420 expect(names[4]).to.equal('video_short.ogv name')
421 expect(names[5]).to.equal('video_short.mp4 name')
425 const now = new Date()
426 const attributes = { originallyPublishedAt: now.toISOString() }
427 await server.videos.update({ id: videoId2, attributes })
429 const { data } = await server.videos.list({ sort: '-originallyPublishedAt' })
430 const names = data.map(v => v.name)
432 expect(names[0]).to.equal('video_short1.webm name')
433 expect(names[1]).to.equal('my super video updated')
434 expect(names[2]).to.equal('video_short2.webm name')
435 expect(names[3]).to.equal('video_short.webm name')
436 expect(names[4]).to.equal('video_short.ogv name')
437 expect(names[5]).to.equal('video_short.mp4 name')
441 after(async function () {
442 await cleanupTests([ server ])
446 describe('Legacy upload', function () {
450 describe('Resumable upload', function () {
451 runSuite('resumable')