1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
10 flushAndRunMultipleServers,
16 setAccessTokensToServers,
22 } from '@shared/extra-utils'
24 const expect = chai.expect
26 describe('Test AP cleaner', function () {
27 let servers: ServerInfo[] = []
28 let videoUUID1: string
29 let videoUUID2: string
30 let videoUUID3: string
32 let videoUUIDs: string[]
34 before(async function () {
39 videos: { cleanup_remote_interactions: true }
42 servers = await flushAndRunMultipleServers(3, config)
44 // Get the access tokens
45 await setAccessTokensToServers(servers)
48 doubleFollow(servers[0], servers[1]),
49 doubleFollow(servers[1], servers[2]),
50 doubleFollow(servers[0], servers[2])
53 // Update 1 local share, check 6 shares
55 // Create 1 comment per video
56 // Update 1 remote URL and 1 local URL on
58 videoUUID1 = (await uploadVideoAndGetId({ server: servers[0], videoName: 'server 1' })).uuid
59 videoUUID2 = (await uploadVideoAndGetId({ server: servers[1], videoName: 'server 2' })).uuid
60 videoUUID3 = (await uploadVideoAndGetId({ server: servers[2], videoName: 'server 3' })).uuid
62 videoUUIDs = [ videoUUID1, videoUUID2, videoUUID3 ]
64 await waitJobs(servers)
66 for (const server of servers) {
67 for (const uuid of videoUUIDs) {
68 await rateVideo(server.url, server.accessToken, uuid, 'like')
69 await server.commentsCommand.createThread({ videoId: uuid, text: 'comment' })
73 await waitJobs(servers)
76 it('Should have the correct likes', async function () {
77 for (const server of servers) {
78 for (const uuid of videoUUIDs) {
79 const res = await getVideo(server.url, uuid)
80 expect(res.body.likes).to.equal(3)
81 expect(res.body.dislikes).to.equal(0)
86 it('Should destroy server 3 internal likes and correctly clean them', async function () {
89 await deleteAll(servers[2].internalServerNumber, 'accountVideoRate')
90 for (const uuid of videoUUIDs) {
91 await setVideoField(servers[2].internalServerNumber, uuid, 'likes', '0')
95 await waitJobs(servers)
97 // Updated rates of my video
99 const res = await getVideo(servers[0].url, videoUUID1)
100 expect(res.body.likes).to.equal(2)
101 expect(res.body.dislikes).to.equal(0)
104 // Did not update rates of a remote video
106 const res = await getVideo(servers[0].url, videoUUID2)
107 expect(res.body.likes).to.equal(3)
108 expect(res.body.dislikes).to.equal(0)
112 it('Should update rates to dislikes', async function () {
115 for (const server of servers) {
116 for (const uuid of videoUUIDs) {
117 await rateVideo(server.url, server.accessToken, uuid, 'dislike')
121 await waitJobs(servers)
123 for (const server of servers) {
124 for (const uuid of videoUUIDs) {
125 const res = await getVideo(server.url, uuid)
126 expect(res.body.likes).to.equal(0)
127 expect(res.body.dislikes).to.equal(3)
132 it('Should destroy server 3 internal dislikes and correctly clean them', async function () {
135 await deleteAll(servers[2].internalServerNumber, 'accountVideoRate')
137 for (const uuid of videoUUIDs) {
138 await setVideoField(servers[2].internalServerNumber, uuid, 'dislikes', '0')
142 await waitJobs(servers)
144 // Updated rates of my video
146 const res = await getVideo(servers[0].url, videoUUID1)
147 expect(res.body.likes).to.equal(0)
148 expect(res.body.dislikes).to.equal(2)
151 // Did not update rates of a remote video
153 const res = await getVideo(servers[0].url, videoUUID2)
154 expect(res.body.likes).to.equal(0)
155 expect(res.body.dislikes).to.equal(3)
159 it('Should destroy server 3 internal shares and correctly clean them', async function () {
162 const preCount = await getCount(servers[0].internalServerNumber, 'videoShare')
163 expect(preCount).to.equal(6)
165 await deleteAll(servers[2].internalServerNumber, 'videoShare')
167 await waitJobs(servers)
169 // Still 6 because we don't have remote shares on local videos
170 const postCount = await getCount(servers[0].internalServerNumber, 'videoShare')
171 expect(postCount).to.equal(6)
174 it('Should destroy server 3 internal comments and correctly clean them', async function () {
178 const { total } = await servers[0].commentsCommand.listThreads({ videoId: videoUUID1 })
179 expect(total).to.equal(3)
182 await deleteAll(servers[2].internalServerNumber, 'videoComment')
185 await waitJobs(servers)
188 const { total } = await servers[0].commentsCommand.listThreads({ videoId: videoUUID1 })
189 expect(total).to.equal(2)
193 it('Should correctly update rate URLs', async function () {
196 async function check (like: string, ofServerUrl: string, urlSuffix: string, remote: 'true' | 'false') {
197 const query = `SELECT "videoId", "accountVideoRate".url FROM "accountVideoRate" ` +
198 `INNER JOIN video ON "accountVideoRate"."videoId" = video.id AND remote IS ${remote} WHERE "accountVideoRate"."url" LIKE '${like}'`
199 const res = await selectQuery(servers[0].internalServerNumber, query)
201 for (const rate of res) {
202 const matcher = new RegExp(`^${ofServerUrl}/accounts/root/dislikes/\\d+${urlSuffix}$`)
203 expect(rate.url).to.match(matcher)
207 async function checkLocal () {
208 const startsWith = 'http://' + servers[0].host + '%'
210 await check(startsWith, servers[0].url, '', 'false')
212 await check(startsWith, servers[0].url, '', 'true')
215 async function checkRemote (suffix: string) {
216 const startsWith = 'http://' + servers[1].host + '%'
218 await check(startsWith, servers[1].url, suffix, 'false')
219 // On remote videos, we should not update URLs so no suffix
220 await check(startsWith, servers[1].url, '', 'true')
224 await checkRemote('')
227 const query = `UPDATE "accountVideoRate" SET url = url || 'stan'`
228 await updateQuery(servers[1].internalServerNumber, query)
231 await waitJobs(servers)
235 await checkRemote('stan')
238 it('Should correctly update comment URLs', async function () {
241 async function check (like: string, ofServerUrl: string, urlSuffix: string, remote: 'true' | 'false') {
242 const query = `SELECT "videoId", "videoComment".url, uuid as "videoUUID" FROM "videoComment" ` +
243 `INNER JOIN video ON "videoComment"."videoId" = video.id AND remote IS ${remote} WHERE "videoComment"."url" LIKE '${like}'`
245 const res = await selectQuery(servers[0].internalServerNumber, query)
247 for (const comment of res) {
248 const matcher = new RegExp(`${ofServerUrl}/videos/watch/${comment.videoUUID}/comments/\\d+${urlSuffix}`)
249 expect(comment.url).to.match(matcher)
253 async function checkLocal () {
254 const startsWith = 'http://' + servers[0].host + '%'
256 await check(startsWith, servers[0].url, '', 'false')
258 await check(startsWith, servers[0].url, '', 'true')
261 async function checkRemote (suffix: string) {
262 const startsWith = 'http://' + servers[1].host + '%'
264 await check(startsWith, servers[1].url, suffix, 'false')
265 // On remote videos, we should not update URLs so no suffix
266 await check(startsWith, servers[1].url, '', 'true')
270 const query = `UPDATE "videoComment" SET url = url || 'kyle'`
271 await updateQuery(servers[1].internalServerNumber, query)
274 await waitJobs(servers)
278 await checkRemote('kyle')
281 after(async function () {
282 await cleanupTests(servers)
284 await closeAllSequelize(servers)