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,
17 setAccessTokensToServers,
23 } from '@shared/extra-utils'
24 import { JobState, Video, VideoPrivacy } from '@shared/models'
26 const expect = chai.expect
28 describe('Test handle downs', function () {
29 let servers: ServerInfo[] = []
30 let threadIdServer1: number
31 let threadIdServer2: number
32 let commentIdServer1: number
33 let commentIdServer2: number
34 let missedVideo1: Video
35 let missedVideo2: Video
36 let unlistedVideo: Video
38 const videoIdsServer1: string[] = []
40 const videoAttributes = {
41 name: 'my super name for server 1',
46 privacy: VideoPrivacy.PUBLIC,
47 description: 'my super description for server 1',
48 support: 'my super support text for server 1',
49 tags: [ 'tag1p1', 'tag2p1' ],
50 fixture: 'video_short1.webm'
53 const unlistedVideoAttributes = immutableAssign(videoAttributes, {
54 privacy: VideoPrivacy.UNLISTED
57 let checkAttributes: any
58 let unlistedCheckAttributes: any
60 let commentCommands: CommentsCommand[]
62 before(async function () {
65 servers = await flushAndRunMultipleServers(3)
66 commentCommands = servers.map(s => s.commentsCommand)
69 name: 'my super name for server 1',
74 description: 'my super description for server 1',
75 support: 'my super support text for server 1',
78 host: 'localhost:' + servers[0].port
82 tags: [ 'tag1p1', 'tag2p1' ],
83 privacy: VideoPrivacy.PUBLIC,
84 commentsEnabled: true,
85 downloadEnabled: true,
88 displayName: 'Main root channel',
92 fixture: 'video_short1.webm',
100 unlistedCheckAttributes = immutableAssign(checkAttributes, {
101 privacy: VideoPrivacy.UNLISTED
104 // Get the access tokens
105 await setAccessTokensToServers(servers)
108 it('Should remove followers that are often down', async function () {
111 // Server 2 and 3 follow server 1
112 await servers[1].followsCommand.follow({ targets: [ servers[0].url ] })
113 await servers[2].followsCommand.follow({ targets: [ servers[0].url ] })
115 await waitJobs(servers)
117 // Upload a video to server 1
118 await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
120 await waitJobs(servers)
122 // And check all servers have this video
123 for (const server of servers) {
124 const res = await getVideosList(server.url)
125 expect(res.body.data).to.be.an('array')
126 expect(res.body.data).to.have.lengthOf(1)
130 await killallServers([ servers[1] ])
132 // Remove server 2 follower
133 for (let i = 0; i < 10; i++) {
134 await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
137 await waitJobs([ servers[0], servers[2] ])
140 await killallServers([ servers[2] ])
142 const resLastVideo1 = await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
143 missedVideo1 = resLastVideo1.body.video
145 const resLastVideo2 = await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
146 missedVideo2 = resLastVideo2.body.video
149 const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, unlistedVideoAttributes)
150 unlistedVideo = resVideo.body.video
152 // Add comments to video 2
154 const text = 'thread 1'
155 let comment = await commentCommands[0].createThread({ videoId: missedVideo2.uuid, text })
156 threadIdServer1 = comment.id
158 comment = await commentCommands[0].addReply({ videoId: missedVideo2.uuid, toCommentId: comment.id, text: 'comment 1-1' })
160 const created = await commentCommands[0].addReply({ videoId: missedVideo2.uuid, toCommentId: comment.id, text: 'comment 1-2' })
161 commentIdServer1 = created.id
164 await waitJobs(servers[0])
168 // Only server 3 is still a follower of server 1
169 const body = await servers[0].followsCommand.getFollowers({ start: 0, count: 2, sort: 'createdAt' })
170 expect(body.data).to.be.an('array')
171 expect(body.data).to.have.lengthOf(1)
172 expect(body.data[0].follower.host).to.equal('localhost:' + servers[2].port)
175 it('Should not have pending/processing jobs anymore', async function () {
176 const states: JobState[] = [ 'waiting', 'active' ]
178 for (const state of states) {
179 const body = await servers[0].jobsCommand.getJobsList({
185 expect(body.data).to.have.length(0)
189 it('Should re-follow server 1', async function () {
192 await reRunServer(servers[1])
193 await reRunServer(servers[2])
195 await servers[1].followsCommand.unfollow({ target: servers[0] })
196 await waitJobs(servers)
198 await servers[1].followsCommand.follow({ targets: [ servers[0].url ] })
200 await waitJobs(servers)
202 const body = await servers[0].followsCommand.getFollowers({ start: 0, count: 2, sort: 'createdAt' })
203 expect(body.data).to.be.an('array')
204 expect(body.data).to.have.lengthOf(2)
207 it('Should send an update to server 3, and automatically fetch the video', async function () {
210 const res1 = await getVideosList(servers[2].url)
211 expect(res1.body.data).to.be.an('array')
212 expect(res1.body.data).to.have.lengthOf(11)
214 await updateVideo(servers[0].url, servers[0].accessToken, missedVideo1.uuid, {})
215 await updateVideo(servers[0].url, servers[0].accessToken, unlistedVideo.uuid, {})
217 await waitJobs(servers)
219 const res = await getVideosList(servers[2].url)
220 expect(res.body.data).to.be.an('array')
221 // 1 video is unlisted
222 expect(res.body.data).to.have.lengthOf(12)
224 // Check unlisted video
225 const resVideo = await getVideo(servers[2].url, unlistedVideo.uuid)
226 expect(resVideo.body).not.to.be.undefined
228 await completeVideoCheck(servers[2].url, resVideo.body, unlistedCheckAttributes)
231 it('Should send comments on a video to server 3, and automatically fetch the video', async function () {
234 await commentCommands[0].addReply({ videoId: missedVideo2.uuid, toCommentId: commentIdServer1, text: 'comment 1-3' })
236 await waitJobs(servers)
238 const resVideo = await getVideo(servers[2].url, missedVideo2.uuid)
239 expect(resVideo.body).not.to.be.undefined
242 const { data } = await servers[2].commentsCommand.listThreads({ videoId: missedVideo2.uuid })
243 expect(data).to.be.an('array')
244 expect(data).to.have.lengthOf(1)
246 threadIdServer2 = data[0].id
248 const tree = await servers[2].commentsCommand.getThread({ videoId: missedVideo2.uuid, threadId: threadIdServer2 })
249 expect(tree.comment.text).equal('thread 1')
250 expect(tree.children).to.have.lengthOf(1)
252 const firstChild = tree.children[0]
253 expect(firstChild.comment.text).to.equal('comment 1-1')
254 expect(firstChild.children).to.have.lengthOf(1)
256 const childOfFirstChild = firstChild.children[0]
257 expect(childOfFirstChild.comment.text).to.equal('comment 1-2')
258 expect(childOfFirstChild.children).to.have.lengthOf(1)
260 const childOfChildFirstChild = childOfFirstChild.children[0]
261 expect(childOfChildFirstChild.comment.text).to.equal('comment 1-3')
262 expect(childOfChildFirstChild.children).to.have.lengthOf(0)
264 commentIdServer2 = childOfChildFirstChild.comment.id
268 it('Should correctly reply to the comment', async function () {
271 await servers[2].commentsCommand.addReply({ videoId: missedVideo2.uuid, toCommentId: commentIdServer2, text: 'comment 1-4' })
273 await waitJobs(servers)
275 const tree = await commentCommands[0].getThread({ videoId: missedVideo2.uuid, threadId: threadIdServer1 })
277 expect(tree.comment.text).equal('thread 1')
278 expect(tree.children).to.have.lengthOf(1)
280 const firstChild = tree.children[0]
281 expect(firstChild.comment.text).to.equal('comment 1-1')
282 expect(firstChild.children).to.have.lengthOf(1)
284 const childOfFirstChild = firstChild.children[0]
285 expect(childOfFirstChild.comment.text).to.equal('comment 1-2')
286 expect(childOfFirstChild.children).to.have.lengthOf(1)
288 const childOfChildFirstChild = childOfFirstChild.children[0]
289 expect(childOfChildFirstChild.comment.text).to.equal('comment 1-3')
290 expect(childOfChildFirstChild.children).to.have.lengthOf(1)
292 const childOfChildOfChildOfFirstChild = childOfChildFirstChild.children[0]
293 expect(childOfChildOfChildOfFirstChild.comment.text).to.equal('comment 1-4')
294 expect(childOfChildOfChildOfFirstChild.children).to.have.lengthOf(0)
297 it('Should upload many videos on server 1', async function () {
300 for (let i = 0; i < 10; i++) {
301 const uuid = (await uploadVideoAndGetId({ server: servers[0], videoName: 'video ' + i })).uuid
302 videoIdsServer1.push(uuid)
305 await waitJobs(servers)
307 for (const id of videoIdsServer1) {
308 await getVideo(servers[1].url, id)
311 await waitJobs(servers)
312 await servers[1].sqlCommand.setActorFollowScores(20)
314 // Wait video expiration
317 // Refresh video -> score + 10 = 30
318 await getVideo(servers[1].url, videoIdsServer1[0])
320 await waitJobs(servers)
323 it('Should remove followings that are down', async function () {
326 await killallServers([ servers[0] ])
328 // Wait video expiration
331 for (let i = 0; i < 5; i++) {
333 await getVideo(servers[1].url, videoIdsServer1[i])
334 await waitJobs([ servers[1] ])
339 for (const id of videoIdsServer1) {
340 await getVideo(servers[1].url, id, HttpStatusCode.FORBIDDEN_403)
344 after(async function () {
345 await cleanupTests(servers)