aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/server-commands/miscs/checks.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-12-17 09:29:23 +0100
committerChocobozzz <me@florianbigard.com>2021-12-17 09:29:23 +0100
commitbf54587a3e2ad9c2c186828f2a5682b91ee2cc00 (patch)
tree54b40aaf01bae210632473285c3c7571d51e4f89 /shared/server-commands/miscs/checks.ts
parent6b5f72beda96d8b7e4d6329c4001827334de27dd (diff)
downloadPeerTube-bf54587a3e2ad9c2c186828f2a5682b91ee2cc00.tar.gz
PeerTube-bf54587a3e2ad9c2c186828f2a5682b91ee2cc00.tar.zst
PeerTube-bf54587a3e2ad9c2c186828f2a5682b91ee2cc00.zip
shared/ typescript types dir server-commands
Diffstat (limited to 'shared/server-commands/miscs/checks.ts')
-rw-r--r--shared/server-commands/miscs/checks.ts58
1 files changed, 58 insertions, 0 deletions
diff --git a/shared/server-commands/miscs/checks.ts b/shared/server-commands/miscs/checks.ts
new file mode 100644
index 000000000..589928997
--- /dev/null
+++ b/shared/server-commands/miscs/checks.ts
@@ -0,0 +1,58 @@
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 { join } from 'path'
6import { root } from '@shared/core-utils'
7import { HttpStatusCode } from '@shared/models'
8import { makeGetRequest } from '../requests'
9import { PeerTubeServer } from '../server'
10
11// Default interval -> 5 minutes
12function dateIsValid (dateString: string, 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
19function expectStartWith (str: string, start: string) {
20 expect(str.startsWith(start), `${str} does not start with ${start}`).to.be.true
21}
22
23async function expectLogDoesNotContain (server: PeerTubeServer, str: string) {
24 const content = await server.servers.getLogContent()
25
26 expect(content.toString()).to.not.contain(str)
27}
28
29async function testImage (url: string, imageName: string, imagePath: string, extension = '.jpg') {
30 const res = await makeGetRequest({
31 url,
32 path: imagePath,
33 expectedStatus: HttpStatusCode.OK_200
34 })
35
36 const body = res.body
37
38 const data = await readFile(join(root(), 'server', 'tests', 'fixtures', imageName + extension))
39 const minLength = body.length - ((30 * body.length) / 100)
40 const maxLength = body.length + ((30 * body.length) / 100)
41
42 expect(data.length).to.be.above(minLength, 'the generated image is way smaller than the recorded fixture')
43 expect(data.length).to.be.below(maxLength, 'the generated image is way larger than the recorded fixture')
44}
45
46async function testFileExistsOrNot (server: PeerTubeServer, directory: string, filePath: string, exist: boolean) {
47 const base = server.servers.buildDirectory(directory)
48
49 expect(await pathExists(join(base, filePath))).to.equal(exist)
50}
51
52export {
53 dateIsValid,
54 testImage,
55 expectLogDoesNotContain,
56 testFileExistsOrNot,
57 expectStartWith
58}