1 /* tslint:disable:no-unused-expression */
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'
8 import { completeVideoCheck, getVideo, immutableAssign, reRunServer, unfollow, viewVideo } from '../../utils'
10 flushAndRunMultipleServers,
14 setAccessTokensToServers,
17 } from '../../utils/index'
18 import { follow, getFollowersListPaginationAndSort } from '../../utils/server/follows'
19 import { getJobsListPaginationAndSort, waitJobs } from '../../utils/server/jobs'
22 addVideoCommentThread,
23 getVideoCommentThreads,
24 getVideoThreadComments
25 } from '../../utils/videos/video-comments'
27 const expect = chai.expect
29 describe('Test handle downs', function () {
30 let servers: ServerInfo[] = []
31 let threadIdServer1: number
32 let threadIdServer2: number
33 let commentIdServer1: number
34 let commentIdServer2: number
35 let missedVideo1: Video
36 let missedVideo2: Video
37 let unlistedVideo: Video
39 const videoAttributes = {
40 name: 'my super name for server 1',
45 privacy: VideoPrivacy.PUBLIC,
46 description: 'my super description for server 1',
47 support: 'my super support text for server 1',
48 tags: [ 'tag1p1', 'tag2p1' ],
49 fixture: 'video_short1.webm'
52 const unlistedVideoAttributes = immutableAssign(videoAttributes, {
53 privacy: VideoPrivacy.UNLISTED
56 const checkAttributes = {
57 name: 'my super name for server 1',
62 description: 'my super description for server 1',
63 support: 'my super support text for server 1',
66 host: 'localhost:9001'
70 tags: [ 'tag1p1', 'tag2p1' ],
71 privacy: VideoPrivacy.PUBLIC,
72 commentsEnabled: true,
74 name: 'Default root channel',
78 fixture: 'video_short1.webm',
87 const unlistedCheckAttributes = immutableAssign(checkAttributes, {
88 privacy: VideoPrivacy.UNLISTED
91 before(async function () {
94 servers = await flushAndRunMultipleServers(3)
96 // Get the access tokens
97 await setAccessTokensToServers(servers)
100 it('Should remove followers that are often down', async function () {
103 // Server 2 and 3 follow server 1
104 await follow(servers[1].url, [ servers[0].url ], servers[1].accessToken)
105 await follow(servers[2].url, [ servers[0].url ], servers[2].accessToken)
107 await waitJobs(servers)
109 // Upload a video to server 1
110 await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
112 await waitJobs(servers)
114 // And check all servers have this video
115 for (const server of servers) {
116 const res = await getVideosList(server.url)
117 expect(res.body.data).to.be.an('array')
118 expect(res.body.data).to.have.lengthOf(1)
122 killallServers([ servers[1] ])
124 // Remove server 2 follower
125 for (let i = 0; i < 10; i++) {
126 await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, videoAttributes)
129 await waitJobs(servers[0])
132 killallServers([ servers[2] ])
134 const resLastVideo1 = await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, videoAttributes)
135 missedVideo1 = resLastVideo1.body.video
137 const resLastVideo2 = await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, videoAttributes)
138 missedVideo2 = resLastVideo2.body.video
141 let resVideo = await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, unlistedVideoAttributes)
142 unlistedVideo = resVideo.body.video
144 // Add comments to video 2
146 const text = 'thread 1'
147 let resComment = await addVideoCommentThread(servers[0].url, servers[0].accessToken, missedVideo2.uuid, text)
148 let comment = resComment.body.comment
149 threadIdServer1 = comment.id
151 resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, missedVideo2.uuid, comment.id, 'comment 1-1')
152 comment = resComment.body.comment
154 resComment = await addVideoCommentReply(servers[0].url, servers[0].accessToken, missedVideo2.uuid, comment.id, 'comment 1-2')
155 commentIdServer1 = resComment.body.comment.id
158 await waitJobs(servers[0])
162 // Only server 3 is still a follower of server 1
163 const res = await getFollowersListPaginationAndSort(servers[0].url, 0, 2, 'createdAt')
164 expect(res.body.data).to.be.an('array')
165 expect(res.body.data).to.have.lengthOf(1)
166 expect(res.body.data[0].follower.host).to.equal('localhost:9003')
169 it('Should not have pending/processing jobs anymore', async function () {
170 const states: JobState[] = [ 'waiting', 'active' ]
172 for (const state of states) {
173 const res = await getJobsListPaginationAndSort(servers[ 0 ].url, servers[ 0 ].accessToken, state,0, 50, '-createdAt')
174 expect(res.body.data).to.have.length(0)
178 it('Should re-follow server 1', async function () {
181 await reRunServer(servers[1])
182 await reRunServer(servers[2])
184 await unfollow(servers[1].url, servers[1].accessToken, servers[0])
185 await waitJobs(servers)
187 await follow(servers[1].url, [ servers[0].url ], servers[1].accessToken)
189 await waitJobs(servers)
191 const res = await getFollowersListPaginationAndSort(servers[0].url, 0, 2, 'createdAt')
192 expect(res.body.data).to.be.an('array')
193 expect(res.body.data).to.have.lengthOf(2)
196 it('Should send a view to server 3, and automatically fetch the video', async function () {
199 const res1 = await getVideosList(servers[2].url)
200 expect(res1.body.data).to.be.an('array')
201 expect(res1.body.data).to.have.lengthOf(11)
203 await viewVideo(servers[0].url, missedVideo1.uuid)
204 await viewVideo(servers[0].url, unlistedVideo.uuid)
206 await waitJobs(servers)
208 const res = await getVideosList(servers[2].url)
209 expect(res.body.data).to.be.an('array')
210 // 1 video is unlisted
211 expect(res.body.data).to.have.lengthOf(12)
213 // Check unlisted video
214 const resVideo = await getVideo(servers[2].url, unlistedVideo.uuid)
215 expect(resVideo.body).not.to.be.undefined
217 await completeVideoCheck(servers[2].url, resVideo.body, unlistedCheckAttributes)
220 it('Should send comments on a video to server 3, and automatically fetch the video', async function () {
223 await addVideoCommentReply(servers[0].url, servers[0].accessToken, missedVideo2.uuid, commentIdServer1, 'comment 1-3')
225 await waitJobs(servers)
227 const resVideo = await getVideo(servers[2].url, missedVideo2.uuid)
228 expect(resVideo.body).not.to.be.undefined
231 let resComment = await getVideoCommentThreads(servers[2].url, missedVideo2.uuid, 0, 5)
232 expect(resComment.body.data).to.be.an('array')
233 expect(resComment.body.data).to.have.lengthOf(1)
235 threadIdServer2 = resComment.body.data[0].id
237 resComment = await getVideoThreadComments(servers[2].url, missedVideo2.uuid, threadIdServer2)
239 const tree: VideoCommentThreadTree = resComment.body
240 expect(tree.comment.text).equal('thread 1')
241 expect(tree.children).to.have.lengthOf(1)
243 const firstChild = tree.children[0]
244 expect(firstChild.comment.text).to.equal('comment 1-1')
245 expect(firstChild.children).to.have.lengthOf(1)
247 const childOfFirstChild = firstChild.children[0]
248 expect(childOfFirstChild.comment.text).to.equal('comment 1-2')
249 expect(childOfFirstChild.children).to.have.lengthOf(1)
251 const childOfChildFirstChild = childOfFirstChild.children[0]
252 expect(childOfChildFirstChild.comment.text).to.equal('comment 1-3')
253 expect(childOfChildFirstChild.children).to.have.lengthOf(0)
255 commentIdServer2 = childOfChildFirstChild.comment.id
259 it('Should correctly reply to the comment', async function () {
262 await addVideoCommentReply(servers[2].url, servers[2].accessToken, missedVideo2.uuid, commentIdServer2, 'comment 1-4')
264 await waitJobs(servers)
267 const resComment = await getVideoThreadComments(servers[0].url, missedVideo2.uuid, threadIdServer1)
269 const tree: VideoCommentThreadTree = resComment.body
270 expect(tree.comment.text).equal('thread 1')
271 expect(tree.children).to.have.lengthOf(1)
273 const firstChild = tree.children[0]
274 expect(firstChild.comment.text).to.equal('comment 1-1')
275 expect(firstChild.children).to.have.lengthOf(1)
277 const childOfFirstChild = firstChild.children[0]
278 expect(childOfFirstChild.comment.text).to.equal('comment 1-2')
279 expect(childOfFirstChild.children).to.have.lengthOf(1)
281 const childOfChildFirstChild = childOfFirstChild.children[0]
282 expect(childOfChildFirstChild.comment.text).to.equal('comment 1-3')
283 expect(childOfChildFirstChild.children).to.have.lengthOf(1)
285 const childOfChildOfChildOfFirstChild = childOfChildFirstChild.children[0]
286 expect(childOfChildOfChildOfFirstChild.comment.text).to.equal('comment 1-4')
287 expect(childOfChildOfChildOfFirstChild.children).to.have.lengthOf(0)
291 after(async function () {
292 killallServers(servers)