PeerTube/packages/tests/src/api/views/videos-views-cleaner.ts

102 lines
3.0 KiB
TypeScript
Raw Normal View History

2020-01-31 16:56:52 +01:00
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2019-04-11 17:33:36 +02:00
import { wait } from '@peertube/peertube-core-utils'
import { buildUUID } from '@peertube/peertube-node-utils'
2019-04-11 17:33:36 +02:00
import {
2020-01-31 16:56:52 +01:00
cleanupTests,
2021-07-16 09:47:51 +02:00
createMultipleServers,
2021-07-16 14:27:30 +02:00
doubleFollow,
2019-04-11 17:33:36 +02:00
killallServers,
2021-07-16 09:47:51 +02:00
PeerTubeServer,
2019-04-11 17:33:36 +02:00
setAccessTokensToServers,
2020-01-31 16:56:52 +01:00
waitJobs
} from '@peertube/peertube-server-commands'
import { SQLCommand } from '@tests/shared/sql-command.js'
import { expect } from 'chai'
2019-04-11 17:33:36 +02:00
describe('Test video views cleaner', function () {
2021-07-16 09:47:51 +02:00
let servers: PeerTubeServer[]
2023-05-10 16:23:55 +02:00
let sqlCommands: SQLCommand[] = []
2019-04-11 17:33:36 +02:00
let videoIdServer1: string
let videoIdServer2: string
before(async function () {
this.timeout(240000)
2019-04-11 17:33:36 +02:00
2021-07-16 09:47:51 +02:00
servers = await createMultipleServers(2)
2019-04-11 17:33:36 +02:00
await setAccessTokensToServers(servers)
await doubleFollow(servers[0], servers[1])
2021-07-16 09:04:35 +02:00
videoIdServer1 = (await servers[0].videos.quickUpload({ name: 'video server 1' })).uuid
videoIdServer2 = (await servers[1].videos.quickUpload({ name: 'video server 2' })).uuid
2019-04-11 17:33:36 +02:00
await waitJobs(servers)
const sessionId = buildUUID()
await servers[0].views.simulateView({ id: videoIdServer1, sessionId })
await servers[1].views.simulateView({ id: videoIdServer1, sessionId })
await servers[0].views.simulateView({ id: videoIdServer2, sessionId })
await servers[1].views.simulateView({ id: videoIdServer2, sessionId })
2019-04-11 17:33:36 +02:00
await waitJobs(servers)
2023-04-21 15:00:01 +02:00
sqlCommands = servers.map(s => new SQLCommand(s))
2019-04-11 17:33:36 +02:00
})
it('Should not clean old video views', async function () {
this.timeout(50000)
2021-07-09 15:37:43 +02:00
await killallServers([ servers[0] ])
2019-04-11 17:33:36 +02:00
2021-07-16 09:47:51 +02:00
await servers[0].run({ views: { videos: { remote: { max_age: '10 days' } } } })
2019-04-11 17:33:36 +02:00
await wait(6000)
// Should still have views
2023-04-21 15:00:01 +02:00
for (let i = 0; i < servers.length; i++) {
const total = await sqlCommands[i].countVideoViewsOf(videoIdServer1)
expect(total).to.equal(2, 'Server ' + servers[i].serverNumber + ' does not have the correct amount of views')
2019-04-11 17:33:36 +02:00
}
2023-04-21 15:00:01 +02:00
for (let i = 0; i < servers.length; i++) {
const total = await sqlCommands[i].countVideoViewsOf(videoIdServer2)
expect(total).to.equal(2, 'Server ' + servers[i].serverNumber + ' does not have the correct amount of views')
2019-04-11 17:33:36 +02:00
}
})
it('Should clean old video views', async function () {
this.timeout(50000)
2021-07-09 15:37:43 +02:00
await killallServers([ servers[0] ])
2019-04-11 17:33:36 +02:00
2021-07-16 09:47:51 +02:00
await servers[0].run({ views: { videos: { remote: { max_age: '5 seconds' } } } })
2019-04-11 17:33:36 +02:00
await wait(6000)
// Should still have views
2023-04-21 15:00:01 +02:00
for (let i = 0; i < servers.length; i++) {
const total = await sqlCommands[i].countVideoViewsOf(videoIdServer1)
expect(total).to.equal(2)
2019-04-11 17:33:36 +02:00
}
2023-04-21 15:00:01 +02:00
const totalServer1 = await sqlCommands[0].countVideoViewsOf(videoIdServer2)
expect(totalServer1).to.equal(0)
2019-04-11 17:33:36 +02:00
2023-04-21 15:00:01 +02:00
const totalServer2 = await sqlCommands[1].countVideoViewsOf(videoIdServer2)
expect(totalServer2).to.equal(2)
2019-04-11 17:33:36 +02:00
})
2019-04-24 15:10:37 +02:00
after(async function () {
2023-04-21 15:00:01 +02:00
for (const sqlCommand of sqlCommands) {
await sqlCommand.cleanup()
}
2019-04-24 15:10:37 +02:00
await cleanupTests(servers)
2019-04-11 17:33:36 +02:00
})
})