1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */
3 import { expect } from 'chai'
4 import { pathExists, readdir } from 'fs-extra'
5 import { join } from 'path'
6 import { getLowercaseExtension } from '@server/helpers/core-utils'
7 import { HttpStatusCode } from '@shared/models'
8 import { VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_PRIVACIES } from '../../../server/initializers/constants'
9 import { dateIsValid, testImage, webtorrentAdd } from '../miscs'
10 import { makeRawRequest } from '../requests/requests'
11 import { waitJobs } from '../server'
12 import { PeerTubeServer } from '../server/server'
13 import { VideoEdit } from './videos-command'
15 async function checkVideoFilesWereRemoved (
17 server: PeerTubeServer,
25 join('playlists', 'hls'),
26 join('redundancy', 'hls')
29 for (const directory of directories) {
30 const directoryPath = server.servers.buildDirectory(directory)
32 const directoryExists = await pathExists(directoryPath)
33 if (directoryExists === false) continue
35 const files = await readdir(directoryPath)
36 for (const file of files) {
37 expect(file, `File ${file} should not exist in ${directoryPath}`).to.not.contain(videoUUID)
42 function checkUploadVideoParam (
43 server: PeerTubeServer,
45 attributes: Partial<VideoEdit>,
46 expectedStatus = HttpStatusCode.OK_200,
47 mode: 'legacy' | 'resumable' = 'legacy'
49 return mode === 'legacy'
50 ? server.videos.buildLegacyUpload({ token, attributes, expectedStatus })
51 : server.videos.buildResumeUpload({ token, attributes, expectedStatus })
54 async function completeVideoCheck (
55 server: PeerTubeServer,
63 commentsEnabled: boolean
64 downloadEnabled: boolean
68 originallyPublishedAt?: string
90 thumbnailfile?: string
94 if (!attributes.likes) attributes.likes = 0
95 if (!attributes.dislikes) attributes.dislikes = 0
97 const host = new URL(server.url).host
98 const originHost = attributes.account.host
100 expect(video.name).to.equal(attributes.name)
101 expect(video.category.id).to.equal(attributes.category)
102 expect(video.category.label).to.equal(attributes.category !== null ? VIDEO_CATEGORIES[attributes.category] : 'Misc')
103 expect(video.licence.id).to.equal(attributes.licence)
104 expect(video.licence.label).to.equal(attributes.licence !== null ? VIDEO_LICENCES[attributes.licence] : 'Unknown')
105 expect(video.language.id).to.equal(attributes.language)
106 expect(video.language.label).to.equal(attributes.language !== null ? VIDEO_LANGUAGES[attributes.language] : 'Unknown')
107 expect(video.privacy.id).to.deep.equal(attributes.privacy)
108 expect(video.privacy.label).to.deep.equal(VIDEO_PRIVACIES[attributes.privacy])
109 expect(video.nsfw).to.equal(attributes.nsfw)
110 expect(video.description).to.equal(attributes.description)
111 expect(video.account.id).to.be.a('number')
112 expect(video.account.host).to.equal(attributes.account.host)
113 expect(video.account.name).to.equal(attributes.account.name)
114 expect(video.channel.displayName).to.equal(attributes.channel.displayName)
115 expect(video.channel.name).to.equal(attributes.channel.name)
116 expect(video.likes).to.equal(attributes.likes)
117 expect(video.dislikes).to.equal(attributes.dislikes)
118 expect(video.isLocal).to.equal(attributes.isLocal)
119 expect(video.duration).to.equal(attributes.duration)
120 expect(dateIsValid(video.createdAt)).to.be.true
121 expect(dateIsValid(video.publishedAt)).to.be.true
122 expect(dateIsValid(video.updatedAt)).to.be.true
124 if (attributes.publishedAt) {
125 expect(video.publishedAt).to.equal(attributes.publishedAt)
128 if (attributes.originallyPublishedAt) {
129 expect(video.originallyPublishedAt).to.equal(attributes.originallyPublishedAt)
131 expect(video.originallyPublishedAt).to.be.null
134 const videoDetails = await server.videos.get({ id: video.uuid })
136 expect(videoDetails.files).to.have.lengthOf(attributes.files.length)
137 expect(videoDetails.tags).to.deep.equal(attributes.tags)
138 expect(videoDetails.account.name).to.equal(attributes.account.name)
139 expect(videoDetails.account.host).to.equal(attributes.account.host)
140 expect(video.channel.displayName).to.equal(attributes.channel.displayName)
141 expect(video.channel.name).to.equal(attributes.channel.name)
142 expect(videoDetails.channel.host).to.equal(attributes.account.host)
143 expect(videoDetails.channel.isLocal).to.equal(attributes.channel.isLocal)
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)
147 expect(videoDetails.downloadEnabled).to.equal(attributes.downloadEnabled)
149 for (const attributeFile of attributes.files) {
150 const file = videoDetails.files.find(f => f.resolution.id === attributeFile.resolution)
151 expect(file).not.to.be.undefined
153 let extension = getLowercaseExtension(attributes.fixture)
154 // Transcoding enabled: extension will always be .mp4
155 if (attributes.files.length > 1) extension = '.mp4'
157 expect(file.magnetUri).to.have.lengthOf.above(2)
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`)
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}`)
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)
173 expect(file.resolution.id).to.equal(attributeFile.resolution)
174 expect(file.resolution.label).to.equal(attributeFile.resolution + 'p')
176 const minSize = attributeFile.size - ((10 * attributeFile.size) / 100)
177 const maxSize = attributeFile.size + ((10 * attributeFile.size) / 100)
180 'File size for resolution ' + file.resolution.label + ' outside confidence interval (' + minSize + '> size <' + maxSize + ')'
181 ).to.be.above(minSize).and.below(maxSize)
183 const torrent = await webtorrentAdd(file.magnetUri, true)
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('')
189 expect(videoDetails.thumbnailPath).to.exist
190 await testImage(server.url, attributes.thumbnailfile || attributes.fixture, videoDetails.thumbnailPath)
192 if (attributes.previewfile) {
193 expect(videoDetails.previewPath).to.exist
194 await testImage(server.url, attributes.previewfile, videoDetails.previewPath)
198 // serverNumber starts from 1
199 async function uploadRandomVideoOnServers (
200 servers: PeerTubeServer[],
201 serverNumber: number,
202 additionalParams?: VideoEdit & { prefixName?: string }
204 const server = servers.find(s => s.serverNumber === serverNumber)
205 const res = await server.videos.randomUpload({ wait: false, additionalParams })
207 await waitJobs(servers)
212 // ---------------------------------------------------------------------------
215 checkUploadVideoParam,
217 uploadRandomVideoOnServers,
218 checkVideoFilesWereRemoved