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.videos.view({ id: videoId })
183 await server.videos.view({ id: videoId })
184 await server.videos.view({ id: videoId })
188 await server.videos.view({ id: videoId })
189 await server.videos.view({ id: videoId })
193 await server.videos.view({ id: videoId })
194 await server.videos.view({ id: videoId })
196 // Wait the repeatable job
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 const video = await server.videos.get({ id: videoId })
364 await completeVideoCheck(server, video, updateCheckAttributes())
367 it('Should update only the tags of a video', async function () {
369 tags: [ 'supertag', 'tag1', 'tag2' ]
371 await server.videos.update({ id: videoId, attributes })
373 const video = await server.videos.get({ id: videoId })
375 await completeVideoCheck(server, video, Object.assign(updateCheckAttributes(), attributes))
378 it('Should update only the description of a video', async function () {
380 description: 'hello everybody'
382 await server.videos.update({ id: videoId, attributes })
384 const video = await server.videos.get({ id: videoId })
386 const expectedAttributes = Object.assign(updateCheckAttributes(), { tags: [ 'supertag', 'tag1', 'tag2' ] }, attributes)
387 await completeVideoCheck(server, video, expectedAttributes)
390 it('Should like a video', async function () {
391 await server.videos.rate({ id: videoId, rating: 'like' })
393 const video = await server.videos.get({ id: videoId })
395 expect(video.likes).to.equal(1)
396 expect(video.dislikes).to.equal(0)
399 it('Should dislike the same video', async function () {
400 await server.videos.rate({ id: videoId, rating: 'dislike' })
402 const video = await server.videos.get({ id: videoId })
404 expect(video.likes).to.equal(0)
405 expect(video.dislikes).to.equal(1)
408 it('Should sort by originallyPublishedAt', async function () {
410 const now = new Date()
411 const attributes = { originallyPublishedAt: now.toISOString() }
412 await server.videos.update({ id: videoId, attributes })
414 const { data } = await server.videos.list({ sort: '-originallyPublishedAt' })
415 const names = data.map(v => v.name)
417 expect(names[0]).to.equal('my super video updated')
418 expect(names[1]).to.equal('video_short2.webm name')
419 expect(names[2]).to.equal('video_short1.webm name')
420 expect(names[3]).to.equal('video_short.webm name')
421 expect(names[4]).to.equal('video_short.ogv name')
422 expect(names[5]).to.equal('video_short.mp4 name')
426 const now = new Date()
427 const attributes = { originallyPublishedAt: now.toISOString() }
428 await server.videos.update({ id: videoId2, attributes })
430 const { data } = await server.videos.list({ sort: '-originallyPublishedAt' })
431 const names = data.map(v => v.name)
433 expect(names[0]).to.equal('video_short1.webm name')
434 expect(names[1]).to.equal('my super video updated')
435 expect(names[2]).to.equal('video_short2.webm name')
436 expect(names[3]).to.equal('video_short.webm name')
437 expect(names[4]).to.equal('video_short.ogv name')
438 expect(names[5]).to.equal('video_short.mp4 name')
442 after(async function () {
443 await cleanupTests([ server ])
447 describe('Legacy upload', function () {
451 describe('Resumable upload', function () {
452 runSuite('resumable')