1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
6 import { cleanupTests, testImage } from '../../../../shared/extra-utils'
13 setAccessTokensToServers,
16 } from '../../../../shared/extra-utils/index'
19 addVideoCommentThread,
21 getAdminVideoComments,
22 getVideoCommentThreads,
23 getVideoThreadComments
24 } from '../../../../shared/extra-utils/videos/video-comments'
25 import { VideoComment, VideoCommentAdmin, VideoCommentThreadTree } from '../../../../shared/models/videos/video-comment.model'
27 const expect = chai.expect
29 describe('Test video comments', function () {
30 let server: ServerInfo
34 let replyToDeleteId: number
36 let userAccessTokenServer1: string
38 before(async function () {
41 server = await flushAndRunServer(1)
43 await setAccessTokensToServers([ server ])
45 const res = await uploadVideo(server.url, server.accessToken, {})
46 videoUUID = res.body.video.uuid
47 videoId = res.body.video.id
49 await updateMyAvatar({
51 accessToken: server.accessToken,
57 accessToken: server.accessToken,
61 userAccessTokenServer1 = await getAccessToken(server.url, 'user1', 'password')
64 describe('User comments', function () {
66 it('Should not have threads on this video', async function () {
67 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
69 expect(res.body.total).to.equal(0)
70 expect(res.body.data).to.be.an('array')
71 expect(res.body.data).to.have.lengthOf(0)
74 it('Should create a thread in this video', async function () {
75 const text = 'my super first comment'
77 const res = await addVideoCommentThread(server.url, server.accessToken, videoUUID, text)
78 const comment = res.body.comment
80 expect(comment.inReplyToCommentId).to.be.null
81 expect(comment.text).equal('my super first comment')
82 expect(comment.videoId).to.equal(videoId)
83 expect(comment.id).to.equal(comment.threadId)
84 expect(comment.account.name).to.equal('root')
85 expect(comment.account.host).to.equal('localhost:' + server.port)
86 expect(comment.account.url).to.equal('http://localhost:' + server.port + '/accounts/root')
87 expect(comment.totalReplies).to.equal(0)
88 expect(comment.totalRepliesFromVideoAuthor).to.equal(0)
89 expect(dateIsValid(comment.createdAt as string)).to.be.true
90 expect(dateIsValid(comment.updatedAt as string)).to.be.true
93 it('Should list threads of this video', async function () {
94 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
96 expect(res.body.total).to.equal(1)
97 expect(res.body.data).to.be.an('array')
98 expect(res.body.data).to.have.lengthOf(1)
100 const comment: VideoComment = res.body.data[0]
101 expect(comment.inReplyToCommentId).to.be.null
102 expect(comment.text).equal('my super first comment')
103 expect(comment.videoId).to.equal(videoId)
104 expect(comment.id).to.equal(comment.threadId)
105 expect(comment.account.name).to.equal('root')
106 expect(comment.account.host).to.equal('localhost:' + server.port)
108 await testImage(server.url, 'avatar-resized', comment.account.avatar.path, '.png')
110 expect(comment.totalReplies).to.equal(0)
111 expect(comment.totalRepliesFromVideoAuthor).to.equal(0)
112 expect(dateIsValid(comment.createdAt as string)).to.be.true
113 expect(dateIsValid(comment.updatedAt as string)).to.be.true
115 threadId = comment.threadId
118 it('Should get all the thread created', async function () {
119 const res = await getVideoThreadComments(server.url, videoUUID, threadId)
121 const rootComment = res.body.comment
122 expect(rootComment.inReplyToCommentId).to.be.null
123 expect(rootComment.text).equal('my super first comment')
124 expect(rootComment.videoId).to.equal(videoId)
125 expect(dateIsValid(rootComment.createdAt as string)).to.be.true
126 expect(dateIsValid(rootComment.updatedAt as string)).to.be.true
129 it('Should create multiple replies in this thread', async function () {
130 const text1 = 'my super answer to thread 1'
131 const childCommentRes = await addVideoCommentReply(server.url, server.accessToken, videoId, threadId, text1)
132 const childCommentId = childCommentRes.body.comment.id
134 const text2 = 'my super answer to answer of thread 1'
135 await addVideoCommentReply(server.url, server.accessToken, videoId, childCommentId, text2)
137 const text3 = 'my second answer to thread 1'
138 await addVideoCommentReply(server.url, server.accessToken, videoId, threadId, text3)
141 it('Should get correctly the replies', async function () {
142 const res = await getVideoThreadComments(server.url, videoUUID, threadId)
144 const tree: VideoCommentThreadTree = res.body
145 expect(tree.comment.text).equal('my super first comment')
146 expect(tree.children).to.have.lengthOf(2)
148 const firstChild = tree.children[0]
149 expect(firstChild.comment.text).to.equal('my super answer to thread 1')
150 expect(firstChild.children).to.have.lengthOf(1)
152 const childOfFirstChild = firstChild.children[0]
153 expect(childOfFirstChild.comment.text).to.equal('my super answer to answer of thread 1')
154 expect(childOfFirstChild.children).to.have.lengthOf(0)
156 const secondChild = tree.children[1]
157 expect(secondChild.comment.text).to.equal('my second answer to thread 1')
158 expect(secondChild.children).to.have.lengthOf(0)
160 replyToDeleteId = secondChild.comment.id
163 it('Should create other threads', async function () {
164 const text1 = 'super thread 2'
165 await addVideoCommentThread(server.url, server.accessToken, videoUUID, text1)
167 const text2 = 'super thread 3'
168 await addVideoCommentThread(server.url, server.accessToken, videoUUID, text2)
171 it('Should list the threads', async function () {
172 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5, 'createdAt')
174 expect(res.body.total).to.equal(3)
175 expect(res.body.data).to.be.an('array')
176 expect(res.body.data).to.have.lengthOf(3)
178 expect(res.body.data[0].text).to.equal('my super first comment')
179 expect(res.body.data[0].totalReplies).to.equal(3)
180 expect(res.body.data[1].text).to.equal('super thread 2')
181 expect(res.body.data[1].totalReplies).to.equal(0)
182 expect(res.body.data[2].text).to.equal('super thread 3')
183 expect(res.body.data[2].totalReplies).to.equal(0)
186 it('Should delete a reply', async function () {
187 await deleteVideoComment(server.url, server.accessToken, videoId, replyToDeleteId)
189 const res = await getVideoThreadComments(server.url, videoUUID, threadId)
191 const tree: VideoCommentThreadTree = res.body
192 expect(tree.comment.text).equal('my super first comment')
193 expect(tree.children).to.have.lengthOf(2)
195 const firstChild = tree.children[0]
196 expect(firstChild.comment.text).to.equal('my super answer to thread 1')
197 expect(firstChild.children).to.have.lengthOf(1)
199 const childOfFirstChild = firstChild.children[0]
200 expect(childOfFirstChild.comment.text).to.equal('my super answer to answer of thread 1')
201 expect(childOfFirstChild.children).to.have.lengthOf(0)
203 const deletedChildOfFirstChild = tree.children[1]
204 expect(deletedChildOfFirstChild.comment.text).to.equal('')
205 expect(deletedChildOfFirstChild.comment.isDeleted).to.be.true
206 expect(deletedChildOfFirstChild.comment.deletedAt).to.not.be.null
207 expect(deletedChildOfFirstChild.comment.account).to.be.null
208 expect(deletedChildOfFirstChild.children).to.have.lengthOf(0)
211 it('Should delete a complete thread', async function () {
212 await deleteVideoComment(server.url, server.accessToken, videoId, threadId)
214 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5, 'createdAt')
215 expect(res.body.total).to.equal(3)
216 expect(res.body.data).to.be.an('array')
217 expect(res.body.data).to.have.lengthOf(3)
219 expect(res.body.data[0].text).to.equal('')
220 expect(res.body.data[0].isDeleted).to.be.true
221 expect(res.body.data[0].deletedAt).to.not.be.null
222 expect(res.body.data[0].account).to.be.null
223 expect(res.body.data[0].totalReplies).to.equal(3)
224 expect(res.body.data[1].text).to.equal('super thread 2')
225 expect(res.body.data[1].totalReplies).to.equal(0)
226 expect(res.body.data[2].text).to.equal('super thread 3')
227 expect(res.body.data[2].totalReplies).to.equal(0)
230 it('Should count replies from the video author correctly', async function () {
231 const text = 'my super first comment'
232 await addVideoCommentThread(server.url, server.accessToken, videoUUID, text)
233 let res = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
234 const comment: VideoComment = res.body.data[0]
235 const threadId2 = comment.threadId
237 const text2 = 'a first answer to thread 4 by a third party'
238 await addVideoCommentReply(server.url, userAccessTokenServer1, videoId, threadId2, text2)
240 const text3 = 'my second answer to thread 4'
241 await addVideoCommentReply(server.url, server.accessToken, videoId, threadId2, text3)
243 res = await getVideoThreadComments(server.url, videoUUID, threadId2)
244 const tree: VideoCommentThreadTree = res.body
245 expect(tree.comment.totalReplies).to.equal(tree.comment.totalRepliesFromVideoAuthor + 1)
249 describe('All instance comments', function () {
250 async function getComments (options: any = {}) {
251 const res = await getAdminVideoComments(Object.assign({
253 token: server.accessToken,
258 return { comments: res.body.data as VideoCommentAdmin[], total: res.body.total as number }
261 it('Should list instance comments as admin', async function () {
262 const { comments } = await getComments({ start: 0, count: 1 })
264 expect(comments[0].text).to.equal('my second answer to thread 4')
267 it('Should filter instance comments by isLocal', async function () {
268 const { total, comments } = await getComments({ isLocal: false })
270 expect(comments).to.have.lengthOf(0)
271 expect(total).to.equal(0)
274 it('Should search instance comments by account', async function () {
275 const { total, comments } = await getComments({ searchAccount: 'user' })
277 expect(comments).to.have.lengthOf(1)
278 expect(total).to.equal(1)
280 expect(comments[0].text).to.equal('a first answer to thread 4 by a third party')
283 it('Should search instance comments by video', async function () {
285 const { total, comments } = await getComments({ searchVideo: 'video' })
287 expect(comments).to.have.lengthOf(7)
288 expect(total).to.equal(7)
292 const { total, comments } = await getComments({ searchVideo: 'hello' })
294 expect(comments).to.have.lengthOf(0)
295 expect(total).to.equal(0)
299 it('Should search instance comments', async function () {
300 const { total, comments } = await getComments({ search: 'super thread 3' })
302 expect(comments).to.have.lengthOf(1)
303 expect(total).to.equal(1)
304 expect(comments[0].text).to.equal('super thread 3')
308 after(async function () {
309 await cleanupTests([ server ])