1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
15 flushAndRunMultipleServers,
17 getVideoCommentThreads,
19 getVideoThreadComments,
23 setAccessTokensToServers,
28 } from '@shared/extra-utils'
29 import { Video, VideoCaption, VideoComment, VideoCommentThreadTree, VideoPrivacy } from '@shared/models'
31 const expect = chai.expect
33 describe('Test follows', function () {
34 let servers: ServerInfo[] = []
35 let followsCommands: FollowsCommand[]
37 before(async function () {
40 servers = await flushAndRunMultipleServers(3)
41 followsCommands = servers.map(s => s.followsCommand)
43 // Get the access tokens
44 await setAccessTokensToServers(servers)
47 it('Should not have followers', async function () {
48 for (const server of servers) {
49 const body = await server.followsCommand.getFollowers({ start: 0, count: 5, sort: 'createdAt' })
50 expect(body.total).to.equal(0)
52 const follows = body.data
53 expect(follows).to.be.an('array')
54 expect(follows.length).to.equal(0)
58 it('Should not have following', async function () {
59 for (const server of servers) {
60 const body = await server.followsCommand.getFollowings({ start: 0, count: 5, sort: 'createdAt' })
61 expect(body.total).to.equal(0)
63 const follows = body.data
64 expect(follows).to.be.an('array')
65 expect(follows.length).to.equal(0)
69 it('Should have server 1 following server 2 and 3', async function () {
72 await followsCommands[0].follow({ targets: [ servers[1].url, servers[2].url ] })
74 await waitJobs(servers)
77 it('Should have 2 followings on server 1', async function () {
78 const body = await followsCommands[0].getFollowings({ start: 0, count: 1, sort: 'createdAt' })
79 expect(body.total).to.equal(2)
81 let follows = body.data
82 expect(follows).to.be.an('array')
83 expect(follows.length).to.equal(1)
85 const body2 = await followsCommands[0].getFollowings({ start: 1, count: 1, sort: 'createdAt' })
86 follows = follows.concat(body2.data)
88 const server2Follow = follows.find(f => f.following.host === 'localhost:' + servers[1].port)
89 const server3Follow = follows.find(f => f.following.host === 'localhost:' + servers[2].port)
91 expect(server2Follow).to.not.be.undefined
92 expect(server3Follow).to.not.be.undefined
93 expect(server2Follow.state).to.equal('accepted')
94 expect(server3Follow.state).to.equal('accepted')
97 it('Should search/filter followings on server 1', async function () {
98 const sort = 'createdAt'
103 const search = ':' + servers[1].port
106 const body = await followsCommands[0].getFollowings({ start, count, sort, search })
107 expect(body.total).to.equal(1)
109 const follows = body.data
110 expect(follows.length).to.equal(1)
111 expect(follows[0].following.host).to.equal('localhost:' + servers[1].port)
115 const body = await followsCommands[0].getFollowings({ start, count, sort, search, state: 'accepted' })
116 expect(body.total).to.equal(1)
117 expect(body.data).to.have.lengthOf(1)
121 const body = await followsCommands[0].getFollowings({ start, count, sort, search, state: 'accepted', actorType: 'Person' })
122 expect(body.total).to.equal(0)
123 expect(body.data).to.have.lengthOf(0)
127 const body = await followsCommands[0].getFollowings({
133 actorType: 'Application'
135 expect(body.total).to.equal(1)
136 expect(body.data).to.have.lengthOf(1)
140 const body = await followsCommands[0].getFollowings({ start, count, sort, search, state: 'pending' })
141 expect(body.total).to.equal(0)
142 expect(body.data).to.have.lengthOf(0)
147 const body = await followsCommands[0].getFollowings({ start, count, sort, search: 'bla' })
148 expect(body.total).to.equal(0)
150 expect(body.data.length).to.equal(0)
154 it('Should have 0 followings on server 2 and 3', async function () {
155 for (const server of [ servers[1], servers[2] ]) {
156 const body = await server.followsCommand.getFollowings({ start: 0, count: 5, sort: 'createdAt' })
157 expect(body.total).to.equal(0)
159 const follows = body.data
160 expect(follows).to.be.an('array')
161 expect(follows.length).to.equal(0)
165 it('Should have 1 followers on server 2 and 3', async function () {
166 for (const server of [ servers[1], servers[2] ]) {
167 const body = await server.followsCommand.getFollowers({ start: 0, count: 1, sort: 'createdAt' })
168 expect(body.total).to.equal(1)
170 const follows = body.data
171 expect(follows).to.be.an('array')
172 expect(follows.length).to.equal(1)
173 expect(follows[0].follower.host).to.equal('localhost:' + servers[0].port)
177 it('Should search/filter followers on server 2', async function () {
180 const sort = 'createdAt'
183 const search = servers[0].port + ''
186 const body = await followsCommands[2].getFollowers({ start, count, sort, search })
187 expect(body.total).to.equal(1)
189 const follows = body.data
190 expect(follows.length).to.equal(1)
191 expect(follows[0].following.host).to.equal('localhost:' + servers[2].port)
195 const body = await followsCommands[2].getFollowers({ start, count, sort, search, state: 'accepted' })
196 expect(body.total).to.equal(1)
197 expect(body.data).to.have.lengthOf(1)
201 const body = await followsCommands[2].getFollowers({ start, count, sort, search, state: 'accepted', actorType: 'Person' })
202 expect(body.total).to.equal(0)
203 expect(body.data).to.have.lengthOf(0)
207 const body = await followsCommands[2].getFollowers({
213 actorType: 'Application'
215 expect(body.total).to.equal(1)
216 expect(body.data).to.have.lengthOf(1)
220 const body = await followsCommands[2].getFollowers({ start, count, sort, search, state: 'pending' })
221 expect(body.total).to.equal(0)
222 expect(body.data).to.have.lengthOf(0)
227 const body = await followsCommands[2].getFollowers({ start, count, sort, search: 'bla' })
228 expect(body.total).to.equal(0)
230 const follows = body.data
231 expect(follows.length).to.equal(0)
235 it('Should have 0 followers on server 1', async function () {
236 const body = await followsCommands[0].getFollowers({ start: 0, count: 5, sort: 'createdAt' })
237 expect(body.total).to.equal(0)
239 const follows = body.data
240 expect(follows).to.be.an('array')
241 expect(follows.length).to.equal(0)
244 it('Should have the correct follows counts', async function () {
245 await expectAccountFollows(servers[0].url, 'peertube@localhost:' + servers[0].port, 0, 2)
246 await expectAccountFollows(servers[0].url, 'peertube@localhost:' + servers[1].port, 1, 0)
247 await expectAccountFollows(servers[0].url, 'peertube@localhost:' + servers[2].port, 1, 0)
249 // Server 2 and 3 does not know server 1 follow another server (there was not a refresh)
250 await expectAccountFollows(servers[1].url, 'peertube@localhost:' + servers[0].port, 0, 1)
251 await expectAccountFollows(servers[1].url, 'peertube@localhost:' + servers[1].port, 1, 0)
253 await expectAccountFollows(servers[2].url, 'peertube@localhost:' + servers[0].port, 0, 1)
254 await expectAccountFollows(servers[2].url, 'peertube@localhost:' + servers[2].port, 1, 0)
257 it('Should unfollow server 3 on server 1', async function () {
260 await followsCommands[0].unfollow({ target: servers[2] })
262 await waitJobs(servers)
265 it('Should not follow server 3 on server 1 anymore', async function () {
266 const body = await followsCommands[0].getFollowings({ start: 0, count: 2, sort: 'createdAt' })
267 expect(body.total).to.equal(1)
269 const follows = body.data
270 expect(follows).to.be.an('array')
271 expect(follows.length).to.equal(1)
273 expect(follows[0].following.host).to.equal('localhost:' + servers[1].port)
276 it('Should not have server 1 as follower on server 3 anymore', async function () {
277 const body = await followsCommands[2].getFollowers({ start: 0, count: 1, sort: 'createdAt' })
278 expect(body.total).to.equal(0)
280 const follows = body.data
281 expect(follows).to.be.an('array')
282 expect(follows.length).to.equal(0)
285 it('Should have the correct follows counts 2', async function () {
286 await expectAccountFollows(servers[0].url, 'peertube@localhost:' + servers[0].port, 0, 1)
287 await expectAccountFollows(servers[0].url, 'peertube@localhost:' + servers[1].port, 1, 0)
289 await expectAccountFollows(servers[1].url, 'peertube@localhost:' + servers[0].port, 0, 1)
290 await expectAccountFollows(servers[1].url, 'peertube@localhost:' + servers[1].port, 1, 0)
292 await expectAccountFollows(servers[2].url, 'peertube@localhost:' + servers[0].port, 0, 0)
293 await expectAccountFollows(servers[2].url, 'peertube@localhost:' + servers[2].port, 0, 0)
296 it('Should upload a video on server 2 and 3 and propagate only the video of server 2', async function () {
299 await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'server2' })
300 await uploadVideo(servers[2].url, servers[2].accessToken, { name: 'server3' })
302 await waitJobs(servers)
304 let res = await getVideosList(servers[0].url)
305 expect(res.body.total).to.equal(1)
306 expect(res.body.data[0].name).to.equal('server2')
308 res = await getVideosList(servers[1].url)
309 expect(res.body.total).to.equal(1)
310 expect(res.body.data[0].name).to.equal('server2')
312 res = await getVideosList(servers[2].url)
313 expect(res.body.total).to.equal(1)
314 expect(res.body.data[0].name).to.equal('server3')
317 describe('Should propagate data on a new following', function () {
320 before(async function () {
323 const video4Attributes = {
328 tags: [ 'tag1', 'tag2', 'tag3' ]
331 await uploadVideo(servers[2].url, servers[2].accessToken, { name: 'server3-2' })
332 await uploadVideo(servers[2].url, servers[2].accessToken, { name: 'server3-3' })
333 await uploadVideo(servers[2].url, servers[2].accessToken, video4Attributes)
334 await uploadVideo(servers[2].url, servers[2].accessToken, { name: 'server3-5' })
335 await uploadVideo(servers[2].url, servers[2].accessToken, { name: 'server3-6' })
338 const user = { username: 'captain', password: 'password' }
339 await createUser({ url: servers[2].url, accessToken: servers[2].accessToken, username: user.username, password: user.password })
340 const userAccessToken = await userLogin(servers[2], user)
342 const resVideos = await getVideosList(servers[2].url)
343 video4 = resVideos.body.data.find(v => v.name === 'server3-4')
346 await rateVideo(servers[2].url, servers[2].accessToken, video4.id, 'like')
347 await rateVideo(servers[2].url, userAccessToken, video4.id, 'dislike')
352 const text = 'my super first comment'
353 const res = await addVideoCommentThread(servers[2].url, servers[2].accessToken, video4.id, text)
354 const threadId = res.body.comment.id
356 const text1 = 'my super answer to thread 1'
357 const childCommentRes = await addVideoCommentReply(servers[2].url, servers[2].accessToken, video4.id, threadId, text1)
358 const childCommentId = childCommentRes.body.comment.id
360 const text2 = 'my super answer to answer of thread 1'
361 await addVideoCommentReply(servers[2].url, servers[2].accessToken, video4.id, childCommentId, text2)
363 const text3 = 'my second answer to thread 1'
364 await addVideoCommentReply(servers[2].url, servers[2].accessToken, video4.id, threadId, text3)
368 const text = 'will be deleted'
369 const res = await addVideoCommentThread(servers[2].url, servers[2].accessToken, video4.id, text)
370 const threadId = res.body.comment.id
372 const text1 = 'answer to deleted'
373 await addVideoCommentReply(servers[2].url, servers[2].accessToken, video4.id, threadId, text1)
375 const text2 = 'will also be deleted'
376 const childCommentRes = await addVideoCommentReply(servers[2].url, servers[2].accessToken, video4.id, threadId, text2)
377 const childCommentId = childCommentRes.body.comment.id
379 const text3 = 'my second answer to deleted'
380 await addVideoCommentReply(servers[2].url, servers[2].accessToken, video4.id, childCommentId, text3)
382 await deleteVideoComment(servers[2].url, servers[2].accessToken, video4.id, threadId)
383 await deleteVideoComment(servers[2].url, servers[2].accessToken, video4.id, childCommentId)
388 await createVideoCaption({
390 accessToken: servers[2].accessToken,
393 fixture: 'subtitle-good2.vtt'
398 await waitJobs(servers)
400 // Server 1 follows server 3
401 await followsCommands[0].follow({ targets: [ servers[2].url ] })
403 await waitJobs(servers)
406 it('Should have the correct follows counts 3', async function () {
407 await expectAccountFollows(servers[0].url, 'peertube@localhost:' + servers[0].port, 0, 2)
408 await expectAccountFollows(servers[0].url, 'peertube@localhost:' + servers[1].port, 1, 0)
409 await expectAccountFollows(servers[0].url, 'peertube@localhost:' + servers[2].port, 1, 0)
411 await expectAccountFollows(servers[1].url, 'peertube@localhost:' + servers[0].port, 0, 1)
412 await expectAccountFollows(servers[1].url, 'peertube@localhost:' + servers[1].port, 1, 0)
414 await expectAccountFollows(servers[2].url, 'peertube@localhost:' + servers[0].port, 0, 1)
415 await expectAccountFollows(servers[2].url, 'peertube@localhost:' + servers[2].port, 1, 0)
418 it('Should have propagated videos', async function () {
419 const res = await getVideosList(servers[0].url)
420 expect(res.body.total).to.equal(7)
422 const video2 = res.body.data.find(v => v.name === 'server3-2')
423 video4 = res.body.data.find(v => v.name === 'server3-4')
424 const video6 = res.body.data.find(v => v.name === 'server3-6')
426 expect(video2).to.not.be.undefined
427 expect(video4).to.not.be.undefined
428 expect(video6).to.not.be.undefined
430 const isLocal = false
431 const checkAttributes = {
437 description: 'my super description',
438 support: 'my super support text',
441 host: 'localhost:' + servers[2].port
444 commentsEnabled: true,
445 downloadEnabled: true,
447 tags: [ 'tag1', 'tag2', 'tag3' ],
448 privacy: VideoPrivacy.PUBLIC,
452 displayName: 'Main root channel',
453 name: 'root_channel',
457 fixture: 'video_short.webm',
465 await completeVideoCheck(servers[0].url, video4, checkAttributes)
468 it('Should have propagated comments', async function () {
469 const res1 = await getVideoCommentThreads(servers[0].url, video4.id, 0, 5, 'createdAt')
471 expect(res1.body.total).to.equal(2)
472 expect(res1.body.data).to.be.an('array')
473 expect(res1.body.data).to.have.lengthOf(2)
476 const comment: VideoComment = res1.body.data[0]
477 expect(comment.inReplyToCommentId).to.be.null
478 expect(comment.text).equal('my super first comment')
479 expect(comment.videoId).to.equal(video4.id)
480 expect(comment.id).to.equal(comment.threadId)
481 expect(comment.account.name).to.equal('root')
482 expect(comment.account.host).to.equal('localhost:' + servers[2].port)
483 expect(comment.totalReplies).to.equal(3)
484 expect(dateIsValid(comment.createdAt as string)).to.be.true
485 expect(dateIsValid(comment.updatedAt as string)).to.be.true
487 const threadId = comment.threadId
489 const res2 = await getVideoThreadComments(servers[0].url, video4.id, threadId)
491 const tree: VideoCommentThreadTree = res2.body
492 expect(tree.comment.text).equal('my super first comment')
493 expect(tree.children).to.have.lengthOf(2)
495 const firstChild = tree.children[0]
496 expect(firstChild.comment.text).to.equal('my super answer to thread 1')
497 expect(firstChild.children).to.have.lengthOf(1)
499 const childOfFirstChild = firstChild.children[0]
500 expect(childOfFirstChild.comment.text).to.equal('my super answer to answer of thread 1')
501 expect(childOfFirstChild.children).to.have.lengthOf(0)
503 const secondChild = tree.children[1]
504 expect(secondChild.comment.text).to.equal('my second answer to thread 1')
505 expect(secondChild.children).to.have.lengthOf(0)
509 const deletedComment: VideoComment = res1.body.data[1]
510 expect(deletedComment).to.not.be.undefined
511 expect(deletedComment.isDeleted).to.be.true
512 expect(deletedComment.deletedAt).to.not.be.null
513 expect(deletedComment.text).to.equal('')
514 expect(deletedComment.inReplyToCommentId).to.be.null
515 expect(deletedComment.account).to.be.null
516 expect(deletedComment.totalReplies).to.equal(2)
517 expect(dateIsValid(deletedComment.deletedAt as string)).to.be.true
519 const res2 = await getVideoThreadComments(servers[0].url, video4.id, deletedComment.threadId)
521 const tree: VideoCommentThreadTree = res2.body
522 const [ commentRoot, deletedChildRoot ] = tree.children
524 expect(deletedChildRoot).to.not.be.undefined
525 expect(deletedChildRoot.comment.isDeleted).to.be.true
526 expect(deletedChildRoot.comment.deletedAt).to.not.be.null
527 expect(deletedChildRoot.comment.text).to.equal('')
528 expect(deletedChildRoot.comment.inReplyToCommentId).to.equal(deletedComment.id)
529 expect(deletedChildRoot.comment.account).to.be.null
530 expect(deletedChildRoot.children).to.have.lengthOf(1)
532 const answerToDeletedChild = deletedChildRoot.children[0]
533 expect(answerToDeletedChild.comment).to.not.be.undefined
534 expect(answerToDeletedChild.comment.inReplyToCommentId).to.equal(deletedChildRoot.comment.id)
535 expect(answerToDeletedChild.comment.text).to.equal('my second answer to deleted')
536 expect(answerToDeletedChild.comment.account.name).to.equal('root')
538 expect(commentRoot.comment).to.not.be.undefined
539 expect(commentRoot.comment.inReplyToCommentId).to.equal(deletedComment.id)
540 expect(commentRoot.comment.text).to.equal('answer to deleted')
541 expect(commentRoot.comment.account.name).to.equal('root')
545 it('Should have propagated captions', async function () {
546 const res = await listVideoCaptions(servers[0].url, video4.id)
547 expect(res.body.total).to.equal(1)
548 expect(res.body.data).to.have.lengthOf(1)
550 const caption1: VideoCaption = res.body.data[0]
551 expect(caption1.language.id).to.equal('ar')
552 expect(caption1.language.label).to.equal('Arabic')
553 expect(caption1.captionPath).to.match(new RegExp('^/lazy-static/video-captions/.+-ar.vtt$'))
554 await testCaptionFile(servers[0].url, caption1.captionPath, 'Subtitle good 2.')
557 it('Should unfollow server 3 on server 1 and does not list server 3 videos', async function () {
560 await followsCommands[0].unfollow({ target: servers[2] })
562 await waitJobs(servers)
564 const res = await getVideosList(servers[0].url)
565 expect(res.body.total).to.equal(1)
570 after(async function () {
571 await cleanupTests(servers)