1 /* tslint:disable:no-unused-expression */
3 import * as chai from 'chai'
5 import { JobState, Video } from '../../../../shared/models'
6 import { VideoPrivacy } from '../../../../shared/models/videos'
7 import { VideoCommentThreadTree } from '../../../../shared/models/videos/video-comment.model'
12 flushAndRunMultipleServers,
19 setAccessTokensToServers,
22 uploadVideo, uploadVideoAndGetId,
24 setActorFollowScores, closeAllSequelize
25 } from '../../../../shared/extra-utils'
26 import { follow, getFollowersListPaginationAndSort } from '../../../../shared/extra-utils/server/follows'
27 import { getJobsListPaginationAndSort, waitJobs } from '../../../../shared/extra-utils/server/jobs'
30 addVideoCommentThread,
31 getVideoCommentThreads,
32 getVideoThreadComments
33 } from '../../../../shared/extra-utils/videos/video-comments'
35 const expect = chai.expect
37 describe('Test handle downs', function () {
38 let servers: ServerInfo[] = []
39 let threadIdServer1: number
40 let threadIdServer2: number
41 let commentIdServer1: number
42 let commentIdServer2: number
43 let missedVideo1: Video
44 let missedVideo2: Video
45 let unlistedVideo: Video
47 let videoIdsServer1: number[] = []
49 const videoAttributes = {
50 name: 'my super name for server 1',
55 privacy: VideoPrivacy.PUBLIC,
56 description: 'my super description for server 1',
57 support: 'my super support text for server 1',
58 tags: [ 'tag1p1', 'tag2p1' ],
59 fixture: 'video_short1.webm'
62 const unlistedVideoAttributes = immutableAssign(videoAttributes, {
63 privacy: VideoPrivacy.UNLISTED
66 let checkAttributes: any
67 let unlistedCheckAttributes: any
69 before(async function () {
72 servers = await flushAndRunMultipleServers(3)
75 name: 'my super name for server 1',
80 description: 'my super description for server 1',
81 support: 'my super support text for server 1',
84 host: 'localhost:' + servers[0].port
88 tags: [ 'tag1p1', 'tag2p1' ],
89 privacy: VideoPrivacy.PUBLIC,
90 commentsEnabled: true,
91 downloadEnabled: true,
94 displayName: 'Main root channel',
98 fixture: 'video_short1.webm',
106 unlistedCheckAttributes = immutableAssign(checkAttributes, {
107 privacy: VideoPrivacy.UNLISTED
110 // Get the access tokens
111 await setAccessTokensToServers(servers)
114 it('Should remove followers that are often down', async function () {
117 // Server 2 and 3 follow server 1
118 await follow(servers[1].url, [ servers[0].url ], servers[1].accessToken)
119 await follow(servers[2].url, [ servers[0].url ], servers[2].accessToken)
121 await waitJobs(servers)
123 // Upload a video to server 1
124 await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
126 await waitJobs(servers)
128 // And check all servers have this video
129 for (const server of servers) {
130 const res = await getVideosList(server.url)
131 expect(res.body.data).to.be.an('array')
132 expect(res.body.data).to.have.lengthOf(1)
136 killallServers([ servers[1] ])
138 // Remove server 2 follower
139 for (let i = 0; i < 10; i++) {
140 await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, videoAttributes)
143 await waitJobs(servers[0])
146 killallServers([ servers[2] ])
148 const resLastVideo1 = await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, videoAttributes)
149 missedVideo1 = resLastVideo1.body.video
151 const resLastVideo2 = await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, videoAttributes)
152 missedVideo2 = resLastVideo2.body.video
155 let resVideo = await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, unlistedVideoAttributes)
156 unlistedVideo = resVideo.body.video
158 // Add comments to video 2
160 const text = 'thread 1'
161 let resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, missedVideo2.uuid, text)
162 let comment = resComment.body.comment
163 threadIdServer1 = comment.id
165 resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, missedVideo2.uuid, comment.id, 'comment 1-1')
166 comment = resComment.body.comment
168 resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, missedVideo2.uuid, comment.id, 'comment 1-2')
169 commentIdServer1 = resComment.body.comment.id
172 await waitJobs(servers[0])
176 // Only server 3 is still a follower of server 1
177 const res = await getFollowersListPaginationAndSort(servers[0].url, 0, 2, 'createdAt')
178 expect(res.body.data).to.be.an('array')
179 expect(res.body.data).to.have.lengthOf(1)
180 expect(res.body.data[0].follower.host).to.equal('localhost:' + servers[2].port)
183 it('Should not have pending/processing jobs anymore', async function () {
184 const states: JobState[] = [ 'waiting', 'active' ]
186 for (const state of states) {
187 const res = await getJobsListPaginationAndSort(servers[ 0 ].url, servers[ 0 ].accessToken, state,0, 50, '-createdAt')
188 expect(res.body.data).to.have.length(0)
192 it('Should re-follow server 1', async function () {
195 await reRunServer(servers[1])
196 await reRunServer(servers[2])
198 await unfollow(servers[1].url, servers[1].accessToken, servers[0])
199 await waitJobs(servers)
201 await follow(servers[1].url, [ servers[0].url ], servers[1].accessToken)
203 await waitJobs(servers)
205 const res = await getFollowersListPaginationAndSort(servers[0].url, 0, 2, 'createdAt')
206 expect(res.body.data).to.be.an('array')
207 expect(res.body.data).to.have.lengthOf(2)
210 it('Should send an update to server 3, and automatically fetch the video', async function () {
213 const res1 = await getVideosList(servers[2].url)
214 expect(res1.body.data).to.be.an('array')
215 expect(res1.body.data).to.have.lengthOf(11)
217 await updateVideo(servers[0].url, servers[0].accessToken, missedVideo1.uuid, { })
218 await updateVideo(servers[0].url, servers[0].accessToken, unlistedVideo.uuid, { })
220 await waitJobs(servers)
222 const res = await getVideosList(servers[2].url)
223 expect(res.body.data).to.be.an('array')
224 // 1 video is unlisted
225 expect(res.body.data).to.have.lengthOf(12)
227 // Check unlisted video
228 const resVideo = await getVideo(servers[2].url, unlistedVideo.uuid)
229 expect(resVideo.body).not.to.be.undefined
231 await completeVideoCheck(servers[2].url, resVideo.body, unlistedCheckAttributes)
234 it('Should send comments on a video to server 3, and automatically fetch the video', async function () {
237 await addVideoCommentReply(servers[0].url, servers[0].accessToken, missedVideo2.uuid, commentIdServer1, 'comment 1-3')
239 await waitJobs(servers)
241 const resVideo = await getVideo(servers[2].url, missedVideo2.uuid)
242 expect(resVideo.body).not.to.be.undefined
245 let resComment = await getVideoCommentThreads(servers[2].url, missedVideo2.uuid, 0, 5)
246 expect(resComment.body.data).to.be.an('array')
247 expect(resComment.body.data).to.have.lengthOf(1)
249 threadIdServer2 = resComment.body.data[0].id
251 resComment = await getVideoThreadComments(servers[2].url, missedVideo2.uuid, threadIdServer2)
253 const tree: VideoCommentThreadTree = resComment.body
254 expect(tree.comment.text).equal('thread 1')
255 expect(tree.children).to.have.lengthOf(1)
257 const firstChild = tree.children[0]
258 expect(firstChild.comment.text).to.equal('comment 1-1')
259 expect(firstChild.children).to.have.lengthOf(1)
261 const childOfFirstChild = firstChild.children[0]
262 expect(childOfFirstChild.comment.text).to.equal('comment 1-2')
263 expect(childOfFirstChild.children).to.have.lengthOf(1)
265 const childOfChildFirstChild = childOfFirstChild.children[0]
266 expect(childOfChildFirstChild.comment.text).to.equal('comment 1-3')
267 expect(childOfChildFirstChild.children).to.have.lengthOf(0)
269 commentIdServer2 = childOfChildFirstChild.comment.id
273 it('Should correctly reply to the comment', async function () {
276 await addVideoCommentReply(servers[2].url, servers[2].accessToken, missedVideo2.uuid, commentIdServer2, 'comment 1-4')
278 await waitJobs(servers)
281 const resComment = await getVideoThreadComments(servers[0].url, missedVideo2.uuid, threadIdServer1)
283 const tree: VideoCommentThreadTree = resComment.body
284 expect(tree.comment.text).equal('thread 1')
285 expect(tree.children).to.have.lengthOf(1)
287 const firstChild = tree.children[0]
288 expect(firstChild.comment.text).to.equal('comment 1-1')
289 expect(firstChild.children).to.have.lengthOf(1)
291 const childOfFirstChild = firstChild.children[0]
292 expect(childOfFirstChild.comment.text).to.equal('comment 1-2')
293 expect(childOfFirstChild.children).to.have.lengthOf(1)
295 const childOfChildFirstChild = childOfFirstChild.children[0]
296 expect(childOfChildFirstChild.comment.text).to.equal('comment 1-3')
297 expect(childOfChildFirstChild.children).to.have.lengthOf(1)
299 const childOfChildOfChildOfFirstChild = childOfChildFirstChild.children[0]
300 expect(childOfChildOfChildOfFirstChild.comment.text).to.equal('comment 1-4')
301 expect(childOfChildOfChildOfFirstChild.children).to.have.lengthOf(0)
305 it('Should upload many videos on server 1', async function () {
308 for (let i = 0; i < 10; i++) {
309 const uuid = (await uploadVideoAndGetId({ server: servers[ 0 ], videoName: 'video ' + i })).uuid
310 videoIdsServer1.push(uuid)
313 await waitJobs(servers)
315 for (const id of videoIdsServer1) {
316 await getVideo(servers[ 1 ].url, id)
319 await waitJobs(servers)
320 await setActorFollowScores(servers[1].internalServerNumber, 20)
322 // Wait video expiration
325 // Refresh video -> score + 10 = 30
326 await getVideo(servers[1].url, videoIdsServer1[0])
328 await waitJobs(servers)
331 it('Should remove followings that are down', async function () {
334 killallServers([ servers[0] ])
336 // Wait video expiration
339 for (let i = 0; i < 3; i++) {
340 await getVideo(servers[1].url, videoIdsServer1[i])
342 await waitJobs([ servers[1] ])
345 for (const id of videoIdsServer1) {
346 await getVideo(servers[1].url, id, 403)
350 after(async function () {
351 await closeAllSequelize([ servers[1] ])
353 await cleanupTests(servers)