]>
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 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' | |
12 | ||
13 | // Default interval -> 5 minutes | |
14 | function dateIsValid (dateString: string | Date, interval = 300000) { | |
15 | const dateToCheck = new Date(dateString) | |
16 | const now = new Date() | |
17 | ||
18 | return Math.abs(now.getTime() - dateToCheck.getTime()) <= interval | |
19 | } | |
20 | ||
21 | function expectStartWith (str: string, start: string) { | |
22 | expect(str.startsWith(start), `${str} does not start with ${start}`).to.be.true | |
23 | } | |
24 | ||
25 | function expectNotStartWith (str: string, start: string) { | |
26 | expect(str.startsWith(start), `${str} does not start with ${start}`).to.be.false | |
27 | } | |
28 | ||
29 | function expectEndWith (str: string, end: string) { | |
30 | expect(str.endsWith(end), `${str} does not end with ${end}`).to.be.true | |
31 | } | |
32 | ||
33 | // --------------------------------------------------------------------------- | |
34 | ||
35 | async function expectLogDoesNotContain (server: PeerTubeServer, str: string) { | |
36 | const content = await server.servers.getLogContent() | |
37 | ||
38 | expect(content.toString()).to.not.contain(str) | |
39 | } | |
40 | ||
41 | async function expectLogContain (server: PeerTubeServer, str: string) { | |
42 | const content = await server.servers.getLogContent() | |
43 | ||
44 | expect(content.toString()).to.contain(str) | |
45 | } | |
46 | ||
47 | async function testImageSize (url: string, imageName: string, imageHTTPPath: string, extension = '.jpg') { | |
48 | const res = await makeGetRequest({ | |
49 | url, | |
50 | path: imageHTTPPath, | |
51 | expectedStatus: HttpStatusCode.OK_200 | |
52 | }) | |
53 | ||
54 | const body = res.body | |
55 | ||
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) | |
59 | ||
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') | |
62 | } | |
63 | ||
64 | async function testImage (url: string, imageName: string, imageHTTPPath: string, extension = '.jpg') { | |
65 | const res = await makeGetRequest({ | |
66 | url, | |
67 | path: imageHTTPPath, | |
68 | expectedStatus: HttpStatusCode.OK_200 | |
69 | }) | |
70 | ||
71 | const body = res.body | |
72 | const data = await readFile(join(root(), 'server', 'tests', 'fixtures', imageName + extension)) | |
73 | ||
74 | const img1 = imageHTTPPath.endsWith('.png') | |
75 | ? PNG.sync.read(body) | |
76 | : JPEG.decode(body) | |
77 | ||
78 | const img2 = extension === '.png' | |
79 | ? PNG.sync.read(data) | |
80 | : JPEG.decode(data) | |
81 | ||
82 | const result = pixelmatch(img1.data, img2.data, null, img1.width, img1.height, { threshold: 0.1 }) | |
83 | ||
84 | expect(result).to.equal(0, `${imageHTTPPath} image is not the same as ${imageName}${extension}`) | |
85 | } | |
86 | ||
87 | async function testFileExistsOrNot (server: PeerTubeServer, directory: string, filePath: string, exist: boolean) { | |
88 | const base = server.servers.buildDirectory(directory) | |
89 | ||
90 | expect(await pathExists(join(base, filePath))).to.equal(exist) | |
91 | } | |
92 | ||
93 | // --------------------------------------------------------------------------- | |
94 | ||
95 | function checkBadStartPagination (url: string, path: string, token?: string, query = {}) { | |
96 | return makeGetRequest({ | |
97 | url, | |
98 | path, | |
99 | token, | |
100 | query: { ...query, start: 'hello' }, | |
101 | expectedStatus: HttpStatusCode.BAD_REQUEST_400 | |
102 | }) | |
103 | } | |
104 | ||
105 | async function checkBadCountPagination (url: string, path: string, token?: string, query = {}) { | |
106 | await makeGetRequest({ | |
107 | url, | |
108 | path, | |
109 | token, | |
110 | query: { ...query, count: 'hello' }, | |
111 | expectedStatus: HttpStatusCode.BAD_REQUEST_400 | |
112 | }) | |
113 | ||
114 | await makeGetRequest({ | |
115 | url, | |
116 | path, | |
117 | token, | |
118 | query: { ...query, count: 2000 }, | |
119 | expectedStatus: HttpStatusCode.BAD_REQUEST_400 | |
120 | }) | |
121 | } | |
122 | ||
123 | function checkBadSortPagination (url: string, path: string, token?: string, query = {}) { | |
124 | return makeGetRequest({ | |
125 | url, | |
126 | path, | |
127 | token, | |
128 | query: { ...query, sort: 'hello' }, | |
129 | expectedStatus: HttpStatusCode.BAD_REQUEST_400 | |
130 | }) | |
131 | } | |
132 | ||
133 | export { | |
134 | dateIsValid, | |
135 | testImageSize, | |
136 | testImage, | |
137 | expectLogDoesNotContain, | |
138 | testFileExistsOrNot, | |
139 | expectStartWith, | |
140 | expectNotStartWith, | |
141 | expectEndWith, | |
142 | checkBadStartPagination, | |
143 | checkBadCountPagination, | |
144 | checkBadSortPagination, | |
145 | expectLogContain | |
146 | } |