diff options
author | lutangar <johan.dufour@gmail.com> | 2021-11-02 19:11:20 +0100 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2021-12-16 10:08:43 +0100 |
commit | 06aad80165d09a8863ab8103149a8ff518b10641 (patch) | |
tree | a97fa31f3ade29ff807ca1b77704eb47085ab99d /server/tests/shared/video.ts | |
parent | 854f533c12bd2b88c70f9d5aeab770059e9a6861 (diff) | |
download | PeerTube-06aad80165d09a8863ab8103149a8ff518b10641.tar.gz PeerTube-06aad80165d09a8863ab8103149a8ff518b10641.tar.zst PeerTube-06aad80165d09a8863ab8103149a8ff518b10641.zip |
chore(refactor): remove shared folder dependencies to the server
Many files from the `shared` folder were importing files from the `server` folder.
When attempting to use Typescript project references to describe dependencies,
it highlighted a circular dependency beetween `shared` <-> `server`.
The Typescript project forbid such usages.
Using project references greatly improve performance by rebuilding only
the updated project and not all source files.
> see https://www.typescriptlang.org/docs/handbook/project-references.html
Diffstat (limited to 'server/tests/shared/video.ts')
-rw-r--r-- | server/tests/shared/video.ts | 148 |
1 files changed, 148 insertions, 0 deletions
diff --git a/server/tests/shared/video.ts b/server/tests/shared/video.ts new file mode 100644 index 000000000..0e6a00f5c --- /dev/null +++ b/server/tests/shared/video.ts | |||
@@ -0,0 +1,148 @@ | |||
1 | /* eslint-disable @typescript-eslint/no-unused-expressions */ | ||
2 | import { dateIsValid, makeRawRequest, PeerTubeServer, testImage, webtorrentAdd } from '@shared/extra-utils' | ||
3 | import { expect } from 'chai' | ||
4 | import { VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_PRIVACIES } from '@server/initializers/constants' | ||
5 | import { getLowercaseExtension, uuidRegex } from '@shared/core-utils' | ||
6 | |||
7 | export async function completeVideoCheck ( | ||
8 | server: PeerTubeServer, | ||
9 | video: any, | ||
10 | attributes: { | ||
11 | name: string | ||
12 | category: number | ||
13 | licence: number | ||
14 | language: string | ||
15 | nsfw: boolean | ||
16 | commentsEnabled: boolean | ||
17 | downloadEnabled: boolean | ||
18 | description: string | ||
19 | publishedAt?: string | ||
20 | support: string | ||
21 | originallyPublishedAt?: string | ||
22 | account: { | ||
23 | name: string | ||
24 | host: string | ||
25 | } | ||
26 | isLocal: boolean | ||
27 | tags: string[] | ||
28 | privacy: number | ||
29 | likes?: number | ||
30 | dislikes?: number | ||
31 | duration: number | ||
32 | channel: { | ||
33 | displayName: string | ||
34 | name: string | ||
35 | description: string | ||
36 | isLocal: boolean | ||
37 | } | ||
38 | fixture: string | ||
39 | files: { | ||
40 | resolution: number | ||
41 | size: number | ||
42 | }[] | ||
43 | thumbnailfile?: string | ||
44 | previewfile?: string | ||
45 | } | ||
46 | ) { | ||
47 | if (!attributes.likes) attributes.likes = 0 | ||
48 | if (!attributes.dislikes) attributes.dislikes = 0 | ||
49 | |||
50 | const host = new URL(server.url).host | ||
51 | const originHost = attributes.account.host | ||
52 | |||
53 | expect(video.name).to.equal(attributes.name) | ||
54 | expect(video.category.id).to.equal(attributes.category) | ||
55 | expect(video.category.label).to.equal(attributes.category !== null ? VIDEO_CATEGORIES[attributes.category] : 'Misc') | ||
56 | expect(video.licence.id).to.equal(attributes.licence) | ||
57 | expect(video.licence.label).to.equal(attributes.licence !== null ? VIDEO_LICENCES[attributes.licence] : 'Unknown') | ||
58 | expect(video.language.id).to.equal(attributes.language) | ||
59 | expect(video.language.label).to.equal(attributes.language !== null ? VIDEO_LANGUAGES[attributes.language] : 'Unknown') | ||
60 | expect(video.privacy.id).to.deep.equal(attributes.privacy) | ||
61 | expect(video.privacy.label).to.deep.equal(VIDEO_PRIVACIES[attributes.privacy]) | ||
62 | expect(video.nsfw).to.equal(attributes.nsfw) | ||
63 | expect(video.description).to.equal(attributes.description) | ||
64 | expect(video.account.id).to.be.a('number') | ||
65 | expect(video.account.host).to.equal(attributes.account.host) | ||
66 | expect(video.account.name).to.equal(attributes.account.name) | ||
67 | expect(video.channel.displayName).to.equal(attributes.channel.displayName) | ||
68 | expect(video.channel.name).to.equal(attributes.channel.name) | ||
69 | expect(video.likes).to.equal(attributes.likes) | ||
70 | expect(video.dislikes).to.equal(attributes.dislikes) | ||
71 | expect(video.isLocal).to.equal(attributes.isLocal) | ||
72 | expect(video.duration).to.equal(attributes.duration) | ||
73 | expect(video.url).to.contain(originHost) | ||
74 | expect(dateIsValid(video.createdAt)).to.be.true | ||
75 | expect(dateIsValid(video.publishedAt)).to.be.true | ||
76 | expect(dateIsValid(video.updatedAt)).to.be.true | ||
77 | |||
78 | if (attributes.publishedAt) { | ||
79 | expect(video.publishedAt).to.equal(attributes.publishedAt) | ||
80 | } | ||
81 | |||
82 | if (attributes.originallyPublishedAt) { | ||
83 | expect(video.originallyPublishedAt).to.equal(attributes.originallyPublishedAt) | ||
84 | } else { | ||
85 | expect(video.originallyPublishedAt).to.be.null | ||
86 | } | ||
87 | |||
88 | const videoDetails = await server.videos.get({ id: video.uuid }) | ||
89 | |||
90 | expect(videoDetails.files).to.have.lengthOf(attributes.files.length) | ||
91 | expect(videoDetails.tags).to.deep.equal(attributes.tags) | ||
92 | expect(videoDetails.account.name).to.equal(attributes.account.name) | ||
93 | expect(videoDetails.account.host).to.equal(attributes.account.host) | ||
94 | expect(video.channel.displayName).to.equal(attributes.channel.displayName) | ||
95 | expect(video.channel.name).to.equal(attributes.channel.name) | ||
96 | expect(videoDetails.channel.host).to.equal(attributes.account.host) | ||
97 | expect(videoDetails.channel.isLocal).to.equal(attributes.channel.isLocal) | ||
98 | expect(dateIsValid(videoDetails.channel.createdAt.toString())).to.be.true | ||
99 | expect(dateIsValid(videoDetails.channel.updatedAt.toString())).to.be.true | ||
100 | expect(videoDetails.commentsEnabled).to.equal(attributes.commentsEnabled) | ||
101 | expect(videoDetails.downloadEnabled).to.equal(attributes.downloadEnabled) | ||
102 | |||
103 | for (const attributeFile of attributes.files) { | ||
104 | const file = videoDetails.files.find(f => f.resolution.id === attributeFile.resolution) | ||
105 | expect(file).not.to.be.undefined | ||
106 | |||
107 | let extension = getLowercaseExtension(attributes.fixture) | ||
108 | // Transcoding enabled: extension will always be .mp4 | ||
109 | if (attributes.files.length > 1) extension = '.mp4' | ||
110 | |||
111 | expect(file.magnetUri).to.have.lengthOf.above(2) | ||
112 | |||
113 | expect(file.torrentDownloadUrl).to.match(new RegExp(`http://${host}/download/torrents/${uuidRegex}-${file.resolution.id}.torrent`)) | ||
114 | expect(file.torrentUrl).to.match(new RegExp(`http://${host}/lazy-static/torrents/${uuidRegex}-${file.resolution.id}.torrent`)) | ||
115 | |||
116 | expect(file.fileUrl).to.match(new RegExp(`http://${originHost}/static/webseed/${uuidRegex}-${file.resolution.id}${extension}`)) | ||
117 | expect(file.fileDownloadUrl).to.match(new RegExp(`http://${originHost}/download/videos/${uuidRegex}-${file.resolution.id}${extension}`)) | ||
118 | |||
119 | await Promise.all([ | ||
120 | makeRawRequest(file.torrentUrl, 200), | ||
121 | makeRawRequest(file.torrentDownloadUrl, 200), | ||
122 | makeRawRequest(file.metadataUrl, 200) | ||
123 | ]) | ||
124 | |||
125 | expect(file.resolution.id).to.equal(attributeFile.resolution) | ||
126 | expect(file.resolution.label).to.equal(attributeFile.resolution + 'p') | ||
127 | |||
128 | const minSize = attributeFile.size - ((10 * attributeFile.size) / 100) | ||
129 | const maxSize = attributeFile.size + ((10 * attributeFile.size) / 100) | ||
130 | expect( | ||
131 | file.size, | ||
132 | 'File size for resolution ' + file.resolution.label + ' outside confidence interval (' + minSize + '> size <' + maxSize + ')' | ||
133 | ).to.be.above(minSize).and.below(maxSize) | ||
134 | |||
135 | const torrent = await webtorrentAdd(file.magnetUri, true) | ||
136 | expect(torrent.files).to.be.an('array') | ||
137 | expect(torrent.files.length).to.equal(1) | ||
138 | expect(torrent.files[0].path).to.exist.and.to.not.equal('') | ||
139 | } | ||
140 | |||
141 | expect(videoDetails.thumbnailPath).to.exist | ||
142 | await testImage(server.url, attributes.thumbnailfile || attributes.fixture, videoDetails.thumbnailPath) | ||
143 | |||
144 | if (attributes.previewfile) { | ||
145 | expect(videoDetails.previewPath).to.exist | ||
146 | await testImage(server.url, attributes.previewfile, videoDetails.previewPath) | ||
147 | } | ||
148 | } | ||