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 { HttpStatusCode, Video } from '@shared/models'
13 setAccessTokensToServers
14 } from '@shared/server-commands'
16 const expect = chai.expect
18 describe('Test videos history', function () {
19 let server: PeerTubeServer = null
21 let video1UUID: string
22 let video2UUID: string
23 let video3UUID: string
24 let video3WatchedDate: Date
25 let userAccessToken: string
26 let command: HistoryCommand
28 before(async function () {
31 server = await createSingleServer(1)
33 await setAccessTokensToServers([ server ])
35 command = server.history
38 const { id, uuid } = await server.videos.upload({ attributes: { name: 'video 1' } })
44 const { uuid } = await server.videos.upload({ attributes: { name: 'video 2' } })
49 const { uuid } = await server.videos.upload({ attributes: { name: 'video 3' } })
55 password: 'super password'
57 await server.users.create({ username: user.username, password: user.password })
58 userAccessToken = await server.login.getAccessToken(user)
61 it('Should get videos, without watching history', async function () {
62 const { data } = await server.videos.listWithToken()
64 for (const video of data) {
65 const videoDetails = await server.videos.getWithToken({ id: video.id })
67 expect(video.userHistory).to.be.undefined
68 expect(videoDetails.userHistory).to.be.undefined
72 it('Should watch the first and second video', async function () {
73 await command.watchVideo({ videoId: video2UUID, currentTime: 8 })
74 await command.watchVideo({ videoId: video1UUID, currentTime: 3 })
77 it('Should return the correct history when listing, searching and getting videos', async function () {
78 const videosOfVideos: Video[][] = []
81 const { data } = await server.videos.listWithToken()
82 videosOfVideos.push(data)
86 const body = await server.search.searchVideos({ token: server.accessToken, search: 'video' })
87 videosOfVideos.push(body.data)
90 for (const videos of videosOfVideos) {
91 const video1 = videos.find(v => v.uuid === video1UUID)
92 const video2 = videos.find(v => v.uuid === video2UUID)
93 const video3 = videos.find(v => v.uuid === video3UUID)
95 expect(video1.userHistory).to.not.be.undefined
96 expect(video1.userHistory.currentTime).to.equal(3)
98 expect(video2.userHistory).to.not.be.undefined
99 expect(video2.userHistory.currentTime).to.equal(8)
101 expect(video3.userHistory).to.be.undefined
105 const videoDetails = await server.videos.getWithToken({ id: video1UUID })
107 expect(videoDetails.userHistory).to.not.be.undefined
108 expect(videoDetails.userHistory.currentTime).to.equal(3)
112 const videoDetails = await server.videos.getWithToken({ id: video2UUID })
114 expect(videoDetails.userHistory).to.not.be.undefined
115 expect(videoDetails.userHistory.currentTime).to.equal(8)
119 const videoDetails = await server.videos.getWithToken({ id: video3UUID })
121 expect(videoDetails.userHistory).to.be.undefined
125 it('Should have these videos when listing my history', async function () {
126 video3WatchedDate = new Date()
127 await command.watchVideo({ videoId: video3UUID, currentTime: 2 })
129 const body = await command.list()
131 expect(body.total).to.equal(3)
133 const videos = body.data
134 expect(videos[0].name).to.equal('video 3')
135 expect(videos[1].name).to.equal('video 1')
136 expect(videos[2].name).to.equal('video 2')
139 it('Should not have videos history on another user', async function () {
140 const body = await command.list({ token: userAccessToken })
142 expect(body.total).to.equal(0)
143 expect(body.data).to.have.lengthOf(0)
146 it('Should be able to search through videos in my history', async function () {
147 const body = await command.list({ search: '2' })
148 expect(body.total).to.equal(1)
150 const videos = body.data
151 expect(videos[0].name).to.equal('video 2')
154 it('Should clear my history', async function () {
155 await command.removeAll({ beforeDate: video3WatchedDate.toISOString() })
158 it('Should have my history cleared', async function () {
159 const body = await command.list()
160 expect(body.total).to.equal(1)
162 const videos = body.data
163 expect(videos[0].name).to.equal('video 3')
166 it('Should disable videos history', async function () {
167 await server.users.updateMe({
168 videosHistoryEnabled: false
171 await command.watchVideo({ videoId: video2UUID, currentTime: 8, expectedStatus: HttpStatusCode.CONFLICT_409 })
174 it('Should re-enable videos history', async function () {
175 await server.users.updateMe({
176 videosHistoryEnabled: true
179 await command.watchVideo({ videoId: video1UUID, currentTime: 8 })
181 const body = await command.list()
182 expect(body.total).to.equal(2)
184 const videos = body.data
185 expect(videos[0].name).to.equal('video 1')
186 expect(videos[1].name).to.equal('video 3')
189 it('Should not clean old history', async function () {
192 await killallServers([ server ])
194 await server.run({ history: { videos: { max_age: '10 days' } } })
198 // Should still have history
200 const body = await command.list()
201 expect(body.total).to.equal(2)
204 it('Should clean old history', async function () {
207 await killallServers([ server ])
209 await server.run({ history: { videos: { max_age: '5 seconds' } } })
213 const body = await command.list()
214 expect(body.total).to.equal(0)
217 it('Should delete a specific history element', async function () {
219 await command.watchVideo({ videoId: video1UUID, currentTime: 4 })
220 await command.watchVideo({ videoId: video2UUID, currentTime: 8 })
224 const body = await command.list()
225 expect(body.total).to.equal(2)
229 await command.removeElement({ videoId: video1Id })
231 const body = await command.list()
232 expect(body.total).to.equal(1)
233 expect(body.data[0].uuid).to.equal(video2UUID)
237 after(async function () {
238 await cleanupTests([ server ])