]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/search/search-index.ts
Introduce stats command
[github/Chocobozzz/PeerTube.git] / server / tests / api / search / search-index.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import * as chai from 'chai'
5 import {
6 cleanupTests,
7 flushAndRunServer,
8 immutableAssign,
9 SearchCommand,
10 ServerInfo,
11 setAccessTokensToServers,
12 updateCustomSubConfig,
13 uploadVideo
14 } from '@shared/extra-utils'
15 import { VideoPlaylistPrivacy, VideoPlaylistType, VideosSearchQuery } from '@shared/models'
16
17 const expect = chai.expect
18
19 describe('Test videos search', function () {
20 const localVideoName = 'local video' + new Date().toISOString()
21
22 let server: ServerInfo = null
23 let command: SearchCommand
24
25 before(async function () {
26 this.timeout(30000)
27
28 server = await flushAndRunServer(1)
29
30 await setAccessTokensToServers([ server ])
31
32 await uploadVideo(server.url, server.accessToken, { name: localVideoName })
33
34 command = server.searchCommand
35 })
36
37 describe('Default search', async function () {
38
39 it('Should make a local videos search by default', async function () {
40 this.timeout(10000)
41
42 await updateCustomSubConfig(server.url, server.accessToken, {
43 search: {
44 searchIndex: {
45 enabled: true,
46 isDefaultSearch: false,
47 disableLocalSearch: false
48 }
49 }
50 })
51
52 const body = await command.searchVideos({ search: 'local video' })
53
54 expect(body.total).to.equal(1)
55 expect(body.data[0].name).to.equal(localVideoName)
56 })
57
58 it('Should make a local channels search by default', async function () {
59 const body = await command.searchChannels({ search: 'root' })
60
61 expect(body.total).to.equal(1)
62 expect(body.data[0].name).to.equal('root_channel')
63 expect(body.data[0].host).to.equal('localhost:' + server.port)
64 })
65
66 it('Should make an index videos search by default', async function () {
67 await updateCustomSubConfig(server.url, server.accessToken, {
68 search: {
69 searchIndex: {
70 enabled: true,
71 isDefaultSearch: true,
72 disableLocalSearch: false
73 }
74 }
75 })
76
77 const body = await command.searchVideos({ search: 'local video' })
78 expect(body.total).to.be.greaterThan(2)
79 })
80
81 it('Should make an index channels search by default', async function () {
82 const body = await command.searchChannels({ search: 'root' })
83 expect(body.total).to.be.greaterThan(2)
84 })
85
86 it('Should make an index videos search if local search is disabled', async function () {
87 await updateCustomSubConfig(server.url, server.accessToken, {
88 search: {
89 searchIndex: {
90 enabled: true,
91 isDefaultSearch: false,
92 disableLocalSearch: true
93 }
94 }
95 })
96
97 const body = await command.searchVideos({ search: 'local video' })
98 expect(body.total).to.be.greaterThan(2)
99 })
100
101 it('Should make an index channels search if local search is disabled', async function () {
102 const body = await command.searchChannels({ search: 'root' })
103 expect(body.total).to.be.greaterThan(2)
104 })
105 })
106
107 describe('Videos search', async function () {
108
109 it('Should make a simple search and not have results', async function () {
110 const body = await command.searchVideos({ search: 'djidane'.repeat(50) })
111
112 expect(body.total).to.equal(0)
113 expect(body.data).to.have.lengthOf(0)
114 })
115
116 it('Should make a simple search and have results', async function () {
117 const body = await command.searchVideos({ search: 'What is PeerTube' })
118
119 expect(body.total).to.be.greaterThan(1)
120 })
121
122 it('Should make a complex search', async function () {
123
124 async function check (search: VideosSearchQuery, exists = true) {
125 const body = await command.advancedVideoSearch({ search })
126
127 if (exists === false) {
128 expect(body.total).to.equal(0)
129 expect(body.data).to.have.lengthOf(0)
130 return
131 }
132
133 expect(body.total).to.equal(1)
134 expect(body.data).to.have.lengthOf(1)
135
136 const video = body.data[0]
137
138 expect(video.name).to.equal('What is PeerTube?')
139 expect(video.category.label).to.equal('Science & Technology')
140 expect(video.licence.label).to.equal('Attribution - Share Alike')
141 expect(video.privacy.label).to.equal('Public')
142 expect(video.duration).to.equal(113)
143 expect(video.thumbnailUrl.startsWith('https://framatube.org/static/thumbnails')).to.be.true
144
145 expect(video.account.host).to.equal('framatube.org')
146 expect(video.account.name).to.equal('framasoft')
147 expect(video.account.url).to.equal('https://framatube.org/accounts/framasoft')
148 expect(video.account.avatar).to.exist
149
150 expect(video.channel.host).to.equal('framatube.org')
151 expect(video.channel.name).to.equal('bf54d359-cfad-4935-9d45-9d6be93f63e8')
152 expect(video.channel.url).to.equal('https://framatube.org/video-channels/bf54d359-cfad-4935-9d45-9d6be93f63e8')
153 expect(video.channel.avatar).to.exist
154 }
155
156 const baseSearch: VideosSearchQuery = {
157 search: 'what is peertube',
158 start: 0,
159 count: 2,
160 categoryOneOf: [ 15 ],
161 licenceOneOf: [ 2 ],
162 tagsAllOf: [ 'framasoft', 'peertube' ],
163 startDate: '2018-10-01T10:50:46.396Z',
164 endDate: '2018-10-01T10:55:46.396Z'
165 }
166
167 {
168 await check(baseSearch)
169 }
170
171 {
172 const search = immutableAssign(baseSearch, { startDate: '2018-10-01T10:54:46.396Z' })
173 await check(search, false)
174 }
175
176 {
177 const search = immutableAssign(baseSearch, { tagsAllOf: [ 'toto', 'framasoft' ] })
178 await check(search, false)
179 }
180
181 {
182 const search = immutableAssign(baseSearch, { durationMin: 2000 })
183 await check(search, false)
184 }
185
186 {
187 const search = immutableAssign(baseSearch, { nsfw: 'true' })
188 await check(search, false)
189 }
190
191 {
192 const search = immutableAssign(baseSearch, { nsfw: 'false' })
193 await check(search, true)
194 }
195
196 {
197 const search = immutableAssign(baseSearch, { nsfw: 'both' })
198 await check(search, true)
199 }
200 })
201
202 it('Should have a correct pagination', async function () {
203 const search = {
204 search: 'video',
205 start: 0,
206 count: 5
207 }
208
209 const body = await command.advancedVideoSearch({ search })
210
211 expect(body.total).to.be.greaterThan(5)
212 expect(body.data).to.have.lengthOf(5)
213 })
214
215 it('Should use the nsfw instance policy as default', async function () {
216 let nsfwUUID: string
217
218 {
219 await updateCustomSubConfig(server.url, server.accessToken, { instance: { defaultNSFWPolicy: 'display' } })
220
221 const body = await command.searchVideos({ search: 'NSFW search index', sort: '-match' })
222 expect(body.data).to.have.length.greaterThan(0)
223
224 const video = body.data[0]
225 expect(video.nsfw).to.be.true
226
227 nsfwUUID = video.uuid
228 }
229
230 {
231 await updateCustomSubConfig(server.url, server.accessToken, { instance: { defaultNSFWPolicy: 'do_not_list' } })
232
233 const body = await command.searchVideos({ search: 'NSFW search index', sort: '-match' })
234
235 try {
236 expect(body.data).to.have.lengthOf(0)
237 } catch {
238 const video = body.data[0]
239
240 expect(video.uuid).not.equal(nsfwUUID)
241 }
242 }
243 })
244 })
245
246 describe('Channels search', async function () {
247
248 it('Should make a simple search and not have results', async function () {
249 const body = await command.searchChannels({ search: 'a'.repeat(500) })
250
251 expect(body.total).to.equal(0)
252 expect(body.data).to.have.lengthOf(0)
253 })
254
255 it('Should make a search and have results', async function () {
256 const body = await command.advancedChannelSearch({ search: { search: 'Framasoft', sort: 'createdAt' } })
257
258 expect(body.total).to.be.greaterThan(0)
259 expect(body.data).to.have.length.greaterThan(0)
260
261 const videoChannel = body.data[0]
262 expect(videoChannel.url).to.equal('https://framatube.org/video-channels/bf54d359-cfad-4935-9d45-9d6be93f63e8')
263 expect(videoChannel.host).to.equal('framatube.org')
264 expect(videoChannel.avatar).to.exist
265 expect(videoChannel.displayName).to.exist
266
267 expect(videoChannel.ownerAccount.url).to.equal('https://framatube.org/accounts/framasoft')
268 expect(videoChannel.ownerAccount.name).to.equal('framasoft')
269 expect(videoChannel.ownerAccount.host).to.equal('framatube.org')
270 expect(videoChannel.ownerAccount.avatar).to.exist
271 })
272
273 it('Should have a correct pagination', async function () {
274 const body = await command.advancedChannelSearch({ search: { search: 'root', start: 0, count: 2 } })
275
276 expect(body.total).to.be.greaterThan(2)
277 expect(body.data).to.have.lengthOf(2)
278 })
279 })
280
281 describe('Playlists search', async function () {
282
283 it('Should make a simple search and not have results', async function () {
284 const body = await command.searchPlaylists({ search: 'a'.repeat(500) })
285
286 expect(body.total).to.equal(0)
287 expect(body.data).to.have.lengthOf(0)
288 })
289
290 it('Should make a search and have results', async function () {
291 const body = await command.advancedPlaylistSearch({ search: { search: 'E2E playlist', sort: '-match' } })
292
293 expect(body.total).to.be.greaterThan(0)
294 expect(body.data).to.have.length.greaterThan(0)
295
296 const videoPlaylist = body.data[0]
297
298 expect(videoPlaylist.url).to.equal('https://peertube2.cpy.re/videos/watch/playlist/73804a40-da9a-40c2-b1eb-2c6d9eec8f0a')
299 expect(videoPlaylist.thumbnailUrl).to.exist
300 expect(videoPlaylist.embedUrl).to.equal('https://peertube2.cpy.re/video-playlists/embed/73804a40-da9a-40c2-b1eb-2c6d9eec8f0a')
301
302 expect(videoPlaylist.type.id).to.equal(VideoPlaylistType.REGULAR)
303 expect(videoPlaylist.privacy.id).to.equal(VideoPlaylistPrivacy.PUBLIC)
304 expect(videoPlaylist.videosLength).to.exist
305
306 expect(videoPlaylist.createdAt).to.exist
307 expect(videoPlaylist.updatedAt).to.exist
308
309 expect(videoPlaylist.uuid).to.equal('73804a40-da9a-40c2-b1eb-2c6d9eec8f0a')
310 expect(videoPlaylist.displayName).to.exist
311
312 expect(videoPlaylist.ownerAccount.url).to.equal('https://peertube2.cpy.re/accounts/chocobozzz')
313 expect(videoPlaylist.ownerAccount.name).to.equal('chocobozzz')
314 expect(videoPlaylist.ownerAccount.host).to.equal('peertube2.cpy.re')
315 expect(videoPlaylist.ownerAccount.avatar).to.exist
316
317 expect(videoPlaylist.videoChannel.url).to.equal('https://peertube2.cpy.re/video-channels/chocobozzz_channel')
318 expect(videoPlaylist.videoChannel.name).to.equal('chocobozzz_channel')
319 expect(videoPlaylist.videoChannel.host).to.equal('peertube2.cpy.re')
320 expect(videoPlaylist.videoChannel.avatar).to.exist
321 })
322
323 it('Should have a correct pagination', async function () {
324 const body = await command.advancedChannelSearch({ search: { search: 'root', start: 0, count: 2 } })
325
326 expect(body.total).to.be.greaterThan(2)
327 expect(body.data).to.have.lengthOf(2)
328 })
329 })
330
331 after(async function () {
332 await cleanupTests([ server ])
333 })
334 })