1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
11 setAccessTokensToServers,
13 } from '@shared/extra-utils'
14 import { HttpStatusCode, Video } from '@shared/models'
16 const expect = chai.expect
18 describe('Test videos history', function () {
19 let server: PeerTubeServer = null
20 let video1UUID: string
21 let video2UUID: string
22 let video3UUID: string
23 let video3WatchedDate: Date
24 let userAccessToken: string
25 let command: HistoryCommand
27 before(async function () {
30 server = await createSingleServer(1)
32 await setAccessTokensToServers([ server ])
34 command = server.history
37 const { uuid } = await server.videos.upload({ attributes: { name: 'video 1' } })
42 const { uuid } = await server.videos.upload({ attributes: { name: 'video 2' } })
47 const { uuid } = await server.videos.upload({ attributes: { name: 'video 3' } })
53 password: 'super password'
55 await server.users.create({ username: user.username, password: user.password })
56 userAccessToken = await server.login.getAccessToken(user)
59 it('Should get videos, without watching history', async function () {
60 const { data } = await server.videos.listWithToken()
62 for (const video of data) {
63 const videoDetails = await server.videos.getWithToken({ id: video.id })
65 expect(video.userHistory).to.be.undefined
66 expect(videoDetails.userHistory).to.be.undefined
70 it('Should watch the first and second video', async function () {
71 await command.wathVideo({ videoId: video2UUID, currentTime: 8 })
72 await command.wathVideo({ videoId: video1UUID, currentTime: 3 })
75 it('Should return the correct history when listing, searching and getting videos', async function () {
76 const videosOfVideos: Video[][] = []
79 const { data } = await server.videos.listWithToken()
80 videosOfVideos.push(data)
84 const body = await server.search.searchVideos({ token: server.accessToken, search: 'video' })
85 videosOfVideos.push(body.data)
88 for (const videos of videosOfVideos) {
89 const video1 = videos.find(v => v.uuid === video1UUID)
90 const video2 = videos.find(v => v.uuid === video2UUID)
91 const video3 = videos.find(v => v.uuid === video3UUID)
93 expect(video1.userHistory).to.not.be.undefined
94 expect(video1.userHistory.currentTime).to.equal(3)
96 expect(video2.userHistory).to.not.be.undefined
97 expect(video2.userHistory.currentTime).to.equal(8)
99 expect(video3.userHistory).to.be.undefined
103 const videoDetails = await server.videos.getWithToken({ id: video1UUID })
105 expect(videoDetails.userHistory).to.not.be.undefined
106 expect(videoDetails.userHistory.currentTime).to.equal(3)
110 const videoDetails = await server.videos.getWithToken({ id: video2UUID })
112 expect(videoDetails.userHistory).to.not.be.undefined
113 expect(videoDetails.userHistory.currentTime).to.equal(8)
117 const videoDetails = await server.videos.getWithToken({ id: video3UUID })
119 expect(videoDetails.userHistory).to.be.undefined
123 it('Should have these videos when listing my history', async function () {
124 video3WatchedDate = new Date()
125 await command.wathVideo({ videoId: video3UUID, currentTime: 2 })
127 const body = await command.list()
129 expect(body.total).to.equal(3)
131 const videos = body.data
132 expect(videos[0].name).to.equal('video 3')
133 expect(videos[1].name).to.equal('video 1')
134 expect(videos[2].name).to.equal('video 2')
137 it('Should not have videos history on another user', async function () {
138 const body = await command.list({ token: userAccessToken })
140 expect(body.total).to.equal(0)
141 expect(body.data).to.have.lengthOf(0)
144 it('Should be able to search through videos in my history', async function () {
145 const body = await command.list({ search: '2' })
146 expect(body.total).to.equal(1)
148 const videos = body.data
149 expect(videos[0].name).to.equal('video 2')
152 it('Should clear my history', async function () {
153 await command.remove({ beforeDate: video3WatchedDate.toISOString() })
156 it('Should have my history cleared', async function () {
157 const body = await command.list()
158 expect(body.total).to.equal(1)
160 const videos = body.data
161 expect(videos[0].name).to.equal('video 3')
164 it('Should disable videos history', async function () {
165 await server.users.updateMe({
166 videosHistoryEnabled: false
169 await command.wathVideo({ videoId: video2UUID, currentTime: 8, expectedStatus: HttpStatusCode.CONFLICT_409 })
172 it('Should re-enable videos history', async function () {
173 await server.users.updateMe({
174 videosHistoryEnabled: true
177 await command.wathVideo({ videoId: video1UUID, currentTime: 8 })
179 const body = await command.list()
180 expect(body.total).to.equal(2)
182 const videos = body.data
183 expect(videos[0].name).to.equal('video 1')
184 expect(videos[1].name).to.equal('video 3')
187 it('Should not clean old history', async function () {
190 await killallServers([ server ])
192 await server.run({ history: { videos: { max_age: '10 days' } } })
196 // Should still have history
198 const body = await command.list()
199 expect(body.total).to.equal(2)
202 it('Should clean old history', async function () {
205 await killallServers([ server ])
207 await server.run({ history: { videos: { max_age: '5 seconds' } } })
211 const body = await command.list()
212 expect(body.total).to.equal(0)
215 after(async function () {
216 await cleanupTests([ server ])