1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { dateIsValid } from '@server/tests/shared'
11 setAccessTokensToServers,
13 } from '@shared/server-commands'
14 import { wait } from '@shared/core-utils'
15 import { uuid } from 'short-uuid'
17 const expect = chai.expect
19 describe('Test jobs', function () {
20 let servers: PeerTubeServer[]
22 before(async function () {
25 servers = await createMultipleServers(2)
27 await setAccessTokensToServers(servers)
29 // Server 1 and server 2 follow each other
30 await doubleFollow(servers[0], servers[1])
33 it('Should create some jobs', async function () {
36 await servers[1].videos.upload({ attributes: { name: 'video1' } })
37 await servers[1].videos.upload({ attributes: { name: 'video2' } })
39 await waitJobs(servers)
42 it('Should list jobs', async function () {
43 const body = await servers[1].jobs.list({ state: 'completed' })
44 expect(body.total).to.be.above(2)
45 expect(body.data).to.have.length.above(2)
48 it('Should list jobs with sort, pagination and job type', async function () {
50 const body = await servers[1].jobs.list({
56 expect(body.total).to.be.above(2)
57 expect(body.data).to.have.lengthOf(2)
59 let job = body.data[0]
61 if (job.type === 'videos-views-stats') job = body.data[1]
63 expect(job.state).to.equal('completed')
64 expect(job.type.startsWith('activitypub-')).to.be.true
65 expect(dateIsValid(job.createdAt as string)).to.be.true
66 expect(dateIsValid(job.processedOn as string)).to.be.true
67 expect(dateIsValid(job.finishedOn as string)).to.be.true
71 const body = await servers[1].jobs.list({
76 jobType: 'activitypub-http-broadcast'
78 expect(body.total).to.be.above(2)
80 for (const j of body.data) {
81 expect(j.type).to.equal('activitypub-http-broadcast')
86 it('Should list all jobs', async function () {
87 const body = await servers[1].jobs.list()
88 expect(body.total).to.be.above(2)
90 const jobs = body.data
91 expect(jobs).to.have.length.above(2)
93 expect(jobs.find(j => j.state === 'completed')).to.not.be.undefined
96 it('Should pause the job queue', async function () {
99 const { uuid } = await servers[1].videos.upload({ attributes: { name: 'video2' } })
100 await waitJobs(servers)
102 await servers[1].jobs.pauseJobQueue()
103 await servers[1].videos.runTranscoding({ videoId: uuid, transcodingType: 'hls' })
107 const body = await servers[1].jobs.list({ state: 'waiting', jobType: 'video-transcoding' })
108 expect(body.data).to.have.lengthOf(4)
111 it('Should resume the job queue', async function () {
114 await servers[1].jobs.resumeJobQueue()
116 await waitJobs(servers)
118 const body = await servers[1].jobs.list({ state: 'waiting', jobType: 'video-transcoding' })
119 expect(body.data).to.have.lengthOf(0)
122 after(async function () {
123 await cleanupTests(servers)