1 /* tslint:disable:no-unused-expression */
3 import * as chai from 'chai'
5 import { VideoComment, VideoCommentThreadTree } from '../../../../shared/models/videos/video-comment.model'
6 import { dateIsValid, flushTests, killallServers, runServer, ServerInfo, setAccessTokensToServers, uploadVideo } from '../../utils/index'
8 addVideoCommentReply, addVideoCommentThread, getVideoCommentThreads,
10 } from '../../utils/videos/video-comments'
12 const expect = chai.expect
14 describe('Test video comments', function () {
15 let server: ServerInfo
20 before(async function () {
25 server = await runServer(1)
27 await setAccessTokensToServers([ server ])
29 const res = await uploadVideo(server.url, server.accessToken, {})
30 videoUUID = res.body.video.uuid
31 videoId = res.body.video.id
34 it('Should not have threads on this video', async function () {
35 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
37 expect(res.body.total).to.equal(0)
38 expect(res.body.data).to.be.an('array')
39 expect(res.body.data).to.have.lengthOf(0)
42 it('Should create a thread in this video', async function () {
43 const text = 'my super first comment'
45 const res = await addVideoCommentThread(server.url, server.accessToken, videoUUID, text)
46 const comment = res.body.comment
48 expect(comment.inReplyToCommentId).to.be.null
49 expect(comment.text).equal('my super first comment')
50 expect(comment.videoId).to.equal(videoId)
51 expect(comment.id).to.equal(comment.threadId)
52 expect(comment.account.name).to.equal('root')
53 expect(comment.account.host).to.equal('localhost:9001')
54 expect(comment.totalReplies).to.equal(0)
55 expect(dateIsValid(comment.createdAt as string)).to.be.true
56 expect(dateIsValid(comment.updatedAt as string)).to.be.true
59 it('Should list threads of this video', async function () {
60 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
62 expect(res.body.total).to.equal(1)
63 expect(res.body.data).to.be.an('array')
64 expect(res.body.data).to.have.lengthOf(1)
66 const comment: VideoComment = res.body.data[0]
67 expect(comment.inReplyToCommentId).to.be.null
68 expect(comment.text).equal('my super first comment')
69 expect(comment.videoId).to.equal(videoId)
70 expect(comment.id).to.equal(comment.threadId)
71 expect(comment.account.name).to.equal('root')
72 expect(comment.account.host).to.equal('localhost:9001')
73 expect(comment.totalReplies).to.equal(0)
74 expect(dateIsValid(comment.createdAt as string)).to.be.true
75 expect(dateIsValid(comment.updatedAt as string)).to.be.true
77 threadId = comment.threadId
80 it('Should get all the thread created', async function () {
81 const res = await getVideoThreadComments(server.url, videoUUID, threadId)
83 const rootComment = res.body.comment
84 expect(rootComment.inReplyToCommentId).to.be.null
85 expect(rootComment.text).equal('my super first comment')
86 expect(rootComment.videoId).to.equal(videoId)
87 expect(dateIsValid(rootComment.createdAt as string)).to.be.true
88 expect(dateIsValid(rootComment.updatedAt as string)).to.be.true
91 it('Should create multiple replies in this thread', async function () {
92 const text1 = 'my super answer to thread 1'
93 const childCommentRes = await addVideoCommentReply(server.url, server.accessToken, videoId, threadId, text1)
94 const childCommentId = childCommentRes.body.comment.id
96 const text2 = 'my super answer to answer of thread 1'
97 await addVideoCommentReply(server.url, server.accessToken, videoId, childCommentId, text2)
99 const text3 = 'my second answer to thread 1'
100 await addVideoCommentReply(server.url, server.accessToken, videoId, threadId, text3)
103 it('Should get correctly the replies', async function () {
104 const res = await getVideoThreadComments(server.url, videoUUID, threadId)
106 const tree: VideoCommentThreadTree = res.body
107 expect(tree.comment.text).equal('my super first comment')
108 expect(tree.children).to.have.lengthOf(2)
110 const firstChild = tree.children[0]
111 expect(firstChild.comment.text).to.equal('my super answer to thread 1')
112 expect(firstChild.children).to.have.lengthOf(1)
114 const childOfFirstChild = firstChild.children[0]
115 expect(childOfFirstChild.comment.text).to.equal('my super answer to answer of thread 1')
116 expect(childOfFirstChild.children).to.have.lengthOf(0)
118 const secondChild = tree.children[1]
119 expect(secondChild.comment.text).to.equal('my second answer to thread 1')
120 expect(secondChild.children).to.have.lengthOf(0)
123 it('Should create other threads', async function () {
124 const text1 = 'super thread 2'
125 await addVideoCommentThread(server.url, server.accessToken, videoUUID, text1)
127 const text2 = 'super thread 3'
128 await addVideoCommentThread(server.url, server.accessToken, videoUUID, text2)
131 it('Should list the threads', async function () {
132 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5, 'createdAt')
134 expect(res.body.total).to.equal(3)
135 expect(res.body.data).to.be.an('array')
136 expect(res.body.data).to.have.lengthOf(3)
138 expect(res.body.data[0].text).to.equal('my super first comment')
139 expect(res.body.data[0].totalReplies).to.equal(3)
140 expect(res.body.data[1].text).to.equal('super thread 2')
141 expect(res.body.data[1].totalReplies).to.equal(0)
142 expect(res.body.data[2].text).to.equal('super thread 3')
143 expect(res.body.data[2].totalReplies).to.equal(0)
146 after(async function () {
147 killallServers([ server ])
149 // Keep the logs if the test failed