1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
10 flushAndRunMultipleServers,
15 setAccessTokensToServers,
19 } from '@shared/extra-utils'
20 import { Video, VideoPrivacy } from '@shared/models'
22 const expect = chai.expect
24 describe('Test follows', function () {
25 let servers: ServerInfo[] = []
26 let followsCommands: FollowsCommand[]
28 before(async function () {
31 servers = await flushAndRunMultipleServers(3)
32 followsCommands = servers.map(s => s.followsCommand)
34 // Get the access tokens
35 await setAccessTokensToServers(servers)
38 it('Should not have followers', async function () {
39 for (const server of servers) {
40 const body = await server.followsCommand.getFollowers({ start: 0, count: 5, sort: 'createdAt' })
41 expect(body.total).to.equal(0)
43 const follows = body.data
44 expect(follows).to.be.an('array')
45 expect(follows.length).to.equal(0)
49 it('Should not have following', async function () {
50 for (const server of servers) {
51 const body = await server.followsCommand.getFollowings({ start: 0, count: 5, sort: 'createdAt' })
52 expect(body.total).to.equal(0)
54 const follows = body.data
55 expect(follows).to.be.an('array')
56 expect(follows.length).to.equal(0)
60 it('Should have server 1 following server 2 and 3', async function () {
63 await followsCommands[0].follow({ targets: [ servers[1].url, servers[2].url ] })
65 await waitJobs(servers)
68 it('Should have 2 followings on server 1', async function () {
69 const body = await followsCommands[0].getFollowings({ start: 0, count: 1, sort: 'createdAt' })
70 expect(body.total).to.equal(2)
72 let follows = body.data
73 expect(follows).to.be.an('array')
74 expect(follows.length).to.equal(1)
76 const body2 = await followsCommands[0].getFollowings({ start: 1, count: 1, sort: 'createdAt' })
77 follows = follows.concat(body2.data)
79 const server2Follow = follows.find(f => f.following.host === 'localhost:' + servers[1].port)
80 const server3Follow = follows.find(f => f.following.host === 'localhost:' + servers[2].port)
82 expect(server2Follow).to.not.be.undefined
83 expect(server3Follow).to.not.be.undefined
84 expect(server2Follow.state).to.equal('accepted')
85 expect(server3Follow.state).to.equal('accepted')
88 it('Should search/filter followings on server 1', async function () {
89 const sort = 'createdAt'
94 const search = ':' + servers[1].port
97 const body = await followsCommands[0].getFollowings({ start, count, sort, search })
98 expect(body.total).to.equal(1)
100 const follows = body.data
101 expect(follows.length).to.equal(1)
102 expect(follows[0].following.host).to.equal('localhost:' + servers[1].port)
106 const body = await followsCommands[0].getFollowings({ start, count, sort, search, state: 'accepted' })
107 expect(body.total).to.equal(1)
108 expect(body.data).to.have.lengthOf(1)
112 const body = await followsCommands[0].getFollowings({ start, count, sort, search, state: 'accepted', actorType: 'Person' })
113 expect(body.total).to.equal(0)
114 expect(body.data).to.have.lengthOf(0)
118 const body = await followsCommands[0].getFollowings({
124 actorType: 'Application'
126 expect(body.total).to.equal(1)
127 expect(body.data).to.have.lengthOf(1)
131 const body = await followsCommands[0].getFollowings({ start, count, sort, search, state: 'pending' })
132 expect(body.total).to.equal(0)
133 expect(body.data).to.have.lengthOf(0)
138 const body = await followsCommands[0].getFollowings({ start, count, sort, search: 'bla' })
139 expect(body.total).to.equal(0)
141 expect(body.data.length).to.equal(0)
145 it('Should have 0 followings on server 2 and 3', async function () {
146 for (const server of [ servers[1], servers[2] ]) {
147 const body = await server.followsCommand.getFollowings({ start: 0, count: 5, sort: 'createdAt' })
148 expect(body.total).to.equal(0)
150 const follows = body.data
151 expect(follows).to.be.an('array')
152 expect(follows.length).to.equal(0)
156 it('Should have 1 followers on server 2 and 3', async function () {
157 for (const server of [ servers[1], servers[2] ]) {
158 const body = await server.followsCommand.getFollowers({ start: 0, count: 1, sort: 'createdAt' })
159 expect(body.total).to.equal(1)
161 const follows = body.data
162 expect(follows).to.be.an('array')
163 expect(follows.length).to.equal(1)
164 expect(follows[0].follower.host).to.equal('localhost:' + servers[0].port)
168 it('Should search/filter followers on server 2', async function () {
171 const sort = 'createdAt'
174 const search = servers[0].port + ''
177 const body = await followsCommands[2].getFollowers({ start, count, sort, search })
178 expect(body.total).to.equal(1)
180 const follows = body.data
181 expect(follows.length).to.equal(1)
182 expect(follows[0].following.host).to.equal('localhost:' + servers[2].port)
186 const body = await followsCommands[2].getFollowers({ start, count, sort, search, state: 'accepted' })
187 expect(body.total).to.equal(1)
188 expect(body.data).to.have.lengthOf(1)
192 const body = await followsCommands[2].getFollowers({ start, count, sort, search, state: 'accepted', actorType: 'Person' })
193 expect(body.total).to.equal(0)
194 expect(body.data).to.have.lengthOf(0)
198 const body = await followsCommands[2].getFollowers({
204 actorType: 'Application'
206 expect(body.total).to.equal(1)
207 expect(body.data).to.have.lengthOf(1)
211 const body = await followsCommands[2].getFollowers({ start, count, sort, search, state: 'pending' })
212 expect(body.total).to.equal(0)
213 expect(body.data).to.have.lengthOf(0)
218 const body = await followsCommands[2].getFollowers({ start, count, sort, search: 'bla' })
219 expect(body.total).to.equal(0)
221 const follows = body.data
222 expect(follows.length).to.equal(0)
226 it('Should have 0 followers on server 1', async function () {
227 const body = await followsCommands[0].getFollowers({ start: 0, count: 5, sort: 'createdAt' })
228 expect(body.total).to.equal(0)
230 const follows = body.data
231 expect(follows).to.be.an('array')
232 expect(follows.length).to.equal(0)
235 it('Should have the correct follows counts', async function () {
236 await expectAccountFollows({ server: servers[0], handle: 'peertube@localhost:' + servers[0].port, followers: 0, following: 2 })
237 await expectAccountFollows({ server: servers[0], handle: 'peertube@localhost:' + servers[1].port, followers: 1, following: 0 })
238 await expectAccountFollows({ server: servers[0], handle: 'peertube@localhost:' + servers[2].port, followers: 1, following: 0 })
240 // Server 2 and 3 does not know server 1 follow another server (there was not a refresh)
241 await expectAccountFollows({ server: servers[1], handle: 'peertube@localhost:' + servers[0].port, followers: 0, following: 1 })
242 await expectAccountFollows({ server: servers[1], handle: 'peertube@localhost:' + servers[1].port, followers: 1, following: 0 })
244 await expectAccountFollows({ server: servers[2], handle: 'peertube@localhost:' + servers[0].port, followers: 0, following: 1 })
245 await expectAccountFollows({ server: servers[2], handle: 'peertube@localhost:' + servers[2].port, followers: 1, following: 0 })
248 it('Should unfollow server 3 on server 1', async function () {
251 await followsCommands[0].unfollow({ target: servers[2] })
253 await waitJobs(servers)
256 it('Should not follow server 3 on server 1 anymore', async function () {
257 const body = await followsCommands[0].getFollowings({ start: 0, count: 2, sort: 'createdAt' })
258 expect(body.total).to.equal(1)
260 const follows = body.data
261 expect(follows).to.be.an('array')
262 expect(follows.length).to.equal(1)
264 expect(follows[0].following.host).to.equal('localhost:' + servers[1].port)
267 it('Should not have server 1 as follower on server 3 anymore', async function () {
268 const body = await followsCommands[2].getFollowers({ start: 0, count: 1, sort: 'createdAt' })
269 expect(body.total).to.equal(0)
271 const follows = body.data
272 expect(follows).to.be.an('array')
273 expect(follows.length).to.equal(0)
276 it('Should have the correct follows counts 2', async function () {
277 await expectAccountFollows({ server: servers[0], handle: 'peertube@localhost:' + servers[0].port, followers: 0, following: 1 })
278 await expectAccountFollows({ server: servers[0], handle: 'peertube@localhost:' + servers[1].port, followers: 1, following: 0 })
280 await expectAccountFollows({ server: servers[1], handle: 'peertube@localhost:' + servers[0].port, followers: 0, following: 1 })
281 await expectAccountFollows({ server: servers[1], handle: 'peertube@localhost:' + servers[1].port, followers: 1, following: 0 })
283 await expectAccountFollows({ server: servers[2], handle: 'peertube@localhost:' + servers[0].port, followers: 0, following: 0 })
284 await expectAccountFollows({ server: servers[2], handle: 'peertube@localhost:' + servers[2].port, followers: 0, following: 0 })
287 it('Should upload a video on server 2 and 3 and propagate only the video of server 2', async function () {
290 await uploadVideo(servers[1].url, servers[1].accessToken, { name: 'server2' })
291 await uploadVideo(servers[2].url, servers[2].accessToken, { name: 'server3' })
293 await waitJobs(servers)
295 let res = await getVideosList(servers[0].url)
296 expect(res.body.total).to.equal(1)
297 expect(res.body.data[0].name).to.equal('server2')
299 res = await getVideosList(servers[1].url)
300 expect(res.body.total).to.equal(1)
301 expect(res.body.data[0].name).to.equal('server2')
303 res = await getVideosList(servers[2].url)
304 expect(res.body.total).to.equal(1)
305 expect(res.body.data[0].name).to.equal('server3')
308 describe('Should propagate data on a new following', function () {
311 before(async function () {
314 const video4Attributes = {
319 tags: [ 'tag1', 'tag2', 'tag3' ]
322 await uploadVideo(servers[2].url, servers[2].accessToken, { name: 'server3-2' })
323 await uploadVideo(servers[2].url, servers[2].accessToken, { name: 'server3-3' })
324 await uploadVideo(servers[2].url, servers[2].accessToken, video4Attributes)
325 await uploadVideo(servers[2].url, servers[2].accessToken, { name: 'server3-5' })
326 await uploadVideo(servers[2].url, servers[2].accessToken, { name: 'server3-6' })
329 const userAccessToken = await servers[2].usersCommand.generateUserAndToken('captain')
331 const resVideos = await getVideosList(servers[2].url)
332 video4 = resVideos.body.data.find(v => v.name === 'server3-4')
335 await rateVideo(servers[2].url, servers[2].accessToken, video4.id, 'like')
336 await rateVideo(servers[2].url, userAccessToken, video4.id, 'dislike')
341 const text = 'my super first comment'
342 const created = await servers[2].commentsCommand.createThread({ videoId: video4.id, text })
343 const threadId = created.id
345 const text1 = 'my super answer to thread 1'
346 const childComment = await servers[2].commentsCommand.addReply({ videoId: video4.id, toCommentId: threadId, text: text1 })
348 const text2 = 'my super answer to answer of thread 1'
349 await servers[2].commentsCommand.addReply({ videoId: video4.id, toCommentId: childComment.id, text: text2 })
351 const text3 = 'my second answer to thread 1'
352 await servers[2].commentsCommand.addReply({ videoId: video4.id, toCommentId: threadId, text: text3 })
356 const text = 'will be deleted'
357 const created = await servers[2].commentsCommand.createThread({ videoId: video4.id, text })
358 const threadId = created.id
360 const text1 = 'answer to deleted'
361 await servers[2].commentsCommand.addReply({ videoId: video4.id, toCommentId: threadId, text: text1 })
363 const text2 = 'will also be deleted'
364 const childComment = await servers[2].commentsCommand.addReply({ videoId: video4.id, toCommentId: threadId, text: text2 })
366 const text3 = 'my second answer to deleted'
367 await servers[2].commentsCommand.addReply({ videoId: video4.id, toCommentId: childComment.id, text: text3 })
369 await servers[2].commentsCommand.delete({ videoId: video4.id, commentId: threadId })
370 await servers[2].commentsCommand.delete({ videoId: video4.id, commentId: childComment.id })
375 await servers[2].captionsCommand.createVideoCaption({
378 fixture: 'subtitle-good2.vtt'
383 await waitJobs(servers)
385 // Server 1 follows server 3
386 await followsCommands[0].follow({ targets: [ servers[2].url ] })
388 await waitJobs(servers)
391 it('Should have the correct follows counts 3', async function () {
392 await expectAccountFollows({ server: servers[0], handle: 'peertube@localhost:' + servers[0].port, followers: 0, following: 2 })
393 await expectAccountFollows({ server: servers[0], handle: 'peertube@localhost:' + servers[1].port, followers: 1, following: 0 })
394 await expectAccountFollows({ server: servers[0], handle: 'peertube@localhost:' + servers[2].port, followers: 1, following: 0 })
396 await expectAccountFollows({ server: servers[1], handle: 'peertube@localhost:' + servers[0].port, followers: 0, following: 1 })
397 await expectAccountFollows({ server: servers[1], handle: 'peertube@localhost:' + servers[1].port, followers: 1, following: 0 })
399 await expectAccountFollows({ server: servers[2], handle: 'peertube@localhost:' + servers[0].port, followers: 0, following: 1 })
400 await expectAccountFollows({ server: servers[2], handle: 'peertube@localhost:' + servers[2].port, followers: 1, following: 0 })
403 it('Should have propagated videos', async function () {
404 const res = await getVideosList(servers[0].url)
405 expect(res.body.total).to.equal(7)
407 const video2 = res.body.data.find(v => v.name === 'server3-2')
408 video4 = res.body.data.find(v => v.name === 'server3-4')
409 const video6 = res.body.data.find(v => v.name === 'server3-6')
411 expect(video2).to.not.be.undefined
412 expect(video4).to.not.be.undefined
413 expect(video6).to.not.be.undefined
415 const isLocal = false
416 const checkAttributes = {
422 description: 'my super description',
423 support: 'my super support text',
426 host: 'localhost:' + servers[2].port
429 commentsEnabled: true,
430 downloadEnabled: true,
432 tags: [ 'tag1', 'tag2', 'tag3' ],
433 privacy: VideoPrivacy.PUBLIC,
437 displayName: 'Main root channel',
438 name: 'root_channel',
442 fixture: 'video_short.webm',
450 await completeVideoCheck(servers[0].url, video4, checkAttributes)
453 it('Should have propagated comments', async function () {
454 const { total, data } = await servers[0].commentsCommand.listThreads({ videoId: video4.id, sort: 'createdAt' })
456 expect(total).to.equal(2)
457 expect(data).to.be.an('array')
458 expect(data).to.have.lengthOf(2)
461 const comment = data[0]
462 expect(comment.inReplyToCommentId).to.be.null
463 expect(comment.text).equal('my super first comment')
464 expect(comment.videoId).to.equal(video4.id)
465 expect(comment.id).to.equal(comment.threadId)
466 expect(comment.account.name).to.equal('root')
467 expect(comment.account.host).to.equal('localhost:' + servers[2].port)
468 expect(comment.totalReplies).to.equal(3)
469 expect(dateIsValid(comment.createdAt as string)).to.be.true
470 expect(dateIsValid(comment.updatedAt as string)).to.be.true
472 const threadId = comment.threadId
474 const tree = await servers[0].commentsCommand.getThread({ videoId: video4.id, threadId })
475 expect(tree.comment.text).equal('my super first comment')
476 expect(tree.children).to.have.lengthOf(2)
478 const firstChild = tree.children[0]
479 expect(firstChild.comment.text).to.equal('my super answer to thread 1')
480 expect(firstChild.children).to.have.lengthOf(1)
482 const childOfFirstChild = firstChild.children[0]
483 expect(childOfFirstChild.comment.text).to.equal('my super answer to answer of thread 1')
484 expect(childOfFirstChild.children).to.have.lengthOf(0)
486 const secondChild = tree.children[1]
487 expect(secondChild.comment.text).to.equal('my second answer to thread 1')
488 expect(secondChild.children).to.have.lengthOf(0)
492 const deletedComment = data[1]
493 expect(deletedComment).to.not.be.undefined
494 expect(deletedComment.isDeleted).to.be.true
495 expect(deletedComment.deletedAt).to.not.be.null
496 expect(deletedComment.text).to.equal('')
497 expect(deletedComment.inReplyToCommentId).to.be.null
498 expect(deletedComment.account).to.be.null
499 expect(deletedComment.totalReplies).to.equal(2)
500 expect(dateIsValid(deletedComment.deletedAt as string)).to.be.true
502 const tree = await servers[0].commentsCommand.getThread({ videoId: video4.id, threadId: deletedComment.threadId })
503 const [ commentRoot, deletedChildRoot ] = tree.children
505 expect(deletedChildRoot).to.not.be.undefined
506 expect(deletedChildRoot.comment.isDeleted).to.be.true
507 expect(deletedChildRoot.comment.deletedAt).to.not.be.null
508 expect(deletedChildRoot.comment.text).to.equal('')
509 expect(deletedChildRoot.comment.inReplyToCommentId).to.equal(deletedComment.id)
510 expect(deletedChildRoot.comment.account).to.be.null
511 expect(deletedChildRoot.children).to.have.lengthOf(1)
513 const answerToDeletedChild = deletedChildRoot.children[0]
514 expect(answerToDeletedChild.comment).to.not.be.undefined
515 expect(answerToDeletedChild.comment.inReplyToCommentId).to.equal(deletedChildRoot.comment.id)
516 expect(answerToDeletedChild.comment.text).to.equal('my second answer to deleted')
517 expect(answerToDeletedChild.comment.account.name).to.equal('root')
519 expect(commentRoot.comment).to.not.be.undefined
520 expect(commentRoot.comment.inReplyToCommentId).to.equal(deletedComment.id)
521 expect(commentRoot.comment.text).to.equal('answer to deleted')
522 expect(commentRoot.comment.account.name).to.equal('root')
526 it('Should have propagated captions', async function () {
527 const body = await servers[0].captionsCommand.listVideoCaptions({ videoId: video4.id })
528 expect(body.total).to.equal(1)
529 expect(body.data).to.have.lengthOf(1)
531 const caption1 = body.data[0]
532 expect(caption1.language.id).to.equal('ar')
533 expect(caption1.language.label).to.equal('Arabic')
534 expect(caption1.captionPath).to.match(new RegExp('^/lazy-static/video-captions/.+-ar.vtt$'))
535 await testCaptionFile(servers[0].url, caption1.captionPath, 'Subtitle good 2.')
538 it('Should unfollow server 3 on server 1 and does not list server 3 videos', async function () {
541 await followsCommands[0].unfollow({ target: servers[2] })
543 await waitJobs(servers)
545 const res = await getVideosList(servers[0].url)
546 expect(res.body.total).to.equal(1)
551 after(async function () {
552 await cleanupTests(servers)