]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/views/video-views-retention-stats.ts
We don't need to import mocha
[github/Chocobozzz/PeerTube.git] / server / tests / api / views / video-views-retention-stats.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import * as chai from 'chai'
4 import { prepareViewsServers, prepareViewsVideos, processViewersStats } from '@server/tests/shared'
5 import { cleanupTests, PeerTubeServer } from '@shared/server-commands'
6
7 const expect = chai.expect
8
9 describe('Test views retention stats', function () {
10 let servers: PeerTubeServer[]
11
12 before(async function () {
13 this.timeout(120000)
14
15 servers = await prepareViewsServers()
16 })
17
18 describe('Test retention stats on VOD', function () {
19 let vodVideoId: string
20
21 before(async function () {
22 this.timeout(120000);
23
24 ({ vodVideoId } = await prepareViewsVideos({ servers, live: false, vod: true }))
25 })
26
27 it('Should display empty retention', async function () {
28 const { data } = await servers[0].videoStats.getRetentionStats({ videoId: vodVideoId })
29 expect(data).to.have.lengthOf(6)
30
31 for (let i = 0; i < 6; i++) {
32 expect(data[i].second).to.equal(i)
33 expect(data[i].retentionPercent).to.equal(0)
34 }
35 })
36
37 it('Should display appropriate retention metrics', async function () {
38 await servers[0].views.simulateViewer({ xForwardedFor: '127.0.0.2,127.0.0.1', id: vodVideoId, currentTimes: [ 0, 1 ] })
39 await servers[0].views.simulateViewer({ xForwardedFor: '127.0.0.3,127.0.0.1', id: vodVideoId, currentTimes: [ 1, 3 ] })
40 await servers[1].views.simulateViewer({ xForwardedFor: '127.0.0.2,127.0.0.1', id: vodVideoId, currentTimes: [ 4 ] })
41 await servers[1].views.simulateViewer({ xForwardedFor: '127.0.0.3,127.0.0.1', id: vodVideoId, currentTimes: [ 0, 1 ] })
42
43 await processViewersStats(servers)
44
45 const { data } = await servers[0].videoStats.getRetentionStats({ videoId: vodVideoId })
46 expect(data).to.have.lengthOf(6)
47
48 expect(data.map(d => d.retentionPercent)).to.deep.equal([ 50, 75, 25, 25, 25, 0 ])
49 })
50 })
51
52 after(async function () {
53 await cleanupTests(servers)
54 })
55 })