1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } from 'chai'
4 import { dateIsValid } from '@server/tests/shared'
5 import { wait } from '@shared/core-utils'
11 setAccessTokensToServers,
13 } from '@shared/server-commands'
15 describe('Test jobs', function () {
16 let servers: PeerTubeServer[]
18 before(async function () {
21 servers = await createMultipleServers(2)
23 await setAccessTokensToServers(servers)
25 // Server 1 and server 2 follow each other
26 await doubleFollow(servers[0], servers[1])
29 it('Should create some jobs', async function () {
32 await servers[1].videos.upload({ attributes: { name: 'video1' } })
33 await servers[1].videos.upload({ attributes: { name: 'video2' } })
35 await waitJobs(servers)
38 it('Should list jobs', async function () {
39 const body = await servers[1].jobs.list({ state: 'completed' })
40 expect(body.total).to.be.above(2)
41 expect(body.data).to.have.length.above(2)
44 it('Should list jobs with sort, pagination and job type', async function () {
46 const body = await servers[1].jobs.list({
52 expect(body.total).to.be.above(2)
53 expect(body.data).to.have.lengthOf(2)
55 let job = body.data[0]
57 if (job.type === 'videos-views-stats') job = body.data[1]
59 expect(job.state).to.equal('completed')
60 expect(dateIsValid(job.createdAt as string)).to.be.true
61 expect(dateIsValid(job.processedOn as string)).to.be.true
62 expect(dateIsValid(job.finishedOn as string)).to.be.true
66 const body = await servers[1].jobs.list({
71 jobType: 'activitypub-http-broadcast'
73 expect(body.total).to.be.above(2)
75 for (const j of body.data) {
76 expect(j.type).to.equal('activitypub-http-broadcast')
81 it('Should list all jobs', async function () {
82 const body = await servers[1].jobs.list()
83 expect(body.total).to.be.above(2)
85 const jobs = body.data
86 expect(jobs).to.have.length.above(2)
88 expect(jobs.find(j => j.state === 'completed')).to.not.be.undefined
91 it('Should pause the job queue', async function () {
94 const { uuid } = await servers[1].videos.upload({ attributes: { name: 'video2' } })
95 await waitJobs(servers)
97 await servers[1].jobs.pauseJobQueue()
98 await servers[1].videos.runTranscoding({ videoId: uuid, transcodingType: 'hls' })
103 const body = await servers[1].jobs.list({ state: 'waiting', jobType: 'video-transcoding' })
104 // waiting includes waiting-children
105 expect(body.data).to.have.lengthOf(4)
109 const body = await servers[1].jobs.list({ state: 'waiting-children', jobType: 'video-transcoding' })
110 expect(body.data).to.have.lengthOf(1)
114 it('Should resume the job queue', async function () {
117 await servers[1].jobs.resumeJobQueue()
119 await waitJobs(servers)
121 const body = await servers[1].jobs.list({ state: 'waiting', jobType: 'video-transcoding' })
122 expect(body.data).to.have.lengthOf(0)
125 after(async function () {
126 await cleanupTests(servers)