]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/tests/cli/regenerate-thumbnails.ts
Introduce videos command
[github/Chocobozzz/PeerTube.git] / server / tests / cli / regenerate-thumbnails.ts
index a9c8642a5bf245ef282d62109589472c2684da38..d59520783ecb88d9720c0d50623d06e2b442f158 100644 (file)
@@ -2,22 +2,31 @@ import 'mocha'
 import { expect } from 'chai'
 import { writeFile } from 'fs-extra'
 import { basename, join } from 'path'
+import { HttpStatusCode } from '@shared/core-utils'
 import { Video } from '@shared/models'
 import {
-  buildServerDirectory,
   cleanupTests,
   doubleFollow,
-  execCLI,
   flushAndRunMultipleServers,
-  getEnvCli,
-  getVideo,
   makeRawRequest,
   ServerInfo,
   setAccessTokensToServers,
-  uploadVideoAndGetId,
   waitJobs
 } from '../../../shared/extra-utils'
-import { HttpStatusCode } from '@shared/core-utils'
+
+async function testThumbnail (server: ServerInfo, videoId: number | string) {
+  const video = await server.videosCommand.get({ id: videoId })
+
+  const requests = [
+    makeRawRequest(join(server.url, video.thumbnailPath), HttpStatusCode.OK_200),
+    makeRawRequest(join(server.url, video.thumbnailPath), HttpStatusCode.OK_200)
+  ]
+
+  for (const req of requests) {
+    const res = await req
+    expect(res.body).to.not.have.lengthOf(0)
+  }
+}
 
 describe('Test regenerate thumbnails script', function () {
   let servers: ServerInfo[]
@@ -38,22 +47,22 @@ describe('Test regenerate thumbnails script', function () {
     await doubleFollow(servers[0], servers[1])
 
     {
-      const videoUUID1 = (await uploadVideoAndGetId({ server: servers[0], videoName: 'video 1' })).uuid
-      video1 = await (getVideo(servers[0].url, videoUUID1).then(res => res.body))
+      const videoUUID1 = (await servers[0].videosCommand.quickUpload({ name: 'video 1' })).uuid
+      video1 = await servers[0].videosCommand.get({ id: videoUUID1 })
 
-      thumbnail1Path = join(buildServerDirectory(servers[0], 'thumbnails'), basename(video1.thumbnailPath))
+      thumbnail1Path = join(servers[0].serversCommand.buildDirectory('thumbnails'), basename(video1.thumbnailPath))
 
-      const videoUUID2 = (await uploadVideoAndGetId({ server: servers[0], videoName: 'video 2' })).uuid
-      video2 = await (getVideo(servers[0].url, videoUUID2).then(res => res.body))
+      const videoUUID2 = (await servers[0].videosCommand.quickUpload({ name: 'video 2' })).uuid
+      video2 = await servers[0].videosCommand.get({ id: videoUUID2 })
     }
 
     {
-      const videoUUID = (await uploadVideoAndGetId({ server: servers[1], videoName: 'video 3' })).uuid
+      const videoUUID = (await servers[1].videosCommand.quickUpload({ name: 'video 3' })).uuid
       await waitJobs(servers)
 
-      remoteVideo = await (getVideo(servers[0].url, videoUUID).then(res => res.body))
+      remoteVideo = await servers[0].videosCommand.get({ id: videoUUID })
 
-      thumbnailRemotePath = join(buildServerDirectory(servers[0], 'thumbnails'), basename(remoteVideo.thumbnailPath))
+      thumbnailRemotePath = join(servers[0].serversCommand.buildDirectory('thumbnails'), basename(remoteVideo.thumbnailPath))
     }
 
     await writeFile(thumbnail1Path, '')
@@ -80,22 +89,24 @@ describe('Test regenerate thumbnails script', function () {
   it('Should regenerate local thumbnails from the CLI', async function () {
     this.timeout(15000)
 
-    const env = getEnvCli(servers[0])
-    await execCLI(`${env} npm run regenerate-thumbnails`)
+    await servers[0].cliCommand.execWithEnv(`npm run regenerate-thumbnails`)
   })
 
-  it('Should have regenerated local thumbnails', async function () {
-    {
-      const res1 = await makeRawRequest(join(servers[0].url, video1.thumbnailPath), HttpStatusCode.OK_200)
-      expect(res1.body).to.not.have.lengthOf(0)
+  it('Should have generated new thumbnail files', async function () {
+    await testThumbnail(servers[0], video1.uuid)
+    await testThumbnail(servers[0], video2.uuid)
 
-      const res2 = await makeRawRequest(join(servers[0].url, video1.previewPath), HttpStatusCode.OK_200)
-      expect(res2.body).to.not.have.lengthOf(0)
+    const res = await makeRawRequest(join(servers[0].url, remoteVideo.thumbnailPath), HttpStatusCode.OK_200)
+    expect(res.body).to.have.lengthOf(0)
+  })
+
+  it('Should have deleted old thumbnail files', async function () {
+    {
+      await makeRawRequest(join(servers[0].url, video1.thumbnailPath), HttpStatusCode.NOT_FOUND_404)
     }
 
     {
-      const res = await makeRawRequest(join(servers[0].url, video2.thumbnailPath), HttpStatusCode.OK_200)
-      expect(res.body).to.not.have.lengthOf(0)
+      await makeRawRequest(join(servers[0].url, video2.thumbnailPath), HttpStatusCode.NOT_FOUND_404)
     }
 
     {