]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - shared/extra-utils/videos/videos.ts
Use an object to represent a server
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / videos / videos.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */
a20399c9
C
2
3import { expect } from 'chai'
d23dd9fb 4import { pathExists, readdir } from 'fs-extra'
ea54cd04 5import { join } from 'path'
ea54cd04 6import { getLowercaseExtension } from '@server/helpers/core-utils'
d61893f7 7import { HttpStatusCode } from '@shared/core-utils'
d23dd9fb
C
8import { VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_PRIVACIES } from '../../../server/initializers/constants'
9import { dateIsValid, testImage, webtorrentAdd } from '../miscs'
10import { makeRawRequest } from '../requests/requests'
11import { waitJobs } from '../server'
254d3579 12import { PeerTubeServer } from '../server/server'
d23dd9fb 13import { VideoEdit } from './videos-command'
68e70a74 14
25378bc8
C
15async function checkVideoFilesWereRemoved (
16 videoUUID: string,
254d3579 17 server: PeerTubeServer,
09209296
C
18 directories = [
19 'redundancy',
20 'videos',
21 'thumbnails',
22 'torrents',
23 'previews',
24 'captions',
25 join('playlists', 'hls'),
26 join('redundancy', 'hls')
27 ]
25378bc8 28) {
25378bc8 29 for (const directory of directories) {
89d241a7 30 const directoryPath = server.servers.buildDirectory(directory)
f05a1c30 31
df0b219d
C
32 const directoryExists = await pathExists(directoryPath)
33 if (directoryExists === false) continue
f05a1c30 34
62689b94 35 const files = await readdir(directoryPath)
f05a1c30 36 for (const file of files) {
7448551f 37 expect(file, `File ${file} should not exist in ${directoryPath}`).to.not.contain(videoUUID)
f05a1c30
C
38 }
39 }
40}
41
f6d6e7f8 42function checkUploadVideoParam (
254d3579 43 server: PeerTubeServer,
f6d6e7f8 44 token: string,
d23dd9fb
C
45 attributes: Partial<VideoEdit>,
46 expectedStatus = HttpStatusCode.OK_200,
f6d6e7f8 47 mode: 'legacy' | 'resumable' = 'legacy'
48) {
49 return mode === 'legacy'
89d241a7
C
50 ? server.videos.buildLegacyUpload({ token, attributes, expectedStatus })
51 : server.videos.buildResumeUpload({ token, attributes, expectedStatus })
fdbda9e3
C
52}
53
a20399c9 54async function completeVideoCheck (
254d3579 55 server: PeerTubeServer,
a20399c9
C
56 video: any,
57 attributes: {
58 name: string
59 category: number
60 licence: number
9d3ef9fe 61 language: string
a20399c9 62 nsfw: boolean
47564bbe 63 commentsEnabled: boolean
7f2cfe3a 64 downloadEnabled: boolean
a20399c9 65 description: string
53a61317 66 publishedAt?: string
2422c46b 67 support: string
a1587156 68 originallyPublishedAt?: string
b64c950a
C
69 account: {
70 name: string
71 host: string
72 }
f6eebcb3
C
73 isLocal: boolean
74 tags: string[]
75 privacy: number
76 likes?: number
77 dislikes?: number
78 duration: number
a20399c9 79 channel: {
f6eebcb3
C
80 displayName: string
81 name: string
b1f5b93e 82 description
a20399c9
C
83 isLocal: boolean
84 }
f6eebcb3 85 fixture: string
a20399c9
C
86 files: {
87 resolution: number
88 size: number
a1587156 89 }[]
ac81d1a0
C
90 thumbnailfile?: string
91 previewfile?: string
a20399c9
C
92 }
93) {
b1f5b93e
C
94 if (!attributes.likes) attributes.likes = 0
95 if (!attributes.dislikes) attributes.dislikes = 0
96
d23dd9fb 97 const host = new URL(server.url).host
90a8bd30
C
98 const originHost = attributes.account.host
99
a20399c9 100 expect(video.name).to.equal(attributes.name)
09700934 101 expect(video.category.id).to.equal(attributes.category)
9d3ef9fe 102 expect(video.category.label).to.equal(attributes.category !== null ? VIDEO_CATEGORIES[attributes.category] : 'Misc')
09700934 103 expect(video.licence.id).to.equal(attributes.licence)
9d3ef9fe 104 expect(video.licence.label).to.equal(attributes.licence !== null ? VIDEO_LICENCES[attributes.licence] : 'Unknown')
09700934 105 expect(video.language.id).to.equal(attributes.language)
9d3ef9fe 106 expect(video.language.label).to.equal(attributes.language !== null ? VIDEO_LANGUAGES[attributes.language] : 'Unknown')
2243730c
C
107 expect(video.privacy.id).to.deep.equal(attributes.privacy)
108 expect(video.privacy.label).to.deep.equal(VIDEO_PRIVACIES[attributes.privacy])
a20399c9
C
109 expect(video.nsfw).to.equal(attributes.nsfw)
110 expect(video.description).to.equal(attributes.description)
03e12d7c 111 expect(video.account.id).to.be.a('number')
b64c950a
C
112 expect(video.account.host).to.equal(attributes.account.host)
113 expect(video.account.name).to.equal(attributes.account.name)
f6eebcb3
C
114 expect(video.channel.displayName).to.equal(attributes.channel.displayName)
115 expect(video.channel.name).to.equal(attributes.channel.name)
b1f5b93e
C
116 expect(video.likes).to.equal(attributes.likes)
117 expect(video.dislikes).to.equal(attributes.dislikes)
a20399c9 118 expect(video.isLocal).to.equal(attributes.isLocal)
b1f5b93e 119 expect(video.duration).to.equal(attributes.duration)
a20399c9 120 expect(dateIsValid(video.createdAt)).to.be.true
c49db162 121 expect(dateIsValid(video.publishedAt)).to.be.true
a20399c9
C
122 expect(dateIsValid(video.updatedAt)).to.be.true
123
53a61317 124 if (attributes.publishedAt) {
53a61317
C
125 expect(video.publishedAt).to.equal(attributes.publishedAt)
126 }
127
7519127b
C
128 if (attributes.originallyPublishedAt) {
129 expect(video.originallyPublishedAt).to.equal(attributes.originallyPublishedAt)
130 } else {
131 expect(video.originallyPublishedAt).to.be.null
132 }
133
89d241a7 134 const videoDetails = await server.videos.get({ id: video.uuid })
a20399c9
C
135
136 expect(videoDetails.files).to.have.lengthOf(attributes.files.length)
137 expect(videoDetails.tags).to.deep.equal(attributes.tags)
19a3b914
C
138 expect(videoDetails.account.name).to.equal(attributes.account.name)
139 expect(videoDetails.account.host).to.equal(attributes.account.host)
f6eebcb3
C
140 expect(video.channel.displayName).to.equal(attributes.channel.displayName)
141 expect(video.channel.name).to.equal(attributes.channel.name)
0f320037 142 expect(videoDetails.channel.host).to.equal(attributes.account.host)
a20399c9 143 expect(videoDetails.channel.isLocal).to.equal(attributes.channel.isLocal)
0f320037
C
144 expect(dateIsValid(videoDetails.channel.createdAt.toString())).to.be.true
145 expect(dateIsValid(videoDetails.channel.updatedAt.toString())).to.be.true
146 expect(videoDetails.commentsEnabled).to.equal(attributes.commentsEnabled)
7f2cfe3a 147 expect(videoDetails.downloadEnabled).to.equal(attributes.downloadEnabled)
a20399c9
C
148
149 for (const attributeFile of attributes.files) {
5d00a3d7 150 const file = videoDetails.files.find(f => f.resolution.id === attributeFile.resolution)
a20399c9
C
151 expect(file).not.to.be.undefined
152
ea54cd04 153 let extension = getLowercaseExtension(attributes.fixture)
48f07b4a
C
154 // Transcoding enabled: extension will always be .mp4
155 if (attributes.files.length > 1) extension = '.mp4'
b1f5b93e 156
a20399c9 157 expect(file.magnetUri).to.have.lengthOf.above(2)
90a8bd30
C
158
159 expect(file.torrentDownloadUrl).to.equal(`http://${host}/download/torrents/${videoDetails.uuid}-${file.resolution.id}.torrent`)
160 expect(file.torrentUrl).to.equal(`http://${host}/lazy-static/torrents/${videoDetails.uuid}-${file.resolution.id}.torrent`)
161
162 expect(file.fileUrl).to.equal(`http://${originHost}/static/webseed/${videoDetails.uuid}-${file.resolution.id}${extension}`)
163 expect(file.fileDownloadUrl).to.equal(`http://${originHost}/download/videos/${videoDetails.uuid}-${file.resolution.id}${extension}`)
164
165 await Promise.all([
166 makeRawRequest(file.torrentUrl, 200),
167 makeRawRequest(file.torrentDownloadUrl, 200),
168 makeRawRequest(file.metadataUrl, 200),
169 // Backward compatibility
170 makeRawRequest(`http://${originHost}/static/torrents/${videoDetails.uuid}-${file.resolution.id}.torrent`, 200)
171 ])
172
09700934
C
173 expect(file.resolution.id).to.equal(attributeFile.resolution)
174 expect(file.resolution.label).to.equal(attributeFile.resolution + 'p')
b1f5b93e
C
175
176 const minSize = attributeFile.size - ((10 * attributeFile.size) / 100)
177 const maxSize = attributeFile.size + ((10 * attributeFile.size) / 100)
a1587156
C
178 expect(
179 file.size,
180 'File size for resolution ' + file.resolution.label + ' outside confidence interval (' + minSize + '> size <' + maxSize + ')'
181 ).to.be.above(minSize).and.below(maxSize)
a20399c9 182
d7a25329 183 const torrent = await webtorrentAdd(file.magnetUri, true)
a20399c9
C
184 expect(torrent.files).to.be.an('array')
185 expect(torrent.files.length).to.equal(1)
186 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
187 }
44d4ee4f 188
08a47c75 189 expect(videoDetails.thumbnailPath).to.exist
d23dd9fb 190 await testImage(server.url, attributes.thumbnailfile || attributes.fixture, videoDetails.thumbnailPath)
44d4ee4f
C
191
192 if (attributes.previewfile) {
08a47c75 193 expect(videoDetails.previewPath).to.exist
d23dd9fb 194 await testImage(server.url, attributes.previewfile, videoDetails.previewPath)
44d4ee4f 195 }
a20399c9
C
196}
197
8eb07b01 198// serverNumber starts from 1
d23dd9fb 199async function uploadRandomVideoOnServers (
254d3579 200 servers: PeerTubeServer[],
d23dd9fb
C
201 serverNumber: number,
202 additionalParams?: VideoEdit & { prefixName?: string }
203) {
8eb07b01 204 const server = servers.find(s => s.serverNumber === serverNumber)
89d241a7 205 const res = await server.videos.randomUpload({ wait: false, ...additionalParams })
8eb07b01
C
206
207 await waitJobs(servers)
208
209 return res
210}
211
0e1dc3e7
C
212// ---------------------------------------------------------------------------
213
214export {
f6d6e7f8 215 checkUploadVideoParam,
f05a1c30 216 completeVideoCheck,
d23dd9fb
C
217 uploadRandomVideoOnServers,
218 checkVideoFilesWereRemoved
0e1dc3e7 219}