2 import { expect } from 'chai'
3 import { writeFile } from 'fs-extra'
4 import { basename, join } from 'path'
5 import { HttpStatusCode } from '@shared/models'
6 import { Video } from '@shared/models'
10 createMultipleServers,
13 setAccessTokensToServers,
15 } from '../../../shared/extra-utils'
17 async function testThumbnail (server: PeerTubeServer, videoId: number | string) {
18 const video = await server.videos.get({ id: videoId })
21 makeRawRequest(join(server.url, video.thumbnailPath), HttpStatusCode.OK_200),
22 makeRawRequest(join(server.url, video.thumbnailPath), HttpStatusCode.OK_200)
25 for (const req of requests) {
27 expect(res.body).to.not.have.lengthOf(0)
31 describe('Test regenerate thumbnails script', function () {
32 let servers: PeerTubeServer[]
36 let remoteVideo: Video
38 let thumbnail1Path: string
39 let thumbnailRemotePath: string
41 before(async function () {
44 servers = await createMultipleServers(2)
45 await setAccessTokensToServers(servers)
47 await doubleFollow(servers[0], servers[1])
50 const videoUUID1 = (await servers[0].videos.quickUpload({ name: 'video 1' })).uuid
51 video1 = await servers[0].videos.get({ id: videoUUID1 })
53 thumbnail1Path = join(servers[0].servers.buildDirectory('thumbnails'), basename(video1.thumbnailPath))
55 const videoUUID2 = (await servers[0].videos.quickUpload({ name: 'video 2' })).uuid
56 video2 = await servers[0].videos.get({ id: videoUUID2 })
60 const videoUUID = (await servers[1].videos.quickUpload({ name: 'video 3' })).uuid
61 await waitJobs(servers)
63 remoteVideo = await servers[0].videos.get({ id: videoUUID })
65 thumbnailRemotePath = join(servers[0].servers.buildDirectory('thumbnails'), basename(remoteVideo.thumbnailPath))
68 await writeFile(thumbnail1Path, '')
69 await writeFile(thumbnailRemotePath, '')
72 it('Should have empty thumbnails', async function () {
74 const res = await makeRawRequest(join(servers[0].url, video1.thumbnailPath), HttpStatusCode.OK_200)
75 expect(res.body).to.have.lengthOf(0)
79 const res = await makeRawRequest(join(servers[0].url, video2.thumbnailPath), HttpStatusCode.OK_200)
80 expect(res.body).to.not.have.lengthOf(0)
84 const res = await makeRawRequest(join(servers[0].url, remoteVideo.thumbnailPath), HttpStatusCode.OK_200)
85 expect(res.body).to.have.lengthOf(0)
89 it('Should regenerate local thumbnails from the CLI', async function () {
92 await servers[0].cli.execWithEnv(`npm run regenerate-thumbnails`)
95 it('Should have generated new thumbnail files', async function () {
96 await testThumbnail(servers[0], video1.uuid)
97 await testThumbnail(servers[0], video2.uuid)
99 const res = await makeRawRequest(join(servers[0].url, remoteVideo.thumbnailPath), HttpStatusCode.OK_200)
100 expect(res.body).to.have.lengthOf(0)
103 it('Should have deleted old thumbnail files', async function () {
105 await makeRawRequest(join(servers[0].url, video1.thumbnailPath), HttpStatusCode.NOT_FOUND_404)
109 await makeRawRequest(join(servers[0].url, video2.thumbnailPath), HttpStatusCode.NOT_FOUND_404)
113 const res = await makeRawRequest(join(servers[0].url, remoteVideo.thumbnailPath), HttpStatusCode.OK_200)
114 expect(res.body).to.have.lengthOf(0)
118 after(async function () {
119 await cleanupTests(servers)