diff options
author | Chocobozzz <me@florianbigard.com> | 2023-02-24 14:48:15 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2023-02-24 15:10:13 +0100 |
commit | d41f4a6dc69d098e9dc9173b7e1a586695ef7b97 (patch) | |
tree | 828ec8e08a1f326369e3720c310e2b059be9ead5 /server/tests/shared | |
parent | 09a7ce0c60d3e98eedf58f245bf2aaba9837785b (diff) | |
download | PeerTube-d41f4a6dc69d098e9dc9173b7e1a586695ef7b97.tar.gz PeerTube-d41f4a6dc69d098e9dc9173b7e1a586695ef7b97.tar.zst PeerTube-d41f4a6dc69d098e9dc9173b7e1a586695ef7b97.zip |
Improve image test comparison
Diffstat (limited to 'server/tests/shared')
-rw-r--r-- | server/tests/shared/checks.ts | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/server/tests/shared/checks.ts b/server/tests/shared/checks.ts index 523d37420..c0098b293 100644 --- a/server/tests/shared/checks.ts +++ b/server/tests/shared/checks.ts | |||
@@ -2,7 +2,10 @@ | |||
2 | 2 | ||
3 | import { expect } from 'chai' | 3 | import { expect } from 'chai' |
4 | import { pathExists, readFile } from 'fs-extra' | 4 | import { pathExists, readFile } from 'fs-extra' |
5 | import JPEG from 'jpeg-js' | ||
5 | import { join } from 'path' | 6 | import { join } from 'path' |
7 | import pixelmatch from 'pixelmatch' | ||
8 | import { PNG } from 'pngjs' | ||
6 | import { root } from '@shared/core-utils' | 9 | import { root } from '@shared/core-utils' |
7 | import { HttpStatusCode } from '@shared/models' | 10 | import { HttpStatusCode } from '@shared/models' |
8 | import { makeGetRequest, PeerTubeServer } from '@shared/server-commands' | 11 | import { makeGetRequest, PeerTubeServer } from '@shared/server-commands' |
@@ -41,7 +44,7 @@ async function expectLogContain (server: PeerTubeServer, str: string) { | |||
41 | expect(content.toString()).to.contain(str) | 44 | expect(content.toString()).to.contain(str) |
42 | } | 45 | } |
43 | 46 | ||
44 | async function testImage (url: string, imageName: string, imageHTTPPath: string, extension = '.jpg') { | 47 | async function testImageSize (url: string, imageName: string, imageHTTPPath: string, extension = '.jpg') { |
45 | const res = await makeGetRequest({ | 48 | const res = await makeGetRequest({ |
46 | url, | 49 | url, |
47 | path: imageHTTPPath, | 50 | path: imageHTTPPath, |
@@ -58,6 +61,29 @@ async function testImage (url: string, imageName: string, imageHTTPPath: string, | |||
58 | expect(body.length).to.be.below(maxLength, 'the generated image is way larger than the recorded fixture') | 61 | expect(body.length).to.be.below(maxLength, 'the generated image is way larger than the recorded fixture') |
59 | } | 62 | } |
60 | 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 | |||
61 | async function testFileExistsOrNot (server: PeerTubeServer, directory: string, filePath: string, exist: boolean) { | 87 | async function testFileExistsOrNot (server: PeerTubeServer, directory: string, filePath: string, exist: boolean) { |
62 | const base = server.servers.buildDirectory(directory) | 88 | const base = server.servers.buildDirectory(directory) |
63 | 89 | ||
@@ -104,6 +130,7 @@ function checkBadSortPagination (url: string, path: string, token?: string, quer | |||
104 | 130 | ||
105 | export { | 131 | export { |
106 | dateIsValid, | 132 | dateIsValid, |
133 | testImageSize, | ||
107 | testImage, | 134 | testImage, |
108 | expectLogDoesNotContain, | 135 | expectLogDoesNotContain, |
109 | testFileExistsOrNot, | 136 | testFileExistsOrNot, |