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,
21 } from '@shared/extra-utils'
22 import { Video, VideoDetails } from '@shared/models'
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
33 let command: HistoryCommand
35 before(async function () {
38 server = await flushAndRunServer(1)
40 await setAccessTokensToServers([ server ])
42 command = server.historyCommand
45 const res = await uploadVideo(server.url, server.accessToken, { name: 'video 1' })
46 video1UUID = res.body.video.uuid
50 const res = await uploadVideo(server.url, server.accessToken, { name: 'video 2' })
51 video2UUID = res.body.video.uuid
55 const res = await uploadVideo(server.url, server.accessToken, { name: 'video 3' })
56 video3UUID = res.body.video.uuid
61 password: 'super password'
63 await createUser({ url: server.url, accessToken: server.accessToken, username: user.username, password: user.password })
64 userAccessToken = await userLogin(server, user)
67 it('Should get videos, without watching history', async function () {
68 const res = await getVideosListWithToken(server.url, server.accessToken)
69 const videos: Video[] = res.body.data
71 for (const video of videos) {
72 const resDetail = await getVideoWithToken(server.url, server.accessToken, video.id)
73 const videoDetails: VideoDetails = resDetail.body
75 expect(video.userHistory).to.be.undefined
76 expect(videoDetails.userHistory).to.be.undefined
80 it('Should watch the first and second video', async function () {
81 await command.wathVideo({ videoId: video2UUID, currentTime: 8 })
82 await command.wathVideo({ videoId: video1UUID, currentTime: 3 })
85 it('Should return the correct history when listing, searching and getting videos', async function () {
86 const videosOfVideos: Video[][] = []
89 const res = await getVideosListWithToken(server.url, server.accessToken)
90 videosOfVideos.push(res.body.data)
94 const body = await server.searchCommand.searchVideos({ token: server.accessToken, search: 'video' })
95 videosOfVideos.push(body.data)
98 for (const videos of videosOfVideos) {
99 const video1 = videos.find(v => v.uuid === video1UUID)
100 const video2 = videos.find(v => v.uuid === video2UUID)
101 const video3 = videos.find(v => v.uuid === video3UUID)
103 expect(video1.userHistory).to.not.be.undefined
104 expect(video1.userHistory.currentTime).to.equal(3)
106 expect(video2.userHistory).to.not.be.undefined
107 expect(video2.userHistory.currentTime).to.equal(8)
109 expect(video3.userHistory).to.be.undefined
113 const resDetail = await getVideoWithToken(server.url, server.accessToken, video1UUID)
114 const videoDetails: VideoDetails = resDetail.body
116 expect(videoDetails.userHistory).to.not.be.undefined
117 expect(videoDetails.userHistory.currentTime).to.equal(3)
121 const resDetail = await getVideoWithToken(server.url, server.accessToken, video2UUID)
122 const videoDetails: VideoDetails = resDetail.body
124 expect(videoDetails.userHistory).to.not.be.undefined
125 expect(videoDetails.userHistory.currentTime).to.equal(8)
129 const resDetail = await getVideoWithToken(server.url, server.accessToken, video3UUID)
130 const videoDetails: VideoDetails = resDetail.body
132 expect(videoDetails.userHistory).to.be.undefined
136 it('Should have these videos when listing my history', async function () {
137 video3WatchedDate = new Date()
138 await command.wathVideo({ videoId: video3UUID, currentTime: 2 })
140 const body = await command.list()
142 expect(body.total).to.equal(3)
144 const videos = body.data
145 expect(videos[0].name).to.equal('video 3')
146 expect(videos[1].name).to.equal('video 1')
147 expect(videos[2].name).to.equal('video 2')
150 it('Should not have videos history on another user', async function () {
151 const body = await command.list({ token: userAccessToken })
153 expect(body.total).to.equal(0)
154 expect(body.data).to.have.lengthOf(0)
157 it('Should be able to search through videos in my history', async function () {
158 const body = await command.list({ search: '2' })
159 expect(body.total).to.equal(1)
161 const videos = body.data
162 expect(videos[0].name).to.equal('video 2')
165 it('Should clear my history', async function () {
166 await command.remove({ beforeDate: video3WatchedDate.toISOString() })
169 it('Should have my history cleared', async function () {
170 const body = await command.list()
171 expect(body.total).to.equal(1)
173 const videos = body.data
174 expect(videos[0].name).to.equal('video 3')
177 it('Should disable videos history', async function () {
180 accessToken: server.accessToken,
181 videosHistoryEnabled: false
184 await command.wathVideo({ videoId: video2UUID, currentTime: 8, expectedStatus: HttpStatusCode.CONFLICT_409 })
187 it('Should re-enable videos history', async function () {
190 accessToken: server.accessToken,
191 videosHistoryEnabled: true
194 await command.wathVideo({ videoId: video1UUID, currentTime: 8 })
196 const body = await command.list()
197 expect(body.total).to.equal(2)
199 const videos = body.data
200 expect(videos[0].name).to.equal('video 1')
201 expect(videos[1].name).to.equal('video 3')
204 it('Should not clean old history', async function () {
207 await killallServers([ server ])
209 await reRunServer(server, { history: { videos: { max_age: '10 days' } } })
213 // Should still have history
215 const body = await command.list()
216 expect(body.total).to.equal(2)
219 it('Should clean old history', async function () {
222 await killallServers([ server ])
224 await reRunServer(server, { history: { videos: { max_age: '5 seconds' } } })
228 const body = await command.list()
229 expect(body.total).to.equal(0)
232 after(async function () {
233 await cleanupTests([ server ])