]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/views/video-views-counter.ts
Improve viewer counter
[github/Chocobozzz/PeerTube.git] / server / tests / api / views / video-views-counter.ts
CommitLineData
b2111066
C
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3import 'mocha'
4import * as chai from 'chai'
5import { FfmpegCommand } from 'fluent-ffmpeg'
6import { prepareViewsServers, prepareViewsVideos, processViewsBuffer } from '@server/tests/shared'
7import { wait } from '@shared/core-utils'
8import { cleanupTests, PeerTubeServer, stopFfmpeg, waitJobs } from '@shared/server-commands'
9
10const expect = chai.expect
11
12describe('Test video views/viewers counters', function () {
13 let servers: PeerTubeServer[]
14
15 async function checkCounter (field: 'views' | 'viewers', id: string, expected: number) {
16 for (const server of servers) {
17 const video = await server.videos.get({ id })
18
19 const messageSuffix = video.isLive
20 ? 'live video'
21 : 'vod video'
22
23 expect(video[field]).to.equal(expected, `${field} not valid on server ${server.serverNumber} for ${messageSuffix} ${video.uuid}`)
24 }
25 }
26
27 before(async function () {
28 this.timeout(120000)
29
30 servers = await prepareViewsServers()
31 })
32
33 describe('Test views counter on VOD', function () {
34 let videoUUID: string
35
36 before(async function () {
37 this.timeout(30000)
38
39 const { uuid } = await servers[0].videos.quickUpload({ name: 'video' })
40 videoUUID = uuid
41
42 await waitJobs(servers)
43 })
44
45 it('Should not view a video if watch time is below the threshold', async function () {
46 await servers[0].views.simulateViewer({ id: videoUUID, currentTimes: [ 1, 2 ] })
47 await processViewsBuffer(servers)
48
49 await checkCounter('views', videoUUID, 0)
50 })
51
52 it('Should view a video if watch time is above the threshold', async function () {
53 await servers[0].views.simulateViewer({ id: videoUUID, currentTimes: [ 1, 4 ] })
54 await processViewsBuffer(servers)
55
56 await checkCounter('views', videoUUID, 1)
57 })
58
59 it('Should not view again this video with the same IP', async function () {
ac907dc7
C
60 await servers[0].views.simulateViewer({ id: videoUUID, xForwardedFor: '0.0.0.1,127.0.0.1', currentTimes: [ 1, 4 ] })
61 await servers[0].views.simulateViewer({ id: videoUUID, xForwardedFor: '0.0.0.1,127.0.0.1', currentTimes: [ 1, 4 ] })
b2111066
C
62 await processViewsBuffer(servers)
63
ac907dc7 64 await checkCounter('views', videoUUID, 2)
b2111066
C
65 })
66
67 it('Should view the video from server 2 and send the event', async function () {
68 await servers[1].views.simulateViewer({ id: videoUUID, currentTimes: [ 1, 4 ] })
69 await waitJobs(servers)
70 await processViewsBuffer(servers)
71
ac907dc7 72 await checkCounter('views', videoUUID, 3)
b2111066
C
73 })
74 })
75
76 describe('Test views and viewers counters on live and VOD', function () {
77 let liveVideoId: string
78 let vodVideoId: string
79 let command: FfmpegCommand
80
81 before(async function () {
ac907dc7 82 this.timeout(120000);
b2111066
C
83
84 ({ vodVideoId, liveVideoId, ffmpegCommand: command } = await prepareViewsVideos({ servers, live: true, vod: true }))
85 })
86
87 it('Should display no views and viewers', async function () {
88 await checkCounter('views', liveVideoId, 0)
89 await checkCounter('viewers', liveVideoId, 0)
90
91 await checkCounter('views', vodVideoId, 0)
92 await checkCounter('viewers', vodVideoId, 0)
93 })
94
95 it('Should view twice and display 1 view/viewer', async function () {
96 this.timeout(30000)
97
98 await servers[0].views.simulateViewer({ id: liveVideoId, currentTimes: [ 0, 35 ] })
99 await servers[0].views.simulateViewer({ id: liveVideoId, currentTimes: [ 0, 35 ] })
100 await servers[0].views.simulateViewer({ id: vodVideoId, currentTimes: [ 0, 5 ] })
101 await servers[0].views.simulateViewer({ id: vodVideoId, currentTimes: [ 0, 5 ] })
102
103 await waitJobs(servers)
104 await checkCounter('viewers', liveVideoId, 1)
105 await checkCounter('viewers', vodVideoId, 1)
106
107 await processViewsBuffer(servers)
108
109 await checkCounter('views', liveVideoId, 1)
110 await checkCounter('views', vodVideoId, 1)
111 })
112
113 it('Should wait and display 0 viewers but still have 1 view', async function () {
114 this.timeout(30000)
115
116 await wait(12000)
117 await waitJobs(servers)
118
119 await checkCounter('views', liveVideoId, 1)
120 await checkCounter('viewers', liveVideoId, 0)
121
122 await checkCounter('views', vodVideoId, 1)
123 await checkCounter('viewers', vodVideoId, 0)
124 })
125
126 it('Should view on a remote and on local and display 2 viewers and 3 views', async function () {
127 this.timeout(30000)
128
129 await servers[0].views.simulateViewer({ id: vodVideoId, currentTimes: [ 0, 5 ] })
130 await servers[1].views.simulateViewer({ id: vodVideoId, currentTimes: [ 0, 5 ] })
131 await servers[1].views.simulateViewer({ id: vodVideoId, currentTimes: [ 0, 5 ] })
132
133 await servers[0].views.simulateViewer({ id: liveVideoId, currentTimes: [ 0, 35 ] })
134 await servers[1].views.simulateViewer({ id: liveVideoId, currentTimes: [ 0, 35 ] })
135 await servers[1].views.simulateViewer({ id: liveVideoId, currentTimes: [ 0, 35 ] })
136
137 await waitJobs(servers)
138
139 await checkCounter('viewers', liveVideoId, 2)
140 await checkCounter('viewers', vodVideoId, 2)
141
142 await processViewsBuffer(servers)
143
144 await checkCounter('views', liveVideoId, 3)
145 await checkCounter('views', vodVideoId, 3)
146 })
147
148 after(async function () {
149 await stopFfmpeg(command)
150 })
151 })
152
153 after(async function () {
154 await cleanupTests(servers)
155 })
156})