1 import { expect } from 'chai'
2 import { writeFile } from 'fs-extra'
3 import { basename, join } from 'path'
4 import { HttpStatusCode, Video } from '@shared/models'
11 setAccessTokensToServers,
13 } from '../../../shared/server-commands'
15 async function testThumbnail (server: PeerTubeServer, videoId: number | string) {
16 const video = await server.videos.get({ id: videoId })
19 makeGetRequest({ url: server.url, path: video.thumbnailPath, expectedStatus: HttpStatusCode.OK_200 }),
20 makeGetRequest({ url: server.url, path: video.thumbnailPath, expectedStatus: HttpStatusCode.OK_200 })
23 for (const req of requests) {
25 expect(res.body).to.not.have.lengthOf(0)
29 describe('Test regenerate thumbnails script', function () {
30 let servers: PeerTubeServer[]
34 let remoteVideo: Video
36 let thumbnail1Path: string
37 let thumbnailRemotePath: string
39 before(async function () {
42 servers = await createMultipleServers(2)
43 await setAccessTokensToServers(servers)
45 await doubleFollow(servers[0], servers[1])
48 const videoUUID1 = (await servers[0].videos.quickUpload({ name: 'video 1' })).uuid
49 video1 = await servers[0].videos.get({ id: videoUUID1 })
51 thumbnail1Path = join(servers[0].servers.buildDirectory('thumbnails'), basename(video1.thumbnailPath))
53 const videoUUID2 = (await servers[0].videos.quickUpload({ name: 'video 2' })).uuid
54 video2 = await servers[0].videos.get({ id: videoUUID2 })
58 const videoUUID = (await servers[1].videos.quickUpload({ name: 'video 3' })).uuid
59 await waitJobs(servers)
61 remoteVideo = await servers[0].videos.get({ id: videoUUID })
63 thumbnailRemotePath = join(servers[0].servers.buildDirectory('thumbnails'), basename(remoteVideo.thumbnailPath))
66 await writeFile(thumbnail1Path, '')
67 await writeFile(thumbnailRemotePath, '')
70 it('Should have empty thumbnails', async function () {
72 const res = await makeGetRequest({ url: servers[0].url, path: video1.thumbnailPath, expectedStatus: HttpStatusCode.OK_200 })
73 expect(res.body).to.have.lengthOf(0)
77 const res = await makeGetRequest({ url: servers[0].url, path: video2.thumbnailPath, expectedStatus: HttpStatusCode.OK_200 })
78 expect(res.body).to.not.have.lengthOf(0)
82 const res = await makeGetRequest({ url: servers[0].url, path: remoteVideo.thumbnailPath, expectedStatus: HttpStatusCode.OK_200 })
83 expect(res.body).to.have.lengthOf(0)
87 it('Should regenerate local thumbnails from the CLI', async function () {
90 await servers[0].cli.execWithEnv(`npm run regenerate-thumbnails`)
93 it('Should have generated new thumbnail files', async function () {
94 await testThumbnail(servers[0], video1.uuid)
95 await testThumbnail(servers[0], video2.uuid)
97 const res = await makeGetRequest({ url: servers[0].url, path: remoteVideo.thumbnailPath, expectedStatus: HttpStatusCode.OK_200 })
98 expect(res.body).to.have.lengthOf(0)
101 it('Should have deleted old thumbnail files', async function () {
103 await makeGetRequest({ url: servers[0].url, path: video1.thumbnailPath, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
107 await makeGetRequest({ url: servers[0].url, path: video2.thumbnailPath, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
111 const res = await makeGetRequest({ url: servers[0].url, path: remoteVideo.thumbnailPath, expectedStatus: HttpStatusCode.OK_200 })
112 expect(res.body).to.have.lengthOf(0)
116 after(async function () {
117 await cleanupTests(servers)