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'
6 import { cleanupTests, CommentsCommand, createSingleServer, PeerTubeServer, setAccessTokensToServers } from '@shared/server-commands'
8 const expect = chai.expect
10 describe('Test video comments', function () {
11 let server: PeerTubeServer
15 let replyToDeleteId: number
17 let userAccessTokenServer1: string
19 let command: CommentsCommand
21 before(async function () {
24 server = await createSingleServer(1)
26 await setAccessTokensToServers([ server ])
28 const { id, uuid } = await server.videos.upload()
32 await server.users.updateMyAvatar({ fixture: 'avatar.png' })
34 userAccessTokenServer1 = await server.users.generateUserAndToken('user1')
36 command = server.comments
39 describe('User comments', function () {
41 it('Should not have threads on this video', async function () {
42 const body = await command.listThreads({ videoId: videoUUID })
44 expect(body.total).to.equal(0)
45 expect(body.totalNotDeletedComments).to.equal(0)
46 expect(body.data).to.be.an('array')
47 expect(body.data).to.have.lengthOf(0)
50 it('Should create a thread in this video', async function () {
51 const text = 'my super first comment'
53 const comment = await command.createThread({ videoId: videoUUID, text })
55 expect(comment.inReplyToCommentId).to.be.null
56 expect(comment.text).equal('my super first comment')
57 expect(comment.videoId).to.equal(videoId)
58 expect(comment.id).to.equal(comment.threadId)
59 expect(comment.account.name).to.equal('root')
60 expect(comment.account.host).to.equal('localhost:' + server.port)
61 expect(comment.account.url).to.equal('http://localhost:' + server.port + '/accounts/root')
62 expect(comment.totalReplies).to.equal(0)
63 expect(comment.totalRepliesFromVideoAuthor).to.equal(0)
64 expect(dateIsValid(comment.createdAt as string)).to.be.true
65 expect(dateIsValid(comment.updatedAt as string)).to.be.true
68 it('Should list threads of this video', async function () {
69 const body = await command.listThreads({ videoId: videoUUID })
71 expect(body.total).to.equal(1)
72 expect(body.totalNotDeletedComments).to.equal(1)
73 expect(body.data).to.be.an('array')
74 expect(body.data).to.have.lengthOf(1)
76 const comment = body.data[0]
77 expect(comment.inReplyToCommentId).to.be.null
78 expect(comment.text).equal('my super first comment')
79 expect(comment.videoId).to.equal(videoId)
80 expect(comment.id).to.equal(comment.threadId)
81 expect(comment.account.name).to.equal('root')
82 expect(comment.account.host).to.equal('localhost:' + server.port)
84 await testImage(server.url, 'avatar-resized', comment.account.avatar.path, '.png')
86 expect(comment.totalReplies).to.equal(0)
87 expect(comment.totalRepliesFromVideoAuthor).to.equal(0)
88 expect(dateIsValid(comment.createdAt as string)).to.be.true
89 expect(dateIsValid(comment.updatedAt as string)).to.be.true
91 threadId = comment.threadId
94 it('Should get all the thread created', async function () {
95 const body = await command.getThread({ videoId: videoUUID, threadId })
97 const rootComment = body.comment
98 expect(rootComment.inReplyToCommentId).to.be.null
99 expect(rootComment.text).equal('my super first comment')
100 expect(rootComment.videoId).to.equal(videoId)
101 expect(dateIsValid(rootComment.createdAt as string)).to.be.true
102 expect(dateIsValid(rootComment.updatedAt as string)).to.be.true
105 it('Should create multiple replies in this thread', async function () {
106 const text1 = 'my super answer to thread 1'
107 const created = await command.addReply({ videoId, toCommentId: threadId, text: text1 })
108 const childCommentId = created.id
110 const text2 = 'my super answer to answer of thread 1'
111 await command.addReply({ videoId, toCommentId: childCommentId, text: text2 })
113 const text3 = 'my second answer to thread 1'
114 await command.addReply({ videoId, toCommentId: threadId, text: text3 })
117 it('Should get correctly the replies', async function () {
118 const tree = await command.getThread({ videoId: videoUUID, threadId })
120 expect(tree.comment.text).equal('my super first comment')
121 expect(tree.children).to.have.lengthOf(2)
123 const firstChild = tree.children[0]
124 expect(firstChild.comment.text).to.equal('my super answer to thread 1')
125 expect(firstChild.children).to.have.lengthOf(1)
127 const childOfFirstChild = firstChild.children[0]
128 expect(childOfFirstChild.comment.text).to.equal('my super answer to answer of thread 1')
129 expect(childOfFirstChild.children).to.have.lengthOf(0)
131 const secondChild = tree.children[1]
132 expect(secondChild.comment.text).to.equal('my second answer to thread 1')
133 expect(secondChild.children).to.have.lengthOf(0)
135 replyToDeleteId = secondChild.comment.id
138 it('Should create other threads', async function () {
139 const text1 = 'super thread 2'
140 await command.createThread({ videoId: videoUUID, text: text1 })
142 const text2 = 'super thread 3'
143 await command.createThread({ videoId: videoUUID, text: text2 })
146 it('Should list the threads', async function () {
147 const body = await command.listThreads({ videoId: videoUUID, sort: 'createdAt' })
149 expect(body.total).to.equal(3)
150 expect(body.totalNotDeletedComments).to.equal(6)
151 expect(body.data).to.be.an('array')
152 expect(body.data).to.have.lengthOf(3)
154 expect(body.data[0].text).to.equal('my super first comment')
155 expect(body.data[0].totalReplies).to.equal(3)
156 expect(body.data[1].text).to.equal('super thread 2')
157 expect(body.data[1].totalReplies).to.equal(0)
158 expect(body.data[2].text).to.equal('super thread 3')
159 expect(body.data[2].totalReplies).to.equal(0)
162 it('Should delete a reply', async function () {
163 await command.delete({ videoId, commentId: replyToDeleteId })
166 const body = await command.listThreads({ videoId: videoUUID, sort: 'createdAt' })
168 expect(body.total).to.equal(3)
169 expect(body.totalNotDeletedComments).to.equal(5)
173 const tree = await command.getThread({ videoId: videoUUID, threadId })
175 expect(tree.comment.text).equal('my super first comment')
176 expect(tree.children).to.have.lengthOf(2)
178 const firstChild = tree.children[0]
179 expect(firstChild.comment.text).to.equal('my super answer to thread 1')
180 expect(firstChild.children).to.have.lengthOf(1)
182 const childOfFirstChild = firstChild.children[0]
183 expect(childOfFirstChild.comment.text).to.equal('my super answer to answer of thread 1')
184 expect(childOfFirstChild.children).to.have.lengthOf(0)
186 const deletedChildOfFirstChild = tree.children[1]
187 expect(deletedChildOfFirstChild.comment.text).to.equal('')
188 expect(deletedChildOfFirstChild.comment.isDeleted).to.be.true
189 expect(deletedChildOfFirstChild.comment.deletedAt).to.not.be.null
190 expect(deletedChildOfFirstChild.comment.account).to.be.null
191 expect(deletedChildOfFirstChild.children).to.have.lengthOf(0)
195 it('Should delete a complete thread', async function () {
196 await command.delete({ videoId, commentId: threadId })
198 const body = await command.listThreads({ videoId: videoUUID, sort: 'createdAt' })
199 expect(body.total).to.equal(3)
200 expect(body.data).to.be.an('array')
201 expect(body.data).to.have.lengthOf(3)
203 expect(body.data[0].text).to.equal('')
204 expect(body.data[0].isDeleted).to.be.true
205 expect(body.data[0].deletedAt).to.not.be.null
206 expect(body.data[0].account).to.be.null
207 expect(body.data[0].totalReplies).to.equal(2)
208 expect(body.data[1].text).to.equal('super thread 2')
209 expect(body.data[1].totalReplies).to.equal(0)
210 expect(body.data[2].text).to.equal('super thread 3')
211 expect(body.data[2].totalReplies).to.equal(0)
214 it('Should count replies from the video author correctly', async function () {
215 await command.createThread({ videoId: videoUUID, text: 'my super first comment' })
217 const { data } = await command.listThreads({ videoId: videoUUID })
218 const threadId2 = data[0].threadId
220 const text2 = 'a first answer to thread 4 by a third party'
221 await command.addReply({ token: userAccessTokenServer1, videoId, toCommentId: threadId2, text: text2 })
223 const text3 = 'my second answer to thread 4'
224 await command.addReply({ videoId, toCommentId: threadId2, text: text3 })
226 const tree = await command.getThread({ videoId: videoUUID, threadId: threadId2 })
227 expect(tree.comment.totalReplies).to.equal(tree.comment.totalRepliesFromVideoAuthor + 1)
231 describe('All instance comments', function () {
233 it('Should list instance comments as admin', async function () {
234 const { data } = await command.listForAdmin({ start: 0, count: 1 })
236 expect(data[0].text).to.equal('my second answer to thread 4')
239 it('Should filter instance comments by isLocal', async function () {
240 const { total, data } = await command.listForAdmin({ isLocal: false })
242 expect(data).to.have.lengthOf(0)
243 expect(total).to.equal(0)
246 it('Should search instance comments by account', async function () {
247 const { total, data } = await command.listForAdmin({ searchAccount: 'user' })
249 expect(data).to.have.lengthOf(1)
250 expect(total).to.equal(1)
252 expect(data[0].text).to.equal('a first answer to thread 4 by a third party')
255 it('Should search instance comments by video', async function () {
257 const { total, data } = await command.listForAdmin({ searchVideo: 'video' })
259 expect(data).to.have.lengthOf(7)
260 expect(total).to.equal(7)
264 const { total, data } = await command.listForAdmin({ searchVideo: 'hello' })
266 expect(data).to.have.lengthOf(0)
267 expect(total).to.equal(0)
271 it('Should search instance comments', async function () {
272 const { total, data } = await command.listForAdmin({ search: 'super thread 3' })
274 expect(total).to.equal(1)
276 expect(data).to.have.lengthOf(1)
277 expect(data[0].text).to.equal('super thread 3')
281 after(async function () {
282 await cleanupTests([ server ])