aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/api/views/video-views-retention-stats.ts
blob: 5b9ce4c929c11d197c0a4c11771529a7d34218e2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */

import { expect } from 'chai'
import { prepareViewsServers, prepareViewsVideos, processViewersStats } from '@server/tests/shared'
import { cleanupTests, PeerTubeServer } from '@shared/server-commands'

describe('Test views retention stats', function () {
  let servers: PeerTubeServer[]

  before(async function () {
    this.timeout(120000)

    servers = await prepareViewsServers()
  })

  describe('Test retention stats on VOD', function () {
    let vodVideoId: string

    before(async function () {
      this.timeout(240000);

      ({ vodVideoId } = await prepareViewsVideos({ servers, live: false, vod: true }))
    })

    it('Should display empty retention', async function () {
      const { data } = await servers[0].videoStats.getRetentionStats({ videoId: vodVideoId })
      expect(data).to.have.lengthOf(6)

      for (let i = 0; i < 6; i++) {
        expect(data[i].second).to.equal(i)
        expect(data[i].retentionPercent).to.equal(0)
      }
    })

    it('Should display appropriate retention metrics', async function () {
      await servers[0].views.simulateViewer({ xForwardedFor: '127.0.0.2,127.0.0.1', id: vodVideoId, currentTimes: [ 0, 1 ] })
      await servers[0].views.simulateViewer({ xForwardedFor: '127.0.0.3,127.0.0.1', id: vodVideoId, currentTimes: [ 1, 3 ] })
      await servers[1].views.simulateViewer({ xForwardedFor: '127.0.0.2,127.0.0.1', id: vodVideoId, currentTimes: [ 4 ] })
      await servers[1].views.simulateViewer({ xForwardedFor: '127.0.0.3,127.0.0.1', id: vodVideoId, currentTimes: [ 0, 1 ] })

      await processViewersStats(servers)

      const { data } = await servers[0].videoStats.getRetentionStats({ videoId: vodVideoId })
      expect(data).to.have.lengthOf(6)

      expect(data.map(d => d.retentionPercent)).to.deep.equal([ 50, 75, 25, 25, 25, 0 ])
    })
  })

  after(async function () {
    await cleanupTests(servers)
  })
})