aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/shared/checks.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2023-07-31 14:34:36 +0200
committerChocobozzz <me@florianbigard.com>2023-08-11 15:02:33 +0200
commit3a4992633ee62d5edfbb484d9c6bcb3cf158489d (patch)
treee4510b39bdac9c318fdb4b47018d08f15368b8f0 /server/tests/shared/checks.ts
parent04d1da5621d25d59bd5fa1543b725c497bf5d9a8 (diff)
downloadPeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.gz
PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.zst
PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.zip
Migrate server to ESM
Sorry for the very big commit that may lead to git log issues and merge conflicts, but it's a major step forward: * Server can be faster at startup because imports() are async and we can easily lazy import big modules * Angular doesn't seem to support ES import (with .js extension), so we had to correctly organize peertube into a monorepo: * Use yarn workspace feature * Use typescript reference projects for dependencies * Shared projects have been moved into "packages", each one is now a node module (with a dedicated package.json/tsconfig.json) * server/tools have been moved into apps/ and is now a dedicated app bundled and published on NPM so users don't have to build peertube cli tools manually * server/tests have been moved into packages/ so we don't compile them every time we want to run the server * Use isolatedModule option: * Had to move from const enum to const (https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums) * Had to explictely specify "type" imports when used in decorators * Prefer tsx (that uses esbuild under the hood) instead of ts-node to load typescript files (tests with mocha or scripts): * To reduce test complexity as esbuild doesn't support decorator metadata, we only test server files that do not import server models * We still build tests files into js files for a faster CI * Remove unmaintained peertube CLI import script * Removed some barrels to speed up execution (less imports)
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}