Add concept of video state, and add ability to wait transcoding before
[oweals/peertube.git] / server / tests / cli / create-import-video-file-job.ts
1 /* tslint:disable:no-unused-expression */
2
3 import 'mocha'
4 import * as chai from 'chai'
5 import { VideoDetails, VideoFile } from '../../../shared/models/videos'
6 import {
7   doubleFollow,
8   execCLI,
9   flushAndRunMultipleServers,
10   flushTests,
11   getEnvCli,
12   getVideo,
13   getVideosList,
14   killallServers,
15   ServerInfo,
16   setAccessTokensToServers,
17   uploadVideo,
18   wait
19 } from '../utils'
20
21 const expect = chai.expect
22
23 function assertVideoProperties (video: VideoFile, resolution: number, extname: string, size?: number) {
24   expect(video).to.have.nested.property('resolution.id', resolution)
25   expect(video).to.have.property('magnetUri').that.includes(`.${extname}`)
26   expect(video).to.have.property('torrentUrl').that.includes(`-${resolution}.torrent`)
27   expect(video).to.have.property('fileUrl').that.includes(`.${extname}`)
28   expect(video).to.have.property('size').that.is.above(0)
29
30   if (size) expect(video.size).to.equal(size)
31 }
32
33 describe('Test create import video jobs', function () {
34   this.timeout(60000)
35
36   let servers: ServerInfo[] = []
37   let video1UUID: string
38   let video2UUID: string
39
40   before(async function () {
41     this.timeout(90000)
42     await flushTests()
43
44     // Run server 2 to have transcoding enabled
45     servers = await flushAndRunMultipleServers(2)
46     await setAccessTokensToServers(servers)
47
48     await doubleFollow(servers[0], servers[1])
49
50     // Upload two videos for our needs
51     const res1 = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video1' })
52     video1UUID = res1.body.video.uuid
53     const res2 = await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'video2' })
54     video2UUID = res2.body.video.uuid
55
56     // Transcoding
57     await wait(40000)
58   })
59
60   it('Should run a import job on video 1 with a lower resolution', async function () {
61     const env = getEnvCli(servers[0])
62     await execCLI(`${env} npm run create-import-video-file-job -- -v ${video1UUID} -i server/tests/fixtures/video_short-480.webm`)
63
64     await wait(30000)
65
66     let magnetUri: string
67     for (const server of servers) {
68       const { data: videos } = (await getVideosList(server.url)).body
69       expect(videos).to.have.lengthOf(2)
70
71       const video = videos.find(({ uuid }) => uuid === video1UUID)
72       const videoDetail: VideoDetails = (await getVideo(server.url, video.uuid)).body
73
74       expect(videoDetail.files).to.have.lengthOf(2)
75       const [originalVideo, transcodedVideo] = videoDetail.files
76       assertVideoProperties(originalVideo, 720, 'webm', 218910)
77       assertVideoProperties(transcodedVideo, 480, 'webm', 69217)
78
79       if (!magnetUri) magnetUri = transcodedVideo.magnetUri
80       else expect(transcodedVideo.magnetUri).to.equal(magnetUri)
81     }
82   })
83
84   it('Should run a import job on video 2 with the same resolution and a different extension', async function () {
85     const env = getEnvCli(servers[1])
86     await execCLI(`${env} npm run create-import-video-file-job -- -v ${video2UUID} -i server/tests/fixtures/video_short.ogv`)
87
88     await wait(30000)
89
90     let magnetUri: string
91     for (const server of servers) {
92       const { data: videos } = (await getVideosList(server.url)).body
93       expect(videos).to.have.lengthOf(2)
94
95       const video = videos.find(({ uuid }) => uuid === video2UUID)
96       const videoDetail: VideoDetails = (await getVideo(server.url, video.uuid)).body
97
98       expect(videoDetail.files).to.have.lengthOf(4)
99       const [originalVideo, transcodedVideo420, transcodedVideo320, transcodedVideo240] = videoDetail.files
100       assertVideoProperties(originalVideo, 720, 'ogv', 140849)
101       assertVideoProperties(transcodedVideo420, 480, 'mp4')
102       assertVideoProperties(transcodedVideo320, 360, 'mp4')
103       assertVideoProperties(transcodedVideo240, 240, 'mp4')
104
105       if (!magnetUri) magnetUri = originalVideo.magnetUri
106       else expect(originalVideo.magnetUri).to.equal(magnetUri)
107     }
108   })
109
110   it('Should run a import job on video 2 with the same resolution and the same extension', async function () {
111     const env = getEnvCli(servers[0])
112     await execCLI(`${env} npm run create-import-video-file-job -- -v ${video1UUID} -i server/tests/fixtures/video_short2.webm`)
113
114     await wait(30000)
115
116     let magnetUri: string
117     for (const server of servers) {
118       const { data: videos } = (await getVideosList(server.url)).body
119       expect(videos).to.have.lengthOf(2)
120
121       const video = videos.find(({ uuid }) => uuid === video1UUID)
122       const videoDetail: VideoDetails = (await getVideo(server.url, video.uuid)).body
123
124       expect(videoDetail.files).to.have.lengthOf(2)
125       const [ video720, video480 ] = videoDetail.files
126       assertVideoProperties(video720, 720, 'webm', 942961)
127       assertVideoProperties(video480, 480, 'webm', 69217)
128
129       if (!magnetUri) magnetUri = video720.magnetUri
130       else expect(video720.magnetUri).to.equal(magnetUri)
131     }
132   })
133
134   after(async function () {
135     killallServers(servers)
136
137     // Keep the logs if the test failed
138     if (this['ok']) {
139       await flushTests()
140     }
141   })
142 })