2 import { expect } from 'chai'
3 import { writeFile } from 'fs-extra'
4 import { basename, join } from 'path'
5 import { HttpStatusCode, Video } from '@shared/models'
12 setAccessTokensToServers,
14 } from '../../../shared/extra-utils'
16 async function testThumbnail (server: PeerTubeServer, videoId: number | string) {
17 const video = await server.videos.get({ id: videoId })
20 makeRawRequest(join(server.url, video.thumbnailPath), HttpStatusCode.OK_200),
21 makeRawRequest(join(server.url, video.thumbnailPath), HttpStatusCode.OK_200)
24 for (const req of requests) {
26 expect(res.body).to.not.have.lengthOf(0)
30 describe('Test regenerate thumbnails script', function () {
31 let servers: PeerTubeServer[]
35 let remoteVideo: Video
37 let thumbnail1Path: string
38 let thumbnailRemotePath: string
40 before(async function () {
43 servers = await createMultipleServers(2)
44 await setAccessTokensToServers(servers)
46 await doubleFollow(servers[0], servers[1])
49 const videoUUID1 = (await servers[0].videos.quickUpload({ name: 'video 1' })).uuid
50 video1 = await servers[0].videos.get({ id: videoUUID1 })
52 thumbnail1Path = join(servers[0].servers.buildDirectory('thumbnails'), basename(video1.thumbnailPath))
54 const videoUUID2 = (await servers[0].videos.quickUpload({ name: 'video 2' })).uuid
55 video2 = await servers[0].videos.get({ id: videoUUID2 })
59 const videoUUID = (await servers[1].videos.quickUpload({ name: 'video 3' })).uuid
60 await waitJobs(servers)
62 remoteVideo = await servers[0].videos.get({ id: videoUUID })
64 thumbnailRemotePath = join(servers[0].servers.buildDirectory('thumbnails'), basename(remoteVideo.thumbnailPath))
67 await writeFile(thumbnail1Path, '')
68 await writeFile(thumbnailRemotePath, '')
71 it('Should have empty thumbnails', async function () {
73 const res = await makeRawRequest(join(servers[0].url, video1.thumbnailPath), HttpStatusCode.OK_200)
74 expect(res.body).to.have.lengthOf(0)
78 const res = await makeRawRequest(join(servers[0].url, video2.thumbnailPath), HttpStatusCode.OK_200)
79 expect(res.body).to.not.have.lengthOf(0)
83 const res = await makeRawRequest(join(servers[0].url, remoteVideo.thumbnailPath), HttpStatusCode.OK_200)
84 expect(res.body).to.have.lengthOf(0)
88 it('Should regenerate local thumbnails from the CLI', async function () {
91 await servers[0].cli.execWithEnv(`npm run regenerate-thumbnails`)
94 it('Should have generated new thumbnail files', async function () {
95 await testThumbnail(servers[0], video1.uuid)
96 await testThumbnail(servers[0], video2.uuid)
98 const res = await makeRawRequest(join(servers[0].url, remoteVideo.thumbnailPath), HttpStatusCode.OK_200)
99 expect(res.body).to.have.lengthOf(0)
102 it('Should have deleted old thumbnail files', async function () {
104 await makeRawRequest(join(servers[0].url, video1.thumbnailPath), HttpStatusCode.NOT_FOUND_404)
108 await makeRawRequest(join(servers[0].url, video2.thumbnailPath), HttpStatusCode.NOT_FOUND_404)
112 const res = await makeRawRequest(join(servers[0].url, remoteVideo.thumbnailPath), HttpStatusCode.OK_200)
113 expect(res.body).to.have.lengthOf(0)
117 after(async function () {
118 await cleanupTests(servers)