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