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,
16 } from '@shared/server-commands'
18 const expect = chai.expect
20 describe('Test a single server', function () {
22 function runSuite (mode: 'legacy' | 'resumable') {
23 let server: PeerTubeServer = null
24 let videoId: number | string
27 let videosListBase: any[] = null
29 const getCheckAttributes = () => ({
30 name: 'my super name',
35 description: 'my super description',
36 support: 'my super support text',
39 host: 'localhost:' + server.port
43 tags: [ 'tag1', 'tag2', 'tag3' ],
44 privacy: VideoPrivacy.PUBLIC,
45 commentsEnabled: true,
46 downloadEnabled: true,
48 displayName: 'Main root channel',
53 fixture: 'video_short.webm',
62 const updateCheckAttributes = () => ({
63 name: 'my super video updated',
68 description: 'my super description updated',
69 support: 'my super support text updated',
72 host: 'localhost:' + server.port
75 tags: [ 'tagup1', 'tagup2' ],
76 privacy: VideoPrivacy.PUBLIC,
78 commentsEnabled: false,
79 downloadEnabled: false,
82 displayName: 'Main root channel',
86 fixture: 'video_short3.webm',
95 before(async function () {
98 server = await createSingleServer(1)
100 await setAccessTokensToServers([ server ])
101 await setDefaultChannelAvatar(server)
102 await setDefaultAccountAvatar(server)
105 it('Should list video categories', async function () {
106 const categories = await server.videos.getCategories()
107 expect(Object.keys(categories)).to.have.length.above(10)
109 expect(categories[11]).to.equal('News & Politics')
112 it('Should list video licences', async function () {
113 const licences = await server.videos.getLicences()
114 expect(Object.keys(licences)).to.have.length.above(5)
116 expect(licences[3]).to.equal('Attribution - No Derivatives')
119 it('Should list video languages', async function () {
120 const languages = await server.videos.getLanguages()
121 expect(Object.keys(languages)).to.have.length.above(5)
123 expect(languages['ru']).to.equal('Russian')
126 it('Should list video privacies', async function () {
127 const privacies = await server.videos.getPrivacies()
128 expect(Object.keys(privacies)).to.have.length.at.least(3)
130 expect(privacies[3]).to.equal('Private')
133 it('Should not have videos', async function () {
134 const { data, total } = await server.videos.list()
136 expect(total).to.equal(0)
137 expect(data).to.be.an('array')
138 expect(data.length).to.equal(0)
141 it('Should upload the video', async function () {
145 name: 'my super name',
149 tags: [ 'tag1', 'tag2', 'tag3' ]
151 const video = await server.videos.upload({ attributes, mode })
152 expect(video).to.not.be.undefined
153 expect(video.id).to.equal(1)
154 expect(video.uuid).to.have.length.above(5)
157 videoUUID = video.uuid
160 it('Should get and seed the uploaded video', async function () {
163 const { data, total } = await server.videos.list()
165 expect(total).to.equal(1)
166 expect(data).to.be.an('array')
167 expect(data.length).to.equal(1)
169 const video = data[0]
170 await completeVideoCheck(server, video, getCheckAttributes())
173 it('Should get the video by UUID', async function () {
176 const video = await server.videos.get({ id: videoUUID })
177 await completeVideoCheck(server, video, getCheckAttributes())
180 it('Should have the views updated', async function () {
183 await server.views.simulateView({ id: videoId })
184 await server.views.simulateView({ id: videoId })
185 await server.views.simulateView({ id: videoId })
189 await server.views.simulateView({ id: videoId })
190 await server.views.simulateView({ id: videoId })
194 await server.views.simulateView({ id: videoId })
195 await server.views.simulateView({ id: videoId })
197 await server.debug.sendCommand({ body: { command: 'process-video-views-buffer' } })
199 const video = await server.videos.get({ id: videoId })
200 expect(video.views).to.equal(3)
203 it('Should remove the video', async function () {
204 const video = await server.videos.get({ id: videoId })
205 await server.videos.remove({ id: videoId })
207 await checkVideoFilesWereRemoved({ video, server })
210 it('Should not have videos', async function () {
211 const { total, data } = await server.videos.list()
213 expect(total).to.equal(0)
214 expect(data).to.be.an('array')
215 expect(data).to.have.lengthOf(0)
218 it('Should upload 6 videos', async function () {
221 const videos = new Set([
222 'video_short.mp4', 'video_short.ogv', 'video_short.webm',
223 'video_short1.webm', 'video_short2.webm', 'video_short3.webm'
226 for (const video of videos) {
228 name: video + ' name',
229 description: video + ' description',
234 tags: [ 'tag1', 'tag2', 'tag3' ],
238 await server.videos.upload({ attributes, mode })
242 it('Should have the correct durations', async function () {
243 const { total, data } = await server.videos.list()
245 expect(total).to.equal(6)
246 expect(data).to.be.an('array')
247 expect(data).to.have.lengthOf(6)
249 const videosByName: { [ name: string ]: Video } = {}
250 data.forEach(v => { videosByName[v.name] = v })
252 expect(videosByName['video_short.mp4 name'].duration).to.equal(5)
253 expect(videosByName['video_short.ogv name'].duration).to.equal(5)
254 expect(videosByName['video_short.webm name'].duration).to.equal(5)
255 expect(videosByName['video_short1.webm name'].duration).to.equal(10)
256 expect(videosByName['video_short2.webm name'].duration).to.equal(5)
257 expect(videosByName['video_short3.webm name'].duration).to.equal(5)
260 it('Should have the correct thumbnails', async function () {
261 const { data } = await server.videos.list()
264 videosListBase = data
266 for (const video of data) {
267 const videoName = video.name.replace(' name', '')
268 await testImage(server.url, videoName, video.thumbnailPath)
272 it('Should list only the two first videos', async function () {
273 const { total, data } = await server.videos.list({ start: 0, count: 2, sort: 'name' })
275 expect(total).to.equal(6)
276 expect(data.length).to.equal(2)
277 expect(data[0].name).to.equal(videosListBase[0].name)
278 expect(data[1].name).to.equal(videosListBase[1].name)
281 it('Should list only the next three videos', async function () {
282 const { total, data } = await server.videos.list({ start: 2, count: 3, sort: 'name' })
284 expect(total).to.equal(6)
285 expect(data.length).to.equal(3)
286 expect(data[0].name).to.equal(videosListBase[2].name)
287 expect(data[1].name).to.equal(videosListBase[3].name)
288 expect(data[2].name).to.equal(videosListBase[4].name)
291 it('Should list the last video', async function () {
292 const { total, data } = await server.videos.list({ start: 5, count: 6, sort: 'name' })
294 expect(total).to.equal(6)
295 expect(data.length).to.equal(1)
296 expect(data[0].name).to.equal(videosListBase[5].name)
299 it('Should not have the total field', async function () {
300 const { total, data } = await server.videos.list({ start: 5, count: 6, sort: 'name', skipCount: true })
302 expect(total).to.not.exist
303 expect(data.length).to.equal(1)
304 expect(data[0].name).to.equal(videosListBase[5].name)
307 it('Should list and sort by name in descending order', async function () {
308 const { total, data } = await server.videos.list({ sort: '-name' })
310 expect(total).to.equal(6)
311 expect(data.length).to.equal(6)
312 expect(data[0].name).to.equal('video_short.webm name')
313 expect(data[1].name).to.equal('video_short.ogv name')
314 expect(data[2].name).to.equal('video_short.mp4 name')
315 expect(data[3].name).to.equal('video_short3.webm name')
316 expect(data[4].name).to.equal('video_short2.webm name')
317 expect(data[5].name).to.equal('video_short1.webm name')
319 videoId = data[3].uuid
320 videoId2 = data[5].uuid
323 it('Should list and sort by trending in descending order', async function () {
324 const { total, data } = await server.videos.list({ start: 0, count: 2, sort: '-trending' })
326 expect(total).to.equal(6)
327 expect(data.length).to.equal(2)
330 it('Should list and sort by hotness in descending order', async function () {
331 const { total, data } = await server.videos.list({ start: 0, count: 2, sort: '-hot' })
333 expect(total).to.equal(6)
334 expect(data.length).to.equal(2)
337 it('Should list and sort by best in descending order', async function () {
338 const { total, data } = await server.videos.list({ start: 0, count: 2, sort: '-best' })
340 expect(total).to.equal(6)
341 expect(data.length).to.equal(2)
344 it('Should update a video', async function () {
346 name: 'my super video updated',
351 description: 'my super description updated',
352 commentsEnabled: false,
353 downloadEnabled: false,
354 tags: [ 'tagup1', 'tagup2' ]
356 await server.videos.update({ id: videoId, attributes })
359 it('Should have the video updated', async function () {
362 await waitJobs([ server ])
364 const video = await server.videos.get({ id: videoId })
366 await completeVideoCheck(server, video, updateCheckAttributes())
369 it('Should update only the tags of a video', async function () {
371 tags: [ 'supertag', 'tag1', 'tag2' ]
373 await server.videos.update({ id: videoId, attributes })
375 const video = await server.videos.get({ id: videoId })
377 await completeVideoCheck(server, video, Object.assign(updateCheckAttributes(), attributes))
380 it('Should update only the description of a video', async function () {
382 description: 'hello everybody'
384 await server.videos.update({ id: videoId, attributes })
386 const video = await server.videos.get({ id: videoId })
388 const expectedAttributes = Object.assign(updateCheckAttributes(), { tags: [ 'supertag', 'tag1', 'tag2' ] }, attributes)
389 await completeVideoCheck(server, video, expectedAttributes)
392 it('Should like a video', async function () {
393 await server.videos.rate({ id: videoId, rating: 'like' })
395 const video = await server.videos.get({ id: videoId })
397 expect(video.likes).to.equal(1)
398 expect(video.dislikes).to.equal(0)
401 it('Should dislike the same video', async function () {
402 await server.videos.rate({ id: videoId, rating: 'dislike' })
404 const video = await server.videos.get({ id: videoId })
406 expect(video.likes).to.equal(0)
407 expect(video.dislikes).to.equal(1)
410 it('Should sort by originallyPublishedAt', async function () {
412 const now = new Date()
413 const attributes = { originallyPublishedAt: now.toISOString() }
414 await server.videos.update({ id: videoId, attributes })
416 const { data } = await server.videos.list({ sort: '-originallyPublishedAt' })
417 const names = data.map(v => v.name)
419 expect(names[0]).to.equal('my super video updated')
420 expect(names[1]).to.equal('video_short2.webm name')
421 expect(names[2]).to.equal('video_short1.webm name')
422 expect(names[3]).to.equal('video_short.webm name')
423 expect(names[4]).to.equal('video_short.ogv name')
424 expect(names[5]).to.equal('video_short.mp4 name')
428 const now = new Date()
429 const attributes = { originallyPublishedAt: now.toISOString() }
430 await server.videos.update({ id: videoId2, attributes })
432 const { data } = await server.videos.list({ sort: '-originallyPublishedAt' })
433 const names = data.map(v => v.name)
435 expect(names[0]).to.equal('video_short1.webm name')
436 expect(names[1]).to.equal('my super video updated')
437 expect(names[2]).to.equal('video_short2.webm name')
438 expect(names[3]).to.equal('video_short.webm name')
439 expect(names[4]).to.equal('video_short.ogv name')
440 expect(names[5]).to.equal('video_short.mp4 name')
444 after(async function () {
445 await cleanupTests([ server ])
449 describe('Legacy upload', function () {
453 describe('Resumable upload', function () {
454 runSuite('resumable')