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,
13 getJobsListPaginationAndSort,
15 getVideoCommentThreads,
17 getVideoThreadComments,
22 setAccessTokensToServers,
29 } from '@shared/extra-utils'
30 import { JobState, Video, VideoCommentThreadTree, VideoPrivacy } from '@shared/models'
32 const expect = chai.expect
34 describe('Test handle downs', function () {
35 let servers: ServerInfo[] = []
36 let threadIdServer1: number
37 let threadIdServer2: number
38 let commentIdServer1: number
39 let commentIdServer2: number
40 let missedVideo1: Video
41 let missedVideo2: Video
42 let unlistedVideo: Video
44 const videoIdsServer1: string[] = []
46 const videoAttributes = {
47 name: 'my super name for server 1',
52 privacy: VideoPrivacy.PUBLIC,
53 description: 'my super description for server 1',
54 support: 'my super support text for server 1',
55 tags: [ 'tag1p1', 'tag2p1' ],
56 fixture: 'video_short1.webm'
59 const unlistedVideoAttributes = immutableAssign(videoAttributes, {
60 privacy: VideoPrivacy.UNLISTED
63 let checkAttributes: any
64 let unlistedCheckAttributes: any
66 before(async function () {
69 servers = await flushAndRunMultipleServers(3)
72 name: 'my super name for server 1',
77 description: 'my super description for server 1',
78 support: 'my super support text for server 1',
81 host: 'localhost:' + servers[0].port
85 tags: [ 'tag1p1', 'tag2p1' ],
86 privacy: VideoPrivacy.PUBLIC,
87 commentsEnabled: true,
88 downloadEnabled: true,
91 displayName: 'Main root channel',
95 fixture: 'video_short1.webm',
103 unlistedCheckAttributes = immutableAssign(checkAttributes, {
104 privacy: VideoPrivacy.UNLISTED
107 // Get the access tokens
108 await setAccessTokensToServers(servers)
111 it('Should remove followers that are often down', async function () {
114 // Server 2 and 3 follow server 1
115 await servers[1].followsCommand.follow({ targets: [ servers[0].url ] })
116 await servers[2].followsCommand.follow({ targets: [ servers[0].url ] })
118 await waitJobs(servers)
120 // Upload a video to server 1
121 await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
123 await waitJobs(servers)
125 // And check all servers have this video
126 for (const server of servers) {
127 const res = await getVideosList(server.url)
128 expect(res.body.data).to.be.an('array')
129 expect(res.body.data).to.have.lengthOf(1)
133 killallServers([ servers[1] ])
135 // Remove server 2 follower
136 for (let i = 0; i < 10; i++) {
137 await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
140 await waitJobs([ servers[0], servers[2] ])
143 killallServers([ servers[2] ])
145 const resLastVideo1 = await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
146 missedVideo1 = resLastVideo1.body.video
148 const resLastVideo2 = await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
149 missedVideo2 = resLastVideo2.body.video
152 const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, unlistedVideoAttributes)
153 unlistedVideo = resVideo.body.video
155 // Add comments to video 2
157 const text = 'thread 1'
158 let resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, missedVideo2.uuid, text)
159 let comment = resComment.body.comment
160 threadIdServer1 = comment.id
162 resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, missedVideo2.uuid, comment.id, 'comment 1-1')
163 comment = resComment.body.comment
165 resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, missedVideo2.uuid, comment.id, 'comment 1-2')
166 commentIdServer1 = resComment.body.comment.id
169 await waitJobs(servers[0])
173 // Only server 3 is still a follower of server 1
174 const body = await servers[0].followsCommand.getFollowers({ start: 0, count: 2, sort: 'createdAt' })
175 expect(body.data).to.be.an('array')
176 expect(body.data).to.have.lengthOf(1)
177 expect(body.data[0].follower.host).to.equal('localhost:' + servers[2].port)
180 it('Should not have pending/processing jobs anymore', async function () {
181 const states: JobState[] = [ 'waiting', 'active' ]
183 for (const state of states) {
184 const res = await getJobsListPaginationAndSort({
186 accessToken: servers[0].accessToken,
192 expect(res.body.data).to.have.length(0)
196 it('Should re-follow server 1', async function () {
199 await reRunServer(servers[1])
200 await reRunServer(servers[2])
202 await servers[1].followsCommand.unfollow({ target: servers[0] })
203 await waitJobs(servers)
205 await servers[1].followsCommand.follow({ targets: [ servers[0].url ] })
207 await waitJobs(servers)
209 const body = await servers[0].followsCommand.getFollowers({ start: 0, count: 2, sort: 'createdAt' })
210 expect(body.data).to.be.an('array')
211 expect(body.data).to.have.lengthOf(2)
214 it('Should send an update to server 3, and automatically fetch the video', async function () {
217 const res1 = await getVideosList(servers[2].url)
218 expect(res1.body.data).to.be.an('array')
219 expect(res1.body.data).to.have.lengthOf(11)
221 await updateVideo(servers[0].url, servers[0].accessToken, missedVideo1.uuid, {})
222 await updateVideo(servers[0].url, servers[0].accessToken, unlistedVideo.uuid, {})
224 await waitJobs(servers)
226 const res = await getVideosList(servers[2].url)
227 expect(res.body.data).to.be.an('array')
228 // 1 video is unlisted
229 expect(res.body.data).to.have.lengthOf(12)
231 // Check unlisted video
232 const resVideo = await getVideo(servers[2].url, unlistedVideo.uuid)
233 expect(resVideo.body).not.to.be.undefined
235 await completeVideoCheck(servers[2].url, resVideo.body, unlistedCheckAttributes)
238 it('Should send comments on a video to server 3, and automatically fetch the video', async function () {
241 await addVideoCommentReply(servers[0].url, servers[0].accessToken, missedVideo2.uuid, commentIdServer1, 'comment 1-3')
243 await waitJobs(servers)
245 const resVideo = await getVideo(servers[2].url, missedVideo2.uuid)
246 expect(resVideo.body).not.to.be.undefined
249 let resComment = await getVideoCommentThreads(servers[2].url, missedVideo2.uuid, 0, 5)
250 expect(resComment.body.data).to.be.an('array')
251 expect(resComment.body.data).to.have.lengthOf(1)
253 threadIdServer2 = resComment.body.data[0].id
255 resComment = await getVideoThreadComments(servers[2].url, missedVideo2.uuid, threadIdServer2)
257 const tree: VideoCommentThreadTree = resComment.body
258 expect(tree.comment.text).equal('thread 1')
259 expect(tree.children).to.have.lengthOf(1)
261 const firstChild = tree.children[0]
262 expect(firstChild.comment.text).to.equal('comment 1-1')
263 expect(firstChild.children).to.have.lengthOf(1)
265 const childOfFirstChild = firstChild.children[0]
266 expect(childOfFirstChild.comment.text).to.equal('comment 1-2')
267 expect(childOfFirstChild.children).to.have.lengthOf(1)
269 const childOfChildFirstChild = childOfFirstChild.children[0]
270 expect(childOfChildFirstChild.comment.text).to.equal('comment 1-3')
271 expect(childOfChildFirstChild.children).to.have.lengthOf(0)
273 commentIdServer2 = childOfChildFirstChild.comment.id
277 it('Should correctly reply to the comment', async function () {
280 await addVideoCommentReply(servers[2].url, servers[2].accessToken, missedVideo2.uuid, commentIdServer2, 'comment 1-4')
282 await waitJobs(servers)
285 const resComment = await getVideoThreadComments(servers[0].url, missedVideo2.uuid, threadIdServer1)
287 const tree: VideoCommentThreadTree = resComment.body
288 expect(tree.comment.text).equal('thread 1')
289 expect(tree.children).to.have.lengthOf(1)
291 const firstChild = tree.children[0]
292 expect(firstChild.comment.text).to.equal('comment 1-1')
293 expect(firstChild.children).to.have.lengthOf(1)
295 const childOfFirstChild = firstChild.children[0]
296 expect(childOfFirstChild.comment.text).to.equal('comment 1-2')
297 expect(childOfFirstChild.children).to.have.lengthOf(1)
299 const childOfChildFirstChild = childOfFirstChild.children[0]
300 expect(childOfChildFirstChild.comment.text).to.equal('comment 1-3')
301 expect(childOfChildFirstChild.children).to.have.lengthOf(1)
303 const childOfChildOfChildOfFirstChild = childOfChildFirstChild.children[0]
304 expect(childOfChildOfChildOfFirstChild.comment.text).to.equal('comment 1-4')
305 expect(childOfChildOfChildOfFirstChild.children).to.have.lengthOf(0)
309 it('Should upload many videos on server 1', async function () {
312 for (let i = 0; i < 10; i++) {
313 const uuid = (await uploadVideoAndGetId({ server: servers[0], videoName: 'video ' + i })).uuid
314 videoIdsServer1.push(uuid)
317 await waitJobs(servers)
319 for (const id of videoIdsServer1) {
320 await getVideo(servers[1].url, id)
323 await waitJobs(servers)
324 await setActorFollowScores(servers[1].internalServerNumber, 20)
326 // Wait video expiration
329 // Refresh video -> score + 10 = 30
330 await getVideo(servers[1].url, videoIdsServer1[0])
332 await waitJobs(servers)
335 it('Should remove followings that are down', async function () {
338 killallServers([ servers[0] ])
340 // Wait video expiration
343 for (let i = 0; i < 5; i++) {
345 await getVideo(servers[1].url, videoIdsServer1[i])
346 await waitJobs([ servers[1] ])
351 for (const id of videoIdsServer1) {
352 await getVideo(servers[1].url, id, HttpStatusCode.FORBIDDEN_403)
356 after(async function () {
357 await closeAllSequelize([ servers[1] ])
359 await cleanupTests(servers)