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 JPEG from 'jpeg-js'
6 import { join } from 'path'
7 import pixelmatch from 'pixelmatch'
8 import { PNG } from 'pngjs'
9 import { root } from '@shared/core-utils'
10 import { HttpStatusCode } from '@shared/models'
11 import { makeGetRequest, PeerTubeServer } from '@shared/server-commands'
13 // Default interval -> 5 minutes
14 function dateIsValid (dateString: string, interval = 300000) {
15 const dateToCheck = new Date(dateString)
16 const now = new Date()
18 return Math.abs(now.getTime() - dateToCheck.getTime()) <= interval
21 function expectStartWith (str: string, start: string) {
22 expect(str.startsWith(start), `${str} does not start with ${start}`).to.be.true
25 function expectNotStartWith (str: string, start: string) {
26 expect(str.startsWith(start), `${str} does not start with ${start}`).to.be.false
29 function expectEndWith (str: string, end: string) {
30 expect(str.endsWith(end), `${str} does not end with ${end}`).to.be.true
33 // ---------------------------------------------------------------------------
35 async function expectLogDoesNotContain (server: PeerTubeServer, str: string) {
36 const content = await server.servers.getLogContent()
38 expect(content.toString()).to.not.contain(str)
41 async function expectLogContain (server: PeerTubeServer, str: string) {
42 const content = await server.servers.getLogContent()
44 expect(content.toString()).to.contain(str)
47 async function testImageSize (url: string, imageName: string, imageHTTPPath: string, extension = '.jpg') {
48 const res = await makeGetRequest({
51 expectedStatus: HttpStatusCode.OK_200
56 const data = await readFile(join(root(), 'server', 'tests', 'fixtures', imageName + extension))
57 const minLength = data.length - ((40 * data.length) / 100)
58 const maxLength = data.length + ((40 * data.length) / 100)
60 expect(body.length).to.be.above(minLength, 'the generated image is way smaller than the recorded fixture')
61 expect(body.length).to.be.below(maxLength, 'the generated image is way larger than the recorded fixture')
64 async function testImage (url: string, imageName: string, imageHTTPPath: string, extension = '.jpg') {
65 const res = await makeGetRequest({
68 expectedStatus: HttpStatusCode.OK_200
72 const data = await readFile(join(root(), 'server', 'tests', 'fixtures', imageName + extension))
74 const img1 = imageHTTPPath.endsWith('.png')
78 const img2 = extension === '.png'
82 const result = pixelmatch(img1.data, img2.data, null, img1.width, img1.height, { threshold: 0.1 })
84 expect(result).to.equal(0, `${imageHTTPPath} image is not the same as ${imageName}${extension}`)
87 async function testFileExistsOrNot (server: PeerTubeServer, directory: string, filePath: string, exist: boolean) {
88 const base = server.servers.buildDirectory(directory)
90 expect(await pathExists(join(base, filePath))).to.equal(exist)
93 function checkBadStartPagination (url: string, path: string, token?: string, query = {}) {
94 return makeGetRequest({
98 query: { ...query, start: 'hello' },
99 expectedStatus: HttpStatusCode.BAD_REQUEST_400
103 async function checkBadCountPagination (url: string, path: string, token?: string, query = {}) {
104 await makeGetRequest({
108 query: { ...query, count: 'hello' },
109 expectedStatus: HttpStatusCode.BAD_REQUEST_400
112 await makeGetRequest({
116 query: { ...query, count: 2000 },
117 expectedStatus: HttpStatusCode.BAD_REQUEST_400
121 function checkBadSortPagination (url: string, path: string, token?: string, query = {}) {
122 return makeGetRequest({
126 query: { ...query, sort: 'hello' },
127 expectedStatus: HttpStatusCode.BAD_REQUEST_400
135 expectLogDoesNotContain,
140 checkBadStartPagination,
141 checkBadCountPagination,
142 checkBadSortPagination,