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 { basename, join } from 'path'
6 import { loadLanguages, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_PRIVACIES } from '@server/initializers/constants'
7 import { getLowercaseExtension, uuidRegex } from '@shared/core-utils'
8 import { HttpStatusCode, VideoCaption, VideoDetails } from '@shared/models'
9 import { makeRawRequest, PeerTubeServer, VideoEdit, waitJobs, webtorrentAdd } from '@shared/server-commands'
10 import { dateIsValid, testImage } from './checks'
14 async function completeVideoCheck (
15 server: PeerTubeServer,
23 commentsEnabled: boolean
24 downloadEnabled: boolean
28 originallyPublishedAt?: string
50 thumbnailfile?: string
54 if (!attributes.likes) attributes.likes = 0
55 if (!attributes.dislikes) attributes.dislikes = 0
57 const host = new URL(server.url).host
58 const originHost = attributes.account.host
60 expect(video.name).to.equal(attributes.name)
61 expect(video.category.id).to.equal(attributes.category)
62 expect(video.category.label).to.equal(attributes.category !== null ? VIDEO_CATEGORIES[attributes.category] : 'Misc')
63 expect(video.licence.id).to.equal(attributes.licence)
64 expect(video.licence.label).to.equal(attributes.licence !== null ? VIDEO_LICENCES[attributes.licence] : 'Unknown')
65 expect(video.language.id).to.equal(attributes.language)
66 expect(video.language.label).to.equal(attributes.language !== null ? VIDEO_LANGUAGES[attributes.language] : 'Unknown')
67 expect(video.privacy.id).to.deep.equal(attributes.privacy)
68 expect(video.privacy.label).to.deep.equal(VIDEO_PRIVACIES[attributes.privacy])
69 expect(video.nsfw).to.equal(attributes.nsfw)
70 expect(video.description).to.equal(attributes.description)
71 expect(video.account.id).to.be.a('number')
72 expect(video.account.host).to.equal(attributes.account.host)
73 expect(video.account.name).to.equal(attributes.account.name)
74 expect(video.channel.displayName).to.equal(attributes.channel.displayName)
75 expect(video.channel.name).to.equal(attributes.channel.name)
76 expect(video.likes).to.equal(attributes.likes)
77 expect(video.dislikes).to.equal(attributes.dislikes)
78 expect(video.isLocal).to.equal(attributes.isLocal)
79 expect(video.duration).to.equal(attributes.duration)
80 expect(video.url).to.contain(originHost)
81 expect(dateIsValid(video.createdAt)).to.be.true
82 expect(dateIsValid(video.publishedAt)).to.be.true
83 expect(dateIsValid(video.updatedAt)).to.be.true
85 if (attributes.publishedAt) {
86 expect(video.publishedAt).to.equal(attributes.publishedAt)
89 if (attributes.originallyPublishedAt) {
90 expect(video.originallyPublishedAt).to.equal(attributes.originallyPublishedAt)
92 expect(video.originallyPublishedAt).to.be.null
95 const videoDetails = await server.videos.get({ id: video.uuid })
97 expect(videoDetails.files).to.have.lengthOf(attributes.files.length)
98 expect(videoDetails.tags).to.deep.equal(attributes.tags)
99 expect(videoDetails.account.name).to.equal(attributes.account.name)
100 expect(videoDetails.account.host).to.equal(attributes.account.host)
101 expect(video.channel.displayName).to.equal(attributes.channel.displayName)
102 expect(video.channel.name).to.equal(attributes.channel.name)
103 expect(videoDetails.channel.host).to.equal(attributes.account.host)
104 expect(videoDetails.channel.isLocal).to.equal(attributes.channel.isLocal)
105 expect(dateIsValid(videoDetails.channel.createdAt.toString())).to.be.true
106 expect(dateIsValid(videoDetails.channel.updatedAt.toString())).to.be.true
107 expect(videoDetails.commentsEnabled).to.equal(attributes.commentsEnabled)
108 expect(videoDetails.downloadEnabled).to.equal(attributes.downloadEnabled)
110 for (const attributeFile of attributes.files) {
111 const file = videoDetails.files.find(f => f.resolution.id === attributeFile.resolution)
112 expect(file).not.to.be.undefined
114 let extension = getLowercaseExtension(attributes.fixture)
115 // Transcoding enabled: extension will always be .mp4
116 if (attributes.files.length > 1) extension = '.mp4'
118 expect(file.id).to.exist
119 expect(file.magnetUri).to.have.lengthOf.above(2)
121 expect(file.torrentDownloadUrl).to.match(new RegExp(`http://${host}/download/torrents/${uuidRegex}-${file.resolution.id}.torrent`))
122 expect(file.torrentUrl).to.match(new RegExp(`http://${host}/lazy-static/torrents/${uuidRegex}-${file.resolution.id}.torrent`))
124 expect(file.fileUrl).to.match(new RegExp(`http://${originHost}/static/webseed/${uuidRegex}-${file.resolution.id}${extension}`))
125 expect(file.fileDownloadUrl).to.match(new RegExp(`http://${originHost}/download/videos/${uuidRegex}-${file.resolution.id}${extension}`))
128 makeRawRequest(file.torrentUrl, 200),
129 makeRawRequest(file.torrentDownloadUrl, 200),
130 makeRawRequest(file.metadataUrl, 200)
133 expect(file.resolution.id).to.equal(attributeFile.resolution)
134 expect(file.resolution.label).to.equal(attributeFile.resolution + 'p')
136 const minSize = attributeFile.size - ((10 * attributeFile.size) / 100)
137 const maxSize = attributeFile.size + ((10 * attributeFile.size) / 100)
140 'File size for resolution ' + file.resolution.label + ' outside confidence interval (' + minSize + '> size <' + maxSize + ')'
141 ).to.be.above(minSize).and.below(maxSize)
143 const torrent = await webtorrentAdd(file.magnetUri, true)
144 expect(torrent.files).to.be.an('array')
145 expect(torrent.files.length).to.equal(1)
146 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
149 expect(videoDetails.thumbnailPath).to.exist
150 await testImage(server.url, attributes.thumbnailfile || attributes.fixture, videoDetails.thumbnailPath)
152 if (attributes.previewfile) {
153 expect(videoDetails.previewPath).to.exist
154 await testImage(server.url, attributes.previewfile, videoDetails.previewPath)
158 async function checkVideoFilesWereRemoved (options: {
159 server: PeerTubeServer
161 captions?: VideoCaption[]
162 onlyVideoFiles?: boolean // default false
164 const { video, server, captions = [], onlyVideoFiles = false } = options
166 const webtorrentFiles = video.files || []
167 const hlsFiles = video.streamingPlaylists[0]?.files || []
169 const thumbnailName = basename(video.thumbnailPath)
170 const previewName = basename(video.previewPath)
172 const torrentNames = webtorrentFiles.concat(hlsFiles).map(f => basename(f.torrentUrl))
174 const captionNames = captions.map(c => basename(c.captionPath))
176 const webtorrentFilenames = webtorrentFiles.map(f => basename(f.fileUrl))
177 const hlsFilenames = hlsFiles.map(f => basename(f.fileUrl))
179 let directories: { [ directory: string ]: string[] } = {
180 videos: webtorrentFilenames,
181 redundancy: webtorrentFilenames,
182 [join('playlists', 'hls')]: hlsFilenames,
183 [join('redundancy', 'hls')]: hlsFilenames
186 if (onlyVideoFiles !== true) {
190 thumbnails: [ thumbnailName ],
191 previews: [ previewName ],
192 torrents: torrentNames,
193 captions: captionNames
197 for (const directory of Object.keys(directories)) {
198 const directoryPath = server.servers.buildDirectory(directory)
200 const directoryExists = await pathExists(directoryPath)
201 if (directoryExists === false) continue
203 const existingFiles = await readdir(directoryPath)
204 for (const existingFile of existingFiles) {
205 for (const shouldNotExist of directories[directory]) {
206 expect(existingFile, `File ${existingFile} should not exist in ${directoryPath}`).to.not.contain(shouldNotExist)
212 async function saveVideoInServers (servers: PeerTubeServer[], uuid: string) {
213 for (const server of servers) {
214 server.store.videoDetails = await server.videos.get({ id: uuid })
218 function checkUploadVideoParam (
219 server: PeerTubeServer,
221 attributes: Partial<VideoEdit>,
222 expectedStatus = HttpStatusCode.OK_200,
223 mode: 'legacy' | 'resumable' = 'legacy'
225 return mode === 'legacy'
226 ? server.videos.buildLegacyUpload({ token, attributes, expectedStatus })
227 : server.videos.buildResumeUpload({ token, attributes, expectedStatus })
230 // serverNumber starts from 1
231 async function uploadRandomVideoOnServers (
232 servers: PeerTubeServer[],
233 serverNumber: number,
234 additionalParams?: VideoEdit & { prefixName?: string }
236 const server = servers.find(s => s.serverNumber === serverNumber)
237 const res = await server.videos.randomUpload({ wait: false, additionalParams })
239 await waitJobs(servers)
244 // ---------------------------------------------------------------------------
248 checkUploadVideoParam,
249 uploadRandomVideoOnServers,
250 checkVideoFilesWereRemoved,