1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */
3 import { expect } from 'chai'
4 import { pathExists, readFile } from 'fs-extra'
5 import { join } from 'path'
6 import { root } from '@server/helpers/core-utils'
7 import { HttpStatusCode } from '@shared/models'
8 import { makeGetRequest } from '../requests'
9 import { PeerTubeServer } from '../server'
11 // Default interval -> 5 minutes
12 function dateIsValid (dateString: string, interval = 300000) {
13 const dateToCheck = new Date(dateString)
14 const now = new Date()
16 return Math.abs(now.getTime() - dateToCheck.getTime()) <= interval
19 async function testImage (url: string, imageName: string, imagePath: string, extension = '.jpg') {
20 const res = await makeGetRequest({
23 expectedStatus: HttpStatusCode.OK_200
28 const data = await readFile(join(root(), 'server', 'tests', 'fixtures', imageName + extension))
29 const minLength = body.length - ((30 * body.length) / 100)
30 const maxLength = body.length + ((30 * body.length) / 100)
32 expect(data.length).to.be.above(minLength, 'the generated image is way smaller than the recorded fixture')
33 expect(data.length).to.be.below(maxLength, 'the generated image is way larger than the recorded fixture')
36 async function testFileExistsOrNot (server: PeerTubeServer, directory: string, filePath: string, exist: boolean) {
37 const base = server.servers.buildDirectory(directory)
39 expect(await pathExists(join(base, filePath))).to.equal(exist)