import * as request from 'supertest'
-import { Job, JobState } from '../../models'
+import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
+import { makeGetRequest } from '../../../shared/extra-utils'
+import { Job, JobState, JobType } from '../../models'
import { wait } from '../miscs/miscs'
import { ServerInfo } from './servers'
-function getJobsList (url: string, accessToken: string, state: JobState) {
- const path = '/api/v1/jobs/' + state
+function buildJobsUrl (state?: JobState) {
+ let path = '/api/v1/jobs'
- return request(url)
- .get(path)
- .set('Accept', 'application/json')
- .set('Authorization', 'Bearer ' + accessToken)
- .expect(200)
- .expect('Content-Type', /json/)
+ if (state) path += '/' + state
+
+ return path
}
-function getJobsListPaginationAndSort (url: string, accessToken: string, state: JobState, start: number, count: number, sort: string) {
- const path = '/api/v1/jobs/' + state
+function getJobsList (url: string, accessToken: string, state?: JobState) {
+ const path = buildJobsUrl(state)
return request(url)
- .get(path)
- .query({ start })
- .query({ count })
- .query({ sort })
- .set('Accept', 'application/json')
- .set('Authorization', 'Bearer ' + accessToken)
- .expect(200)
- .expect('Content-Type', /json/)
+ .get(path)
+ .set('Accept', 'application/json')
+ .set('Authorization', 'Bearer ' + accessToken)
+ .expect(HttpStatusCode.OK_200)
+ .expect('Content-Type', /json/)
+}
+
+function getJobsListPaginationAndSort (options: {
+ url: string
+ accessToken: string
+ start: number
+ count: number
+ sort: string
+ state?: JobState
+ jobType?: JobType
+}) {
+ const { url, accessToken, state, start, count, sort, jobType } = options
+ const path = buildJobsUrl(state)
+
+ const query = {
+ start,
+ count,
+ sort,
+ jobType
+ }
+
+ return makeGetRequest({
+ url,
+ path,
+ token: accessToken,
+ statusCodeExpected: HttpStatusCode.OK_200,
+ query
+ })
}
async function waitJobs (serversArg: ServerInfo[] | ServerInfo) {
// Check if each server has pending request
for (const server of servers) {
for (const state of states) {
- const p = getJobsListPaginationAndSort(server.url, server.accessToken, state, 0, 10, '-createdAt')
+ const p = getJobsListPaginationAndSort({
+ url: server.url,
+ accessToken: server.accessToken,
+ state: state,
+ start: 0,
+ count: 10,
+ sort: '-createdAt'
+ })
.then(res => res.body.data)
.then((jobs: Job[]) => jobs.filter(j => j.type !== 'videos-views'))
.then(jobs => {