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({ url: servers[ 0 ].url, start: 0, count: 2, sort: '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({
188 url: servers[ 0 ].url,
189 accessToken: servers[ 0 ].accessToken,
195 expect(res.body.data).to.have.length(0)
199 it('Should re-follow server 1', async function () {
202 await reRunServer(servers[1])
203 await reRunServer(servers[2])
205 await unfollow(servers[1].url, servers[1].accessToken, servers[0])
206 await waitJobs(servers)
208 await follow(servers[1].url, [ servers[0].url ], servers[1].accessToken)
210 await waitJobs(servers)
212 const res = await getFollowersListPaginationAndSort({ url: servers[ 0 ].url, start: 0, count: 2, sort: 'createdAt' })
213 expect(res.body.data).to.be.an('array')
214 expect(res.body.data).to.have.lengthOf(2)
217 it('Should send an update to server 3, and automatically fetch the video', async function () {
220 const res1 = await getVideosList(servers[2].url)
221 expect(res1.body.data).to.be.an('array')
222 expect(res1.body.data).to.have.lengthOf(11)
224 await updateVideo(servers[0].url, servers[0].accessToken, missedVideo1.uuid, { })
225 await updateVideo(servers[0].url, servers[0].accessToken, unlistedVideo.uuid, { })
227 await waitJobs(servers)
229 const res = await getVideosList(servers[2].url)
230 expect(res.body.data).to.be.an('array')
231 // 1 video is unlisted
232 expect(res.body.data).to.have.lengthOf(12)
234 // Check unlisted video
235 const resVideo = await getVideo(servers[2].url, unlistedVideo.uuid)
236 expect(resVideo.body).not.to.be.undefined
238 await completeVideoCheck(servers[2].url, resVideo.body, unlistedCheckAttributes)
241 it('Should send comments on a video to server 3, and automatically fetch the video', async function () {
244 await addVideoCommentReply(servers[0].url, servers[0].accessToken, missedVideo2.uuid, commentIdServer1, 'comment 1-3')
246 await waitJobs(servers)
248 const resVideo = await getVideo(servers[2].url, missedVideo2.uuid)
249 expect(resVideo.body).not.to.be.undefined
252 let resComment = await getVideoCommentThreads(servers[2].url, missedVideo2.uuid, 0, 5)
253 expect(resComment.body.data).to.be.an('array')
254 expect(resComment.body.data).to.have.lengthOf(1)
256 threadIdServer2 = resComment.body.data[0].id
258 resComment = await getVideoThreadComments(servers[2].url, missedVideo2.uuid, threadIdServer2)
260 const tree: VideoCommentThreadTree = resComment.body
261 expect(tree.comment.text).equal('thread 1')
262 expect(tree.children).to.have.lengthOf(1)
264 const firstChild = tree.children[0]
265 expect(firstChild.comment.text).to.equal('comment 1-1')
266 expect(firstChild.children).to.have.lengthOf(1)
268 const childOfFirstChild = firstChild.children[0]
269 expect(childOfFirstChild.comment.text).to.equal('comment 1-2')
270 expect(childOfFirstChild.children).to.have.lengthOf(1)
272 const childOfChildFirstChild = childOfFirstChild.children[0]
273 expect(childOfChildFirstChild.comment.text).to.equal('comment 1-3')
274 expect(childOfChildFirstChild.children).to.have.lengthOf(0)
276 commentIdServer2 = childOfChildFirstChild.comment.id
280 it('Should correctly reply to the comment', async function () {
283 await addVideoCommentReply(servers[2].url, servers[2].accessToken, missedVideo2.uuid, commentIdServer2, 'comment 1-4')
285 await waitJobs(servers)
288 const resComment = await getVideoThreadComments(servers[0].url, missedVideo2.uuid, threadIdServer1)
290 const tree: VideoCommentThreadTree = resComment.body
291 expect(tree.comment.text).equal('thread 1')
292 expect(tree.children).to.have.lengthOf(1)
294 const firstChild = tree.children[0]
295 expect(firstChild.comment.text).to.equal('comment 1-1')
296 expect(firstChild.children).to.have.lengthOf(1)
298 const childOfFirstChild = firstChild.children[0]
299 expect(childOfFirstChild.comment.text).to.equal('comment 1-2')
300 expect(childOfFirstChild.children).to.have.lengthOf(1)
302 const childOfChildFirstChild = childOfFirstChild.children[0]
303 expect(childOfChildFirstChild.comment.text).to.equal('comment 1-3')
304 expect(childOfChildFirstChild.children).to.have.lengthOf(1)
306 const childOfChildOfChildOfFirstChild = childOfChildFirstChild.children[0]
307 expect(childOfChildOfChildOfFirstChild.comment.text).to.equal('comment 1-4')
308 expect(childOfChildOfChildOfFirstChild.children).to.have.lengthOf(0)
312 it('Should upload many videos on server 1', async function () {
315 for (let i = 0; i < 10; i++) {
316 const uuid = (await uploadVideoAndGetId({ server: servers[ 0 ], videoName: 'video ' + i })).uuid
317 videoIdsServer1.push(uuid)
320 await waitJobs(servers)
322 for (const id of videoIdsServer1) {
323 await getVideo(servers[ 1 ].url, id)
326 await waitJobs(servers)
327 await setActorFollowScores(servers[1].internalServerNumber, 20)
329 // Wait video expiration
332 // Refresh video -> score + 10 = 30
333 await getVideo(servers[1].url, videoIdsServer1[0])
335 await waitJobs(servers)
338 it('Should remove followings that are down', async function () {
341 killallServers([ servers[0] ])
343 // Wait video expiration
346 for (let i = 0; i < 3; i++) {
347 await getVideo(servers[1].url, videoIdsServer1[i])
349 await waitJobs([ servers[1] ])
352 for (const id of videoIdsServer1) {
353 await getVideo(servers[1].url, id, 403)
357 after(async function () {
358 await closeAllSequelize([ servers[1] ])
360 await cleanupTests(servers)