allow limiting video-comments rss feeds to an account or video channel
[oweals/peertube.git] / server / tests / api / videos / videos-views-cleaner.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import {
6   cleanupTests,
7   closeAllSequelize,
8   countVideoViewsOf,
9   doubleFollow,
10   flushAndRunMultipleServers,
11   killallServers,
12   reRunServer,
13   ServerInfo,
14   setAccessTokensToServers,
15   uploadVideoAndGetId,
16   viewVideo,
17   wait,
18   waitJobs
19 } from '../../../../shared/extra-utils'
20
21 const expect = chai.expect
22
23 describe('Test video views cleaner', function () {
24   let servers: ServerInfo[]
25
26   let videoIdServer1: string
27   let videoIdServer2: string
28
29   before(async function () {
30     this.timeout(50000)
31
32     servers = await flushAndRunMultipleServers(2)
33     await setAccessTokensToServers(servers)
34
35     await doubleFollow(servers[0], servers[1])
36
37     videoIdServer1 = (await uploadVideoAndGetId({ server: servers[0], videoName: 'video server 1' })).uuid
38     videoIdServer2 = (await uploadVideoAndGetId({ server: servers[1], videoName: 'video server 2' })).uuid
39
40     await waitJobs(servers)
41
42     await viewVideo(servers[0].url, videoIdServer1)
43     await viewVideo(servers[1].url, videoIdServer1)
44     await viewVideo(servers[0].url, videoIdServer2)
45     await viewVideo(servers[1].url, videoIdServer2)
46
47     await waitJobs(servers)
48   })
49
50   it('Should not clean old video views', async function () {
51     this.timeout(50000)
52
53     killallServers([ servers[0] ])
54
55     await reRunServer(servers[0], { views: { videos: { remote: { max_age: '10 days' } } } })
56
57     await wait(6000)
58
59     // Should still have views
60
61     {
62       for (const server of servers) {
63         const total = await countVideoViewsOf(server.internalServerNumber, videoIdServer1)
64         expect(total).to.equal(2)
65       }
66     }
67
68     {
69       for (const server of servers) {
70         const total = await countVideoViewsOf(server.internalServerNumber, videoIdServer2)
71         expect(total).to.equal(2)
72       }
73     }
74   })
75
76   it('Should clean old video views', async function () {
77     this.timeout(50000)
78
79     killallServers([ servers[0] ])
80
81     await reRunServer(servers[0], { views: { videos: { remote: { max_age: '5 seconds' } } } })
82
83     await wait(6000)
84
85     // Should still have views
86
87     {
88       for (const server of servers) {
89         const total = await countVideoViewsOf(server.internalServerNumber, videoIdServer1)
90         expect(total).to.equal(2)
91       }
92     }
93
94     {
95       const totalServer1 = await countVideoViewsOf(servers[0].internalServerNumber, videoIdServer2)
96       expect(totalServer1).to.equal(0)
97
98       const totalServer2 = await countVideoViewsOf(servers[1].internalServerNumber, videoIdServer2)
99       expect(totalServer2).to.equal(2)
100     }
101   })
102
103   after(async function () {
104     await closeAllSequelize(servers)
105
106     await cleanupTests(servers)
107   })
108 })