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'
8 dateIsValid, flushTests, killallServers, runServer, ServerInfo, setAccessTokensToServers, updateMyAvatar,
10 } from '../../utils/index'
12 addVideoCommentReply, addVideoCommentThread, deleteVideoComment, getVideoCommentThreads,
13 getVideoThreadComments
14 } from '../../utils/videos/video-comments'
16 const expect = chai.expect
18 describe('Test video comments', function () {
19 let server: ServerInfo
23 let replyToDeleteId: number
25 before(async function () {
30 server = await runServer(1)
32 await setAccessTokensToServers([ server ])
34 const res = await uploadVideo(server.url, server.accessToken, {})
35 videoUUID = res.body.video.uuid
36 videoId = res.body.video.id
38 await updateMyAvatar({
40 accessToken: server.accessToken,
45 it('Should not have threads on this video', async function () {
46 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
48 expect(res.body.total).to.equal(0)
49 expect(res.body.data).to.be.an('array')
50 expect(res.body.data).to.have.lengthOf(0)
53 it('Should create a thread in this video', async function () {
54 const text = 'my super first comment'
56 const res = await addVideoCommentThread(server.url, server.accessToken, videoUUID, text)
57 const comment = res.body.comment
59 expect(comment.inReplyToCommentId).to.be.null
60 expect(comment.text).equal('my super first comment')
61 expect(comment.videoId).to.equal(videoId)
62 expect(comment.id).to.equal(comment.threadId)
63 expect(comment.account.name).to.equal('root')
64 expect(comment.account.host).to.equal('localhost:9001')
65 expect(comment.account.url).to.equal('http://localhost:9001/accounts/root')
66 expect(comment.totalReplies).to.equal(0)
67 expect(dateIsValid(comment.createdAt as string)).to.be.true
68 expect(dateIsValid(comment.updatedAt as string)).to.be.true
71 it('Should list threads of this video', async function () {
72 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
74 expect(res.body.total).to.equal(1)
75 expect(res.body.data).to.be.an('array')
76 expect(res.body.data).to.have.lengthOf(1)
78 const comment: VideoComment = res.body.data[0]
79 expect(comment.inReplyToCommentId).to.be.null
80 expect(comment.text).equal('my super first comment')
81 expect(comment.videoId).to.equal(videoId)
82 expect(comment.id).to.equal(comment.threadId)
83 expect(comment.account.name).to.equal('root')
84 expect(comment.account.host).to.equal('localhost:9001')
86 await testImage(server.url, 'avatar-resized', comment.account.avatar.path, '.png')
88 expect(comment.totalReplies).to.equal(0)
89 expect(dateIsValid(comment.createdAt as string)).to.be.true
90 expect(dateIsValid(comment.updatedAt as string)).to.be.true
92 threadId = comment.threadId
95 it('Should get all the thread created', async function () {
96 const res = await getVideoThreadComments(server.url, videoUUID, threadId)
98 const rootComment = res.body.comment
99 expect(rootComment.inReplyToCommentId).to.be.null
100 expect(rootComment.text).equal('my super first comment')
101 expect(rootComment.videoId).to.equal(videoId)
102 expect(dateIsValid(rootComment.createdAt as string)).to.be.true
103 expect(dateIsValid(rootComment.updatedAt as string)).to.be.true
106 it('Should create multiple replies in this thread', async function () {
107 const text1 = 'my super answer to thread 1'
108 const childCommentRes = await addVideoCommentReply(server.url, server.accessToken, videoId, threadId, text1)
109 const childCommentId = childCommentRes.body.comment.id
111 const text2 = 'my super answer to answer of thread 1'
112 await addVideoCommentReply(server.url, server.accessToken, videoId, childCommentId, text2)
114 const text3 = 'my second answer to thread 1'
115 await addVideoCommentReply(server.url, server.accessToken, videoId, threadId, text3)
118 it('Should get correctly the replies', async function () {
119 const res = await getVideoThreadComments(server.url, videoUUID, threadId)
121 const tree: VideoCommentThreadTree = res.body
122 expect(tree.comment.text).equal('my super first comment')
123 expect(tree.children).to.have.lengthOf(2)
125 const firstChild = tree.children[0]
126 expect(firstChild.comment.text).to.equal('my super answer to thread 1')
127 expect(firstChild.children).to.have.lengthOf(1)
129 const childOfFirstChild = firstChild.children[0]
130 expect(childOfFirstChild.comment.text).to.equal('my super answer to answer of thread 1')
131 expect(childOfFirstChild.children).to.have.lengthOf(0)
133 const secondChild = tree.children[1]
134 expect(secondChild.comment.text).to.equal('my second answer to thread 1')
135 expect(secondChild.children).to.have.lengthOf(0)
137 replyToDeleteId = secondChild.comment.id
140 it('Should create other threads', async function () {
141 const text1 = 'super thread 2'
142 await addVideoCommentThread(server.url, server.accessToken, videoUUID, text1)
144 const text2 = 'super thread 3'
145 await addVideoCommentThread(server.url, server.accessToken, videoUUID, text2)
148 it('Should list the threads', async function () {
149 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5, 'createdAt')
151 expect(res.body.total).to.equal(3)
152 expect(res.body.data).to.be.an('array')
153 expect(res.body.data).to.have.lengthOf(3)
155 expect(res.body.data[0].text).to.equal('my super first comment')
156 expect(res.body.data[0].totalReplies).to.equal(3)
157 expect(res.body.data[1].text).to.equal('super thread 2')
158 expect(res.body.data[1].totalReplies).to.equal(0)
159 expect(res.body.data[2].text).to.equal('super thread 3')
160 expect(res.body.data[2].totalReplies).to.equal(0)
163 it('Should delete a reply', async function () {
164 await deleteVideoComment(server.url, server.accessToken, videoId, replyToDeleteId)
166 const res = await getVideoThreadComments(server.url, videoUUID, threadId)
168 const tree: VideoCommentThreadTree = res.body
169 expect(tree.comment.text).equal('my super first comment')
170 expect(tree.children).to.have.lengthOf(1)
172 const firstChild = tree.children[0]
173 expect(firstChild.comment.text).to.equal('my super answer to thread 1')
174 expect(firstChild.children).to.have.lengthOf(1)
176 const childOfFirstChild = firstChild.children[0]
177 expect(childOfFirstChild.comment.text).to.equal('my super answer to answer of thread 1')
178 expect(childOfFirstChild.children).to.have.lengthOf(0)
181 it('Should delete a complete thread', async function () {
182 await deleteVideoComment(server.url, server.accessToken, videoId, threadId)
184 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5, 'createdAt')
185 expect(res.body.total).to.equal(2)
186 expect(res.body.data).to.be.an('array')
187 expect(res.body.data).to.have.lengthOf(2)
189 expect(res.body.data[0].text).to.equal('super thread 2')
190 expect(res.body.data[0].totalReplies).to.equal(0)
191 expect(res.body.data[1].text).to.equal('super thread 3')
192 expect(res.body.data[1].totalReplies).to.equal(0)
195 after(async function () {
196 killallServers([ server ])
198 // Keep the logs if the test failed