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