1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
11 setAccessTokensToServers,
13 } from '@shared/extra-utils'
15 const expect = chai.expect
17 describe('Test video comments', function () {
18 let server: PeerTubeServer
22 let replyToDeleteId: number
24 let userAccessTokenServer1: string
26 let command: CommentsCommand
28 before(async function () {
31 server = await createSingleServer(1)
33 await setAccessTokensToServers([ server ])
35 const { id, uuid } = await server.videos.upload()
39 await server.users.updateMyAvatar({ fixture: 'avatar.png' })
41 userAccessTokenServer1 = await server.users.generateUserAndToken('user1')
43 command = server.comments
46 describe('User comments', function () {
48 it('Should not have threads on this video', async function () {
49 const body = await command.listThreads({ videoId: videoUUID })
51 expect(body.total).to.equal(0)
52 expect(body.totalNotDeletedComments).to.equal(0)
53 expect(body.data).to.be.an('array')
54 expect(body.data).to.have.lengthOf(0)
57 it('Should create a thread in this video', async function () {
58 const text = 'my super first comment'
60 const comment = await command.createThread({ videoId: videoUUID, text })
62 expect(comment.inReplyToCommentId).to.be.null
63 expect(comment.text).equal('my super first comment')
64 expect(comment.videoId).to.equal(videoId)
65 expect(comment.id).to.equal(comment.threadId)
66 expect(comment.account.name).to.equal('root')
67 expect(comment.account.host).to.equal('localhost:' + server.port)
68 expect(comment.account.url).to.equal('http://localhost:' + server.port + '/accounts/root')
69 expect(comment.totalReplies).to.equal(0)
70 expect(comment.totalRepliesFromVideoAuthor).to.equal(0)
71 expect(dateIsValid(comment.createdAt as string)).to.be.true
72 expect(dateIsValid(comment.updatedAt as string)).to.be.true
75 it('Should list threads of this video', async function () {
76 const body = await command.listThreads({ videoId: videoUUID })
78 expect(body.total).to.equal(1)
79 expect(body.totalNotDeletedComments).to.equal(1)
80 expect(body.data).to.be.an('array')
81 expect(body.data).to.have.lengthOf(1)
83 const comment = body.data[0]
84 expect(comment.inReplyToCommentId).to.be.null
85 expect(comment.text).equal('my super first comment')
86 expect(comment.videoId).to.equal(videoId)
87 expect(comment.id).to.equal(comment.threadId)
88 expect(comment.account.name).to.equal('root')
89 expect(comment.account.host).to.equal('localhost:' + server.port)
91 await testImage(server.url, 'avatar-resized', comment.account.avatar.path, '.png')
93 expect(comment.totalReplies).to.equal(0)
94 expect(comment.totalRepliesFromVideoAuthor).to.equal(0)
95 expect(dateIsValid(comment.createdAt as string)).to.be.true
96 expect(dateIsValid(comment.updatedAt as string)).to.be.true
98 threadId = comment.threadId
101 it('Should get all the thread created', async function () {
102 const body = await command.getThread({ videoId: videoUUID, threadId })
104 const rootComment = body.comment
105 expect(rootComment.inReplyToCommentId).to.be.null
106 expect(rootComment.text).equal('my super first comment')
107 expect(rootComment.videoId).to.equal(videoId)
108 expect(dateIsValid(rootComment.createdAt as string)).to.be.true
109 expect(dateIsValid(rootComment.updatedAt as string)).to.be.true
112 it('Should create multiple replies in this thread', async function () {
113 const text1 = 'my super answer to thread 1'
114 const created = await command.addReply({ videoId, toCommentId: threadId, text: text1 })
115 const childCommentId = created.id
117 const text2 = 'my super answer to answer of thread 1'
118 await command.addReply({ videoId, toCommentId: childCommentId, text: text2 })
120 const text3 = 'my second answer to thread 1'
121 await command.addReply({ videoId, toCommentId: threadId, text: text3 })
124 it('Should get correctly the replies', async function () {
125 const tree = await command.getThread({ videoId: videoUUID, threadId })
127 expect(tree.comment.text).equal('my super first comment')
128 expect(tree.children).to.have.lengthOf(2)
130 const firstChild = tree.children[0]
131 expect(firstChild.comment.text).to.equal('my super answer to thread 1')
132 expect(firstChild.children).to.have.lengthOf(1)
134 const childOfFirstChild = firstChild.children[0]
135 expect(childOfFirstChild.comment.text).to.equal('my super answer to answer of thread 1')
136 expect(childOfFirstChild.children).to.have.lengthOf(0)
138 const secondChild = tree.children[1]
139 expect(secondChild.comment.text).to.equal('my second answer to thread 1')
140 expect(secondChild.children).to.have.lengthOf(0)
142 replyToDeleteId = secondChild.comment.id
145 it('Should create other threads', async function () {
146 const text1 = 'super thread 2'
147 await command.createThread({ videoId: videoUUID, text: text1 })
149 const text2 = 'super thread 3'
150 await command.createThread({ videoId: videoUUID, text: text2 })
153 it('Should list the threads', async function () {
154 const body = await command.listThreads({ videoId: videoUUID, sort: 'createdAt' })
156 expect(body.total).to.equal(3)
157 expect(body.totalNotDeletedComments).to.equal(6)
158 expect(body.data).to.be.an('array')
159 expect(body.data).to.have.lengthOf(3)
161 expect(body.data[0].text).to.equal('my super first comment')
162 expect(body.data[0].totalReplies).to.equal(3)
163 expect(body.data[1].text).to.equal('super thread 2')
164 expect(body.data[1].totalReplies).to.equal(0)
165 expect(body.data[2].text).to.equal('super thread 3')
166 expect(body.data[2].totalReplies).to.equal(0)
169 it('Should delete a reply', async function () {
170 await command.delete({ videoId, commentId: replyToDeleteId })
173 const body = await command.listThreads({ videoId: videoUUID, sort: 'createdAt' })
175 expect(body.total).to.equal(3)
176 expect(body.totalNotDeletedComments).to.equal(5)
180 const tree = await command.getThread({ videoId: videoUUID, threadId })
182 expect(tree.comment.text).equal('my super first comment')
183 expect(tree.children).to.have.lengthOf(2)
185 const firstChild = tree.children[0]
186 expect(firstChild.comment.text).to.equal('my super answer to thread 1')
187 expect(firstChild.children).to.have.lengthOf(1)
189 const childOfFirstChild = firstChild.children[0]
190 expect(childOfFirstChild.comment.text).to.equal('my super answer to answer of thread 1')
191 expect(childOfFirstChild.children).to.have.lengthOf(0)
193 const deletedChildOfFirstChild = tree.children[1]
194 expect(deletedChildOfFirstChild.comment.text).to.equal('')
195 expect(deletedChildOfFirstChild.comment.isDeleted).to.be.true
196 expect(deletedChildOfFirstChild.comment.deletedAt).to.not.be.null
197 expect(deletedChildOfFirstChild.comment.account).to.be.null
198 expect(deletedChildOfFirstChild.children).to.have.lengthOf(0)
202 it('Should delete a complete thread', async function () {
203 await command.delete({ videoId, commentId: threadId })
205 const body = await command.listThreads({ videoId: videoUUID, sort: 'createdAt' })
206 expect(body.total).to.equal(3)
207 expect(body.data).to.be.an('array')
208 expect(body.data).to.have.lengthOf(3)
210 expect(body.data[0].text).to.equal('')
211 expect(body.data[0].isDeleted).to.be.true
212 expect(body.data[0].deletedAt).to.not.be.null
213 expect(body.data[0].account).to.be.null
214 expect(body.data[0].totalReplies).to.equal(2)
215 expect(body.data[1].text).to.equal('super thread 2')
216 expect(body.data[1].totalReplies).to.equal(0)
217 expect(body.data[2].text).to.equal('super thread 3')
218 expect(body.data[2].totalReplies).to.equal(0)
221 it('Should count replies from the video author correctly', async function () {
222 await command.createThread({ videoId: videoUUID, text: 'my super first comment' })
224 const { data } = await command.listThreads({ videoId: videoUUID })
225 const threadId2 = data[0].threadId
227 const text2 = 'a first answer to thread 4 by a third party'
228 await command.addReply({ token: userAccessTokenServer1, videoId, toCommentId: threadId2, text: text2 })
230 const text3 = 'my second answer to thread 4'
231 await command.addReply({ videoId, toCommentId: threadId2, text: text3 })
233 const tree = await command.getThread({ videoId: videoUUID, threadId: threadId2 })
234 expect(tree.comment.totalReplies).to.equal(tree.comment.totalRepliesFromVideoAuthor + 1)
238 describe('All instance comments', function () {
240 it('Should list instance comments as admin', async function () {
241 const { data } = await command.listForAdmin({ start: 0, count: 1 })
243 expect(data[0].text).to.equal('my second answer to thread 4')
246 it('Should filter instance comments by isLocal', async function () {
247 const { total, data } = await command.listForAdmin({ isLocal: false })
249 expect(data).to.have.lengthOf(0)
250 expect(total).to.equal(0)
253 it('Should search instance comments by account', async function () {
254 const { total, data } = await command.listForAdmin({ searchAccount: 'user' })
256 expect(data).to.have.lengthOf(1)
257 expect(total).to.equal(1)
259 expect(data[0].text).to.equal('a first answer to thread 4 by a third party')
262 it('Should search instance comments by video', async function () {
264 const { total, data } = await command.listForAdmin({ searchVideo: 'video' })
266 expect(data).to.have.lengthOf(7)
267 expect(total).to.equal(7)
271 const { total, data } = await command.listForAdmin({ searchVideo: 'hello' })
273 expect(data).to.have.lengthOf(0)
274 expect(total).to.equal(0)
278 it('Should search instance comments', async function () {
279 const { total, data } = await command.listForAdmin({ search: 'super thread 3' })
281 expect(total).to.equal(1)
283 expect(data).to.have.lengthOf(1)
284 expect(data[0].text).to.equal('super thread 3')
288 after(async function () {
289 await cleanupTests([ server ])