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 { testImage } from '../../utils'
8 dateIsValid, flushTests, killallServers, runServer, ServerInfo, setAccessTokensToServers, updateMyAvatar,
10 } from '../../utils/index'
12 addVideoCommentReply, addVideoCommentThread, deleteVideoComment, getVideoCommentThreads,
13 getVideoThreadComments
14 } from '../../utils/videos/video-comments'
16 const expect = chai.expect
18 describe('Test video comments', function () {
19 let server: ServerInfo
23 let replyToDeleteId: number
25 before(async function () {
30 server = await runServer(1)
32 await setAccessTokensToServers([ server ])
34 const res = await uploadVideo(server.url, server.accessToken, {})
35 videoUUID = res.body.video.uuid
36 videoId = res.body.video.id
38 await updateMyAvatar({
40 accessToken: server.accessToken,
45 it('Should not have threads on this video', async function () {
46 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
48 expect(res.body.total).to.equal(0)
49 expect(res.body.data).to.be.an('array')
50 expect(res.body.data).to.have.lengthOf(0)
53 it('Should create a thread in this video', async function () {
54 const text = 'my super first comment'
56 const res = await addVideoCommentThread(server.url, server.accessToken, videoUUID, text)
57 const comment = res.body.comment
59 expect(comment.inReplyToCommentId).to.be.null
60 expect(comment.text).equal('my super first comment')
61 expect(comment.videoId).to.equal(videoId)
62 expect(comment.id).to.equal(comment.threadId)
63 expect(comment.account.name).to.equal('root')
64 expect(comment.account.host).to.equal('localhost:9001')
65 expect(comment.account.url).to.equal('http://localhost:9001/accounts/root')
66 expect(comment.totalReplies).to.equal(0)
67 expect(dateIsValid(comment.createdAt as string)).to.be.true
68 expect(dateIsValid(comment.updatedAt as string)).to.be.true
71 it('Should list threads of this video', async function () {
72 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
74 expect(res.body.total).to.equal(1)
75 expect(res.body.data).to.be.an('array')
76 expect(res.body.data).to.have.lengthOf(1)
78 const comment: VideoComment = res.body.data[0]
79 expect(comment.inReplyToCommentId).to.be.null
80 expect(comment.text).equal('my super first comment')
81 expect(comment.videoId).to.equal(videoId)
82 expect(comment.id).to.equal(comment.threadId)
83 expect(comment.account.name).to.equal('root')
84 expect(comment.account.host).to.equal('localhost:9001')
86 const test = await testImage(server.url, 'avatar-resized', comment.account.avatar.path, '.png')
87 expect(test).to.equal(true)
89 expect(comment.totalReplies).to.equal(0)
90 expect(dateIsValid(comment.createdAt as string)).to.be.true
91 expect(dateIsValid(comment.updatedAt as string)).to.be.true
93 threadId = comment.threadId
96 it('Should get all the thread created', async function () {
97 const res = await getVideoThreadComments(server.url, videoUUID, threadId)
99 const rootComment = res.body.comment
100 expect(rootComment.inReplyToCommentId).to.be.null
101 expect(rootComment.text).equal('my super first comment')
102 expect(rootComment.videoId).to.equal(videoId)
103 expect(dateIsValid(rootComment.createdAt as string)).to.be.true
104 expect(dateIsValid(rootComment.updatedAt as string)).to.be.true
107 it('Should create multiple replies in this thread', async function () {
108 const text1 = 'my super answer to thread 1'
109 const childCommentRes = await addVideoCommentReply(server.url, server.accessToken, videoId, threadId, text1)
110 const childCommentId = childCommentRes.body.comment.id
112 const text2 = 'my super answer to answer of thread 1'
113 await addVideoCommentReply(server.url, server.accessToken, videoId, childCommentId, text2)
115 const text3 = 'my second answer to thread 1'
116 await addVideoCommentReply(server.url, server.accessToken, videoId, threadId, text3)
119 it('Should get correctly the replies', async function () {
120 const res = await getVideoThreadComments(server.url, videoUUID, threadId)
122 const tree: VideoCommentThreadTree = res.body
123 expect(tree.comment.text).equal('my super first comment')
124 expect(tree.children).to.have.lengthOf(2)
126 const firstChild = tree.children[0]
127 expect(firstChild.comment.text).to.equal('my super answer to thread 1')
128 expect(firstChild.children).to.have.lengthOf(1)
130 const childOfFirstChild = firstChild.children[0]
131 expect(childOfFirstChild.comment.text).to.equal('my super answer to answer of thread 1')
132 expect(childOfFirstChild.children).to.have.lengthOf(0)
134 const secondChild = tree.children[1]
135 expect(secondChild.comment.text).to.equal('my second answer to thread 1')
136 expect(secondChild.children).to.have.lengthOf(0)
138 replyToDeleteId = secondChild.comment.id
141 it('Should create other threads', async function () {
142 const text1 = 'super thread 2'
143 await addVideoCommentThread(server.url, server.accessToken, videoUUID, text1)
145 const text2 = 'super thread 3'
146 await addVideoCommentThread(server.url, server.accessToken, videoUUID, text2)
149 it('Should list the threads', async function () {
150 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5, 'createdAt')
152 expect(res.body.total).to.equal(3)
153 expect(res.body.data).to.be.an('array')
154 expect(res.body.data).to.have.lengthOf(3)
156 expect(res.body.data[0].text).to.equal('my super first comment')
157 expect(res.body.data[0].totalReplies).to.equal(3)
158 expect(res.body.data[1].text).to.equal('super thread 2')
159 expect(res.body.data[1].totalReplies).to.equal(0)
160 expect(res.body.data[2].text).to.equal('super thread 3')
161 expect(res.body.data[2].totalReplies).to.equal(0)
164 it('Should delete a reply', async function () {
165 await deleteVideoComment(server.url, server.accessToken, videoId, replyToDeleteId)
167 const res = await getVideoThreadComments(server.url, videoUUID, threadId)
169 const tree: VideoCommentThreadTree = res.body
170 expect(tree.comment.text).equal('my super first comment')
171 expect(tree.children).to.have.lengthOf(1)
173 const firstChild = tree.children[0]
174 expect(firstChild.comment.text).to.equal('my super answer to thread 1')
175 expect(firstChild.children).to.have.lengthOf(1)
177 const childOfFirstChild = firstChild.children[0]
178 expect(childOfFirstChild.comment.text).to.equal('my super answer to answer of thread 1')
179 expect(childOfFirstChild.children).to.have.lengthOf(0)
182 it('Should delete a complete thread', async function () {
183 await deleteVideoComment(server.url, server.accessToken, videoId, threadId)
185 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5, 'createdAt')
186 expect(res.body.total).to.equal(2)
187 expect(res.body.data).to.be.an('array')
188 expect(res.body.data).to.have.lengthOf(2)
190 expect(res.body.data[0].text).to.equal('super thread 2')
191 expect(res.body.data[0].totalReplies).to.equal(0)
192 expect(res.body.data[1].text).to.equal('super thread 3')
193 expect(res.body.data[1].totalReplies).to.equal(0)
196 after(async function () {
197 killallServers([ server ])
199 // Keep the logs if the test failed