]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/search/search-index.ts
Refactor search query options
[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 { cleanupTests, createSingleServer, PeerTubeServer, SearchCommand, setAccessTokensToServers } from '@shared/extra-utils'
6 import {
7 BooleanBothQuery,
8 VideoChannelsSearchQuery,
9 VideoPlaylistPrivacy,
10 VideoPlaylistsSearchQuery,
11 VideoPlaylistType,
12 VideosSearchQuery
13 } from '@shared/models'
14
15 const expect = chai.expect
16
17 describe('Test videos search', function () {
18 const localVideoName = 'local video' + new Date().toISOString()
19
20 let server: PeerTubeServer = null
21 let command: SearchCommand
22
23 before(async function () {
24 this.timeout(30000)
25
26 server = await createSingleServer(1)
27
28 await setAccessTokensToServers([ server ])
29
30 await server.videos.upload({ attributes: { name: localVideoName } })
31
32 command = server.search
33 })
34
35 describe('Default search', async function () {
36
37 it('Should make a local videos search by default', async function () {
38 this.timeout(10000)
39
40 await server.config.updateCustomSubConfig({
41 newConfig: {
42 search: {
43 searchIndex: {
44 enabled: true,
45 isDefaultSearch: false,
46 disableLocalSearch: false
47 }
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 server.config.updateCustomSubConfig({
68 newConfig: {
69 search: {
70 searchIndex: {
71 enabled: true,
72 isDefaultSearch: true,
73 disableLocalSearch: false
74 }
75 }
76 }
77 })
78
79 const body = await command.searchVideos({ search: 'local video' })
80 expect(body.total).to.be.greaterThan(2)
81 })
82
83 it('Should make an index channels search by default', async function () {
84 const body = await command.searchChannels({ search: 'root' })
85 expect(body.total).to.be.greaterThan(2)
86 })
87
88 it('Should make an index videos search if local search is disabled', async function () {
89 await server.config.updateCustomSubConfig({
90 newConfig: {
91 search: {
92 searchIndex: {
93 enabled: true,
94 isDefaultSearch: false,
95 disableLocalSearch: true
96 }
97 }
98 }
99 })
100
101 const body = await command.searchVideos({ search: 'local video' })
102 expect(body.total).to.be.greaterThan(2)
103 })
104
105 it('Should make an index channels search if local search is disabled', async function () {
106 const body = await command.searchChannels({ search: 'root' })
107 expect(body.total).to.be.greaterThan(2)
108 })
109 })
110
111 describe('Videos search', async function () {
112
113 it('Should make a simple search and not have results', async function () {
114 const body = await command.searchVideos({ search: 'djidane'.repeat(50) })
115
116 expect(body.total).to.equal(0)
117 expect(body.data).to.have.lengthOf(0)
118 })
119
120 it('Should make a simple search and have results', async function () {
121 const body = await command.searchVideos({ search: 'What is PeerTube' })
122
123 expect(body.total).to.be.greaterThan(1)
124 })
125
126 it('Should make a complex search', async function () {
127
128 async function check (search: VideosSearchQuery, exists = true) {
129 const body = await command.advancedVideoSearch({ search })
130
131 if (exists === false) {
132 expect(body.total).to.equal(0)
133 expect(body.data).to.have.lengthOf(0)
134 return
135 }
136
137 expect(body.total).to.equal(1)
138 expect(body.data).to.have.lengthOf(1)
139
140 const video = body.data[0]
141
142 expect(video.name).to.equal('What is PeerTube?')
143 expect(video.category.label).to.equal('Science & Technology')
144 expect(video.licence.label).to.equal('Attribution - Share Alike')
145 expect(video.privacy.label).to.equal('Public')
146 expect(video.duration).to.equal(113)
147 expect(video.thumbnailUrl.startsWith('https://framatube.org/static/thumbnails')).to.be.true
148
149 expect(video.account.host).to.equal('framatube.org')
150 expect(video.account.name).to.equal('framasoft')
151 expect(video.account.url).to.equal('https://framatube.org/accounts/framasoft')
152 expect(video.account.avatar).to.exist
153
154 expect(video.channel.host).to.equal('framatube.org')
155 expect(video.channel.name).to.equal('bf54d359-cfad-4935-9d45-9d6be93f63e8')
156 expect(video.channel.url).to.equal('https://framatube.org/video-channels/bf54d359-cfad-4935-9d45-9d6be93f63e8')
157 expect(video.channel.avatar).to.exist
158 }
159
160 const baseSearch: VideosSearchQuery = {
161 search: 'what is peertube',
162 start: 0,
163 count: 2,
164 categoryOneOf: [ 15 ],
165 licenceOneOf: [ 2 ],
166 tagsAllOf: [ 'framasoft', 'peertube' ],
167 startDate: '2018-10-01T10:50:46.396Z',
168 endDate: '2018-10-01T10:55:46.396Z'
169 }
170
171 {
172 await check(baseSearch)
173 }
174
175 {
176 const search = { ...baseSearch, startDate: '2018-10-01T10:54:46.396Z' }
177 await check(search, false)
178 }
179
180 {
181 const search = { ...baseSearch, tagsAllOf: [ 'toto', 'framasoft' ] }
182 await check(search, false)
183 }
184
185 {
186 const search = { ...baseSearch, durationMin: 2000 }
187 await check(search, false)
188 }
189
190 {
191 const search = { ...baseSearch, nsfw: 'true' as BooleanBothQuery }
192 await check(search, false)
193 }
194
195 {
196 const search = { ...baseSearch, nsfw: 'false' as BooleanBothQuery }
197 await check(search, true)
198 }
199
200 {
201 const search = { ...baseSearch, nsfw: 'both' as BooleanBothQuery }
202 await check(search, true)
203 }
204
205 {
206 const search = { ...baseSearch, host: 'example.com' }
207 await check(search, false)
208 }
209
210 {
211 const search = { ...baseSearch, host: 'framatube.org' }
212 await check(search, true)
213 }
214
215 {
216 const goodUUID = '9c9de5e8-0a1e-484a-b099-e80766180a6d'
217 const goodShortUUID = 'kkGMgK9ZtnKfYAgnEtQxbv'
218 const badUUID = 'c29c5b77-4a04-493d-96a9-2e9267e308f0'
219 const badShortUUID = 'rP5RgUeX9XwTSrspCdkDej'
220
221 {
222 const uuidsMatrix = [
223 [ goodUUID ],
224 [ goodUUID, badShortUUID ],
225 [ badShortUUID, goodShortUUID ],
226 [ goodUUID, goodShortUUID ]
227 ]
228
229 for (const uuids of uuidsMatrix) {
230 const search = { ...baseSearch, uuids }
231 await check(search, true)
232 }
233 }
234
235 {
236 const uuidsMatrix = [
237 [ badUUID ],
238 [ badShortUUID ]
239 ]
240
241 for (const uuids of uuidsMatrix) {
242 const search = { ...baseSearch, uuids }
243 await check(search, false)
244 }
245 }
246 }
247 })
248
249 it('Should have a correct pagination', async function () {
250 const search = {
251 search: 'video',
252 start: 0,
253 count: 5
254 }
255
256 const body = await command.advancedVideoSearch({ search })
257
258 expect(body.total).to.be.greaterThan(5)
259 expect(body.data).to.have.lengthOf(5)
260 })
261
262 it('Should use the nsfw instance policy as default', async function () {
263 let nsfwUUID: string
264
265 {
266 await server.config.updateCustomSubConfig({
267 newConfig: {
268 instance: { defaultNSFWPolicy: 'display' }
269 }
270 })
271
272 const body = await command.searchVideos({ search: 'NSFW search index', sort: '-match' })
273 expect(body.data).to.have.length.greaterThan(0)
274
275 const video = body.data[0]
276 expect(video.nsfw).to.be.true
277
278 nsfwUUID = video.uuid
279 }
280
281 {
282 await server.config.updateCustomSubConfig({
283 newConfig: {
284 instance: { defaultNSFWPolicy: 'do_not_list' }
285 }
286 })
287
288 const body = await command.searchVideos({ search: 'NSFW search index', sort: '-match' })
289
290 try {
291 expect(body.data).to.have.lengthOf(0)
292 } catch {
293 const video = body.data[0]
294
295 expect(video.uuid).not.equal(nsfwUUID)
296 }
297 }
298 })
299 })
300
301 describe('Channels search', async function () {
302
303 it('Should make a simple search and not have results', async function () {
304 const body = await command.searchChannels({ search: 'a'.repeat(500) })
305
306 expect(body.total).to.equal(0)
307 expect(body.data).to.have.lengthOf(0)
308 })
309
310 it('Should make a search and have results', async function () {
311
312 async function check (search: VideoChannelsSearchQuery, exists = true) {
313 const body = await command.advancedChannelSearch({ search })
314
315 if (exists === false) {
316 expect(body.total).to.equal(0)
317 expect(body.data).to.have.lengthOf(0)
318 return
319 }
320
321 expect(body.total).to.be.greaterThan(0)
322 expect(body.data).to.have.length.greaterThan(0)
323
324 const videoChannel = body.data[0]
325 expect(videoChannel.url).to.equal('https://framatube.org/video-channels/bf54d359-cfad-4935-9d45-9d6be93f63e8')
326 expect(videoChannel.host).to.equal('framatube.org')
327 expect(videoChannel.avatar).to.exist
328 expect(videoChannel.displayName).to.exist
329
330 expect(videoChannel.ownerAccount.url).to.equal('https://framatube.org/accounts/framasoft')
331 expect(videoChannel.ownerAccount.name).to.equal('framasoft')
332 expect(videoChannel.ownerAccount.host).to.equal('framatube.org')
333 expect(videoChannel.ownerAccount.avatar).to.exist
334 }
335
336 await check({ search: 'Framasoft', sort: 'createdAt' }, true)
337 await check({ search: 'Framasoft', host: 'example.com' }, false)
338 await check({ search: 'Framasoft', host: 'framatube.org' }, true)
339 })
340
341 it('Should have a correct pagination', async function () {
342 const body = await command.advancedChannelSearch({ search: { search: 'root', start: 0, count: 2 } })
343
344 expect(body.total).to.be.greaterThan(2)
345 expect(body.data).to.have.lengthOf(2)
346 })
347 })
348
349 describe('Playlists search', async function () {
350
351 async function check (search: VideoPlaylistsSearchQuery, exists = true) {
352 const body = await command.advancedPlaylistSearch({ search })
353
354 if (exists === false) {
355 expect(body.total).to.equal(0)
356 expect(body.data).to.have.lengthOf(0)
357 return
358 }
359
360 expect(body.total).to.be.greaterThan(0)
361 expect(body.data).to.have.length.greaterThan(0)
362
363 const videoPlaylist = body.data[0]
364
365 expect(videoPlaylist.url).to.equal('https://peertube2.cpy.re/videos/watch/playlist/73804a40-da9a-40c2-b1eb-2c6d9eec8f0a')
366 expect(videoPlaylist.thumbnailUrl).to.exist
367 expect(videoPlaylist.embedUrl).to.equal('https://peertube2.cpy.re/video-playlists/embed/73804a40-da9a-40c2-b1eb-2c6d9eec8f0a')
368
369 expect(videoPlaylist.type.id).to.equal(VideoPlaylistType.REGULAR)
370 expect(videoPlaylist.privacy.id).to.equal(VideoPlaylistPrivacy.PUBLIC)
371 expect(videoPlaylist.videosLength).to.exist
372
373 expect(videoPlaylist.createdAt).to.exist
374 expect(videoPlaylist.updatedAt).to.exist
375
376 expect(videoPlaylist.uuid).to.equal('73804a40-da9a-40c2-b1eb-2c6d9eec8f0a')
377 expect(videoPlaylist.displayName).to.exist
378
379 expect(videoPlaylist.ownerAccount.url).to.equal('https://peertube2.cpy.re/accounts/chocobozzz')
380 expect(videoPlaylist.ownerAccount.name).to.equal('chocobozzz')
381 expect(videoPlaylist.ownerAccount.host).to.equal('peertube2.cpy.re')
382 expect(videoPlaylist.ownerAccount.avatar).to.exist
383
384 expect(videoPlaylist.videoChannel.url).to.equal('https://peertube2.cpy.re/video-channels/chocobozzz_channel')
385 expect(videoPlaylist.videoChannel.name).to.equal('chocobozzz_channel')
386 expect(videoPlaylist.videoChannel.host).to.equal('peertube2.cpy.re')
387 expect(videoPlaylist.videoChannel.avatar).to.exist
388 }
389
390 it('Should make a simple search and not have results', async function () {
391 const body = await command.searchPlaylists({ search: 'a'.repeat(500) })
392
393 expect(body.total).to.equal(0)
394 expect(body.data).to.have.lengthOf(0)
395 })
396
397 it('Should make a search and have results', async function () {
398 await check({ search: 'E2E playlist', sort: '-match' }, true)
399 })
400
401 it('Should make host search and have appropriate results', async function () {
402 await check({ search: 'E2E playlist', host: 'example.com' }, false)
403 await check({ search: 'E2E playlist', host: 'peertube2.cpy.re', sort: '-match' }, true)
404 })
405
406 it('Should make a search by uuids and have appropriate results', async function () {
407 const goodUUID = '73804a40-da9a-40c2-b1eb-2c6d9eec8f0a'
408 const goodShortUUID = 'fgei1ws1oa6FCaJ2qZPG29'
409 const badUUID = 'c29c5b77-4a04-493d-96a9-2e9267e308f0'
410 const badShortUUID = 'rP5RgUeX9XwTSrspCdkDej'
411
412 {
413 const uuidsMatrix = [
414 [ goodUUID ],
415 [ goodUUID, badShortUUID ],
416 [ badShortUUID, goodShortUUID ],
417 [ goodUUID, goodShortUUID ]
418 ]
419
420 for (const uuids of uuidsMatrix) {
421 const search = { search: 'E2E playlist', sort: '-match', uuids }
422 await check(search, true)
423 }
424 }
425
426 {
427 const uuidsMatrix = [
428 [ badUUID ],
429 [ badShortUUID ]
430 ]
431
432 for (const uuids of uuidsMatrix) {
433 const search = { search: 'E2E playlist', sort: '-match', uuids }
434 await check(search, false)
435 }
436 }
437 })
438
439 it('Should have a correct pagination', async function () {
440 const body = await command.advancedChannelSearch({ search: { search: 'root', start: 0, count: 2 } })
441
442 expect(body.total).to.be.greaterThan(2)
443 expect(body.data).to.have.lengthOf(2)
444 })
445 })
446
447 after(async function () {
448 await cleanupTests([ server ])
449 })
450 })