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 { testImage } from '../../utils'
13 setAccessTokensToServers,
16 } from '../../utils/index'
19 addVideoCommentThread,
21 getVideoCommentThreads,
22 getVideoThreadComments
23 } from '../../utils/videos/video-comments'
25 const expect = chai.expect
27 describe('Test video comments', function () {
28 let server: ServerInfo
32 let replyToDeleteId: number
34 before(async function () {
39 server = await runServer(1)
41 await setAccessTokensToServers([ server ])
43 const res = await uploadVideo(server.url, server.accessToken, {})
44 videoUUID = res.body.video.uuid
45 videoId = res.body.video.id
47 await updateMyAvatar({
49 accessToken: server.accessToken,
54 it('Should not have threads on this video', async function () {
55 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
57 expect(res.body.total).to.equal(0)
58 expect(res.body.data).to.be.an('array')
59 expect(res.body.data).to.have.lengthOf(0)
62 it('Should create a thread in this video', async function () {
63 const text = 'my super first comment'
65 const res = await addVideoCommentThread(server.url, server.accessToken, videoUUID, text)
66 const comment = res.body.comment
68 expect(comment.inReplyToCommentId).to.be.null
69 expect(comment.text).equal('my super first comment')
70 expect(comment.videoId).to.equal(videoId)
71 expect(comment.id).to.equal(comment.threadId)
72 expect(comment.account.name).to.equal('root')
73 expect(comment.account.host).to.equal('localhost:9001')
74 expect(comment.account.url).to.equal('http://localhost:9001/accounts/root')
75 expect(comment.totalReplies).to.equal(0)
76 expect(dateIsValid(comment.createdAt as string)).to.be.true
77 expect(dateIsValid(comment.updatedAt as string)).to.be.true
80 it('Should list threads of this video', async function () {
81 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
83 expect(res.body.total).to.equal(1)
84 expect(res.body.data).to.be.an('array')
85 expect(res.body.data).to.have.lengthOf(1)
87 const comment: VideoComment = res.body.data[0]
88 expect(comment.inReplyToCommentId).to.be.null
89 expect(comment.text).equal('my super first comment')
90 expect(comment.videoId).to.equal(videoId)
91 expect(comment.id).to.equal(comment.threadId)
92 expect(comment.account.name).to.equal('root')
93 expect(comment.account.host).to.equal('localhost:9001')
95 await testImage(server.url, 'avatar-resized', comment.account.avatar.path, '.png')
97 expect(comment.totalReplies).to.equal(0)
98 expect(dateIsValid(comment.createdAt as string)).to.be.true
99 expect(dateIsValid(comment.updatedAt as string)).to.be.true
101 threadId = comment.threadId
104 it('Should get all the thread created', async function () {
105 const res = await getVideoThreadComments(server.url, videoUUID, threadId)
107 const rootComment = res.body.comment
108 expect(rootComment.inReplyToCommentId).to.be.null
109 expect(rootComment.text).equal('my super first comment')
110 expect(rootComment.videoId).to.equal(videoId)
111 expect(dateIsValid(rootComment.createdAt as string)).to.be.true
112 expect(dateIsValid(rootComment.updatedAt as string)).to.be.true
115 it('Should create multiple replies in this thread', async function () {
116 const text1 = 'my super answer to thread 1'
117 const childCommentRes = await addVideoCommentReply(server.url, server.accessToken, videoId, threadId, text1)
118 const childCommentId = childCommentRes.body.comment.id
120 const text2 = 'my super answer to answer of thread 1'
121 await addVideoCommentReply(server.url, server.accessToken, videoId, childCommentId, text2)
123 const text3 = 'my second answer to thread 1'
124 await addVideoCommentReply(server.url, server.accessToken, videoId, threadId, text3)
127 it('Should get correctly the replies', async function () {
128 const res = await getVideoThreadComments(server.url, videoUUID, threadId)
130 const tree: VideoCommentThreadTree = res.body
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 addVideoCommentThread(server.url, server.accessToken, videoUUID, text1)
153 const text2 = 'super thread 3'
154 await addVideoCommentThread(server.url, server.accessToken, videoUUID, text2)
157 it('Should list the threads', async function () {
158 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5, 'createdAt')
160 expect(res.body.total).to.equal(3)
161 expect(res.body.data).to.be.an('array')
162 expect(res.body.data).to.have.lengthOf(3)
164 expect(res.body.data[0].text).to.equal('my super first comment')
165 expect(res.body.data[0].totalReplies).to.equal(3)
166 expect(res.body.data[1].text).to.equal('super thread 2')
167 expect(res.body.data[1].totalReplies).to.equal(0)
168 expect(res.body.data[2].text).to.equal('super thread 3')
169 expect(res.body.data[2].totalReplies).to.equal(0)
172 it('Should delete a reply', async function () {
173 await deleteVideoComment(server.url, server.accessToken, videoId, replyToDeleteId)
175 const res = await getVideoThreadComments(server.url, videoUUID, threadId)
177 const tree: VideoCommentThreadTree = res.body
178 expect(tree.comment.text).equal('my super first comment')
179 expect(tree.children).to.have.lengthOf(1)
181 const firstChild = tree.children[0]
182 expect(firstChild.comment.text).to.equal('my super answer to thread 1')
183 expect(firstChild.children).to.have.lengthOf(1)
185 const childOfFirstChild = firstChild.children[0]
186 expect(childOfFirstChild.comment.text).to.equal('my super answer to answer of thread 1')
187 expect(childOfFirstChild.children).to.have.lengthOf(0)
190 it('Should delete a complete thread', async function () {
191 await deleteVideoComment(server.url, server.accessToken, videoId, threadId)
193 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5, 'createdAt')
194 expect(res.body.total).to.equal(2)
195 expect(res.body.data).to.be.an('array')
196 expect(res.body.data).to.have.lengthOf(2)
198 expect(res.body.data[0].text).to.equal('super thread 2')
199 expect(res.body.data[0].totalReplies).to.equal(0)
200 expect(res.body.data[1].text).to.equal('super thread 3')
201 expect(res.body.data[1].totalReplies).to.equal(0)
204 after(async function () {
205 killallServers([ server ])