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'
6 import { wait } from '@shared/core-utils'
12 setAccessTokensToServers,
14 } from '@shared/server-commands'
16 const expect = chai.expect
18 describe('Test jobs', function () {
19 let servers: PeerTubeServer[]
21 before(async function () {
24 servers = await createMultipleServers(2)
26 await setAccessTokensToServers(servers)
28 // Server 1 and server 2 follow each other
29 await doubleFollow(servers[0], servers[1])
32 it('Should create some jobs', async function () {
35 await servers[1].videos.upload({ attributes: { name: 'video1' } })
36 await servers[1].videos.upload({ attributes: { name: 'video2' } })
38 await waitJobs(servers)
41 it('Should list jobs', async function () {
42 const body = await servers[1].jobs.list({ state: 'completed' })
43 expect(body.total).to.be.above(2)
44 expect(body.data).to.have.length.above(2)
47 it('Should list jobs with sort, pagination and job type', async function () {
49 const body = await servers[1].jobs.list({
55 expect(body.total).to.be.above(2)
56 expect(body.data).to.have.lengthOf(2)
58 let job = body.data[0]
60 if (job.type === 'videos-views-stats') job = body.data[1]
62 expect(job.state).to.equal('completed')
63 expect(job.type.startsWith('activitypub-')).to.be.true
64 expect(dateIsValid(job.createdAt as string)).to.be.true
65 expect(dateIsValid(job.processedOn as string)).to.be.true
66 expect(dateIsValid(job.finishedOn as string)).to.be.true
70 const body = await servers[1].jobs.list({
75 jobType: 'activitypub-http-broadcast'
77 expect(body.total).to.be.above(2)
79 for (const j of body.data) {
80 expect(j.type).to.equal('activitypub-http-broadcast')
85 it('Should list all jobs', async function () {
86 const body = await servers[1].jobs.list()
87 expect(body.total).to.be.above(2)
89 const jobs = body.data
90 expect(jobs).to.have.length.above(2)
92 expect(jobs.find(j => j.state === 'completed')).to.not.be.undefined
95 it('Should pause the job queue', async function () {
98 const { uuid } = await servers[1].videos.upload({ attributes: { name: 'video2' } })
99 await waitJobs(servers)
101 await servers[1].jobs.pauseJobQueue()
102 await servers[1].videos.runTranscoding({ videoId: uuid, transcodingType: 'hls' })
106 const body = await servers[1].jobs.list({ state: 'waiting', jobType: 'video-transcoding' })
107 expect(body.data).to.have.lengthOf(4)
110 it('Should resume the job queue', async function () {
113 await servers[1].jobs.resumeJobQueue()
115 await waitJobs(servers)
117 const body = await servers[1].jobs.list({ state: 'waiting', jobType: 'video-transcoding' })
118 expect(body.data).to.have.lengthOf(0)
121 after(async function () {
122 await cleanupTests(servers)