1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import * as chai from 'chai'
4 import { wait } from '@shared/core-utils'
5 import { Video } from '@shared/models'
6 import { cleanupTests, createSingleServer, killallServers, PeerTubeServer, setAccessTokensToServers } from '@shared/server-commands'
8 const expect = chai.expect
10 describe('Test videos history', function () {
11 let server: PeerTubeServer = null
13 let video1UUID: string
14 let video2UUID: string
15 let video3UUID: string
16 let video3WatchedDate: Date
17 let userAccessToken: string
19 before(async function () {
22 server = await createSingleServer(1)
24 await setAccessTokensToServers([ server ])
27 const fixture = 'video_short1.webm'
30 const { id, uuid } = await server.videos.upload({ attributes: { name: 'video 1', fixture } })
36 const { uuid } = await server.videos.upload({ attributes: { name: 'video 2', fixture } })
41 const { uuid } = await server.videos.upload({ attributes: { name: 'video 3', fixture } })
45 userAccessToken = await server.users.generateUserAndToken('user_1')
48 it('Should get videos, without watching history', async function () {
49 const { data } = await server.videos.listWithToken()
51 for (const video of data) {
52 const videoDetails = await server.videos.getWithToken({ id: video.id })
54 expect(video.userHistory).to.be.undefined
55 expect(videoDetails.userHistory).to.be.undefined
59 it('Should watch the first and second video', async function () {
60 await server.views.view({ id: video2UUID, token: server.accessToken, currentTime: 8 })
61 await server.views.view({ id: video1UUID, token: server.accessToken, currentTime: 3 })
64 it('Should return the correct history when listing, searching and getting videos', async function () {
65 const videosOfVideos: Video[][] = []
68 const { data } = await server.videos.listWithToken()
69 videosOfVideos.push(data)
73 const body = await server.search.searchVideos({ token: server.accessToken, search: 'video' })
74 videosOfVideos.push(body.data)
77 for (const videos of videosOfVideos) {
78 const video1 = videos.find(v => v.uuid === video1UUID)
79 const video2 = videos.find(v => v.uuid === video2UUID)
80 const video3 = videos.find(v => v.uuid === video3UUID)
82 expect(video1.userHistory).to.not.be.undefined
83 expect(video1.userHistory.currentTime).to.equal(3)
85 expect(video2.userHistory).to.not.be.undefined
86 expect(video2.userHistory.currentTime).to.equal(8)
88 expect(video3.userHistory).to.be.undefined
92 const videoDetails = await server.videos.getWithToken({ id: video1UUID })
94 expect(videoDetails.userHistory).to.not.be.undefined
95 expect(videoDetails.userHistory.currentTime).to.equal(3)
99 const videoDetails = await server.videos.getWithToken({ id: video2UUID })
101 expect(videoDetails.userHistory).to.not.be.undefined
102 expect(videoDetails.userHistory.currentTime).to.equal(8)
106 const videoDetails = await server.videos.getWithToken({ id: video3UUID })
108 expect(videoDetails.userHistory).to.be.undefined
112 it('Should have these videos when listing my history', async function () {
113 video3WatchedDate = new Date()
114 await server.views.view({ id: video3UUID, token: server.accessToken, currentTime: 2 })
116 const body = await server.history.list()
118 expect(body.total).to.equal(3)
120 const videos = body.data
121 expect(videos[0].name).to.equal('video 3')
122 expect(videos[1].name).to.equal('video 1')
123 expect(videos[2].name).to.equal('video 2')
126 it('Should not have videos history on another user', async function () {
127 const body = await server.history.list({ token: userAccessToken })
129 expect(body.total).to.equal(0)
130 expect(body.data).to.have.lengthOf(0)
133 it('Should be able to search through videos in my history', async function () {
134 const body = await server.history.list({ search: '2' })
135 expect(body.total).to.equal(1)
137 const videos = body.data
138 expect(videos[0].name).to.equal('video 2')
141 it('Should clear my history', async function () {
142 await server.history.removeAll({ beforeDate: video3WatchedDate.toISOString() })
145 it('Should have my history cleared', async function () {
146 const body = await server.history.list()
147 expect(body.total).to.equal(1)
149 const videos = body.data
150 expect(videos[0].name).to.equal('video 3')
153 it('Should disable videos history', async function () {
154 await server.users.updateMe({
155 videosHistoryEnabled: false
158 await server.views.view({ id: video2UUID, token: server.accessToken, currentTime: 8 })
160 const { data } = await server.history.list()
161 expect(data[0].name).to.not.equal('video 2')
164 it('Should re-enable videos history', async function () {
165 await server.users.updateMe({
166 videosHistoryEnabled: true
169 await server.views.view({ id: video2UUID, token: server.accessToken, currentTime: 8 })
171 const { data } = await server.history.list()
172 expect(data[0].name).to.equal('video 2')
175 it('Should not clean old history', async function () {
178 await killallServers([ server ])
180 await server.run({ history: { videos: { max_age: '10 days' } } })
184 // Should still have history
186 const body = await server.history.list()
187 expect(body.total).to.equal(2)
190 it('Should clean old history', async function () {
193 await killallServers([ server ])
195 await server.run({ history: { videos: { max_age: '5 seconds' } } })
199 const body = await server.history.list()
200 expect(body.total).to.equal(0)
203 it('Should delete a specific history element', async function () {
205 await server.views.view({ id: video1UUID, token: server.accessToken, currentTime: 4 })
206 await server.views.view({ id: video2UUID, token: server.accessToken, currentTime: 8 })
210 const body = await server.history.list()
211 expect(body.total).to.equal(2)
215 await server.history.removeElement({ videoId: video1Id })
217 const body = await server.history.list()
218 expect(body.total).to.equal(1)
219 expect(body.data[0].uuid).to.equal(video2UUID)
223 after(async function () {
224 await cleanupTests([ server ])