1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { HttpStatusCode } from '@shared/core-utils'
10 getVideosListWithToken,
16 setAccessTokensToServers,
20 } from '@shared/extra-utils'
21 import { Video, VideoDetails } from '@shared/models'
23 const expect = chai.expect
25 describe('Test videos history', function () {
26 let server: ServerInfo = null
27 let video1UUID: string
28 let video2UUID: string
29 let video3UUID: string
30 let video3WatchedDate: Date
31 let userAccessToken: string
32 let command: HistoryCommand
34 before(async function () {
37 server = await flushAndRunServer(1)
39 await setAccessTokensToServers([ server ])
41 command = server.historyCommand
44 const res = await uploadVideo(server.url, server.accessToken, { name: 'video 1' })
45 video1UUID = res.body.video.uuid
49 const res = await uploadVideo(server.url, server.accessToken, { name: 'video 2' })
50 video2UUID = res.body.video.uuid
54 const res = await uploadVideo(server.url, server.accessToken, { name: 'video 3' })
55 video3UUID = res.body.video.uuid
60 password: 'super password'
62 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
63 userAccessToken = await server.loginCommand.getAccessToken(user)
66 it('Should get videos, without watching history', async function () {
67 const res = await getVideosListWithToken(server.url, server.accessToken)
68 const videos: Video[] = res.body.data
70 for (const video of videos) {
71 const resDetail = await getVideoWithToken(server.url, server.accessToken, video.id)
72 const videoDetails: VideoDetails = resDetail.body
74 expect(video.userHistory).to.be.undefined
75 expect(videoDetails.userHistory).to.be.undefined
79 it('Should watch the first and second video', async function () {
80 await command.wathVideo({ videoId: video2UUID, currentTime: 8 })
81 await command.wathVideo({ videoId: video1UUID, currentTime: 3 })
84 it('Should return the correct history when listing, searching and getting videos', async function () {
85 const videosOfVideos: Video[][] = []
88 const res = await getVideosListWithToken(server.url, server.accessToken)
89 videosOfVideos.push(res.body.data)
93 const body = await server.searchCommand.searchVideos({ token: server.accessToken, search: 'video' })
94 videosOfVideos.push(body.data)
97 for (const videos of videosOfVideos) {
98 const video1 = videos.find(v => v.uuid === video1UUID)
99 const video2 = videos.find(v => v.uuid === video2UUID)
100 const video3 = videos.find(v => v.uuid === video3UUID)
102 expect(video1.userHistory).to.not.be.undefined
103 expect(video1.userHistory.currentTime).to.equal(3)
105 expect(video2.userHistory).to.not.be.undefined
106 expect(video2.userHistory.currentTime).to.equal(8)
108 expect(video3.userHistory).to.be.undefined
112 const resDetail = await getVideoWithToken(server.url, server.accessToken, video1UUID)
113 const videoDetails: VideoDetails = resDetail.body
115 expect(videoDetails.userHistory).to.not.be.undefined
116 expect(videoDetails.userHistory.currentTime).to.equal(3)
120 const resDetail = await getVideoWithToken(server.url, server.accessToken, video2UUID)
121 const videoDetails: VideoDetails = resDetail.body
123 expect(videoDetails.userHistory).to.not.be.undefined
124 expect(videoDetails.userHistory.currentTime).to.equal(8)
128 const resDetail = await getVideoWithToken(server.url, server.accessToken, video3UUID)
129 const videoDetails: VideoDetails = resDetail.body
131 expect(videoDetails.userHistory).to.be.undefined
135 it('Should have these videos when listing my history', async function () {
136 video3WatchedDate = new Date()
137 await command.wathVideo({ videoId: video3UUID, currentTime: 2 })
139 const body = await command.list()
141 expect(body.total).to.equal(3)
143 const videos = body.data
144 expect(videos[0].name).to.equal('video 3')
145 expect(videos[1].name).to.equal('video 1')
146 expect(videos[2].name).to.equal('video 2')
149 it('Should not have videos history on another user', async function () {
150 const body = await command.list({ token: userAccessToken })
152 expect(body.total).to.equal(0)
153 expect(body.data).to.have.lengthOf(0)
156 it('Should be able to search through videos in my history', async function () {
157 const body = await command.list({ search: '2' })
158 expect(body.total).to.equal(1)
160 const videos = body.data
161 expect(videos[0].name).to.equal('video 2')
164 it('Should clear my history', async function () {
165 await command.remove({ beforeDate: video3WatchedDate.toISOString() })
168 it('Should have my history cleared', async function () {
169 const body = await command.list()
170 expect(body.total).to.equal(1)
172 const videos = body.data
173 expect(videos[0].name).to.equal('video 3')
176 it('Should disable videos history', async function () {
179 accessToken: server.accessToken,
180 videosHistoryEnabled: false
183 await command.wathVideo({ videoId: video2UUID, currentTime: 8, expectedStatus: HttpStatusCode.CONFLICT_409 })
186 it('Should re-enable videos history', async function () {
189 accessToken: server.accessToken,
190 videosHistoryEnabled: true
193 await command.wathVideo({ videoId: video1UUID, currentTime: 8 })
195 const body = await command.list()
196 expect(body.total).to.equal(2)
198 const videos = body.data
199 expect(videos[0].name).to.equal('video 1')
200 expect(videos[1].name).to.equal('video 3')
203 it('Should not clean old history', async function () {
206 await killallServers([ server ])
208 await reRunServer(server, { history: { videos: { max_age: '10 days' } } })
212 // Should still have history
214 const body = await command.list()
215 expect(body.total).to.equal(2)
218 it('Should clean old history', async function () {
221 await killallServers([ server ])
223 await reRunServer(server, { history: { videos: { max_age: '5 seconds' } } })
227 const body = await command.list()
228 expect(body.total).to.equal(0)
231 after(async function () {
232 await cleanupTests([ server ])