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'
10 flushAndRunMultipleServers,
14 setAccessTokensToServers,
17 } from '@shared/extra-utils'
18 import { JobState, VideoCreateResult, VideoPrivacy } from '@shared/models'
20 const expect = chai.expect
22 describe('Test handle downs', function () {
23 let servers: ServerInfo[] = []
24 let threadIdServer1: number
25 let threadIdServer2: number
26 let commentIdServer1: number
27 let commentIdServer2: number
28 let missedVideo1: VideoCreateResult
29 let missedVideo2: VideoCreateResult
30 let unlistedVideo: VideoCreateResult
32 const videoIdsServer1: string[] = []
34 const videoAttributes = {
35 name: 'my super name for server 1',
40 privacy: VideoPrivacy.PUBLIC,
41 description: 'my super description for server 1',
42 support: 'my super support text for server 1',
43 tags: [ 'tag1p1', 'tag2p1' ],
44 fixture: 'video_short1.webm'
47 const unlistedVideoAttributes = { ...videoAttributes, privacy: VideoPrivacy.UNLISTED }
49 let checkAttributes: any
50 let unlistedCheckAttributes: any
52 let commentCommands: CommentsCommand[]
54 before(async function () {
57 servers = await flushAndRunMultipleServers(3)
58 commentCommands = servers.map(s => s.commentsCommand)
61 name: 'my super name for server 1',
66 description: 'my super description for server 1',
67 support: 'my super support text for server 1',
70 host: 'localhost:' + servers[0].port
74 tags: [ 'tag1p1', 'tag2p1' ],
75 privacy: VideoPrivacy.PUBLIC,
76 commentsEnabled: true,
77 downloadEnabled: true,
80 displayName: 'Main root channel',
84 fixture: 'video_short1.webm',
92 unlistedCheckAttributes = { ...checkAttributes, privacy: VideoPrivacy.UNLISTED }
94 // Get the access tokens
95 await setAccessTokensToServers(servers)
98 it('Should remove followers that are often down', async function () {
101 // Server 2 and 3 follow server 1
102 await servers[1].followsCommand.follow({ targets: [ servers[0].url ] })
103 await servers[2].followsCommand.follow({ targets: [ servers[0].url ] })
105 await waitJobs(servers)
107 // Upload a video to server 1
108 await servers[0].videosCommand.upload({ attributes: videoAttributes })
110 await waitJobs(servers)
112 // And check all servers have this video
113 for (const server of servers) {
114 const { data } = await server.videosCommand.list()
115 expect(data).to.be.an('array')
116 expect(data).to.have.lengthOf(1)
120 await killallServers([ servers[1] ])
122 // Remove server 2 follower
123 for (let i = 0; i < 10; i++) {
124 await servers[0].videosCommand.upload({ attributes: videoAttributes })
127 await waitJobs([ servers[0], servers[2] ])
130 await killallServers([ servers[2] ])
132 missedVideo1 = await servers[0].videosCommand.upload({ attributes: videoAttributes })
134 missedVideo2 = await servers[0].videosCommand.upload({ attributes: videoAttributes })
137 unlistedVideo = await servers[0].videosCommand.upload({ attributes: unlistedVideoAttributes })
139 // Add comments to video 2
141 const text = 'thread 1'
142 let comment = await commentCommands[0].createThread({ videoId: missedVideo2.uuid, text })
143 threadIdServer1 = comment.id
145 comment = await commentCommands[0].addReply({ videoId: missedVideo2.uuid, toCommentId: comment.id, text: 'comment 1-1' })
147 const created = await commentCommands[0].addReply({ videoId: missedVideo2.uuid, toCommentId: comment.id, text: 'comment 1-2' })
148 commentIdServer1 = created.id
151 await waitJobs(servers[0])
155 // Only server 3 is still a follower of server 1
156 const body = await servers[0].followsCommand.getFollowers({ start: 0, count: 2, sort: 'createdAt' })
157 expect(body.data).to.be.an('array')
158 expect(body.data).to.have.lengthOf(1)
159 expect(body.data[0].follower.host).to.equal('localhost:' + servers[2].port)
162 it('Should not have pending/processing jobs anymore', async function () {
163 const states: JobState[] = [ 'waiting', 'active' ]
165 for (const state of states) {
166 const body = await servers[0].jobsCommand.getJobsList({
172 expect(body.data).to.have.length(0)
176 it('Should re-follow server 1', async function () {
179 await reRunServer(servers[1])
180 await reRunServer(servers[2])
182 await servers[1].followsCommand.unfollow({ target: servers[0] })
183 await waitJobs(servers)
185 await servers[1].followsCommand.follow({ targets: [ servers[0].url ] })
187 await waitJobs(servers)
189 const body = await servers[0].followsCommand.getFollowers({ start: 0, count: 2, sort: 'createdAt' })
190 expect(body.data).to.be.an('array')
191 expect(body.data).to.have.lengthOf(2)
194 it('Should send an update to server 3, and automatically fetch the video', async function () {
198 const { data } = await servers[2].videosCommand.list()
199 expect(data).to.be.an('array')
200 expect(data).to.have.lengthOf(11)
203 await servers[0].videosCommand.update({ id: missedVideo1.uuid })
204 await servers[0].videosCommand.update({ id: unlistedVideo.uuid })
206 await waitJobs(servers)
209 const { data } = await servers[2].videosCommand.list()
210 expect(data).to.be.an('array')
211 // 1 video is unlisted
212 expect(data).to.have.lengthOf(12)
215 // Check unlisted video
216 const video = await servers[2].videosCommand.get({ id: unlistedVideo.uuid })
217 await completeVideoCheck(servers[2], video, unlistedCheckAttributes)
220 it('Should send comments on a video to server 3, and automatically fetch the video', async function () {
223 await commentCommands[0].addReply({ videoId: missedVideo2.uuid, toCommentId: commentIdServer1, text: 'comment 1-3' })
225 await waitJobs(servers)
227 await servers[2].videosCommand.get({ id: missedVideo2.uuid })
230 const { data } = await servers[2].commentsCommand.listThreads({ videoId: missedVideo2.uuid })
231 expect(data).to.be.an('array')
232 expect(data).to.have.lengthOf(1)
234 threadIdServer2 = data[0].id
236 const tree = await servers[2].commentsCommand.getThread({ videoId: missedVideo2.uuid, threadId: threadIdServer2 })
237 expect(tree.comment.text).equal('thread 1')
238 expect(tree.children).to.have.lengthOf(1)
240 const firstChild = tree.children[0]
241 expect(firstChild.comment.text).to.equal('comment 1-1')
242 expect(firstChild.children).to.have.lengthOf(1)
244 const childOfFirstChild = firstChild.children[0]
245 expect(childOfFirstChild.comment.text).to.equal('comment 1-2')
246 expect(childOfFirstChild.children).to.have.lengthOf(1)
248 const childOfChildFirstChild = childOfFirstChild.children[0]
249 expect(childOfChildFirstChild.comment.text).to.equal('comment 1-3')
250 expect(childOfChildFirstChild.children).to.have.lengthOf(0)
252 commentIdServer2 = childOfChildFirstChild.comment.id
256 it('Should correctly reply to the comment', async function () {
259 await servers[2].commentsCommand.addReply({ videoId: missedVideo2.uuid, toCommentId: commentIdServer2, text: 'comment 1-4' })
261 await waitJobs(servers)
263 const tree = await commentCommands[0].getThread({ videoId: missedVideo2.uuid, threadId: threadIdServer1 })
265 expect(tree.comment.text).equal('thread 1')
266 expect(tree.children).to.have.lengthOf(1)
268 const firstChild = tree.children[0]
269 expect(firstChild.comment.text).to.equal('comment 1-1')
270 expect(firstChild.children).to.have.lengthOf(1)
272 const childOfFirstChild = firstChild.children[0]
273 expect(childOfFirstChild.comment.text).to.equal('comment 1-2')
274 expect(childOfFirstChild.children).to.have.lengthOf(1)
276 const childOfChildFirstChild = childOfFirstChild.children[0]
277 expect(childOfChildFirstChild.comment.text).to.equal('comment 1-3')
278 expect(childOfChildFirstChild.children).to.have.lengthOf(1)
280 const childOfChildOfChildOfFirstChild = childOfChildFirstChild.children[0]
281 expect(childOfChildOfChildOfFirstChild.comment.text).to.equal('comment 1-4')
282 expect(childOfChildOfChildOfFirstChild.children).to.have.lengthOf(0)
285 it('Should upload many videos on server 1', async function () {
288 for (let i = 0; i < 10; i++) {
289 const uuid = (await servers[0].videosCommand.quickUpload({ name: 'video ' + i })).uuid
290 videoIdsServer1.push(uuid)
293 await waitJobs(servers)
295 for (const id of videoIdsServer1) {
296 await servers[1].videosCommand.get({ id })
299 await waitJobs(servers)
300 await servers[1].sqlCommand.setActorFollowScores(20)
302 // Wait video expiration
305 // Refresh video -> score + 10 = 30
306 await servers[1].videosCommand.get({ id: videoIdsServer1[0] })
308 await waitJobs(servers)
311 it('Should remove followings that are down', async function () {
314 await killallServers([ servers[0] ])
316 // Wait video expiration
319 for (let i = 0; i < 5; i++) {
321 await servers[1].videosCommand.get({ id: videoIdsServer1[i] })
322 await waitJobs([ servers[1] ])
327 for (const id of videoIdsServer1) {
328 await servers[1].videosCommand.get({ id, expectedStatus: HttpStatusCode.FORBIDDEN_403 })
332 after(async function () {
333 await cleanupTests(servers)