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 /server/tests/shared/videos.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 'server/tests/shared/videos.ts')
-rw-r--r-- | server/tests/shared/videos.ts | 315 |
1 files changed, 0 insertions, 315 deletions
diff --git a/server/tests/shared/videos.ts b/server/tests/shared/videos.ts deleted file mode 100644 index ac24bb173..000000000 --- a/server/tests/shared/videos.ts +++ /dev/null | |||
@@ -1,315 +0,0 @@ | |||
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */ | ||
2 | |||
3 | import { expect } from 'chai' | ||
4 | import { pathExists, readdir } from 'fs-extra' | ||
5 | import { basename, join } from 'path' | ||
6 | import { loadLanguages, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_PRIVACIES } from '@server/initializers/constants' | ||
7 | import { getLowercaseExtension, pick, uuidRegex } from '@shared/core-utils' | ||
8 | import { HttpStatusCode, VideoCaption, VideoDetails, VideoPrivacy, VideoResolution } from '@shared/models' | ||
9 | import { makeRawRequest, PeerTubeServer, VideoEdit, waitJobs } from '@shared/server-commands' | ||
10 | import { dateIsValid, expectStartWith, testImageGeneratedByFFmpeg } from './checks' | ||
11 | import { checkWebTorrentWorks } from './webtorrent' | ||
12 | |||
13 | loadLanguages() | ||
14 | |||
15 | async function completeWebVideoFilesCheck (options: { | ||
16 | server: PeerTubeServer | ||
17 | originServer: PeerTubeServer | ||
18 | videoUUID: string | ||
19 | fixture: string | ||
20 | files: { | ||
21 | resolution: number | ||
22 | size?: number | ||
23 | }[] | ||
24 | objectStorageBaseUrl?: string | ||
25 | }) { | ||
26 | const { originServer, server, videoUUID, files, fixture, objectStorageBaseUrl } = options | ||
27 | const video = await server.videos.getWithToken({ id: videoUUID }) | ||
28 | const serverConfig = await originServer.config.getConfig() | ||
29 | const requiresAuth = video.privacy.id === VideoPrivacy.PRIVATE || video.privacy.id === VideoPrivacy.INTERNAL | ||
30 | |||
31 | const transcodingEnabled = serverConfig.transcoding.web_videos.enabled | ||
32 | |||
33 | for (const attributeFile of files) { | ||
34 | const file = video.files.find(f => f.resolution.id === attributeFile.resolution) | ||
35 | expect(file, `resolution ${attributeFile.resolution} does not exist`).not.to.be.undefined | ||
36 | |||
37 | let extension = getLowercaseExtension(fixture) | ||
38 | // Transcoding enabled: extension will always be .mp4 | ||
39 | if (transcodingEnabled) extension = '.mp4' | ||
40 | |||
41 | expect(file.id).to.exist | ||
42 | expect(file.magnetUri).to.have.lengthOf.above(2) | ||
43 | |||
44 | { | ||
45 | const privatePath = requiresAuth | ||
46 | ? 'private/' | ||
47 | : '' | ||
48 | const nameReg = `${uuidRegex}-${file.resolution.id}` | ||
49 | |||
50 | expect(file.torrentDownloadUrl).to.match(new RegExp(`${server.url}/download/torrents/${nameReg}.torrent`)) | ||
51 | expect(file.torrentUrl).to.match(new RegExp(`${server.url}/lazy-static/torrents/${nameReg}.torrent`)) | ||
52 | |||
53 | if (objectStorageBaseUrl && requiresAuth) { | ||
54 | const regexp = new RegExp(`${originServer.url}/object-storage-proxy/web-videos/${privatePath}${nameReg}${extension}`) | ||
55 | expect(file.fileUrl).to.match(regexp) | ||
56 | } else if (objectStorageBaseUrl) { | ||
57 | expectStartWith(file.fileUrl, objectStorageBaseUrl) | ||
58 | } else { | ||
59 | expect(file.fileUrl).to.match(new RegExp(`${originServer.url}/static/web-videos/${privatePath}${nameReg}${extension}`)) | ||
60 | } | ||
61 | |||
62 | expect(file.fileDownloadUrl).to.match(new RegExp(`${originServer.url}/download/videos/${nameReg}${extension}`)) | ||
63 | } | ||
64 | |||
65 | { | ||
66 | const token = requiresAuth | ||
67 | ? server.accessToken | ||
68 | : undefined | ||
69 | |||
70 | await Promise.all([ | ||
71 | makeRawRequest({ url: file.torrentUrl, token, expectedStatus: HttpStatusCode.OK_200 }), | ||
72 | makeRawRequest({ url: file.torrentDownloadUrl, token, expectedStatus: HttpStatusCode.OK_200 }), | ||
73 | makeRawRequest({ url: file.metadataUrl, token, expectedStatus: HttpStatusCode.OK_200 }), | ||
74 | makeRawRequest({ url: file.fileUrl, token, expectedStatus: HttpStatusCode.OK_200 }), | ||
75 | makeRawRequest({ | ||
76 | url: file.fileDownloadUrl, | ||
77 | token, | ||
78 | expectedStatus: objectStorageBaseUrl ? HttpStatusCode.FOUND_302 : HttpStatusCode.OK_200 | ||
79 | }) | ||
80 | ]) | ||
81 | } | ||
82 | |||
83 | expect(file.resolution.id).to.equal(attributeFile.resolution) | ||
84 | |||
85 | if (file.resolution.id === VideoResolution.H_NOVIDEO) { | ||
86 | expect(file.resolution.label).to.equal('Audio') | ||
87 | } else { | ||
88 | expect(file.resolution.label).to.equal(attributeFile.resolution + 'p') | ||
89 | } | ||
90 | |||
91 | if (attributeFile.size) { | ||
92 | const minSize = attributeFile.size - ((10 * attributeFile.size) / 100) | ||
93 | const maxSize = attributeFile.size + ((10 * attributeFile.size) / 100) | ||
94 | expect( | ||
95 | file.size, | ||
96 | 'File size for resolution ' + file.resolution.label + ' outside confidence interval (' + minSize + '> size <' + maxSize + ')' | ||
97 | ).to.be.above(minSize).and.below(maxSize) | ||
98 | } | ||
99 | |||
100 | await checkWebTorrentWorks(file.magnetUri) | ||
101 | } | ||
102 | } | ||
103 | |||
104 | async function completeVideoCheck (options: { | ||
105 | server: PeerTubeServer | ||
106 | originServer: PeerTubeServer | ||
107 | videoUUID: string | ||
108 | attributes: { | ||
109 | name: string | ||
110 | category: number | ||
111 | licence: number | ||
112 | language: string | ||
113 | nsfw: boolean | ||
114 | commentsEnabled: boolean | ||
115 | downloadEnabled: boolean | ||
116 | description: string | ||
117 | publishedAt?: string | ||
118 | support: string | ||
119 | originallyPublishedAt?: string | ||
120 | account: { | ||
121 | name: string | ||
122 | host: string | ||
123 | } | ||
124 | isLocal: boolean | ||
125 | tags: string[] | ||
126 | privacy: number | ||
127 | likes?: number | ||
128 | dislikes?: number | ||
129 | duration: number | ||
130 | channel: { | ||
131 | displayName: string | ||
132 | name: string | ||
133 | description: string | ||
134 | isLocal: boolean | ||
135 | } | ||
136 | fixture: string | ||
137 | files: { | ||
138 | resolution: number | ||
139 | size: number | ||
140 | }[] | ||
141 | thumbnailfile?: string | ||
142 | previewfile?: string | ||
143 | } | ||
144 | }) { | ||
145 | const { attributes, originServer, server, videoUUID } = options | ||
146 | |||
147 | const video = await server.videos.get({ id: videoUUID }) | ||
148 | |||
149 | if (!attributes.likes) attributes.likes = 0 | ||
150 | if (!attributes.dislikes) attributes.dislikes = 0 | ||
151 | |||
152 | expect(video.name).to.equal(attributes.name) | ||
153 | expect(video.category.id).to.equal(attributes.category) | ||
154 | expect(video.category.label).to.equal(attributes.category !== null ? VIDEO_CATEGORIES[attributes.category] : 'Unknown') | ||
155 | expect(video.licence.id).to.equal(attributes.licence) | ||
156 | expect(video.licence.label).to.equal(attributes.licence !== null ? VIDEO_LICENCES[attributes.licence] : 'Unknown') | ||
157 | expect(video.language.id).to.equal(attributes.language) | ||
158 | expect(video.language.label).to.equal(attributes.language !== null ? VIDEO_LANGUAGES[attributes.language] : 'Unknown') | ||
159 | expect(video.privacy.id).to.deep.equal(attributes.privacy) | ||
160 | expect(video.privacy.label).to.deep.equal(VIDEO_PRIVACIES[attributes.privacy]) | ||
161 | expect(video.nsfw).to.equal(attributes.nsfw) | ||
162 | expect(video.description).to.equal(attributes.description) | ||
163 | expect(video.account.id).to.be.a('number') | ||
164 | expect(video.account.host).to.equal(attributes.account.host) | ||
165 | expect(video.account.name).to.equal(attributes.account.name) | ||
166 | expect(video.channel.displayName).to.equal(attributes.channel.displayName) | ||
167 | expect(video.channel.name).to.equal(attributes.channel.name) | ||
168 | expect(video.likes).to.equal(attributes.likes) | ||
169 | expect(video.dislikes).to.equal(attributes.dislikes) | ||
170 | expect(video.isLocal).to.equal(attributes.isLocal) | ||
171 | expect(video.duration).to.equal(attributes.duration) | ||
172 | expect(video.url).to.contain(originServer.host) | ||
173 | expect(dateIsValid(video.createdAt)).to.be.true | ||
174 | expect(dateIsValid(video.publishedAt)).to.be.true | ||
175 | expect(dateIsValid(video.updatedAt)).to.be.true | ||
176 | |||
177 | if (attributes.publishedAt) { | ||
178 | expect(video.publishedAt).to.equal(attributes.publishedAt) | ||
179 | } | ||
180 | |||
181 | if (attributes.originallyPublishedAt) { | ||
182 | expect(video.originallyPublishedAt).to.equal(attributes.originallyPublishedAt) | ||
183 | } else { | ||
184 | expect(video.originallyPublishedAt).to.be.null | ||
185 | } | ||
186 | |||
187 | expect(video.files).to.have.lengthOf(attributes.files.length) | ||
188 | expect(video.tags).to.deep.equal(attributes.tags) | ||
189 | expect(video.account.name).to.equal(attributes.account.name) | ||
190 | expect(video.account.host).to.equal(attributes.account.host) | ||
191 | expect(video.channel.displayName).to.equal(attributes.channel.displayName) | ||
192 | expect(video.channel.name).to.equal(attributes.channel.name) | ||
193 | expect(video.channel.host).to.equal(attributes.account.host) | ||
194 | expect(video.channel.isLocal).to.equal(attributes.channel.isLocal) | ||
195 | expect(dateIsValid(video.channel.createdAt.toString())).to.be.true | ||
196 | expect(dateIsValid(video.channel.updatedAt.toString())).to.be.true | ||
197 | expect(video.commentsEnabled).to.equal(attributes.commentsEnabled) | ||
198 | expect(video.downloadEnabled).to.equal(attributes.downloadEnabled) | ||
199 | |||
200 | expect(video.thumbnailPath).to.exist | ||
201 | await testImageGeneratedByFFmpeg(server.url, attributes.thumbnailfile || attributes.fixture, video.thumbnailPath) | ||
202 | |||
203 | if (attributes.previewfile) { | ||
204 | expect(video.previewPath).to.exist | ||
205 | await testImageGeneratedByFFmpeg(server.url, attributes.previewfile, video.previewPath) | ||
206 | } | ||
207 | |||
208 | await completeWebVideoFilesCheck({ server, originServer, videoUUID: video.uuid, ...pick(attributes, [ 'fixture', 'files' ]) }) | ||
209 | } | ||
210 | |||
211 | async function checkVideoFilesWereRemoved (options: { | ||
212 | server: PeerTubeServer | ||
213 | video: VideoDetails | ||
214 | captions?: VideoCaption[] | ||
215 | onlyVideoFiles?: boolean // default false | ||
216 | }) { | ||
217 | const { video, server, captions = [], onlyVideoFiles = false } = options | ||
218 | |||
219 | const webVideoFiles = video.files || [] | ||
220 | const hlsFiles = video.streamingPlaylists[0]?.files || [] | ||
221 | |||
222 | const thumbnailName = basename(video.thumbnailPath) | ||
223 | const previewName = basename(video.previewPath) | ||
224 | |||
225 | const torrentNames = webVideoFiles.concat(hlsFiles).map(f => basename(f.torrentUrl)) | ||
226 | |||
227 | const captionNames = captions.map(c => basename(c.captionPath)) | ||
228 | |||
229 | const webVideoFilenames = webVideoFiles.map(f => basename(f.fileUrl)) | ||
230 | const hlsFilenames = hlsFiles.map(f => basename(f.fileUrl)) | ||
231 | |||
232 | let directories: { [ directory: string ]: string[] } = { | ||
233 | videos: webVideoFilenames, | ||
234 | redundancy: webVideoFilenames, | ||
235 | [join('playlists', 'hls')]: hlsFilenames, | ||
236 | [join('redundancy', 'hls')]: hlsFilenames | ||
237 | } | ||
238 | |||
239 | if (onlyVideoFiles !== true) { | ||
240 | directories = { | ||
241 | ...directories, | ||
242 | |||
243 | thumbnails: [ thumbnailName ], | ||
244 | previews: [ previewName ], | ||
245 | torrents: torrentNames, | ||
246 | captions: captionNames | ||
247 | } | ||
248 | } | ||
249 | |||
250 | for (const directory of Object.keys(directories)) { | ||
251 | const directoryPath = server.servers.buildDirectory(directory) | ||
252 | |||
253 | const directoryExists = await pathExists(directoryPath) | ||
254 | if (directoryExists === false) continue | ||
255 | |||
256 | const existingFiles = await readdir(directoryPath) | ||
257 | for (const existingFile of existingFiles) { | ||
258 | for (const shouldNotExist of directories[directory]) { | ||
259 | expect(existingFile, `File ${existingFile} should not exist in ${directoryPath}`).to.not.contain(shouldNotExist) | ||
260 | } | ||
261 | } | ||
262 | } | ||
263 | } | ||
264 | |||
265 | async function saveVideoInServers (servers: PeerTubeServer[], uuid: string) { | ||
266 | for (const server of servers) { | ||
267 | server.store.videoDetails = await server.videos.get({ id: uuid }) | ||
268 | } | ||
269 | } | ||
270 | |||
271 | function checkUploadVideoParam (options: { | ||
272 | server: PeerTubeServer | ||
273 | token: string | ||
274 | attributes: Partial<VideoEdit> | ||
275 | expectedStatus?: HttpStatusCode | ||
276 | completedExpectedStatus?: HttpStatusCode | ||
277 | mode?: 'legacy' | 'resumable' | ||
278 | }) { | ||
279 | const { server, token, attributes, completedExpectedStatus, expectedStatus, mode = 'legacy' } = options | ||
280 | |||
281 | return mode === 'legacy' | ||
282 | ? server.videos.buildLegacyUpload({ token, attributes, expectedStatus: expectedStatus || completedExpectedStatus }) | ||
283 | : server.videos.buildResumeUpload({ | ||
284 | token, | ||
285 | attributes, | ||
286 | expectedStatus, | ||
287 | completedExpectedStatus, | ||
288 | path: '/api/v1/videos/upload-resumable' | ||
289 | }) | ||
290 | } | ||
291 | |||
292 | // serverNumber starts from 1 | ||
293 | async function uploadRandomVideoOnServers ( | ||
294 | servers: PeerTubeServer[], | ||
295 | serverNumber: number, | ||
296 | additionalParams?: VideoEdit & { prefixName?: string } | ||
297 | ) { | ||
298 | const server = servers.find(s => s.serverNumber === serverNumber) | ||
299 | const res = await server.videos.randomUpload({ wait: false, additionalParams }) | ||
300 | |||
301 | await waitJobs(servers) | ||
302 | |||
303 | return res | ||
304 | } | ||
305 | |||
306 | // --------------------------------------------------------------------------- | ||
307 | |||
308 | export { | ||
309 | completeVideoCheck, | ||
310 | completeWebVideoFilesCheck, | ||
311 | checkUploadVideoParam, | ||
312 | uploadRandomVideoOnServers, | ||
313 | checkVideoFilesWereRemoved, | ||
314 | saveVideoInServers | ||
315 | } | ||