1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { dateIsValid, testImage } from '@server/tests/shared'
11 setAccessTokensToServers,
12 setDefaultAccountAvatar,
13 setDefaultChannelAvatar
14 } from '@shared/server-commands'
16 const expect = chai.expect
18 describe('Test video comments', function () {
19 let server: PeerTubeServer
23 let replyToDeleteId: number
25 let userAccessTokenServer1: string
27 let command: CommentsCommand
29 before(async function () {
32 server = await createSingleServer(1)
34 await setAccessTokensToServers([ server ])
36 const { id, uuid } = await server.videos.upload()
40 await setDefaultChannelAvatar(server)
41 await setDefaultAccountAvatar(server)
43 userAccessTokenServer1 = await server.users.generateUserAndToken('user1')
45 command = server.comments
48 describe('User comments', function () {
50 it('Should not have threads on this video', async function () {
51 const body = await command.listThreads({ videoId: videoUUID })
53 expect(body.total).to.equal(0)
54 expect(body.totalNotDeletedComments).to.equal(0)
55 expect(body.data).to.be.an('array')
56 expect(body.data).to.have.lengthOf(0)
59 it('Should create a thread in this video', async function () {
60 const text = 'my super first comment'
62 const comment = await command.createThread({ videoId: videoUUID, text })
64 expect(comment.inReplyToCommentId).to.be.null
65 expect(comment.text).equal('my super first comment')
66 expect(comment.videoId).to.equal(videoId)
67 expect(comment.id).to.equal(comment.threadId)
68 expect(comment.account.name).to.equal('root')
69 expect(comment.account.host).to.equal('localhost:' + server.port)
70 expect(comment.account.url).to.equal('http://localhost:' + server.port + '/accounts/root')
71 expect(comment.totalReplies).to.equal(0)
72 expect(comment.totalRepliesFromVideoAuthor).to.equal(0)
73 expect(dateIsValid(comment.createdAt as string)).to.be.true
74 expect(dateIsValid(comment.updatedAt as string)).to.be.true
77 it('Should list threads of this video', async function () {
78 const body = await command.listThreads({ videoId: videoUUID })
80 expect(body.total).to.equal(1)
81 expect(body.totalNotDeletedComments).to.equal(1)
82 expect(body.data).to.be.an('array')
83 expect(body.data).to.have.lengthOf(1)
85 const comment = body.data[0]
86 expect(comment.inReplyToCommentId).to.be.null
87 expect(comment.text).equal('my super first comment')
88 expect(comment.videoId).to.equal(videoId)
89 expect(comment.id).to.equal(comment.threadId)
90 expect(comment.account.name).to.equal('root')
91 expect(comment.account.host).to.equal('localhost:' + server.port)
93 for (const avatar of comment.account.avatars) {
94 await testImage(server.url, `avatar-resized-${avatar.width}x${avatar.width}`, avatar.path, '.png')
97 expect(comment.totalReplies).to.equal(0)
98 expect(comment.totalRepliesFromVideoAuthor).to.equal(0)
99 expect(dateIsValid(comment.createdAt as string)).to.be.true
100 expect(dateIsValid(comment.updatedAt as string)).to.be.true
102 threadId = comment.threadId
105 it('Should get all the thread created', async function () {
106 const body = await command.getThread({ videoId: videoUUID, threadId })
108 const rootComment = body.comment
109 expect(rootComment.inReplyToCommentId).to.be.null
110 expect(rootComment.text).equal('my super first comment')
111 expect(rootComment.videoId).to.equal(videoId)
112 expect(dateIsValid(rootComment.createdAt as string)).to.be.true
113 expect(dateIsValid(rootComment.updatedAt as string)).to.be.true
116 it('Should create multiple replies in this thread', async function () {
117 const text1 = 'my super answer to thread 1'
118 const created = await command.addReply({ videoId, toCommentId: threadId, text: text1 })
119 const childCommentId = created.id
121 const text2 = 'my super answer to answer of thread 1'
122 await command.addReply({ videoId, toCommentId: childCommentId, text: text2 })
124 const text3 = 'my second answer to thread 1'
125 await command.addReply({ videoId, toCommentId: threadId, text: text3 })
128 it('Should get correctly the replies', async function () {
129 const tree = await command.getThread({ videoId: videoUUID, threadId })
131 expect(tree.comment.text).equal('my super first comment')
132 expect(tree.children).to.have.lengthOf(2)
134 const firstChild = tree.children[0]
135 expect(firstChild.comment.text).to.equal('my super answer to thread 1')
136 expect(firstChild.children).to.have.lengthOf(1)
138 const childOfFirstChild = firstChild.children[0]
139 expect(childOfFirstChild.comment.text).to.equal('my super answer to answer of thread 1')
140 expect(childOfFirstChild.children).to.have.lengthOf(0)
142 const secondChild = tree.children[1]
143 expect(secondChild.comment.text).to.equal('my second answer to thread 1')
144 expect(secondChild.children).to.have.lengthOf(0)
146 replyToDeleteId = secondChild.comment.id
149 it('Should create other threads', async function () {
150 const text1 = 'super thread 2'
151 await command.createThread({ videoId: videoUUID, text: text1 })
153 const text2 = 'super thread 3'
154 await command.createThread({ videoId: videoUUID, text: text2 })
157 it('Should list the threads', async function () {
158 const body = await command.listThreads({ videoId: videoUUID, sort: 'createdAt' })
160 expect(body.total).to.equal(3)
161 expect(body.totalNotDeletedComments).to.equal(6)
162 expect(body.data).to.be.an('array')
163 expect(body.data).to.have.lengthOf(3)
165 expect(body.data[0].text).to.equal('my super first comment')
166 expect(body.data[0].totalReplies).to.equal(3)
167 expect(body.data[1].text).to.equal('super thread 2')
168 expect(body.data[1].totalReplies).to.equal(0)
169 expect(body.data[2].text).to.equal('super thread 3')
170 expect(body.data[2].totalReplies).to.equal(0)
173 it('Should delete a reply', async function () {
174 await command.delete({ videoId, commentId: replyToDeleteId })
177 const body = await command.listThreads({ videoId: videoUUID, sort: 'createdAt' })
179 expect(body.total).to.equal(3)
180 expect(body.totalNotDeletedComments).to.equal(5)
184 const tree = await command.getThread({ videoId: videoUUID, threadId })
186 expect(tree.comment.text).equal('my super first comment')
187 expect(tree.children).to.have.lengthOf(2)
189 const firstChild = tree.children[0]
190 expect(firstChild.comment.text).to.equal('my super answer to thread 1')
191 expect(firstChild.children).to.have.lengthOf(1)
193 const childOfFirstChild = firstChild.children[0]
194 expect(childOfFirstChild.comment.text).to.equal('my super answer to answer of thread 1')
195 expect(childOfFirstChild.children).to.have.lengthOf(0)
197 const deletedChildOfFirstChild = tree.children[1]
198 expect(deletedChildOfFirstChild.comment.text).to.equal('')
199 expect(deletedChildOfFirstChild.comment.isDeleted).to.be.true
200 expect(deletedChildOfFirstChild.comment.deletedAt).to.not.be.null
201 expect(deletedChildOfFirstChild.comment.account).to.be.null
202 expect(deletedChildOfFirstChild.children).to.have.lengthOf(0)
206 it('Should delete a complete thread', async function () {
207 await command.delete({ videoId, commentId: threadId })
209 const body = await command.listThreads({ videoId: videoUUID, sort: 'createdAt' })
210 expect(body.total).to.equal(3)
211 expect(body.data).to.be.an('array')
212 expect(body.data).to.have.lengthOf(3)
214 expect(body.data[0].text).to.equal('')
215 expect(body.data[0].isDeleted).to.be.true
216 expect(body.data[0].deletedAt).to.not.be.null
217 expect(body.data[0].account).to.be.null
218 expect(body.data[0].totalReplies).to.equal(2)
219 expect(body.data[1].text).to.equal('super thread 2')
220 expect(body.data[1].totalReplies).to.equal(0)
221 expect(body.data[2].text).to.equal('super thread 3')
222 expect(body.data[2].totalReplies).to.equal(0)
225 it('Should count replies from the video author correctly', async function () {
226 await command.createThread({ videoId: videoUUID, text: 'my super first comment' })
228 const { data } = await command.listThreads({ videoId: videoUUID })
229 const threadId2 = data[0].threadId
231 const text2 = 'a first answer to thread 4 by a third party'
232 await command.addReply({ token: userAccessTokenServer1, videoId, toCommentId: threadId2, text: text2 })
234 const text3 = 'my second answer to thread 4'
235 await command.addReply({ videoId, toCommentId: threadId2, text: text3 })
237 const tree = await command.getThread({ videoId: videoUUID, threadId: threadId2 })
238 expect(tree.comment.totalReplies).to.equal(tree.comment.totalRepliesFromVideoAuthor + 1)
242 describe('All instance comments', function () {
244 it('Should list instance comments as admin', async function () {
245 const { data } = await command.listForAdmin({ start: 0, count: 1 })
247 expect(data[0].text).to.equal('my second answer to thread 4')
250 it('Should filter instance comments by isLocal', async function () {
251 const { total, data } = await command.listForAdmin({ isLocal: false })
253 expect(data).to.have.lengthOf(0)
254 expect(total).to.equal(0)
257 it('Should search instance comments by account', async function () {
258 const { total, data } = await command.listForAdmin({ searchAccount: 'user' })
260 expect(data).to.have.lengthOf(1)
261 expect(total).to.equal(1)
263 expect(data[0].text).to.equal('a first answer to thread 4 by a third party')
266 it('Should search instance comments by video', async function () {
268 const { total, data } = await command.listForAdmin({ searchVideo: 'video' })
270 expect(data).to.have.lengthOf(7)
271 expect(total).to.equal(7)
275 const { total, data } = await command.listForAdmin({ searchVideo: 'hello' })
277 expect(data).to.have.lengthOf(0)
278 expect(total).to.equal(0)
282 it('Should search instance comments', async function () {
283 const { total, data } = await command.listForAdmin({ search: 'super thread 3' })
285 expect(total).to.equal(1)
287 expect(data).to.have.lengthOf(1)
288 expect(data[0].text).to.equal('super thread 3')
292 after(async function () {
293 await cleanupTests([ server ])