1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { HttpStatusCode } from '@shared/core-utils'
12 flushAndRunMultipleServers,
14 getVideoCommentThreads,
16 getVideoThreadComments,
21 setAccessTokensToServers,
28 } from '@shared/extra-utils'
29 import { JobState, Video, VideoCommentThreadTree, VideoPrivacy } from '@shared/models'
31 const expect = chai.expect
33 describe('Test handle downs', function () {
34 let servers: ServerInfo[] = []
35 let threadIdServer1: number
36 let threadIdServer2: number
37 let commentIdServer1: number
38 let commentIdServer2: number
39 let missedVideo1: Video
40 let missedVideo2: Video
41 let unlistedVideo: Video
43 const videoIdsServer1: string[] = []
45 const videoAttributes = {
46 name: 'my super name for server 1',
51 privacy: VideoPrivacy.PUBLIC,
52 description: 'my super description for server 1',
53 support: 'my super support text for server 1',
54 tags: [ 'tag1p1', 'tag2p1' ],
55 fixture: 'video_short1.webm'
58 const unlistedVideoAttributes = immutableAssign(videoAttributes, {
59 privacy: VideoPrivacy.UNLISTED
62 let checkAttributes: any
63 let unlistedCheckAttributes: any
65 before(async function () {
68 servers = await flushAndRunMultipleServers(3)
71 name: 'my super name for server 1',
76 description: 'my super description for server 1',
77 support: 'my super support text for server 1',
80 host: 'localhost:' + servers[0].port
84 tags: [ 'tag1p1', 'tag2p1' ],
85 privacy: VideoPrivacy.PUBLIC,
86 commentsEnabled: true,
87 downloadEnabled: true,
90 displayName: 'Main root channel',
94 fixture: 'video_short1.webm',
102 unlistedCheckAttributes = immutableAssign(checkAttributes, {
103 privacy: VideoPrivacy.UNLISTED
106 // Get the access tokens
107 await setAccessTokensToServers(servers)
110 it('Should remove followers that are often down', async function () {
113 // Server 2 and 3 follow server 1
114 await servers[1].followsCommand.follow({ targets: [ servers[0].url ] })
115 await servers[2].followsCommand.follow({ targets: [ servers[0].url ] })
117 await waitJobs(servers)
119 // Upload a video to server 1
120 await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
122 await waitJobs(servers)
124 // And check all servers have this video
125 for (const server of servers) {
126 const res = await getVideosList(server.url)
127 expect(res.body.data).to.be.an('array')
128 expect(res.body.data).to.have.lengthOf(1)
132 killallServers([ servers[1] ])
134 // Remove server 2 follower
135 for (let i = 0; i < 10; i++) {
136 await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
139 await waitJobs([ servers[0], servers[2] ])
142 killallServers([ servers[2] ])
144 const resLastVideo1 = await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
145 missedVideo1 = resLastVideo1.body.video
147 const resLastVideo2 = await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
148 missedVideo2 = resLastVideo2.body.video
151 const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, unlistedVideoAttributes)
152 unlistedVideo = resVideo.body.video
154 // Add comments to video 2
156 const text = 'thread 1'
157 let resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, missedVideo2.uuid, text)
158 let comment = resComment.body.comment
159 threadIdServer1 = comment.id
161 resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, missedVideo2.uuid, comment.id, 'comment 1-1')
162 comment = resComment.body.comment
164 resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, missedVideo2.uuid, comment.id, 'comment 1-2')
165 commentIdServer1 = resComment.body.comment.id
168 await waitJobs(servers[0])
172 // Only server 3 is still a follower of server 1
173 const body = await servers[0].followsCommand.getFollowers({ start: 0, count: 2, sort: 'createdAt' })
174 expect(body.data).to.be.an('array')
175 expect(body.data).to.have.lengthOf(1)
176 expect(body.data[0].follower.host).to.equal('localhost:' + servers[2].port)
179 it('Should not have pending/processing jobs anymore', async function () {
180 const states: JobState[] = [ 'waiting', 'active' ]
182 for (const state of states) {
183 const body = await servers[0].jobsCommand.getJobsList({
189 expect(body.data).to.have.length(0)
193 it('Should re-follow server 1', async function () {
196 await reRunServer(servers[1])
197 await reRunServer(servers[2])
199 await servers[1].followsCommand.unfollow({ target: servers[0] })
200 await waitJobs(servers)
202 await servers[1].followsCommand.follow({ targets: [ servers[0].url ] })
204 await waitJobs(servers)
206 const body = await servers[0].followsCommand.getFollowers({ start: 0, count: 2, sort: 'createdAt' })
207 expect(body.data).to.be.an('array')
208 expect(body.data).to.have.lengthOf(2)
211 it('Should send an update to server 3, and automatically fetch the video', async function () {
214 const res1 = await getVideosList(servers[2].url)
215 expect(res1.body.data).to.be.an('array')
216 expect(res1.body.data).to.have.lengthOf(11)
218 await updateVideo(servers[0].url, servers[0].accessToken, missedVideo1.uuid, {})
219 await updateVideo(servers[0].url, servers[0].accessToken, unlistedVideo.uuid, {})
221 await waitJobs(servers)
223 const res = await getVideosList(servers[2].url)
224 expect(res.body.data).to.be.an('array')
225 // 1 video is unlisted
226 expect(res.body.data).to.have.lengthOf(12)
228 // Check unlisted video
229 const resVideo = await getVideo(servers[2].url, unlistedVideo.uuid)
230 expect(resVideo.body).not.to.be.undefined
232 await completeVideoCheck(servers[2].url, resVideo.body, unlistedCheckAttributes)
235 it('Should send comments on a video to server 3, and automatically fetch the video', async function () {
238 await addVideoCommentReply(servers[0].url, servers[0].accessToken, missedVideo2.uuid, commentIdServer1, 'comment 1-3')
240 await waitJobs(servers)
242 const resVideo = await getVideo(servers[2].url, missedVideo2.uuid)
243 expect(resVideo.body).not.to.be.undefined
246 let resComment = await getVideoCommentThreads(servers[2].url, missedVideo2.uuid, 0, 5)
247 expect(resComment.body.data).to.be.an('array')
248 expect(resComment.body.data).to.have.lengthOf(1)
250 threadIdServer2 = resComment.body.data[0].id
252 resComment = await getVideoThreadComments(servers[2].url, missedVideo2.uuid, threadIdServer2)
254 const tree: VideoCommentThreadTree = resComment.body
255 expect(tree.comment.text).equal('thread 1')
256 expect(tree.children).to.have.lengthOf(1)
258 const firstChild = tree.children[0]
259 expect(firstChild.comment.text).to.equal('comment 1-1')
260 expect(firstChild.children).to.have.lengthOf(1)
262 const childOfFirstChild = firstChild.children[0]
263 expect(childOfFirstChild.comment.text).to.equal('comment 1-2')
264 expect(childOfFirstChild.children).to.have.lengthOf(1)
266 const childOfChildFirstChild = childOfFirstChild.children[0]
267 expect(childOfChildFirstChild.comment.text).to.equal('comment 1-3')
268 expect(childOfChildFirstChild.children).to.have.lengthOf(0)
270 commentIdServer2 = childOfChildFirstChild.comment.id
274 it('Should correctly reply to the comment', async function () {
277 await addVideoCommentReply(servers[2].url, servers[2].accessToken, missedVideo2.uuid, commentIdServer2, 'comment 1-4')
279 await waitJobs(servers)
282 const resComment = await getVideoThreadComments(servers[0].url, missedVideo2.uuid, threadIdServer1)
284 const tree: VideoCommentThreadTree = resComment.body
285 expect(tree.comment.text).equal('thread 1')
286 expect(tree.children).to.have.lengthOf(1)
288 const firstChild = tree.children[0]
289 expect(firstChild.comment.text).to.equal('comment 1-1')
290 expect(firstChild.children).to.have.lengthOf(1)
292 const childOfFirstChild = firstChild.children[0]
293 expect(childOfFirstChild.comment.text).to.equal('comment 1-2')
294 expect(childOfFirstChild.children).to.have.lengthOf(1)
296 const childOfChildFirstChild = childOfFirstChild.children[0]
297 expect(childOfChildFirstChild.comment.text).to.equal('comment 1-3')
298 expect(childOfChildFirstChild.children).to.have.lengthOf(1)
300 const childOfChildOfChildOfFirstChild = childOfChildFirstChild.children[0]
301 expect(childOfChildOfChildOfFirstChild.comment.text).to.equal('comment 1-4')
302 expect(childOfChildOfChildOfFirstChild.children).to.have.lengthOf(0)
306 it('Should upload many videos on server 1', async function () {
309 for (let i = 0; i < 10; i++) {
310 const uuid = (await uploadVideoAndGetId({ server: servers[0], videoName: 'video ' + i })).uuid
311 videoIdsServer1.push(uuid)
314 await waitJobs(servers)
316 for (const id of videoIdsServer1) {
317 await getVideo(servers[1].url, id)
320 await waitJobs(servers)
321 await setActorFollowScores(servers[1].internalServerNumber, 20)
323 // Wait video expiration
326 // Refresh video -> score + 10 = 30
327 await getVideo(servers[1].url, videoIdsServer1[0])
329 await waitJobs(servers)
332 it('Should remove followings that are down', async function () {
335 killallServers([ servers[0] ])
337 // Wait video expiration
340 for (let i = 0; i < 5; i++) {
342 await getVideo(servers[1].url, videoIdsServer1[i])
343 await waitJobs([ servers[1] ])
348 for (const id of videoIdsServer1) {
349 await getVideo(servers[1].url, id, HttpStatusCode.FORBIDDEN_403)
353 after(async function () {
354 await closeAllSequelize([ servers[1] ])
356 await cleanupTests(servers)