]>
Commit | Line | Data |
---|---|---|
a1587156 | 1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ |
6e46de09 | 2 | |
86347717 | 3 | import { expect } from 'chai' |
c55e3d72 | 4 | import { wait } from '@shared/core-utils' |
b2111066 C |
5 | import { Video } from '@shared/models' |
6 | import { cleanupTests, createSingleServer, killallServers, PeerTubeServer, setAccessTokensToServers } from '@shared/server-commands' | |
6e46de09 | 7 | |
6e46de09 | 8 | describe('Test videos history', function () { |
254d3579 | 9 | let server: PeerTubeServer = null |
7177b46c | 10 | let video1Id: number |
6e46de09 C |
11 | let video1UUID: string |
12 | let video2UUID: string | |
13 | let video3UUID: string | |
8b9a525a C |
14 | let video3WatchedDate: Date |
15 | let userAccessToken: string | |
6e46de09 C |
16 | |
17 | before(async function () { | |
8d57e728 | 18 | this.timeout(120000) |
6e46de09 | 19 | |
254d3579 | 20 | server = await createSingleServer(1) |
6e46de09 C |
21 | |
22 | await setAccessTokensToServers([ server ]) | |
23 | ||
b2111066 | 24 | // 10 seconds long |
d74bb064 | 25 | const fixture = 'video_short1.webm' |
313228e9 | 26 | |
6e46de09 | 27 | { |
b2111066 | 28 | const { id, uuid } = await server.videos.upload({ attributes: { name: 'video 1', fixture } }) |
d23dd9fb | 29 | video1UUID = uuid |
7177b46c | 30 | video1Id = id |
6e46de09 C |
31 | } |
32 | ||
33 | { | |
b2111066 | 34 | const { uuid } = await server.videos.upload({ attributes: { name: 'video 2', fixture } }) |
d23dd9fb | 35 | video2UUID = uuid |
6e46de09 C |
36 | } |
37 | ||
38 | { | |
b2111066 | 39 | const { uuid } = await server.videos.upload({ attributes: { name: 'video 3', fixture } }) |
d23dd9fb | 40 | video3UUID = uuid |
6e46de09 | 41 | } |
8b9a525a | 42 | |
b2111066 | 43 | userAccessToken = await server.users.generateUserAndToken('user_1') |
6e46de09 C |
44 | }) |
45 | ||
46 | it('Should get videos, without watching history', async function () { | |
89d241a7 | 47 | const { data } = await server.videos.listWithToken() |
6e46de09 | 48 | |
d23dd9fb | 49 | for (const video of data) { |
89d241a7 | 50 | const videoDetails = await server.videos.getWithToken({ id: video.id }) |
6e46de09 C |
51 | |
52 | expect(video.userHistory).to.be.undefined | |
53 | expect(videoDetails.userHistory).to.be.undefined | |
54 | } | |
55 | }) | |
56 | ||
57 | it('Should watch the first and second video', async function () { | |
b2111066 C |
58 | await server.views.view({ id: video2UUID, token: server.accessToken, currentTime: 8 }) |
59 | await server.views.view({ id: video1UUID, token: server.accessToken, currentTime: 3 }) | |
6e46de09 C |
60 | }) |
61 | ||
62 | it('Should return the correct history when listing, searching and getting videos', async function () { | |
63 | const videosOfVideos: Video[][] = [] | |
64 | ||
65 | { | |
89d241a7 | 66 | const { data } = await server.videos.listWithToken() |
d23dd9fb | 67 | videosOfVideos.push(data) |
6e46de09 C |
68 | } |
69 | ||
70 | { | |
89d241a7 | 71 | const body = await server.search.searchVideos({ token: server.accessToken, search: 'video' }) |
af971e06 | 72 | videosOfVideos.push(body.data) |
6e46de09 C |
73 | } |
74 | ||
75 | for (const videos of videosOfVideos) { | |
76 | const video1 = videos.find(v => v.uuid === video1UUID) | |
77 | const video2 = videos.find(v => v.uuid === video2UUID) | |
78 | const video3 = videos.find(v => v.uuid === video3UUID) | |
79 | ||
80 | expect(video1.userHistory).to.not.be.undefined | |
81 | expect(video1.userHistory.currentTime).to.equal(3) | |
82 | ||
83 | expect(video2.userHistory).to.not.be.undefined | |
84 | expect(video2.userHistory.currentTime).to.equal(8) | |
85 | ||
86 | expect(video3.userHistory).to.be.undefined | |
87 | } | |
88 | ||
89 | { | |
89d241a7 | 90 | const videoDetails = await server.videos.getWithToken({ id: video1UUID }) |
6e46de09 C |
91 | |
92 | expect(videoDetails.userHistory).to.not.be.undefined | |
93 | expect(videoDetails.userHistory.currentTime).to.equal(3) | |
94 | } | |
95 | ||
96 | { | |
89d241a7 | 97 | const videoDetails = await server.videos.getWithToken({ id: video2UUID }) |
6e46de09 C |
98 | |
99 | expect(videoDetails.userHistory).to.not.be.undefined | |
100 | expect(videoDetails.userHistory.currentTime).to.equal(8) | |
101 | } | |
102 | ||
103 | { | |
89d241a7 | 104 | const videoDetails = await server.videos.getWithToken({ id: video3UUID }) |
6e46de09 C |
105 | |
106 | expect(videoDetails.userHistory).to.be.undefined | |
107 | } | |
108 | }) | |
109 | ||
8b9a525a C |
110 | it('Should have these videos when listing my history', async function () { |
111 | video3WatchedDate = new Date() | |
b2111066 | 112 | await server.views.view({ id: video3UUID, token: server.accessToken, currentTime: 2 }) |
8b9a525a | 113 | |
b2111066 | 114 | const body = await server.history.list() |
8b9a525a | 115 | |
313228e9 | 116 | expect(body.total).to.equal(3) |
8b9a525a | 117 | |
313228e9 | 118 | const videos = body.data |
8b9a525a C |
119 | expect(videos[0].name).to.equal('video 3') |
120 | expect(videos[1].name).to.equal('video 1') | |
121 | expect(videos[2].name).to.equal('video 2') | |
122 | }) | |
123 | ||
124 | it('Should not have videos history on another user', async function () { | |
b2111066 | 125 | const body = await server.history.list({ token: userAccessToken }) |
8b9a525a | 126 | |
313228e9 C |
127 | expect(body.total).to.equal(0) |
128 | expect(body.data).to.have.lengthOf(0) | |
8b9a525a C |
129 | }) |
130 | ||
d8b34ee5 | 131 | it('Should be able to search through videos in my history', async function () { |
b2111066 | 132 | const body = await server.history.list({ search: '2' }) |
313228e9 | 133 | expect(body.total).to.equal(1) |
d8b34ee5 | 134 | |
313228e9 | 135 | const videos = body.data |
d8b34ee5 RK |
136 | expect(videos[0].name).to.equal('video 2') |
137 | }) | |
138 | ||
8b9a525a | 139 | it('Should clear my history', async function () { |
b2111066 | 140 | await server.history.removeAll({ beforeDate: video3WatchedDate.toISOString() }) |
8b9a525a C |
141 | }) |
142 | ||
143 | it('Should have my history cleared', async function () { | |
b2111066 | 144 | const body = await server.history.list() |
313228e9 | 145 | expect(body.total).to.equal(1) |
8b9a525a | 146 | |
313228e9 | 147 | const videos = body.data |
8b9a525a C |
148 | expect(videos[0].name).to.equal('video 3') |
149 | }) | |
150 | ||
151 | it('Should disable videos history', async function () { | |
89d241a7 | 152 | await server.users.updateMe({ |
8b9a525a C |
153 | videosHistoryEnabled: false |
154 | }) | |
155 | ||
b2111066 C |
156 | await server.views.view({ id: video2UUID, token: server.accessToken, currentTime: 8 }) |
157 | ||
158 | const { data } = await server.history.list() | |
159 | expect(data[0].name).to.not.equal('video 2') | |
8b9a525a C |
160 | }) |
161 | ||
162 | it('Should re-enable videos history', async function () { | |
89d241a7 | 163 | await server.users.updateMe({ |
8b9a525a C |
164 | videosHistoryEnabled: true |
165 | }) | |
166 | ||
b2111066 | 167 | await server.views.view({ id: video2UUID, token: server.accessToken, currentTime: 8 }) |
8b9a525a | 168 | |
b2111066 C |
169 | const { data } = await server.history.list() |
170 | expect(data[0].name).to.equal('video 2') | |
8b9a525a C |
171 | }) |
172 | ||
8f0bc73d C |
173 | it('Should not clean old history', async function () { |
174 | this.timeout(50000) | |
175 | ||
9293139f | 176 | await killallServers([ server ]) |
8f0bc73d | 177 | |
254d3579 | 178 | await server.run({ history: { videos: { max_age: '10 days' } } }) |
8f0bc73d C |
179 | |
180 | await wait(6000) | |
181 | ||
182 | // Should still have history | |
183 | ||
b2111066 | 184 | const body = await server.history.list() |
313228e9 | 185 | expect(body.total).to.equal(2) |
8f0bc73d C |
186 | }) |
187 | ||
188 | it('Should clean old history', async function () { | |
189 | this.timeout(50000) | |
190 | ||
9293139f | 191 | await killallServers([ server ]) |
8f0bc73d | 192 | |
254d3579 | 193 | await server.run({ history: { videos: { max_age: '5 seconds' } } }) |
8f0bc73d C |
194 | |
195 | await wait(6000) | |
196 | ||
b2111066 | 197 | const body = await server.history.list() |
313228e9 | 198 | expect(body.total).to.equal(0) |
8f0bc73d C |
199 | }) |
200 | ||
7177b46c C |
201 | it('Should delete a specific history element', async function () { |
202 | { | |
b2111066 C |
203 | await server.views.view({ id: video1UUID, token: server.accessToken, currentTime: 4 }) |
204 | await server.views.view({ id: video2UUID, token: server.accessToken, currentTime: 8 }) | |
7177b46c C |
205 | } |
206 | ||
207 | { | |
b2111066 | 208 | const body = await server.history.list() |
7177b46c C |
209 | expect(body.total).to.equal(2) |
210 | } | |
211 | ||
212 | { | |
b2111066 | 213 | await server.history.removeElement({ videoId: video1Id }) |
7177b46c | 214 | |
b2111066 | 215 | const body = await server.history.list() |
7177b46c C |
216 | expect(body.total).to.equal(1) |
217 | expect(body.data[0].uuid).to.equal(video2UUID) | |
218 | } | |
219 | }) | |
220 | ||
7c3b7976 C |
221 | after(async function () { |
222 | await cleanupTests([ server ]) | |
6e46de09 C |
223 | }) |
224 | }) |