1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
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'
13 flushAndRunMultipleServers,
20 setAccessTokensToServers,
27 } from '../../../../shared/extra-utils'
28 import { follow, getFollowersListPaginationAndSort } from '../../../../shared/extra-utils/server/follows'
29 import { getJobsListPaginationAndSort, waitJobs } from '../../../../shared/extra-utils/server/jobs'
32 addVideoCommentThread,
33 getVideoCommentThreads,
34 getVideoThreadComments
35 } from '../../../../shared/extra-utils/videos/video-comments'
36 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
38 const expect = chai.expect
40 describe('Test handle downs', function () {
41 let servers: ServerInfo[] = []
42 let threadIdServer1: number
43 let threadIdServer2: number
44 let commentIdServer1: number
45 let commentIdServer2: number
46 let missedVideo1: Video
47 let missedVideo2: Video
48 let unlistedVideo: Video
50 const videoIdsServer1: number[] = []
52 const videoAttributes = {
53 name: 'my super name for server 1',
58 privacy: VideoPrivacy.PUBLIC,
59 description: 'my super description for server 1',
60 support: 'my super support text for server 1',
61 tags: [ 'tag1p1', 'tag2p1' ],
62 fixture: 'video_short1.webm'
65 const unlistedVideoAttributes = immutableAssign(videoAttributes, {
66 privacy: VideoPrivacy.UNLISTED
69 let checkAttributes: any
70 let unlistedCheckAttributes: any
72 before(async function () {
75 servers = await flushAndRunMultipleServers(3)
78 name: 'my super name for server 1',
83 description: 'my super description for server 1',
84 support: 'my super support text for server 1',
87 host: 'localhost:' + servers[0].port
91 tags: [ 'tag1p1', 'tag2p1' ],
92 privacy: VideoPrivacy.PUBLIC,
93 commentsEnabled: true,
94 downloadEnabled: true,
97 displayName: 'Main root channel',
101 fixture: 'video_short1.webm',
109 unlistedCheckAttributes = immutableAssign(checkAttributes, {
110 privacy: VideoPrivacy.UNLISTED
113 // Get the access tokens
114 await setAccessTokensToServers(servers)
117 it('Should remove followers that are often down', async function () {
120 // Server 2 and 3 follow server 1
121 await follow(servers[1].url, [ servers[0].url ], servers[1].accessToken)
122 await follow(servers[2].url, [ servers[0].url ], servers[2].accessToken)
124 await waitJobs(servers)
126 // Upload a video to server 1
127 await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
129 await waitJobs(servers)
131 // And check all servers have this video
132 for (const server of servers) {
133 const res = await getVideosList(server.url)
134 expect(res.body.data).to.be.an('array')
135 expect(res.body.data).to.have.lengthOf(1)
139 killallServers([ servers[1] ])
141 // Remove server 2 follower
142 for (let i = 0; i < 10; i++) {
143 await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
146 await waitJobs(servers[0])
149 killallServers([ servers[2] ])
151 const resLastVideo1 = await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
152 missedVideo1 = resLastVideo1.body.video
154 const resLastVideo2 = await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
155 missedVideo2 = resLastVideo2.body.video
158 const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, unlistedVideoAttributes)
159 unlistedVideo = resVideo.body.video
161 // Add comments to video 2
163 const text = 'thread 1'
164 let resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, missedVideo2.uuid, text)
165 let comment = resComment.body.comment
166 threadIdServer1 = comment.id
168 resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, missedVideo2.uuid, comment.id, 'comment 1-1')
169 comment = resComment.body.comment
171 resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, missedVideo2.uuid, comment.id, 'comment 1-2')
172 commentIdServer1 = resComment.body.comment.id
175 await waitJobs(servers[0])
179 // Only server 3 is still a follower of server 1
180 const res = await getFollowersListPaginationAndSort({ url: servers[0].url, start: 0, count: 2, sort: 'createdAt' })
181 expect(res.body.data).to.be.an('array')
182 expect(res.body.data).to.have.lengthOf(1)
183 expect(res.body.data[0].follower.host).to.equal('localhost:' + servers[2].port)
186 it('Should not have pending/processing jobs anymore', async function () {
187 const states: JobState[] = [ 'waiting', 'active' ]
189 for (const state of states) {
190 const res = await getJobsListPaginationAndSort({
192 accessToken: servers[0].accessToken,
198 expect(res.body.data).to.have.length(0)
202 it('Should re-follow server 1', async function () {
205 await reRunServer(servers[1])
206 await reRunServer(servers[2])
208 await unfollow(servers[1].url, servers[1].accessToken, servers[0])
209 await waitJobs(servers)
211 await follow(servers[1].url, [ servers[0].url ], servers[1].accessToken)
213 await waitJobs(servers)
215 const res = await getFollowersListPaginationAndSort({ url: servers[0].url, start: 0, count: 2, sort: 'createdAt' })
216 expect(res.body.data).to.be.an('array')
217 expect(res.body.data).to.have.lengthOf(2)
220 it('Should send an update to server 3, and automatically fetch the video', async function () {
223 const res1 = await getVideosList(servers[2].url)
224 expect(res1.body.data).to.be.an('array')
225 expect(res1.body.data).to.have.lengthOf(11)
227 await updateVideo(servers[0].url, servers[0].accessToken, missedVideo1.uuid, {})
228 await updateVideo(servers[0].url, servers[0].accessToken, unlistedVideo.uuid, {})
230 await waitJobs(servers)
232 const res = await getVideosList(servers[2].url)
233 expect(res.body.data).to.be.an('array')
234 // 1 video is unlisted
235 expect(res.body.data).to.have.lengthOf(12)
237 // Check unlisted video
238 const resVideo = await getVideo(servers[2].url, unlistedVideo.uuid)
239 expect(resVideo.body).not.to.be.undefined
241 await completeVideoCheck(servers[2].url, resVideo.body, unlistedCheckAttributes)
244 it('Should send comments on a video to server 3, and automatically fetch the video', async function () {
247 await addVideoCommentReply(servers[0].url, servers[0].accessToken, missedVideo2.uuid, commentIdServer1, 'comment 1-3')
249 await waitJobs(servers)
251 const resVideo = await getVideo(servers[2].url, missedVideo2.uuid)
252 expect(resVideo.body).not.to.be.undefined
255 let resComment = await getVideoCommentThreads(servers[2].url, missedVideo2.uuid, 0, 5)
256 expect(resComment.body.data).to.be.an('array')
257 expect(resComment.body.data).to.have.lengthOf(1)
259 threadIdServer2 = resComment.body.data[0].id
261 resComment = await getVideoThreadComments(servers[2].url, missedVideo2.uuid, threadIdServer2)
263 const tree: VideoCommentThreadTree = resComment.body
264 expect(tree.comment.text).equal('thread 1')
265 expect(tree.children).to.have.lengthOf(1)
267 const firstChild = tree.children[0]
268 expect(firstChild.comment.text).to.equal('comment 1-1')
269 expect(firstChild.children).to.have.lengthOf(1)
271 const childOfFirstChild = firstChild.children[0]
272 expect(childOfFirstChild.comment.text).to.equal('comment 1-2')
273 expect(childOfFirstChild.children).to.have.lengthOf(1)
275 const childOfChildFirstChild = childOfFirstChild.children[0]
276 expect(childOfChildFirstChild.comment.text).to.equal('comment 1-3')
277 expect(childOfChildFirstChild.children).to.have.lengthOf(0)
279 commentIdServer2 = childOfChildFirstChild.comment.id
283 it('Should correctly reply to the comment', async function () {
286 await addVideoCommentReply(servers[2].url, servers[2].accessToken, missedVideo2.uuid, commentIdServer2, 'comment 1-4')
288 await waitJobs(servers)
291 const resComment = await getVideoThreadComments(servers[0].url, missedVideo2.uuid, threadIdServer1)
293 const tree: VideoCommentThreadTree = resComment.body
294 expect(tree.comment.text).equal('thread 1')
295 expect(tree.children).to.have.lengthOf(1)
297 const firstChild = tree.children[0]
298 expect(firstChild.comment.text).to.equal('comment 1-1')
299 expect(firstChild.children).to.have.lengthOf(1)
301 const childOfFirstChild = firstChild.children[0]
302 expect(childOfFirstChild.comment.text).to.equal('comment 1-2')
303 expect(childOfFirstChild.children).to.have.lengthOf(1)
305 const childOfChildFirstChild = childOfFirstChild.children[0]
306 expect(childOfChildFirstChild.comment.text).to.equal('comment 1-3')
307 expect(childOfChildFirstChild.children).to.have.lengthOf(1)
309 const childOfChildOfChildOfFirstChild = childOfChildFirstChild.children[0]
310 expect(childOfChildOfChildOfFirstChild.comment.text).to.equal('comment 1-4')
311 expect(childOfChildOfChildOfFirstChild.children).to.have.lengthOf(0)
315 it('Should upload many videos on server 1', async function () {
318 for (let i = 0; i < 10; i++) {
319 const uuid = (await uploadVideoAndGetId({ server: servers[0], videoName: 'video ' + i })).uuid
320 videoIdsServer1.push(uuid)
323 await waitJobs(servers)
325 for (const id of videoIdsServer1) {
326 await getVideo(servers[1].url, id)
329 await waitJobs(servers)
330 await setActorFollowScores(servers[1].internalServerNumber, 20)
332 // Wait video expiration
335 // Refresh video -> score + 10 = 30
336 await getVideo(servers[1].url, videoIdsServer1[0])
338 await waitJobs(servers)
341 it('Should remove followings that are down', async function () {
344 killallServers([ servers[0] ])
346 // Wait video expiration
349 for (let i = 0; i < 3; i++) {
350 await getVideo(servers[1].url, videoIdsServer1[i])
351 await waitJobs([ servers[1] ])
355 for (const id of videoIdsServer1) {
356 await getVideo(servers[1].url, id, HttpStatusCode.FORBIDDEN_403)
360 after(async function () {
361 await closeAllSequelize([ servers[1] ])
363 await cleanupTests(servers)