aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/extra-utils/miscs
diff options
context:
space:
mode:
Diffstat (limited to 'shared/extra-utils/miscs')
-rw-r--r--shared/extra-utils/miscs/checks.ts58
-rw-r--r--shared/extra-utils/miscs/generate.ts75
-rw-r--r--shared/extra-utils/miscs/index.ts5
-rw-r--r--shared/extra-utils/miscs/sql-command.ts141
-rw-r--r--shared/extra-utils/miscs/tests.ts101
-rw-r--r--shared/extra-utils/miscs/webtorrent.ts46
6 files changed, 0 insertions, 426 deletions
diff --git a/shared/extra-utils/miscs/checks.ts b/shared/extra-utils/miscs/checks.ts
deleted file mode 100644
index 589928997..000000000
--- a/shared/extra-utils/miscs/checks.ts
+++ /dev/null
@@ -1,58 +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 { 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}
diff --git a/shared/extra-utils/miscs/generate.ts b/shared/extra-utils/miscs/generate.ts
deleted file mode 100644
index 93673a063..000000000
--- a/shared/extra-utils/miscs/generate.ts
+++ /dev/null
@@ -1,75 +0,0 @@
1import { expect } from 'chai'
2import ffmpeg from 'fluent-ffmpeg'
3import { ensureDir, pathExists } from 'fs-extra'
4import { dirname } from 'path'
5import { getVideoFileBitrate, getVideoFileFPS, getVideoFileResolution } from '@shared/extra-utils/ffprobe'
6import { getMaxBitrate } from '@shared/core-utils'
7import { buildAbsoluteFixturePath } from './tests'
8
9async function ensureHasTooBigBitrate (fixturePath: string) {
10 const bitrate = await getVideoFileBitrate(fixturePath)
11 const dataResolution = await getVideoFileResolution(fixturePath)
12 const fps = await getVideoFileFPS(fixturePath)
13
14 const maxBitrate = getMaxBitrate({ ...dataResolution, fps })
15 expect(bitrate).to.be.above(maxBitrate)
16}
17
18async function generateHighBitrateVideo () {
19 const tempFixturePath = buildAbsoluteFixturePath('video_high_bitrate_1080p.mp4', true)
20
21 await ensureDir(dirname(tempFixturePath))
22
23 const exists = await pathExists(tempFixturePath)
24 if (!exists) {
25 console.log('Generating high bitrate video.')
26
27 // Generate a random, high bitrate video on the fly, so we don't have to include
28 // a large file in the repo. The video needs to have a certain minimum length so
29 // that FFmpeg properly applies bitrate limits.
30 // https://stackoverflow.com/a/15795112
31 return new Promise<string>((res, rej) => {
32 ffmpeg()
33 .outputOptions([ '-f rawvideo', '-video_size 1920x1080', '-i /dev/urandom' ])
34 .outputOptions([ '-ac 2', '-f s16le', '-i /dev/urandom', '-t 10' ])
35 .outputOptions([ '-maxrate 10M', '-bufsize 10M' ])
36 .output(tempFixturePath)
37 .on('error', rej)
38 .on('end', () => res(tempFixturePath))
39 .run()
40 })
41 }
42
43 await ensureHasTooBigBitrate(tempFixturePath)
44
45 return tempFixturePath
46}
47
48async function generateVideoWithFramerate (fps = 60) {
49 const tempFixturePath = buildAbsoluteFixturePath(`video_${fps}fps.mp4`, true)
50
51 await ensureDir(dirname(tempFixturePath))
52
53 const exists = await pathExists(tempFixturePath)
54 if (!exists) {
55 console.log('Generating video with framerate %d.', fps)
56
57 return new Promise<string>((res, rej) => {
58 ffmpeg()
59 .outputOptions([ '-f rawvideo', '-video_size 1280x720', '-i /dev/urandom' ])
60 .outputOptions([ '-ac 2', '-f s16le', '-i /dev/urandom', '-t 10' ])
61 .outputOptions([ `-r ${fps}` ])
62 .output(tempFixturePath)
63 .on('error', rej)
64 .on('end', () => res(tempFixturePath))
65 .run()
66 })
67 }
68
69 return tempFixturePath
70}
71
72export {
73 generateHighBitrateVideo,
74 generateVideoWithFramerate
75}
diff --git a/shared/extra-utils/miscs/index.ts b/shared/extra-utils/miscs/index.ts
deleted file mode 100644
index 4474661de..000000000
--- a/shared/extra-utils/miscs/index.ts
+++ /dev/null
@@ -1,5 +0,0 @@
1export * from './checks'
2export * from './generate'
3export * from './sql-command'
4export * from './tests'
5export * from './webtorrent'
diff --git a/shared/extra-utils/miscs/sql-command.ts b/shared/extra-utils/miscs/sql-command.ts
deleted file mode 100644
index bedb3349b..000000000
--- a/shared/extra-utils/miscs/sql-command.ts
+++ /dev/null
@@ -1,141 +0,0 @@
1import { QueryTypes, Sequelize } from 'sequelize'
2import { AbstractCommand } from '../shared/abstract-command'
3
4export class SQLCommand extends AbstractCommand {
5 private sequelize: Sequelize
6
7 deleteAll (table: string) {
8 const seq = this.getSequelize()
9
10 const options = { type: QueryTypes.DELETE }
11
12 return seq.query(`DELETE FROM "${table}"`, options)
13 }
14
15 async getCount (table: string) {
16 const seq = this.getSequelize()
17
18 const options = { type: QueryTypes.SELECT as QueryTypes.SELECT }
19
20 const [ { total } ] = await seq.query<{ total: string }>(`SELECT COUNT(*) as total FROM "${table}"`, options)
21 if (total === null) return 0
22
23 return parseInt(total, 10)
24 }
25
26 setActorField (to: string, field: string, value: string) {
27 const seq = this.getSequelize()
28
29 const options = { type: QueryTypes.UPDATE }
30
31 return seq.query(`UPDATE actor SET "${field}" = '${value}' WHERE url = '${to}'`, options)
32 }
33
34 setVideoField (uuid: string, field: string, value: string) {
35 const seq = this.getSequelize()
36
37 const options = { type: QueryTypes.UPDATE }
38
39 return seq.query(`UPDATE video SET "${field}" = '${value}' WHERE uuid = '${uuid}'`, options)
40 }
41
42 setPlaylistField (uuid: string, field: string, value: string) {
43 const seq = this.getSequelize()
44
45 const options = { type: QueryTypes.UPDATE }
46
47 return seq.query(`UPDATE "videoPlaylist" SET "${field}" = '${value}' WHERE uuid = '${uuid}'`, options)
48 }
49
50 async countVideoViewsOf (uuid: string) {
51 const seq = this.getSequelize()
52
53 const query = 'SELECT SUM("videoView"."views") AS "total" FROM "videoView" ' +
54 `INNER JOIN "video" ON "video"."id" = "videoView"."videoId" WHERE "video"."uuid" = '${uuid}'`
55
56 const options = { type: QueryTypes.SELECT as QueryTypes.SELECT }
57 const [ { total } ] = await seq.query<{ total: number }>(query, options)
58
59 if (!total) return 0
60
61 return parseInt(total + '', 10)
62 }
63
64 getActorImage (filename: string) {
65 return this.selectQuery(`SELECT * FROM "actorImage" WHERE filename = '${filename}'`)
66 .then(rows => rows[0])
67 }
68
69 selectQuery (query: string) {
70 const seq = this.getSequelize()
71 const options = { type: QueryTypes.SELECT as QueryTypes.SELECT }
72
73 return seq.query<any>(query, options)
74 }
75
76 updateQuery (query: string) {
77 const seq = this.getSequelize()
78 const options = { type: QueryTypes.UPDATE as QueryTypes.UPDATE }
79
80 return seq.query(query, options)
81 }
82
83 setPluginField (pluginName: string, field: string, value: string) {
84 const seq = this.getSequelize()
85
86 const options = { type: QueryTypes.UPDATE }
87
88 return seq.query(`UPDATE "plugin" SET "${field}" = '${value}' WHERE "name" = '${pluginName}'`, options)
89 }
90
91 setPluginVersion (pluginName: string, newVersion: string) {
92 return this.setPluginField(pluginName, 'version', newVersion)
93 }
94
95 setPluginLatestVersion (pluginName: string, newVersion: string) {
96 return this.setPluginField(pluginName, 'latestVersion', newVersion)
97 }
98
99 setActorFollowScores (newScore: number) {
100 const seq = this.getSequelize()
101
102 const options = { type: QueryTypes.UPDATE }
103
104 return seq.query(`UPDATE "actorFollow" SET "score" = ${newScore}`, options)
105 }
106
107 setTokenField (accessToken: string, field: string, value: string) {
108 const seq = this.getSequelize()
109
110 const options = { type: QueryTypes.UPDATE }
111
112 return seq.query(`UPDATE "oAuthToken" SET "${field}" = '${value}' WHERE "accessToken" = '${accessToken}'`, options)
113 }
114
115 async cleanup () {
116 if (!this.sequelize) return
117
118 await this.sequelize.close()
119 this.sequelize = undefined
120 }
121
122 private getSequelize () {
123 if (this.sequelize) return this.sequelize
124
125 const dbname = 'peertube_test' + this.server.internalServerNumber
126 const username = 'peertube'
127 const password = 'peertube'
128 const host = 'localhost'
129 const port = 5432
130
131 this.sequelize = new Sequelize(dbname, username, password, {
132 dialect: 'postgres',
133 host,
134 port,
135 logging: false
136 })
137
138 return this.sequelize
139 }
140
141}
diff --git a/shared/extra-utils/miscs/tests.ts b/shared/extra-utils/miscs/tests.ts
deleted file mode 100644
index 658fe5fd3..000000000
--- a/shared/extra-utils/miscs/tests.ts
+++ /dev/null
@@ -1,101 +0,0 @@
1import { stat } from 'fs-extra'
2import { basename, isAbsolute, join, resolve } from 'path'
3
4const FIXTURE_URLS = {
5 peertube_long: 'https://peertube2.cpy.re/videos/watch/122d093a-1ede-43bd-bd34-59d2931ffc5e',
6 peertube_short: 'https://peertube2.cpy.re/w/3fbif9S3WmtTP8gGsC5HBd',
7
8 youtube: 'https://www.youtube.com/watch?v=msX3jv1XdvM',
9
10 /**
11 * The video is used to check format-selection correctness wrt. HDR,
12 * which brings its own set of oddities outside of a MediaSource.
13 *
14 * The video needs to have the following format_ids:
15 * (which you can check by using `youtube-dl <url> -F`):
16 * - (webm vp9)
17 * - (mp4 avc1)
18 * - (webm vp9.2 HDR)
19 */
20 youtubeHDR: 'https://www.youtube.com/watch?v=RQgnBB9z_N4',
21
22 // eslint-disable-next-line max-len
23 magnet: 'magnet:?xs=https%3A%2F%2Fpeertube2.cpy.re%2Flazy-static%2Ftorrents%2Fb209ca00-c8bb-4b2b-b421-1ede169f3dbc-720.torrent&xt=urn:btih:0f498834733e8057ed5c6f2ee2b4efd8d84a76ee&dn=super+peertube2+video&tr=https%3A%2F%2Fpeertube2.cpy.re%2Ftracker%2Fannounce&tr=wss%3A%2F%2Fpeertube2.cpy.re%3A443%2Ftracker%2Fsocket&ws=https%3A%2F%2Fpeertube2.cpy.re%2Fstatic%2Fwebseed%2Fb209ca00-c8bb-4b2b-b421-1ede169f3dbc-720.mp4',
24
25 badVideo: 'https://download.cpy.re/peertube/bad_video.mp4',
26 goodVideo: 'https://download.cpy.re/peertube/good_video.mp4',
27 goodVideo720: 'https://download.cpy.re/peertube/good_video_720.mp4',
28
29 file4K: 'https://download.cpy.re/peertube/4k_file.txt'
30}
31
32function parallelTests () {
33 return process.env.MOCHA_PARALLEL === 'true'
34}
35
36function isGithubCI () {
37 return !!process.env.GITHUB_WORKSPACE
38}
39
40function areHttpImportTestsDisabled () {
41 const disabled = process.env.DISABLE_HTTP_IMPORT_TESTS === 'true'
42
43 if (disabled) console.log('DISABLE_HTTP_IMPORT_TESTS env set to "true" so import tests are disabled')
44
45 return disabled
46}
47
48function areObjectStorageTestsDisabled () {
49 const disabled = process.env.ENABLE_OBJECT_STORAGE_TESTS !== 'true'
50
51 if (disabled) console.log('ENABLE_OBJECT_STORAGE_TESTS env is not set to "true" so object storage tests are disabled')
52
53 return disabled
54}
55
56function buildAbsoluteFixturePath (path: string, customCIPath = false) {
57 if (isAbsolute(path)) return path
58
59 if (customCIPath && process.env.GITHUB_WORKSPACE) {
60 return join(process.env.GITHUB_WORKSPACE, 'fixtures', path)
61 }
62
63 return join(root(), 'server', 'tests', 'fixtures', path)
64}
65
66function root () {
67 // We are in /miscs
68 let root = join(__dirname, '..', '..', '..')
69
70 if (basename(root) === 'dist') root = resolve(root, '..')
71
72 return root
73}
74
75function wait (milliseconds: number) {
76 return new Promise(resolve => setTimeout(resolve, milliseconds))
77}
78
79async function getFileSize (path: string) {
80 const stats = await stat(path)
81
82 return stats.size
83}
84
85function buildRequestStub (): any {
86 return { }
87}
88
89export {
90 FIXTURE_URLS,
91
92 parallelTests,
93 isGithubCI,
94 areHttpImportTestsDisabled,
95 buildAbsoluteFixturePath,
96 getFileSize,
97 buildRequestStub,
98 areObjectStorageTestsDisabled,
99 wait,
100 root
101}
diff --git a/shared/extra-utils/miscs/webtorrent.ts b/shared/extra-utils/miscs/webtorrent.ts
deleted file mode 100644
index 0683f8893..000000000
--- a/shared/extra-utils/miscs/webtorrent.ts
+++ /dev/null
@@ -1,46 +0,0 @@
1import { readFile } from 'fs-extra'
2import parseTorrent from 'parse-torrent'
3import { basename, join } from 'path'
4import * as WebTorrent from 'webtorrent'
5import { VideoFile } from '@shared/models'
6import { PeerTubeServer } from '../server'
7
8let webtorrent: WebTorrent.Instance
9
10function webtorrentAdd (torrentId: string, refreshWebTorrent = false) {
11 const WebTorrent = require('webtorrent')
12
13 if (webtorrent && refreshWebTorrent) webtorrent.destroy()
14 if (!webtorrent || refreshWebTorrent) webtorrent = new WebTorrent()
15
16 webtorrent.on('error', err => console.error('Error in webtorrent', err))
17
18 return new Promise<WebTorrent.Torrent>(res => {
19 const torrent = webtorrent.add(torrentId, res)
20
21 torrent.on('error', err => console.error('Error in webtorrent torrent', err))
22 torrent.on('warning', warn => {
23 const msg = typeof warn === 'string'
24 ? warn
25 : warn.message
26
27 if (msg.includes('Unsupported')) return
28
29 console.error('Warning in webtorrent torrent', warn)
30 })
31 })
32}
33
34async function parseTorrentVideo (server: PeerTubeServer, file: VideoFile) {
35 const torrentName = basename(file.torrentUrl)
36 const torrentPath = server.servers.buildDirectory(join('torrents', torrentName))
37
38 const data = await readFile(torrentPath)
39
40 return parseTorrent(data)
41}
42
43export {
44 webtorrentAdd,
45 parseTorrentVideo
46}