]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/shared/checks.ts
Remove transcoding scripts
[github/Chocobozzz/PeerTube.git] / server / tests / shared / checks.ts
CommitLineData
6c5065a0
C
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */
2
3import { expect } from 'chai'
4import { pathExists, readFile } from 'fs-extra'
d41f4a6d 5import JPEG from 'jpeg-js'
6c5065a0 6import { join } from 'path'
d41f4a6d
C
7import pixelmatch from 'pixelmatch'
8import { PNG } from 'pngjs'
06aad801 9import { root } from '@shared/core-utils'
c0e8b12e 10import { HttpStatusCode } from '@shared/models'
c55e3d72 11import { makeGetRequest, PeerTubeServer } from '@shared/server-commands'
6c5065a0
C
12
13// Default interval -> 5 minutes
14function dateIsValid (dateString: string, 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
0305db28
JB
21function expectStartWith (str: string, start: string) {
22 expect(str.startsWith(start), `${str} does not start with ${start}`).to.be.true
23}
24
ca3d5912
C
25function expectNotStartWith (str: string, start: string) {
26 expect(str.startsWith(start), `${str} does not start with ${start}`).to.be.false
27}
28
71e3e879
C
29function expectEndWith (str: string, end: string) {
30 expect(str.endsWith(end), `${str} does not end with ${end}`).to.be.true
31}
32
33// ---------------------------------------------------------------------------
34
1f6125be
C
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
630d0a1b
C
41async function expectLogContain (server: PeerTubeServer, str: string) {
42 const content = await server.servers.getLogContent()
43
44 expect(content.toString()).to.contain(str)
45}
46
d41f4a6d 47async function testImageSize (url: string, imageName: string, imageHTTPPath: string, extension = '.jpg') {
6c5065a0
C
48 const res = await makeGetRequest({
49 url,
4c6d99e5 50 path: imageHTTPPath,
c0e8b12e 51 expectedStatus: HttpStatusCode.OK_200
6c5065a0
C
52 })
53
54 const body = res.body
55
56 const data = await readFile(join(root(), 'server', 'tests', 'fixtures', imageName + extension))
c47c3bcb
C
57 const minLength = data.length - ((40 * data.length) / 100)
58 const maxLength = data.length + ((40 * data.length) / 100)
6c5065a0 59
4c6d99e5
C
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')
6c5065a0
C
62}
63
d41f4a6d
C
64async 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
254d3579 87async function testFileExistsOrNot (server: PeerTubeServer, directory: string, filePath: string, exist: boolean) {
89d241a7 88 const base = server.servers.buildDirectory(directory)
6c5065a0
C
89
90 expect(await pathExists(join(base, filePath))).to.equal(exist)
91}
92
c55e3d72
C
93function checkBadStartPagination (url: string, path: string, token?: string, query = {}) {
94 return makeGetRequest({
95 url,
96 path,
97 token,
98 query: { ...query, start: 'hello' },
99 expectedStatus: HttpStatusCode.BAD_REQUEST_400
100 })
101}
102
103async function checkBadCountPagination (url: string, path: string, token?: string, query = {}) {
104 await makeGetRequest({
105 url,
106 path,
107 token,
108 query: { ...query, count: 'hello' },
109 expectedStatus: HttpStatusCode.BAD_REQUEST_400
110 })
111
112 await makeGetRequest({
113 url,
114 path,
115 token,
116 query: { ...query, count: 2000 },
117 expectedStatus: HttpStatusCode.BAD_REQUEST_400
118 })
119}
120
121function checkBadSortPagination (url: string, path: string, token?: string, query = {}) {
122 return makeGetRequest({
123 url,
124 path,
125 token,
126 query: { ...query, sort: 'hello' },
127 expectedStatus: HttpStatusCode.BAD_REQUEST_400
128 })
129}
130
6c5065a0
C
131export {
132 dateIsValid,
d41f4a6d 133 testImageSize,
6c5065a0 134 testImage,
1f6125be 135 expectLogDoesNotContain,
0305db28 136 testFileExistsOrNot,
c55e3d72 137 expectStartWith,
ca3d5912 138 expectNotStartWith,
71e3e879 139 expectEndWith,
c55e3d72
C
140 checkBadStartPagination,
141 checkBadCountPagination,
630d0a1b
C
142 checkBadSortPagination,
143 expectLogContain
6c5065a0 144}