1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { HttpStatusCode } from '@shared/models'
12 setAccessTokensToServers,
14 } from '@shared/extra-utils'
15 import { Video } from '@shared/models'
17 const expect = chai.expect
19 describe('Test videos history', function () {
20 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 { uuid } = await server.videos.upload({ attributes: { name: 'video 1' } })
43 const { uuid } = await server.videos.upload({ attributes: { name: 'video 2' } })
48 const { uuid } = await server.videos.upload({ attributes: { name: 'video 3' } })
54 password: 'super password'
56 await server.users.create({ username: user.username, password: user.password })
57 userAccessToken = await server.login.getAccessToken(user)
60 it('Should get videos, without watching history', async function () {
61 const { data } = await server.videos.listWithToken()
63 for (const video of data) {
64 const videoDetails = await server.videos.getWithToken({ id: video.id })
66 expect(video.userHistory).to.be.undefined
67 expect(videoDetails.userHistory).to.be.undefined
71 it('Should watch the first and second video', async function () {
72 await command.wathVideo({ videoId: video2UUID, currentTime: 8 })
73 await command.wathVideo({ videoId: video1UUID, currentTime: 3 })
76 it('Should return the correct history when listing, searching and getting videos', async function () {
77 const videosOfVideos: Video[][] = []
80 const { data } = await server.videos.listWithToken()
81 videosOfVideos.push(data)
85 const body = await server.search.searchVideos({ token: server.accessToken, search: 'video' })
86 videosOfVideos.push(body.data)
89 for (const videos of videosOfVideos) {
90 const video1 = videos.find(v => v.uuid === video1UUID)
91 const video2 = videos.find(v => v.uuid === video2UUID)
92 const video3 = videos.find(v => v.uuid === video3UUID)
94 expect(video1.userHistory).to.not.be.undefined
95 expect(video1.userHistory.currentTime).to.equal(3)
97 expect(video2.userHistory).to.not.be.undefined
98 expect(video2.userHistory.currentTime).to.equal(8)
100 expect(video3.userHistory).to.be.undefined
104 const videoDetails = await server.videos.getWithToken({ id: video1UUID })
106 expect(videoDetails.userHistory).to.not.be.undefined
107 expect(videoDetails.userHistory.currentTime).to.equal(3)
111 const videoDetails = await server.videos.getWithToken({ id: video2UUID })
113 expect(videoDetails.userHistory).to.not.be.undefined
114 expect(videoDetails.userHistory.currentTime).to.equal(8)
118 const videoDetails = await server.videos.getWithToken({ id: video3UUID })
120 expect(videoDetails.userHistory).to.be.undefined
124 it('Should have these videos when listing my history', async function () {
125 video3WatchedDate = new Date()
126 await command.wathVideo({ videoId: video3UUID, currentTime: 2 })
128 const body = await command.list()
130 expect(body.total).to.equal(3)
132 const videos = body.data
133 expect(videos[0].name).to.equal('video 3')
134 expect(videos[1].name).to.equal('video 1')
135 expect(videos[2].name).to.equal('video 2')
138 it('Should not have videos history on another user', async function () {
139 const body = await command.list({ token: userAccessToken })
141 expect(body.total).to.equal(0)
142 expect(body.data).to.have.lengthOf(0)
145 it('Should be able to search through videos in my history', async function () {
146 const body = await command.list({ search: '2' })
147 expect(body.total).to.equal(1)
149 const videos = body.data
150 expect(videos[0].name).to.equal('video 2')
153 it('Should clear my history', async function () {
154 await command.remove({ beforeDate: video3WatchedDate.toISOString() })
157 it('Should have my history cleared', async function () {
158 const body = await command.list()
159 expect(body.total).to.equal(1)
161 const videos = body.data
162 expect(videos[0].name).to.equal('video 3')
165 it('Should disable videos history', async function () {
166 await server.users.updateMe({
167 videosHistoryEnabled: false
170 await command.wathVideo({ videoId: video2UUID, currentTime: 8, expectedStatus: HttpStatusCode.CONFLICT_409 })
173 it('Should re-enable videos history', async function () {
174 await server.users.updateMe({
175 videosHistoryEnabled: true
178 await command.wathVideo({ videoId: video1UUID, currentTime: 8 })
180 const body = await command.list()
181 expect(body.total).to.equal(2)
183 const videos = body.data
184 expect(videos[0].name).to.equal('video 1')
185 expect(videos[1].name).to.equal('video 3')
188 it('Should not clean old history', async function () {
191 await killallServers([ server ])
193 await server.run({ history: { videos: { max_age: '10 days' } } })
197 // Should still have history
199 const body = await command.list()
200 expect(body.total).to.equal(2)
203 it('Should clean old history', async function () {
206 await killallServers([ server ])
208 await server.run({ history: { videos: { max_age: '5 seconds' } } })
212 const body = await command.list()
213 expect(body.total).to.equal(0)
216 after(async function () {
217 await cleanupTests([ server ])