1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
15 } from '../../../../shared/extra-utils'
16 import { flushAndRunMultipleServers, ServerInfo, setAccessTokensToServers } from '../../../../shared/extra-utils/index'
17 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
18 import { addVideoCommentThread, getVideoCommentThreads } from '../../../../shared/extra-utils/videos/video-comments'
19 import { getVideo, rateVideo, uploadVideoAndGetId } from '../../../../shared/extra-utils/videos/videos'
21 const expect = chai.expect
23 describe('Test AP cleaner', function () {
24 let servers: ServerInfo[] = []
25 let videoUUID1: string
26 let videoUUID2: string
27 let videoUUID3: string
29 let videoUUIDs: string[]
31 before(async function () {
36 videos: { cleanup_remote_interactions: true }
39 servers = await flushAndRunMultipleServers(3, config)
41 // Get the access tokens
42 await setAccessTokensToServers(servers)
45 doubleFollow(servers[0], servers[1]),
46 doubleFollow(servers[1], servers[2]),
47 doubleFollow(servers[0], servers[2])
50 // Update 1 local share, check 6 shares
52 // Create 1 comment per video
53 // Update 1 remote URL and 1 local URL on
55 videoUUID1 = (await uploadVideoAndGetId({ server: servers[0], videoName: 'server 1' })).uuid
56 videoUUID2 = (await uploadVideoAndGetId({ server: servers[1], videoName: 'server 2' })).uuid
57 videoUUID3 = (await uploadVideoAndGetId({ server: servers[2], videoName: 'server 3' })).uuid
59 videoUUIDs = [ videoUUID1, videoUUID2, videoUUID3 ]
61 await waitJobs(servers)
63 for (const server of servers) {
64 for (const uuid of videoUUIDs) {
65 await rateVideo(server.url, server.accessToken, uuid, 'like')
66 await addVideoCommentThread(server.url, server.accessToken, uuid, 'comment')
70 await waitJobs(servers)
73 it('Should have the correct likes', async function () {
74 for (const server of servers) {
75 for (const uuid of videoUUIDs) {
76 const res = await getVideo(server.url, uuid)
77 expect(res.body.likes).to.equal(3)
78 expect(res.body.dislikes).to.equal(0)
83 it('Should destroy server 3 internal likes and correctly clean them', async function () {
86 await deleteAll(servers[2].internalServerNumber, 'accountVideoRate')
87 for (const uuid of videoUUIDs) {
88 await setVideoField(servers[2].internalServerNumber, uuid, 'likes', '0')
92 await waitJobs(servers)
94 // Updated rates of my video
96 const res = await getVideo(servers[0].url, videoUUID1)
97 expect(res.body.likes).to.equal(2)
98 expect(res.body.dislikes).to.equal(0)
101 // Did not update rates of a remote video
103 const res = await getVideo(servers[0].url, videoUUID2)
104 expect(res.body.likes).to.equal(3)
105 expect(res.body.dislikes).to.equal(0)
109 it('Should update rates to dislikes', async function () {
112 for (const server of servers) {
113 for (const uuid of videoUUIDs) {
114 await rateVideo(server.url, server.accessToken, uuid, 'dislike')
118 await waitJobs(servers)
120 for (const server of servers) {
121 for (const uuid of videoUUIDs) {
122 const res = await getVideo(server.url, uuid)
123 expect(res.body.likes).to.equal(0)
124 expect(res.body.dislikes).to.equal(3)
129 it('Should destroy server 3 internal dislikes and correctly clean them', async function () {
132 await deleteAll(servers[2].internalServerNumber, 'accountVideoRate')
134 for (const uuid of videoUUIDs) {
135 await setVideoField(servers[2].internalServerNumber, uuid, 'dislikes', '0')
139 await waitJobs(servers)
141 // Updated rates of my video
143 const res = await getVideo(servers[0].url, videoUUID1)
144 expect(res.body.likes).to.equal(0)
145 expect(res.body.dislikes).to.equal(2)
148 // Did not update rates of a remote video
150 const res = await getVideo(servers[0].url, videoUUID2)
151 expect(res.body.likes).to.equal(0)
152 expect(res.body.dislikes).to.equal(3)
156 it('Should destroy server 3 internal shares and correctly clean them', async function () {
159 const preCount = await getCount(servers[0].internalServerNumber, 'videoShare')
160 expect(preCount).to.equal(6)
162 await deleteAll(servers[2].internalServerNumber, 'videoShare')
164 await waitJobs(servers)
166 // Still 6 because we don't have remote shares on local videos
167 const postCount = await getCount(servers[0].internalServerNumber, 'videoShare')
168 expect(postCount).to.equal(6)
171 it('Should destroy server 3 internal comments and correctly clean them', async function () {
175 const res = await getVideoCommentThreads(servers[0].url, videoUUID1, 0, 5)
176 expect(res.body.total).to.equal(3)
179 await deleteAll(servers[2].internalServerNumber, 'videoComment')
182 await waitJobs(servers)
185 const res = await getVideoCommentThreads(servers[0].url, videoUUID1, 0, 5)
186 expect(res.body.total).to.equal(2)
190 it('Should correctly update rate URLs', async function () {
193 async function check (like: string, ofServerUrl: string, urlSuffix: string, remote: 'true' | 'false') {
194 const query = `SELECT "videoId", "accountVideoRate".url FROM "accountVideoRate" ` +
195 `INNER JOIN video ON "accountVideoRate"."videoId" = video.id AND remote IS ${remote} WHERE "accountVideoRate"."url" LIKE '${like}'`
196 const res = await selectQuery(servers[0].internalServerNumber, query)
198 for (const rate of res) {
199 const matcher = new RegExp(`^${ofServerUrl}/accounts/root/dislikes/\\d+${urlSuffix}$`)
200 expect(rate.url).to.match(matcher)
204 async function checkLocal () {
205 const startsWith = 'http://' + servers[0].host + '%'
207 await check(startsWith, servers[0].url, '', 'false')
209 await check(startsWith, servers[0].url, '', 'true')
212 async function checkRemote (suffix: string) {
213 const startsWith = 'http://' + servers[1].host + '%'
215 await check(startsWith, servers[1].url, suffix, 'false')
216 // On remote videos, we should not update URLs so no suffix
217 await check(startsWith, servers[1].url, '', 'true')
221 await checkRemote('')
224 const query = `UPDATE "accountVideoRate" SET url = url || 'stan'`
225 await updateQuery(servers[1].internalServerNumber, query)
228 await waitJobs(servers)
232 await checkRemote('stan')
235 it('Should correctly update comment URLs', async function () {
238 async function check (like: string, ofServerUrl: string, urlSuffix: string, remote: 'true' | 'false') {
239 const query = `SELECT "videoId", "videoComment".url, uuid as "videoUUID" FROM "videoComment" ` +
240 `INNER JOIN video ON "videoComment"."videoId" = video.id AND remote IS ${remote} WHERE "videoComment"."url" LIKE '${like}'`
242 const res = await selectQuery(servers[0].internalServerNumber, query)
244 for (const comment of res) {
245 const matcher = new RegExp(`${ofServerUrl}/videos/watch/${comment.videoUUID}/comments/\\d+${urlSuffix}`)
246 expect(comment.url).to.match(matcher)
250 async function checkLocal () {
251 const startsWith = 'http://' + servers[0].host + '%'
253 await check(startsWith, servers[0].url, '', 'false')
255 await check(startsWith, servers[0].url, '', 'true')
258 async function checkRemote (suffix: string) {
259 const startsWith = 'http://' + servers[1].host + '%'
261 await check(startsWith, servers[1].url, suffix, 'false')
262 // On remote videos, we should not update URLs so no suffix
263 await check(startsWith, servers[1].url, '', 'true')
267 const query = `UPDATE "videoComment" SET url = url || 'kyle'`
268 await updateQuery(servers[1].internalServerNumber, query)
271 await waitJobs(servers)
275 await checkRemote('kyle')
278 after(async function () {
279 await cleanupTests(servers)
281 await closeAllSequelize(servers)