]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/live/live-views.ts
Move test functions outside extra-utils
[github/Chocobozzz/PeerTube.git] / server / tests / api / live / live-views.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import * as chai from 'chai'
5 import { FfmpegCommand } from 'fluent-ffmpeg'
6 import { wait } from '@shared/core-utils'
7 import { VideoPrivacy } from '@shared/models'
8 import {
9 cleanupTests,
10 createMultipleServers,
11 doubleFollow,
12 PeerTubeServer,
13 setAccessTokensToServers,
14 setDefaultVideoChannel,
15 stopFfmpeg,
16 waitJobs,
17 waitUntilLivePublishedOnAllServers
18 } from '@shared/server-commands'
19
20 const expect = chai.expect
21
22 describe('Live views', function () {
23 let servers: PeerTubeServer[] = []
24
25 before(async function () {
26 this.timeout(120000)
27
28 servers = await createMultipleServers(2)
29
30 // Get the access tokens
31 await setAccessTokensToServers(servers)
32 await setDefaultVideoChannel(servers)
33
34 await servers[0].config.updateCustomSubConfig({
35 newConfig: {
36 live: {
37 enabled: true,
38 allowReplay: true,
39 transcoding: {
40 enabled: false
41 }
42 }
43 }
44 })
45
46 // Server 1 and server 2 follow each other
47 await doubleFollow(servers[0], servers[1])
48 })
49
50 let liveVideoId: string
51 let command: FfmpegCommand
52
53 async function countViewers (expectedViewers: number) {
54 for (const server of servers) {
55 const video = await server.videos.get({ id: liveVideoId })
56 expect(video.viewers).to.equal(expectedViewers)
57 }
58 }
59
60 async function countViews (expectedViews: number) {
61 for (const server of servers) {
62 const video = await server.videos.get({ id: liveVideoId })
63 expect(video.views).to.equal(expectedViews)
64 }
65 }
66
67 before(async function () {
68 this.timeout(30000)
69
70 const liveAttributes = {
71 name: 'live video',
72 channelId: servers[0].store.channel.id,
73 privacy: VideoPrivacy.PUBLIC
74 }
75
76 const live = await servers[0].live.create({ fields: liveAttributes })
77 liveVideoId = live.uuid
78
79 command = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoId })
80 await waitUntilLivePublishedOnAllServers(servers, liveVideoId)
81 await waitJobs(servers)
82 })
83
84 it('Should display no views and viewers for a live', async function () {
85 await countViews(0)
86 await countViewers(0)
87 })
88
89 it('Should view a live twice and display 1 view/viewer', async function () {
90 this.timeout(30000)
91
92 await servers[0].videos.view({ id: liveVideoId })
93 await servers[0].videos.view({ id: liveVideoId })
94
95 await waitJobs(servers)
96 await countViewers(1)
97
98 await wait(7000)
99 await countViews(1)
100 })
101
102 it('Should wait and display 0 viewers while still have 1 view', async function () {
103 this.timeout(30000)
104
105 await wait(12000)
106 await waitJobs(servers)
107
108 await countViews(1)
109 await countViewers(0)
110 })
111
112 it('Should view a live on a remote and on local and display 2 viewers and 3 views', async function () {
113 this.timeout(30000)
114
115 await servers[0].videos.view({ id: liveVideoId })
116 await servers[1].videos.view({ id: liveVideoId })
117 await servers[1].videos.view({ id: liveVideoId })
118 await waitJobs(servers)
119
120 await countViewers(2)
121
122 await wait(7000)
123 await waitJobs(servers)
124
125 await countViews(3)
126 })
127
128 after(async function () {
129 await stopFfmpeg(command)
130 await cleanupTests(servers)
131 })
132 })