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