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 { testVideoImage } from '../../utils'
8 dateIsValid, flushTests, killallServers, runServer, ServerInfo, setAccessTokensToServers, updateMyAvatar,
10 } from '../../utils/index'
12 addVideoCommentReply, addVideoCommentThread, getVideoCommentThreads,
13 getVideoThreadComments
14 } from '../../utils/videos/video-comments'
16 const expect = chai.expect
18 describe('Test video comments', function () {
19 let server: ServerInfo
24 before(async function () {
29 server = await runServer(1)
31 await setAccessTokensToServers([ server ])
33 const res = await uploadVideo(server.url, server.accessToken, {})
34 videoUUID = res.body.video.uuid
35 videoId = res.body.video.id
37 await updateMyAvatar({
39 accessToken: server.accessToken,
44 it('Should not have threads on this video', async function () {
45 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
47 expect(res.body.total).to.equal(0)
48 expect(res.body.data).to.be.an('array')
49 expect(res.body.data).to.have.lengthOf(0)
52 it('Should create a thread in this video', async function () {
53 const text = 'my super first comment'
55 const res = await addVideoCommentThread(server.url, server.accessToken, videoUUID, text)
56 const comment = res.body.comment
58 expect(comment.inReplyToCommentId).to.be.null
59 expect(comment.text).equal('my super first comment')
60 expect(comment.videoId).to.equal(videoId)
61 expect(comment.id).to.equal(comment.threadId)
62 expect(comment.account.name).to.equal('root')
63 expect(comment.account.host).to.equal('localhost:9001')
64 expect(comment.totalReplies).to.equal(0)
65 expect(dateIsValid(comment.createdAt as string)).to.be.true
66 expect(dateIsValid(comment.updatedAt as string)).to.be.true
69 it('Should list threads of this video', async function () {
70 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5)
72 expect(res.body.total).to.equal(1)
73 expect(res.body.data).to.be.an('array')
74 expect(res.body.data).to.have.lengthOf(1)
76 const comment: VideoComment = res.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:9001')
84 const test = await testVideoImage(server.url, 'avatar-resized', comment.account.avatar.path, '.png')
85 expect(test).to.equal(true)
87 expect(comment.totalReplies).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 res = await getVideoThreadComments(server.url, videoUUID, threadId)
97 const rootComment = res.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 childCommentRes = await addVideoCommentReply(server.url, server.accessToken, videoId, threadId, text1)
108 const childCommentId = childCommentRes.body.comment.id
110 const text2 = 'my super answer to answer of thread 1'
111 await addVideoCommentReply(server.url, server.accessToken, videoId, childCommentId, text2)
113 const text3 = 'my second answer to thread 1'
114 await addVideoCommentReply(server.url, server.accessToken, videoId, threadId, text3)
117 it('Should get correctly the replies', async function () {
118 const res = await getVideoThreadComments(server.url, videoUUID, threadId)
120 const tree: VideoCommentThreadTree = res.body
121 expect(tree.comment.text).equal('my super first comment')
122 expect(tree.children).to.have.lengthOf(2)
124 const firstChild = tree.children[0]
125 expect(firstChild.comment.text).to.equal('my super answer to thread 1')
126 expect(firstChild.children).to.have.lengthOf(1)
128 const childOfFirstChild = firstChild.children[0]
129 expect(childOfFirstChild.comment.text).to.equal('my super answer to answer of thread 1')
130 expect(childOfFirstChild.children).to.have.lengthOf(0)
132 const secondChild = tree.children[1]
133 expect(secondChild.comment.text).to.equal('my second answer to thread 1')
134 expect(secondChild.children).to.have.lengthOf(0)
137 it('Should create other threads', async function () {
138 const text1 = 'super thread 2'
139 await addVideoCommentThread(server.url, server.accessToken, videoUUID, text1)
141 const text2 = 'super thread 3'
142 await addVideoCommentThread(server.url, server.accessToken, videoUUID, text2)
145 it('Should list the threads', async function () {
146 const res = await getVideoCommentThreads(server.url, videoUUID, 0, 5, 'createdAt')
148 expect(res.body.total).to.equal(3)
149 expect(res.body.data).to.be.an('array')
150 expect(res.body.data).to.have.lengthOf(3)
152 expect(res.body.data[0].text).to.equal('my super first comment')
153 expect(res.body.data[0].totalReplies).to.equal(3)
154 expect(res.body.data[1].text).to.equal('super thread 2')
155 expect(res.body.data[1].totalReplies).to.equal(0)
156 expect(res.body.data[2].text).to.equal('super thread 3')
157 expect(res.body.data[2].totalReplies).to.equal(0)
160 after(async function () {
161 killallServers([ server ])
163 // Keep the logs if the test failed