]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/cli/regenerate-thumbnails.ts
shared/ typescript types dir server-commands
[github/Chocobozzz/PeerTube.git] / server / tests / cli / regenerate-thumbnails.ts
1 import 'mocha'
2 import { expect } from 'chai'
3 import { writeFile } from 'fs-extra'
4 import { basename, join } from 'path'
5 import { HttpStatusCode, Video } from '@shared/models'
6 import {
7 cleanupTests,
8 createMultipleServers,
9 doubleFollow,
10 makeRawRequest,
11 PeerTubeServer,
12 setAccessTokensToServers,
13 waitJobs
14 } from '../../../shared/server-commands'
15
16 async function testThumbnail (server: PeerTubeServer, videoId: number | string) {
17 const video = await server.videos.get({ id: videoId })
18
19 const requests = [
20 makeRawRequest(join(server.url, video.thumbnailPath), HttpStatusCode.OK_200),
21 makeRawRequest(join(server.url, video.thumbnailPath), HttpStatusCode.OK_200)
22 ]
23
24 for (const req of requests) {
25 const res = await req
26 expect(res.body).to.not.have.lengthOf(0)
27 }
28 }
29
30 describe('Test regenerate thumbnails script', function () {
31 let servers: PeerTubeServer[]
32
33 let video1: Video
34 let video2: Video
35 let remoteVideo: Video
36
37 let thumbnail1Path: string
38 let thumbnailRemotePath: string
39
40 before(async function () {
41 this.timeout(60000)
42
43 servers = await createMultipleServers(2)
44 await setAccessTokensToServers(servers)
45
46 await doubleFollow(servers[0], servers[1])
47
48 {
49 const videoUUID1 = (await servers[0].videos.quickUpload({ name: 'video 1' })).uuid
50 video1 = await servers[0].videos.get({ id: videoUUID1 })
51
52 thumbnail1Path = join(servers[0].servers.buildDirectory('thumbnails'), basename(video1.thumbnailPath))
53
54 const videoUUID2 = (await servers[0].videos.quickUpload({ name: 'video 2' })).uuid
55 video2 = await servers[0].videos.get({ id: videoUUID2 })
56 }
57
58 {
59 const videoUUID = (await servers[1].videos.quickUpload({ name: 'video 3' })).uuid
60 await waitJobs(servers)
61
62 remoteVideo = await servers[0].videos.get({ id: videoUUID })
63
64 thumbnailRemotePath = join(servers[0].servers.buildDirectory('thumbnails'), basename(remoteVideo.thumbnailPath))
65 }
66
67 await writeFile(thumbnail1Path, '')
68 await writeFile(thumbnailRemotePath, '')
69 })
70
71 it('Should have empty thumbnails', async function () {
72 {
73 const res = await makeRawRequest(join(servers[0].url, video1.thumbnailPath), HttpStatusCode.OK_200)
74 expect(res.body).to.have.lengthOf(0)
75 }
76
77 {
78 const res = await makeRawRequest(join(servers[0].url, video2.thumbnailPath), HttpStatusCode.OK_200)
79 expect(res.body).to.not.have.lengthOf(0)
80 }
81
82 {
83 const res = await makeRawRequest(join(servers[0].url, remoteVideo.thumbnailPath), HttpStatusCode.OK_200)
84 expect(res.body).to.have.lengthOf(0)
85 }
86 })
87
88 it('Should regenerate local thumbnails from the CLI', async function () {
89 this.timeout(15000)
90
91 await servers[0].cli.execWithEnv(`npm run regenerate-thumbnails`)
92 })
93
94 it('Should have generated new thumbnail files', async function () {
95 await testThumbnail(servers[0], video1.uuid)
96 await testThumbnail(servers[0], video2.uuid)
97
98 const res = await makeRawRequest(join(servers[0].url, remoteVideo.thumbnailPath), HttpStatusCode.OK_200)
99 expect(res.body).to.have.lengthOf(0)
100 })
101
102 it('Should have deleted old thumbnail files', async function () {
103 {
104 await makeRawRequest(join(servers[0].url, video1.thumbnailPath), HttpStatusCode.NOT_FOUND_404)
105 }
106
107 {
108 await makeRawRequest(join(servers[0].url, video2.thumbnailPath), HttpStatusCode.NOT_FOUND_404)
109 }
110
111 {
112 const res = await makeRawRequest(join(servers[0].url, remoteVideo.thumbnailPath), HttpStatusCode.OK_200)
113 expect(res.body).to.have.lengthOf(0)
114 }
115 })
116
117 after(async function () {
118 await cleanupTests(servers)
119 })
120 })