1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { wait } from '@shared/core-utils'
6 import { VideoPrivacy } from '@shared/models'
10 createMultipleServers,
13 setAccessTokensToServers,
14 setDefaultVideoChannel,
16 } from '@shared/server-commands'
17 import { checkLiveCleanupAfterSave } from '../../shared'
19 const expect = chai.expect
21 describe('Test live constraints', function () {
22 let servers: PeerTubeServer[] = []
24 let userAccessToken: string
25 let userChannelId: number
27 async function createLiveWrapper (saveReplay: boolean) {
28 const liveAttributes = {
30 channelId: userChannelId,
31 privacy: VideoPrivacy.PUBLIC,
35 const { uuid } = await servers[0].live.create({ token: userAccessToken, fields: liveAttributes })
39 async function checkSaveReplay (videoId: string, resolutions = [ 720 ]) {
40 for (const server of servers) {
41 const video = await server.videos.get({ id: videoId })
42 expect(video.isLive).to.be.false
43 expect(video.duration).to.be.greaterThan(0)
46 await checkLiveCleanupAfterSave(servers[0], videoId, resolutions)
49 async function waitUntilLivePublishedOnAllServers (videoId: string) {
50 for (const server of servers) {
51 await server.live.waitUntilPublished({ videoId })
55 function updateQuota (options: { total: number, daily: number }) {
56 return servers[0].users.update({
58 videoQuota: options.total,
59 videoQuotaDaily: options.daily
63 before(async function () {
66 servers = await createMultipleServers(2)
68 // Get the access tokens
69 await setAccessTokensToServers(servers)
70 await setDefaultVideoChannel(servers)
72 await servers[0].config.updateCustomSubConfig({
85 const res = await servers[0].users.generate('user1')
87 userChannelId = res.userChannelId
88 userAccessToken = res.token
90 await updateQuota({ total: 1, daily: -1 })
93 // Server 1 and server 2 follow each other
94 await doubleFollow(servers[0], servers[1])
97 it('Should not have size limit if save replay is disabled', async function () {
100 const userVideoLiveoId = await createLiveWrapper(false)
101 await servers[0].live.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: false })
104 it('Should have size limit depending on user global quota if save replay is enabled', async function () {
107 // Wait for user quota memoize cache invalidation
110 const userVideoLiveoId = await createLiveWrapper(true)
111 await servers[0].live.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: true })
113 await waitUntilLivePublishedOnAllServers(userVideoLiveoId)
114 await waitJobs(servers)
116 await checkSaveReplay(userVideoLiveoId)
119 it('Should have size limit depending on user daily quota if save replay is enabled', async function () {
122 // Wait for user quota memoize cache invalidation
125 await updateQuota({ total: -1, daily: 1 })
127 const userVideoLiveoId = await createLiveWrapper(true)
128 await servers[0].live.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: true })
130 await waitUntilLivePublishedOnAllServers(userVideoLiveoId)
131 await waitJobs(servers)
133 await checkSaveReplay(userVideoLiveoId)
136 it('Should succeed without quota limit', async function () {
139 // Wait for user quota memoize cache invalidation
142 await updateQuota({ total: 10 * 1000 * 1000, daily: -1 })
144 const userVideoLiveoId = await createLiveWrapper(true)
145 await servers[0].live.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: false })
148 it('Should have max duration limit', async function () {
151 await servers[0].config.updateCustomSubConfig({
159 resolutions: ConfigCommand.getCustomConfigResolutions(true)
165 const userVideoLiveoId = await createLiveWrapper(true)
166 await servers[0].live.runAndTestStreamError({ token: userAccessToken, videoId: userVideoLiveoId, shouldHaveError: true })
168 await waitUntilLivePublishedOnAllServers(userVideoLiveoId)
169 await waitJobs(servers)
171 await checkSaveReplay(userVideoLiveoId, [ 720, 480, 360, 240, 144 ])
174 after(async function () {
175 await cleanupTests(servers)