aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/shared/checks.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/tests/shared/checks.ts')
-rw-r--r--server/tests/shared/checks.ts174
1 files changed, 0 insertions, 174 deletions
diff --git a/server/tests/shared/checks.ts b/server/tests/shared/checks.ts
deleted file mode 100644
index 90179c6ac..000000000
--- a/server/tests/shared/checks.ts
+++ /dev/null
@@ -1,174 +0,0 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */
2
3import { expect } from 'chai'
4import { pathExists, readFile } from 'fs-extra'
5import JPEG from 'jpeg-js'
6import { join } from 'path'
7import pixelmatch from 'pixelmatch'
8import { PNG } from 'pngjs'
9import { root } from '@shared/core-utils'
10import { HttpStatusCode } from '@shared/models'
11import { makeGetRequest, PeerTubeServer } from '@shared/server-commands'
12
13// Default interval -> 5 minutes
14function 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
21function expectStartWith (str: string, start: string) {
22 expect(str.startsWith(start), `${str} does not start with ${start}`).to.be.true
23}
24
25function expectNotStartWith (str: string, start: string) {
26 expect(str.startsWith(start), `${str} does not start with ${start}`).to.be.false
27}
28
29function expectEndWith (str: string, end: string) {
30 expect(str.endsWith(end), `${str} does not end with ${end}`).to.be.true
31}
32
33// ---------------------------------------------------------------------------
34
35async function expectLogDoesNotContain (server: PeerTubeServer, str: string) {
36 const content = await server.servers.getLogContent()
37
38 expect(content.toString()).to.not.contain(str)
39}
40
41async function expectLogContain (server: PeerTubeServer, str: string) {
42 const content = await server.servers.getLogContent()
43
44 expect(content.toString()).to.contain(str)
45}
46
47async 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
64async function testImageGeneratedByFFmpeg (url: string, imageName: string, imageHTTPPath: string, extension = '.jpg') {
65 if (process.env.ENABLE_FFMPEG_THUMBNAIL_PIXEL_COMPARISON_TESTS !== 'true') {
66 console.log(
67 'Pixel comparison of image generated by ffmpeg is disabled. ' +
68 'You can enable it using `ENABLE_FFMPEG_THUMBNAIL_PIXEL_COMPARISON_TESTS=true env variable')
69 }
70
71 return testImage(url, imageName, imageHTTPPath, extension)
72}
73
74async function testImage (url: string, imageName: string, imageHTTPPath: string, extension = '.jpg') {
75 const res = await makeGetRequest({
76 url,
77 path: imageHTTPPath,
78 expectedStatus: HttpStatusCode.OK_200
79 })
80
81 const body = res.body
82 const data = await readFile(join(root(), 'server', 'tests', 'fixtures', imageName + extension))
83
84 const img1 = imageHTTPPath.endsWith('.png')
85 ? PNG.sync.read(body)
86 : JPEG.decode(body)
87
88 const img2 = extension === '.png'
89 ? PNG.sync.read(data)
90 : JPEG.decode(data)
91
92 const result = pixelmatch(img1.data, img2.data, null, img1.width, img1.height, { threshold: 0.1 })
93
94 expect(result).to.equal(0, `${imageHTTPPath} image is not the same as ${imageName}${extension}`)
95}
96
97async function testFileExistsOrNot (server: PeerTubeServer, directory: string, filePath: string, exist: boolean) {
98 const base = server.servers.buildDirectory(directory)
99
100 expect(await pathExists(join(base, filePath))).to.equal(exist)
101}
102
103// ---------------------------------------------------------------------------
104
105function checkBadStartPagination (url: string, path: string, token?: string, query = {}) {
106 return makeGetRequest({
107 url,
108 path,
109 token,
110 query: { ...query, start: 'hello' },
111 expectedStatus: HttpStatusCode.BAD_REQUEST_400
112 })
113}
114
115async function checkBadCountPagination (url: string, path: string, token?: string, query = {}) {
116 await makeGetRequest({
117 url,
118 path,
119 token,
120 query: { ...query, count: 'hello' },
121 expectedStatus: HttpStatusCode.BAD_REQUEST_400
122 })
123
124 await makeGetRequest({
125 url,
126 path,
127 token,
128 query: { ...query, count: 2000 },
129 expectedStatus: HttpStatusCode.BAD_REQUEST_400
130 })
131}
132
133function checkBadSortPagination (url: string, path: string, token?: string, query = {}) {
134 return makeGetRequest({
135 url,
136 path,
137 token,
138 query: { ...query, sort: 'hello' },
139 expectedStatus: HttpStatusCode.BAD_REQUEST_400
140 })
141}
142
143// ---------------------------------------------------------------------------
144
145async function checkVideoDuration (server: PeerTubeServer, videoUUID: string, duration: number) {
146 const video = await server.videos.get({ id: videoUUID })
147
148 expect(video.duration).to.be.approximately(duration, 1)
149
150 for (const file of video.files) {
151 const metadata = await server.videos.getFileMetadata({ url: file.metadataUrl })
152
153 for (const stream of metadata.streams) {
154 expect(Math.round(stream.duration)).to.be.approximately(duration, 1)
155 }
156 }
157}
158
159export {
160 dateIsValid,
161 testImageGeneratedByFFmpeg,
162 testImageSize,
163 testImage,
164 expectLogDoesNotContain,
165 testFileExistsOrNot,
166 expectStartWith,
167 expectNotStartWith,
168 expectEndWith,
169 checkBadStartPagination,
170 checkBadCountPagination,
171 checkBadSortPagination,
172 checkVideoDuration,
173 expectLogContain
174}