aboutsummaryrefslogtreecommitdiffhomepage
path: root/packages/tests/src/api/videos/videos-history.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2023-07-31 14:34:36 +0200
committerChocobozzz <me@florianbigard.com>2023-08-11 15:02:33 +0200
commit3a4992633ee62d5edfbb484d9c6bcb3cf158489d (patch)
treee4510b39bdac9c318fdb4b47018d08f15368b8f0 /packages/tests/src/api/videos/videos-history.ts
parent04d1da5621d25d59bd5fa1543b725c497bf5d9a8 (diff)
downloadPeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.gz
PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.zst
PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.zip
Migrate server to ESM
Sorry for the very big commit that may lead to git log issues and merge conflicts, but it's a major step forward: * Server can be faster at startup because imports() are async and we can easily lazy import big modules * Angular doesn't seem to support ES import (with .js extension), so we had to correctly organize peertube into a monorepo: * Use yarn workspace feature * Use typescript reference projects for dependencies * Shared projects have been moved into "packages", each one is now a node module (with a dedicated package.json/tsconfig.json) * server/tools have been moved into apps/ and is now a dedicated app bundled and published on NPM so users don't have to build peertube cli tools manually * server/tests have been moved into packages/ so we don't compile them every time we want to run the server * Use isolatedModule option: * Had to move from const enum to const (https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums) * Had to explictely specify "type" imports when used in decorators * Prefer tsx (that uses esbuild under the hood) instead of ts-node to load typescript files (tests with mocha or scripts): * To reduce test complexity as esbuild doesn't support decorator metadata, we only test server files that do not import server models * We still build tests files into js files for a faster CI * Remove unmaintained peertube CLI import script * Removed some barrels to speed up execution (less imports)
Diffstat (limited to 'packages/tests/src/api/videos/videos-history.ts')
-rw-r--r--packages/tests/src/api/videos/videos-history.ts230
1 files changed, 230 insertions, 0 deletions
diff --git a/packages/tests/src/api/videos/videos-history.ts b/packages/tests/src/api/videos/videos-history.ts
new file mode 100644
index 000000000..75c0fcebd
--- /dev/null
+++ b/packages/tests/src/api/videos/videos-history.ts
@@ -0,0 +1,230 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3import { expect } from 'chai'
4import { wait } from '@peertube/peertube-core-utils'
5import { Video } from '@peertube/peertube-models'
6import {
7 cleanupTests,
8 createSingleServer,
9 killallServers,
10 PeerTubeServer,
11 setAccessTokensToServers
12} from '@peertube/peertube-server-commands'
13
14describe('Test videos history', function () {
15 let server: PeerTubeServer = null
16 let video1Id: number
17 let video1UUID: string
18 let video2UUID: string
19 let video3UUID: string
20 let video3WatchedDate: Date
21 let userAccessToken: string
22
23 before(async function () {
24 this.timeout(120000)
25
26 server = await createSingleServer(1)
27
28 await setAccessTokensToServers([ server ])
29
30 // 10 seconds long
31 const fixture = 'video_short1.webm'
32
33 {
34 const { id, uuid } = await server.videos.upload({ attributes: { name: 'video 1', fixture } })
35 video1UUID = uuid
36 video1Id = id
37 }
38
39 {
40 const { uuid } = await server.videos.upload({ attributes: { name: 'video 2', fixture } })
41 video2UUID = uuid
42 }
43
44 {
45 const { uuid } = await server.videos.upload({ attributes: { name: 'video 3', fixture } })
46 video3UUID = uuid
47 }
48
49 userAccessToken = await server.users.generateUserAndToken('user_1')
50 })
51
52 it('Should get videos, without watching history', async function () {
53 const { data } = await server.videos.listWithToken()
54
55 for (const video of data) {
56 const videoDetails = await server.videos.getWithToken({ id: video.id })
57
58 expect(video.userHistory).to.be.undefined
59 expect(videoDetails.userHistory).to.be.undefined
60 }
61 })
62
63 it('Should watch the first and second video', async function () {
64 await server.views.view({ id: video2UUID, token: server.accessToken, currentTime: 8 })
65 await server.views.view({ id: video1UUID, token: server.accessToken, currentTime: 3 })
66 })
67
68 it('Should return the correct history when listing, searching and getting videos', async function () {
69 const videosOfVideos: Video[][] = []
70
71 {
72 const { data } = await server.videos.listWithToken()
73 videosOfVideos.push(data)
74 }
75
76 {
77 const body = await server.search.searchVideos({ token: server.accessToken, search: 'video' })
78 videosOfVideos.push(body.data)
79 }
80
81 for (const videos of videosOfVideos) {
82 const video1 = videos.find(v => v.uuid === video1UUID)
83 const video2 = videos.find(v => v.uuid === video2UUID)
84 const video3 = videos.find(v => v.uuid === video3UUID)
85
86 expect(video1.userHistory).to.not.be.undefined
87 expect(video1.userHistory.currentTime).to.equal(3)
88
89 expect(video2.userHistory).to.not.be.undefined
90 expect(video2.userHistory.currentTime).to.equal(8)
91
92 expect(video3.userHistory).to.be.undefined
93 }
94
95 {
96 const videoDetails = await server.videos.getWithToken({ id: video1UUID })
97
98 expect(videoDetails.userHistory).to.not.be.undefined
99 expect(videoDetails.userHistory.currentTime).to.equal(3)
100 }
101
102 {
103 const videoDetails = await server.videos.getWithToken({ id: video2UUID })
104
105 expect(videoDetails.userHistory).to.not.be.undefined
106 expect(videoDetails.userHistory.currentTime).to.equal(8)
107 }
108
109 {
110 const videoDetails = await server.videos.getWithToken({ id: video3UUID })
111
112 expect(videoDetails.userHistory).to.be.undefined
113 }
114 })
115
116 it('Should have these videos when listing my history', async function () {
117 video3WatchedDate = new Date()
118 await server.views.view({ id: video3UUID, token: server.accessToken, currentTime: 2 })
119
120 const body = await server.history.list()
121
122 expect(body.total).to.equal(3)
123
124 const videos = body.data
125 expect(videos[0].name).to.equal('video 3')
126 expect(videos[1].name).to.equal('video 1')
127 expect(videos[2].name).to.equal('video 2')
128 })
129
130 it('Should not have videos history on another user', async function () {
131 const body = await server.history.list({ token: userAccessToken })
132
133 expect(body.total).to.equal(0)
134 expect(body.data).to.have.lengthOf(0)
135 })
136
137 it('Should be able to search through videos in my history', async function () {
138 const body = await server.history.list({ search: '2' })
139 expect(body.total).to.equal(1)
140
141 const videos = body.data
142 expect(videos[0].name).to.equal('video 2')
143 })
144
145 it('Should clear my history', async function () {
146 await server.history.removeAll({ beforeDate: video3WatchedDate.toISOString() })
147 })
148
149 it('Should have my history cleared', async function () {
150 const body = await server.history.list()
151 expect(body.total).to.equal(1)
152
153 const videos = body.data
154 expect(videos[0].name).to.equal('video 3')
155 })
156
157 it('Should disable videos history', async function () {
158 await server.users.updateMe({
159 videosHistoryEnabled: false
160 })
161
162 await server.views.view({ id: video2UUID, token: server.accessToken, currentTime: 8 })
163
164 const { data } = await server.history.list()
165 expect(data[0].name).to.not.equal('video 2')
166 })
167
168 it('Should re-enable videos history', async function () {
169 await server.users.updateMe({
170 videosHistoryEnabled: true
171 })
172
173 await server.views.view({ id: video2UUID, token: server.accessToken, currentTime: 8 })
174
175 const { data } = await server.history.list()
176 expect(data[0].name).to.equal('video 2')
177 })
178
179 it('Should not clean old history', async function () {
180 this.timeout(50000)
181
182 await killallServers([ server ])
183
184 await server.run({ history: { videos: { max_age: '10 days' } } })
185
186 await wait(6000)
187
188 // Should still have history
189
190 const body = await server.history.list()
191 expect(body.total).to.equal(2)
192 })
193
194 it('Should clean old history', async function () {
195 this.timeout(50000)
196
197 await killallServers([ server ])
198
199 await server.run({ history: { videos: { max_age: '5 seconds' } } })
200
201 await wait(6000)
202
203 const body = await server.history.list()
204 expect(body.total).to.equal(0)
205 })
206
207 it('Should delete a specific history element', async function () {
208 {
209 await server.views.view({ id: video1UUID, token: server.accessToken, currentTime: 4 })
210 await server.views.view({ id: video2UUID, token: server.accessToken, currentTime: 8 })
211 }
212
213 {
214 const body = await server.history.list()
215 expect(body.total).to.equal(2)
216 }
217
218 {
219 await server.history.removeElement({ videoId: video1Id })
220
221 const body = await server.history.list()
222 expect(body.total).to.equal(1)
223 expect(body.data[0].uuid).to.equal(video2UUID)
224 }
225 })
226
227 after(async function () {
228 await cleanupTests([ server ])
229 })
230})