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 { cleanupTests, testImage } from '../../../../shared/extra-utils'
11 setAccessTokensToServers,
14 } from '../../../../shared/extra-utils/index'
17 addVideoCommentThread,
19 getVideoCommentThreads,
20 getVideoThreadComments
21 } from '../../../../shared/extra-utils/videos/video-comments'
23 const expect = chai.expect
25 describe('Test video comments', function () {
26 let server: ServerInfo
30 let replyToDeleteId: number
32 before(async function () {
35 server = await flushAndRunServer(1)
37 await setAccessTokensToServers([ server ])
39 const res = await uploadVideo(server.url, server.accessToken, {})
40 videoUUID = res.body.video.uuid
41 videoId = res.body.video.id
43 await updateMyAvatar({
45 accessToken: server.accessToken,
50 it('Should not have threads on this video', async function () {
51 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
53 expect(res.body.total).to.equal(0)
54 expect(res.body.data).to.be.an('array')
55 expect(res.body.data).to.have.lengthOf(0)
58 it('Should create a thread in this video', async function () {
59 const text = 'my super first comment'
61 const res = await addVideoCommentThread(server.url, server.accessToken, videoUUID, text)
62 const comment = res.body.comment
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(dateIsValid(comment.createdAt as string)).to.be.true
73 expect(dateIsValid(comment.updatedAt as string)).to.be.true
76 it('Should list threads of this video', async function () {
77 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
79 expect(res.body.total).to.equal(1)
80 expect(res.body.data).to.be.an('array')
81 expect(res.body.data).to.have.lengthOf(1)
83 const comment: VideoComment = res.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(dateIsValid(comment.createdAt as string)).to.be.true
95 expect(dateIsValid(comment.updatedAt as string)).to.be.true
97 threadId = comment.threadId
100 it('Should get all the thread created', async function () {
101 const res = await getVideoThreadComments(server.url, videoUUID, threadId)
103 const rootComment = res.body.comment
104 expect(rootComment.inReplyToCommentId).to.be.null
105 expect(rootComment.text).equal('my super first comment')
106 expect(rootComment.videoId).to.equal(videoId)
107 expect(dateIsValid(rootComment.createdAt as string)).to.be.true
108 expect(dateIsValid(rootComment.updatedAt as string)).to.be.true
111 it('Should create multiple replies in this thread', async function () {
112 const text1 = 'my super answer to thread 1'
113 const childCommentRes = await addVideoCommentReply(server.url, server.accessToken, videoId, threadId, text1)
114 const childCommentId = childCommentRes.body.comment.id
116 const text2 = 'my super answer to answer of thread 1'
117 await addVideoCommentReply(server.url, server.accessToken, videoId, childCommentId, text2)
119 const text3 = 'my second answer to thread 1'
120 await addVideoCommentReply(server.url, server.accessToken, videoId, threadId, text3)
123 it('Should get correctly the replies', async function () {
124 const res = await getVideoThreadComments(server.url, videoUUID, threadId)
126 const tree: VideoCommentThreadTree = res.body
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 addVideoCommentThread(server.url, server.accessToken, videoUUID, text1)
149 const text2 = 'super thread 3'
150 await addVideoCommentThread(server.url, server.accessToken, videoUUID, text2)
153 it('Should list the threads', async function () {
154 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5, 'createdAt')
156 expect(res.body.total).to.equal(3)
157 expect(res.body.data).to.be.an('array')
158 expect(res.body.data).to.have.lengthOf(3)
160 expect(res.body.data[0].text).to.equal('my super first comment')
161 expect(res.body.data[0].totalReplies).to.equal(3)
162 expect(res.body.data[1].text).to.equal('super thread 2')
163 expect(res.body.data[1].totalReplies).to.equal(0)
164 expect(res.body.data[2].text).to.equal('super thread 3')
165 expect(res.body.data[2].totalReplies).to.equal(0)
168 it('Should delete a reply', async function () {
169 await deleteVideoComment(server.url, server.accessToken, videoId, replyToDeleteId)
171 const res = await getVideoThreadComments(server.url, videoUUID, threadId)
173 const tree: VideoCommentThreadTree = res.body
174 expect(tree.comment.text).equal('my super first comment')
175 expect(tree.children).to.have.lengthOf(1)
177 const firstChild = tree.children[0]
178 expect(firstChild.comment.text).to.equal('my super answer to thread 1')
179 expect(firstChild.children).to.have.lengthOf(1)
181 const childOfFirstChild = firstChild.children[0]
182 expect(childOfFirstChild.comment.text).to.equal('my super answer to answer of thread 1')
183 expect(childOfFirstChild.children).to.have.lengthOf(0)
186 it('Should delete a complete thread', async function () {
187 await deleteVideoComment(server.url, server.accessToken, videoId, threadId)
189 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5, 'createdAt')
190 expect(res.body.total).to.equal(2)
191 expect(res.body.data).to.be.an('array')
192 expect(res.body.data).to.have.lengthOf(2)
194 expect(res.body.data[0].text).to.equal('super thread 2')
195 expect(res.body.data[0].totalReplies).to.equal(0)
196 expect(res.body.data[1].text).to.equal('super thread 3')
197 expect(res.body.data[1].totalReplies).to.equal(0)
200 after(async function () {
201 await cleanupTests([ server ])