1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
8 flushAndRunMultipleServers,
12 setAccessTokensToServers,
16 } from '@shared/extra-utils'
18 const expect = chai.expect
20 describe('Test AP cleaner', function () {
21 let servers: ServerInfo[] = []
22 let videoUUID1: string
23 let videoUUID2: string
24 let videoUUID3: string
26 let videoUUIDs: string[]
28 before(async function () {
33 videos: { cleanup_remote_interactions: true }
36 servers = await flushAndRunMultipleServers(3, config)
38 // Get the access tokens
39 await setAccessTokensToServers(servers)
42 doubleFollow(servers[0], servers[1]),
43 doubleFollow(servers[1], servers[2]),
44 doubleFollow(servers[0], servers[2])
47 // Update 1 local share, check 6 shares
49 // Create 1 comment per video
50 // Update 1 remote URL and 1 local URL on
52 videoUUID1 = (await uploadVideoAndGetId({ server: servers[0], videoName: 'server 1' })).uuid
53 videoUUID2 = (await uploadVideoAndGetId({ server: servers[1], videoName: 'server 2' })).uuid
54 videoUUID3 = (await uploadVideoAndGetId({ server: servers[2], videoName: 'server 3' })).uuid
56 videoUUIDs = [ videoUUID1, videoUUID2, videoUUID3 ]
58 await waitJobs(servers)
60 for (const server of servers) {
61 for (const uuid of videoUUIDs) {
62 await rateVideo(server.url, server.accessToken, uuid, 'like')
63 await server.commentsCommand.createThread({ videoId: uuid, text: 'comment' })
67 await waitJobs(servers)
70 it('Should have the correct likes', async function () {
71 for (const server of servers) {
72 for (const uuid of videoUUIDs) {
73 const res = await getVideo(server.url, uuid)
74 expect(res.body.likes).to.equal(3)
75 expect(res.body.dislikes).to.equal(0)
80 it('Should destroy server 3 internal likes and correctly clean them', async function () {
83 await servers[2].sqlCommand.deleteAll('accountVideoRate')
84 for (const uuid of videoUUIDs) {
85 await servers[2].sqlCommand.setVideoField(uuid, 'likes', '0')
89 await waitJobs(servers)
91 // Updated rates of my video
93 const res = await getVideo(servers[0].url, videoUUID1)
94 expect(res.body.likes).to.equal(2)
95 expect(res.body.dislikes).to.equal(0)
98 // Did not update rates of a remote video
100 const res = await getVideo(servers[0].url, videoUUID2)
101 expect(res.body.likes).to.equal(3)
102 expect(res.body.dislikes).to.equal(0)
106 it('Should update rates to dislikes', async function () {
109 for (const server of servers) {
110 for (const uuid of videoUUIDs) {
111 await rateVideo(server.url, server.accessToken, uuid, 'dislike')
115 await waitJobs(servers)
117 for (const server of servers) {
118 for (const uuid of videoUUIDs) {
119 const res = await getVideo(server.url, uuid)
120 expect(res.body.likes).to.equal(0)
121 expect(res.body.dislikes).to.equal(3)
126 it('Should destroy server 3 internal dislikes and correctly clean them', async function () {
129 await servers[2].sqlCommand.deleteAll('accountVideoRate')
131 for (const uuid of videoUUIDs) {
132 await servers[2].sqlCommand.setVideoField(uuid, 'dislikes', '0')
136 await waitJobs(servers)
138 // Updated rates of my video
140 const res = await getVideo(servers[0].url, videoUUID1)
141 expect(res.body.likes).to.equal(0)
142 expect(res.body.dislikes).to.equal(2)
145 // Did not update rates of a remote video
147 const res = await getVideo(servers[0].url, videoUUID2)
148 expect(res.body.likes).to.equal(0)
149 expect(res.body.dislikes).to.equal(3)
153 it('Should destroy server 3 internal shares and correctly clean them', async function () {
156 const preCount = await servers[0].sqlCommand.getCount('videoShare')
157 expect(preCount).to.equal(6)
159 await servers[2].sqlCommand.deleteAll('videoShare')
161 await waitJobs(servers)
163 // Still 6 because we don't have remote shares on local videos
164 const postCount = await servers[0].sqlCommand.getCount('videoShare')
165 expect(postCount).to.equal(6)
168 it('Should destroy server 3 internal comments and correctly clean them', async function () {
172 const { total } = await servers[0].commentsCommand.listThreads({ videoId: videoUUID1 })
173 expect(total).to.equal(3)
176 await servers[2].sqlCommand.deleteAll('videoComment')
179 await waitJobs(servers)
182 const { total } = await servers[0].commentsCommand.listThreads({ videoId: videoUUID1 })
183 expect(total).to.equal(2)
187 it('Should correctly update rate URLs', async function () {
190 async function check (like: string, ofServerUrl: string, urlSuffix: string, remote: 'true' | 'false') {
191 const query = `SELECT "videoId", "accountVideoRate".url FROM "accountVideoRate" ` +
192 `INNER JOIN video ON "accountVideoRate"."videoId" = video.id AND remote IS ${remote} WHERE "accountVideoRate"."url" LIKE '${like}'`
193 const res = await servers[0].sqlCommand.selectQuery(query)
195 for (const rate of res) {
196 const matcher = new RegExp(`^${ofServerUrl}/accounts/root/dislikes/\\d+${urlSuffix}$`)
197 expect(rate.url).to.match(matcher)
201 async function checkLocal () {
202 const startsWith = 'http://' + servers[0].host + '%'
204 await check(startsWith, servers[0].url, '', 'false')
206 await check(startsWith, servers[0].url, '', 'true')
209 async function checkRemote (suffix: string) {
210 const startsWith = 'http://' + servers[1].host + '%'
212 await check(startsWith, servers[1].url, suffix, 'false')
213 // On remote videos, we should not update URLs so no suffix
214 await check(startsWith, servers[1].url, '', 'true')
218 await checkRemote('')
221 const query = `UPDATE "accountVideoRate" SET url = url || 'stan'`
222 await servers[1].sqlCommand.updateQuery(query)
225 await waitJobs(servers)
229 await checkRemote('stan')
232 it('Should correctly update comment URLs', async function () {
235 async function check (like: string, ofServerUrl: string, urlSuffix: string, remote: 'true' | 'false') {
236 const query = `SELECT "videoId", "videoComment".url, uuid as "videoUUID" FROM "videoComment" ` +
237 `INNER JOIN video ON "videoComment"."videoId" = video.id AND remote IS ${remote} WHERE "videoComment"."url" LIKE '${like}'`
239 const res = await servers[0].sqlCommand.selectQuery(query)
241 for (const comment of res) {
242 const matcher = new RegExp(`${ofServerUrl}/videos/watch/${comment.videoUUID}/comments/\\d+${urlSuffix}`)
243 expect(comment.url).to.match(matcher)
247 async function checkLocal () {
248 const startsWith = 'http://' + servers[0].host + '%'
250 await check(startsWith, servers[0].url, '', 'false')
252 await check(startsWith, servers[0].url, '', 'true')
255 async function checkRemote (suffix: string) {
256 const startsWith = 'http://' + servers[1].host + '%'
258 await check(startsWith, servers[1].url, suffix, 'false')
259 // On remote videos, we should not update URLs so no suffix
260 await check(startsWith, servers[1].url, '', 'true')
264 const query = `UPDATE "videoComment" SET url = url || 'kyle'`
265 await servers[1].sqlCommand.updateQuery(query)
268 await waitJobs(servers)
272 await checkRemote('kyle')
275 after(async function () {
276 await cleanupTests(servers)