PeerTube/server/tests/cli/regenerate-thumbnails.ts

121 lines
3.6 KiB
TypeScript
Raw Normal View History

2021-03-12 10:04:49 -06:00
import 'mocha'
import { expect } from 'chai'
import { writeFile } from 'fs-extra'
import { basename, join } from 'path'
2021-07-16 07:27:30 -05:00
import { HttpStatusCode, Video } from '@shared/models'
2021-03-12 10:04:49 -06:00
import {
cleanupTests,
2021-07-16 02:47:51 -05:00
createMultipleServers,
2021-07-16 07:27:30 -05:00
doubleFollow,
2021-03-12 10:04:49 -06:00
makeRawRequest,
2021-07-16 02:47:51 -05:00
PeerTubeServer,
2021-03-12 10:04:49 -06:00
setAccessTokensToServers,
waitJobs
} from '../../../shared/extra-utils'
2021-07-16 02:47:51 -05:00
async function testThumbnail (server: PeerTubeServer, videoId: number | string) {
2021-07-16 02:04:35 -05:00
const video = await server.videos.get({ id: videoId })
2021-07-15 03:02:54 -05:00
const requests = [
makeRawRequest(join(server.url, video.thumbnailPath), HttpStatusCode.OK_200),
makeRawRequest(join(server.url, video.thumbnailPath), HttpStatusCode.OK_200)
]
2021-07-15 03:02:54 -05:00
for (const req of requests) {
const res = await req
expect(res.body).to.not.have.lengthOf(0)
}
}
2021-03-12 10:04:49 -06:00
describe('Test regenerate thumbnails script', function () {
2021-07-16 02:47:51 -05:00
let servers: PeerTubeServer[]
2021-03-12 10:04:49 -06:00
let video1: Video
let video2: Video
let remoteVideo: Video
let thumbnail1Path: string
let thumbnailRemotePath: string
before(async function () {
this.timeout(60000)
2021-07-16 02:47:51 -05:00
servers = await createMultipleServers(2)
2021-03-12 10:04:49 -06:00
await setAccessTokensToServers(servers)
await doubleFollow(servers[0], servers[1])
{
2021-07-16 02:04:35 -05:00
const videoUUID1 = (await servers[0].videos.quickUpload({ name: 'video 1' })).uuid
video1 = await servers[0].videos.get({ id: videoUUID1 })
2021-03-12 10:04:49 -06:00
2021-07-16 02:04:35 -05:00
thumbnail1Path = join(servers[0].servers.buildDirectory('thumbnails'), basename(video1.thumbnailPath))
2021-03-12 10:04:49 -06:00
2021-07-16 02:04:35 -05:00
const videoUUID2 = (await servers[0].videos.quickUpload({ name: 'video 2' })).uuid
video2 = await servers[0].videos.get({ id: videoUUID2 })
2021-03-12 10:04:49 -06:00
}
{
2021-07-16 02:04:35 -05:00
const videoUUID = (await servers[1].videos.quickUpload({ name: 'video 3' })).uuid
2021-03-12 10:04:49 -06:00
await waitJobs(servers)
2021-07-16 02:04:35 -05:00
remoteVideo = await servers[0].videos.get({ id: videoUUID })
2021-03-12 10:04:49 -06:00
2021-07-16 02:04:35 -05:00
thumbnailRemotePath = join(servers[0].servers.buildDirectory('thumbnails'), basename(remoteVideo.thumbnailPath))
2021-03-12 10:04:49 -06:00
}
await writeFile(thumbnail1Path, '')
await writeFile(thumbnailRemotePath, '')
})
it('Should have empty thumbnails', async function () {
{
const res = await makeRawRequest(join(servers[0].url, video1.thumbnailPath), HttpStatusCode.OK_200)
expect(res.body).to.have.lengthOf(0)
}
{
const res = await makeRawRequest(join(servers[0].url, video2.thumbnailPath), HttpStatusCode.OK_200)
expect(res.body).to.not.have.lengthOf(0)
}
{
const res = await makeRawRequest(join(servers[0].url, remoteVideo.thumbnailPath), HttpStatusCode.OK_200)
expect(res.body).to.have.lengthOf(0)
}
})
2021-04-08 03:08:11 -05:00
it('Should regenerate local thumbnails from the CLI', async function () {
2021-03-12 10:04:49 -06:00
this.timeout(15000)
2021-07-16 02:04:35 -05:00
await servers[0].cli.execWithEnv(`npm run regenerate-thumbnails`)
2021-03-12 10:04:49 -06:00
})
it('Should have generated new thumbnail files', async function () {
await testThumbnail(servers[0], video1.uuid)
await testThumbnail(servers[0], video2.uuid)
2021-03-12 10:04:49 -06:00
const res = await makeRawRequest(join(servers[0].url, remoteVideo.thumbnailPath), HttpStatusCode.OK_200)
expect(res.body).to.have.lengthOf(0)
})
it('Should have deleted old thumbnail files', async function () {
{
await makeRawRequest(join(servers[0].url, video1.thumbnailPath), HttpStatusCode.NOT_FOUND_404)
2021-03-12 10:04:49 -06:00
}
{
await makeRawRequest(join(servers[0].url, video2.thumbnailPath), HttpStatusCode.NOT_FOUND_404)
2021-03-12 10:04:49 -06:00
}
{
const res = await makeRawRequest(join(servers[0].url, remoteVideo.thumbnailPath), HttpStatusCode.OK_200)
expect(res.body).to.have.lengthOf(0)
}
})
after(async function () {
await cleanupTests(servers)
})
})