1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import * as chai from 'chai'
9 getVideosListWithToken,
15 setAccessTokensToServers,
20 } from '../../../../shared/extra-utils'
21 import { Video, VideoDetails } from '../../../../shared/models/videos'
22 import { listMyVideosHistory, removeMyVideosHistory, userWatchVideo } from '../../../../shared/extra-utils/videos/video-history'
24 const expect = chai.expect
26 describe('Test videos history', function () {
27 let server: ServerInfo = null
28 let video1UUID: string
29 let video2UUID: string
30 let video3UUID: string
31 let video3WatchedDate: Date
32 let userAccessToken: string
34 before(async function () {
37 server = await flushAndRunServer(1)
39 await setAccessTokensToServers([ server ])
42 const res = await uploadVideo(server.url, server.accessToken, { name: 'video 1' })
43 video1UUID = res.body.video.uuid
47 const res = await uploadVideo(server.url, server.accessToken, { name: 'video 2' })
48 video2UUID = res.body.video.uuid
52 const res = await uploadVideo(server.url, server.accessToken, { name: 'video 3' })
53 video3UUID = res.body.video.uuid
58 password: 'super password'
60 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
61 userAccessToken = await userLogin(server, user)
64 it('Should get videos, without watching history', async function () {
65 const res = await getVideosListWithToken(server.url, server.accessToken)
66 const videos: Video[] = res.body.data
68 for (const video of videos) {
69 const resDetail = await getVideoWithToken(server.url, server.accessToken, video.id)
70 const videoDetails: VideoDetails = resDetail.body
72 expect(video.userHistory).to.be.undefined
73 expect(videoDetails.userHistory).to.be.undefined
77 it('Should watch the first and second video', async function () {
78 await userWatchVideo(server.url, server.accessToken, video2UUID, 8)
79 await userWatchVideo(server.url, server.accessToken, video1UUID, 3)
82 it('Should return the correct history when listing, searching and getting videos', async function () {
83 const videosOfVideos: Video[][] = []
86 const res = await getVideosListWithToken(server.url, server.accessToken)
87 videosOfVideos.push(res.body.data)
91 const res = await searchVideoWithToken(server.url, 'video', server.accessToken)
92 videosOfVideos.push(res.body.data)
95 for (const videos of videosOfVideos) {
96 const video1 = videos.find(v => v.uuid === video1UUID)
97 const video2 = videos.find(v => v.uuid === video2UUID)
98 const video3 = videos.find(v => v.uuid === video3UUID)
100 expect(video1.userHistory).to.not.be.undefined
101 expect(video1.userHistory.currentTime).to.equal(3)
103 expect(video2.userHistory).to.not.be.undefined
104 expect(video2.userHistory.currentTime).to.equal(8)
106 expect(video3.userHistory).to.be.undefined
110 const resDetail = await getVideoWithToken(server.url, server.accessToken, video1UUID)
111 const videoDetails: VideoDetails = resDetail.body
113 expect(videoDetails.userHistory).to.not.be.undefined
114 expect(videoDetails.userHistory.currentTime).to.equal(3)
118 const resDetail = await getVideoWithToken(server.url, server.accessToken, video2UUID)
119 const videoDetails: VideoDetails = resDetail.body
121 expect(videoDetails.userHistory).to.not.be.undefined
122 expect(videoDetails.userHistory.currentTime).to.equal(8)
126 const resDetail = await getVideoWithToken(server.url, server.accessToken, video3UUID)
127 const videoDetails: VideoDetails = resDetail.body
129 expect(videoDetails.userHistory).to.be.undefined
133 it('Should have these videos when listing my history', async function () {
134 video3WatchedDate = new Date()
135 await userWatchVideo(server.url, server.accessToken, video3UUID, 2)
137 const res = await listMyVideosHistory(server.url, server.accessToken)
139 expect(res.body.total).to.equal(3)
141 const videos: Video[] = res.body.data
142 expect(videos[0].name).to.equal('video 3')
143 expect(videos[1].name).to.equal('video 1')
144 expect(videos[2].name).to.equal('video 2')
147 it('Should not have videos history on another user', async function () {
148 const res = await listMyVideosHistory(server.url, userAccessToken)
150 expect(res.body.total).to.equal(0)
151 expect(res.body.data).to.have.lengthOf(0)
154 it('Should clear my history', async function () {
155 await removeMyVideosHistory(server.url, server.accessToken, video3WatchedDate.toISOString())
158 it('Should have my history cleared', async function () {
159 const res = await listMyVideosHistory(server.url, server.accessToken)
161 expect(res.body.total).to.equal(1)
163 const videos: Video[] = res.body.data
164 expect(videos[0].name).to.equal('video 3')
167 it('Should disable videos history', async function () {
170 accessToken: server.accessToken,
171 videosHistoryEnabled: false
174 await userWatchVideo(server.url, server.accessToken, video2UUID, 8, 409)
177 it('Should re-enable videos history', async function () {
180 accessToken: server.accessToken,
181 videosHistoryEnabled: true
184 await userWatchVideo(server.url, server.accessToken, video1UUID, 8)
186 const res = await listMyVideosHistory(server.url, server.accessToken)
188 expect(res.body.total).to.equal(2)
190 const videos: Video[] = res.body.data
191 expect(videos[0].name).to.equal('video 1')
192 expect(videos[1].name).to.equal('video 3')
195 it('Should not clean old history', async function () {
198 killallServers([ server ])
200 await reRunServer(server, { history: { videos: { max_age: '10 days' } } })
204 // Should still have history
206 const res = await listMyVideosHistory(server.url, server.accessToken)
208 expect(res.body.total).to.equal(2)
211 it('Should clean old history', async function () {
214 killallServers([ server ])
216 await reRunServer(server, { history: { videos: { max_age: '5 seconds' } } })
220 const res = await listMyVideosHistory(server.url, server.accessToken)
221 expect(res.body.total).to.equal(0)
224 after(async function () {
225 await cleanupTests([ server ])