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 { Video } from '@shared/models'
7 import { cleanupTests, createSingleServer, killallServers, PeerTubeServer, setAccessTokensToServers } from '@shared/server-commands'
9 const expect = chai.expect
11 describe('Test videos history', function () {
12 let server: PeerTubeServer = null
14 let video1UUID: string
15 let video2UUID: string
16 let video3UUID: string
17 let video3WatchedDate: Date
18 let userAccessToken: string
20 before(async function () {
23 server = await createSingleServer(1)
25 await setAccessTokensToServers([ server ])
28 const fixture = 'video_short1.webm'
31 const { id, uuid } = await server.videos.upload({ attributes: { name: 'video 1', fixture } })
37 const { uuid } = await server.videos.upload({ attributes: { name: 'video 2', fixture } })
42 const { uuid } = await server.videos.upload({ attributes: { name: 'video 3', fixture } })
46 userAccessToken = await server.users.generateUserAndToken('user_1')
49 it('Should get videos, without watching history', async function () {
50 const { data } = await server.videos.listWithToken()
52 for (const video of data) {
53 const videoDetails = await server.videos.getWithToken({ id: video.id })
55 expect(video.userHistory).to.be.undefined
56 expect(videoDetails.userHistory).to.be.undefined
60 it('Should watch the first and second video', async function () {
61 await server.views.view({ id: video2UUID, token: server.accessToken, currentTime: 8 })
62 await server.views.view({ id: video1UUID, token: server.accessToken, currentTime: 3 })
65 it('Should return the correct history when listing, searching and getting videos', async function () {
66 const videosOfVideos: Video[][] = []
69 const { data } = await server.videos.listWithToken()
70 videosOfVideos.push(data)
74 const body = await server.search.searchVideos({ token: server.accessToken, search: 'video' })
75 videosOfVideos.push(body.data)
78 for (const videos of videosOfVideos) {
79 const video1 = videos.find(v => v.uuid === video1UUID)
80 const video2 = videos.find(v => v.uuid === video2UUID)
81 const video3 = videos.find(v => v.uuid === video3UUID)
83 expect(video1.userHistory).to.not.be.undefined
84 expect(video1.userHistory.currentTime).to.equal(3)
86 expect(video2.userHistory).to.not.be.undefined
87 expect(video2.userHistory.currentTime).to.equal(8)
89 expect(video3.userHistory).to.be.undefined
93 const videoDetails = await server.videos.getWithToken({ id: video1UUID })
95 expect(videoDetails.userHistory).to.not.be.undefined
96 expect(videoDetails.userHistory.currentTime).to.equal(3)
100 const videoDetails = await server.videos.getWithToken({ id: video2UUID })
102 expect(videoDetails.userHistory).to.not.be.undefined
103 expect(videoDetails.userHistory.currentTime).to.equal(8)
107 const videoDetails = await server.videos.getWithToken({ id: video3UUID })
109 expect(videoDetails.userHistory).to.be.undefined
113 it('Should have these videos when listing my history', async function () {
114 video3WatchedDate = new Date()
115 await server.views.view({ id: video3UUID, token: server.accessToken, currentTime: 2 })
117 const body = await server.history.list()
119 expect(body.total).to.equal(3)
121 const videos = body.data
122 expect(videos[0].name).to.equal('video 3')
123 expect(videos[1].name).to.equal('video 1')
124 expect(videos[2].name).to.equal('video 2')
127 it('Should not have videos history on another user', async function () {
128 const body = await server.history.list({ token: userAccessToken })
130 expect(body.total).to.equal(0)
131 expect(body.data).to.have.lengthOf(0)
134 it('Should be able to search through videos in my history', async function () {
135 const body = await server.history.list({ search: '2' })
136 expect(body.total).to.equal(1)
138 const videos = body.data
139 expect(videos[0].name).to.equal('video 2')
142 it('Should clear my history', async function () {
143 await server.history.removeAll({ beforeDate: video3WatchedDate.toISOString() })
146 it('Should have my history cleared', async function () {
147 const body = await server.history.list()
148 expect(body.total).to.equal(1)
150 const videos = body.data
151 expect(videos[0].name).to.equal('video 3')
154 it('Should disable videos history', async function () {
155 await server.users.updateMe({
156 videosHistoryEnabled: false
159 await server.views.view({ id: video2UUID, token: server.accessToken, currentTime: 8 })
161 const { data } = await server.history.list()
162 expect(data[0].name).to.not.equal('video 2')
165 it('Should re-enable videos history', async function () {
166 await server.users.updateMe({
167 videosHistoryEnabled: true
170 await server.views.view({ id: video2UUID, token: server.accessToken, currentTime: 8 })
172 const { data } = await server.history.list()
173 expect(data[0].name).to.equal('video 2')
176 it('Should not clean old history', async function () {
179 await killallServers([ server ])
181 await server.run({ history: { videos: { max_age: '10 days' } } })
185 // Should still have history
187 const body = await server.history.list()
188 expect(body.total).to.equal(2)
191 it('Should clean old history', async function () {
194 await killallServers([ server ])
196 await server.run({ history: { videos: { max_age: '5 seconds' } } })
200 const body = await server.history.list()
201 expect(body.total).to.equal(0)
204 it('Should delete a specific history element', async function () {
206 await server.views.view({ id: video1UUID, token: server.accessToken, currentTime: 4 })
207 await server.views.view({ id: video2UUID, token: server.accessToken, currentTime: 8 })
211 const body = await server.history.list()
212 expect(body.total).to.equal(2)
216 await server.history.removeElement({ videoId: video1Id })
218 const body = await server.history.list()
219 expect(body.total).to.equal(1)
220 expect(body.data[0].uuid).to.equal(video2UUID)
224 after(async function () {
225 await cleanupTests([ server ])