]>
Commit | Line | Data |
---|---|---|
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */ | |
2 | ||
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' | |
10 | ||
11 | // Default interval -> 5 minutes | |
12 | function dateIsValid (dateString: string, interval = 300000) { | |
13 | const dateToCheck = new Date(dateString) | |
14 | const now = new Date() | |
15 | ||
16 | return Math.abs(now.getTime() - dateToCheck.getTime()) <= interval | |
17 | } | |
18 | ||
19 | function expectStartWith (str: string, start: string) { | |
20 | expect(str.startsWith(start), `${str} does not start with ${start}`).to.be.true | |
21 | } | |
22 | ||
23 | async function expectLogDoesNotContain (server: PeerTubeServer, str: string) { | |
24 | const content = await server.servers.getLogContent() | |
25 | ||
26 | expect(content.toString()).to.not.contain(str) | |
27 | } | |
28 | ||
29 | async function testImage (url: string, imageName: string, imagePath: string, extension = '.jpg') { | |
30 | const res = await makeGetRequest({ | |
31 | url, | |
32 | path: imagePath, | |
33 | expectedStatus: HttpStatusCode.OK_200 | |
34 | }) | |
35 | ||
36 | const body = res.body | |
37 | ||
38 | const data = await readFile(join(root(), 'server', 'tests', 'fixtures', imageName + extension)) | |
39 | const minLength = body.length - ((30 * body.length) / 100) | |
40 | const maxLength = body.length + ((30 * body.length) / 100) | |
41 | ||
42 | expect(data.length).to.be.above(minLength, 'the generated image is way smaller than the recorded fixture') | |
43 | expect(data.length).to.be.below(maxLength, 'the generated image is way larger than the recorded fixture') | |
44 | } | |
45 | ||
46 | async function testFileExistsOrNot (server: PeerTubeServer, directory: string, filePath: string, exist: boolean) { | |
47 | const base = server.servers.buildDirectory(directory) | |
48 | ||
49 | expect(await pathExists(join(base, filePath))).to.equal(exist) | |
50 | } | |
51 | ||
52 | export { | |
53 | dateIsValid, | |
54 | testImage, | |
55 | expectLogDoesNotContain, | |
56 | testFileExistsOrNot, | |
57 | expectStartWith | |
58 | } |