aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/api/search/search-index.ts
blob: 53b91e1cbc5766b63086dd5d977da39444c1ba42 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */

import 'mocha'
import * as chai from 'chai'
import { cleanupTests, createSingleServer, PeerTubeServer, SearchCommand, setAccessTokensToServers } from '@shared/server-commands'
import {
  BooleanBothQuery,
  VideoChannelsSearchQuery,
  VideoPlaylistPrivacy,
  VideoPlaylistsSearchQuery,
  VideoPlaylistType,
  VideosSearchQuery
} from '@shared/models'

const expect = chai.expect

describe('Test index search', function () {
  const localVideoName = 'local video' + new Date().toISOString()

  let server: PeerTubeServer = null
  let command: SearchCommand

  before(async function () {
    this.timeout(30000)

    server = await createSingleServer(1)

    await setAccessTokensToServers([ server ])

    await server.videos.upload({ attributes: { name: localVideoName } })

    command = server.search
  })

  describe('Default search', async function () {

    it('Should make a local videos search by default', async function () {
      this.timeout(10000)

      await server.config.updateCustomSubConfig({
        newConfig: {
          search: {
            searchIndex: {
              enabled: true,
              isDefaultSearch: false,
              disableLocalSearch: false
            }
          }
        }
      })

      const body = await command.searchVideos({ search: 'local video' })

      expect(body.total).to.equal(1)
      expect(body.data[0].name).to.equal(localVideoName)
    })

    it('Should make a local channels search by default', async function () {
      const body = await command.searchChannels({ search: 'root' })

      expect(body.total).to.equal(1)
      expect(body.data[0].name).to.equal('root_channel')
      expect(body.data[0].host).to.equal('localhost:' + server.port)
    })

    it('Should make an index videos search by default', async function () {
      await server.config.updateCustomSubConfig({
        newConfig: {
          search: {
            searchIndex: {
              enabled: true,
              isDefaultSearch: true,
              disableLocalSearch: false
            }
          }
        }
      })

      const body = await command.searchVideos({ search: 'local video' })
      expect(body.total).to.be.greaterThan(2)
    })

    it('Should make an index channels search by default', async function () {
      const body = await command.searchChannels({ search: 'root' })
      expect(body.total).to.be.greaterThan(2)
    })

    it('Should make an index videos search if local search is disabled', async function () {
      await server.config.updateCustomSubConfig({
        newConfig: {
          search: {
            searchIndex: {
              enabled: true,
              isDefaultSearch: false,
              disableLocalSearch: true
            }
          }
        }
      })

      const body = await command.searchVideos({ search: 'local video' })
      expect(body.total).to.be.greaterThan(2)
    })

    it('Should make an index channels search if local search is disabled', async function () {
      const body = await command.searchChannels({ search: 'root' })
      expect(body.total).to.be.greaterThan(2)
    })
  })

  describe('Videos search', async function () {

    async function check (search: VideosSearchQuery, exists = true) {
      const body = await command.advancedVideoSearch({ search })

      if (exists === false) {
        expect(body.total).to.equal(0)
        expect(body.data).to.have.lengthOf(0)
        return
      }

      expect(body.total).to.equal(1)
      expect(body.data).to.have.lengthOf(1)

      const video = body.data[0]

      expect(video.name).to.equal('What is PeerTube?')
      expect(video.category.label).to.equal('Science & Technology')
      expect(video.licence.label).to.equal('Attribution - Share Alike')
      expect(video.privacy.label).to.equal('Public')
      expect(video.duration).to.equal(113)
      expect(video.thumbnailUrl.startsWith('https://framatube.org/static/thumbnails')).to.be.true

      expect(video.account.host).to.equal('framatube.org')
      expect(video.account.name).to.equal('framasoft')
      expect(video.account.url).to.equal('https://framatube.org/accounts/framasoft')
      // TODO: remove, deprecated in 4.2
      expect(video.account.avatar).to.exist
      expect(video.account.avatars.length).to.equal(2, 'Account should have one avatar image')

      expect(video.channel.host).to.equal('framatube.org')
      expect(video.channel.name).to.equal('joinpeertube')
      expect(video.channel.url).to.equal('https://framatube.org/video-channels/joinpeertube')
      // TODO: remove, deprecated in 4.2
      expect(video.channel.avatar).to.exist
      expect(video.channel.avatars.length).to.equal(2, 'Channel should have one avatar image')
    }

    const baseSearch: VideosSearchQuery = {
      search: 'what is peertube',
      start: 0,
      count: 2,
      categoryOneOf: [ 15 ],
      licenceOneOf: [ 2 ],
      tagsAllOf: [ 'framasoft', 'peertube' ],
      startDate: '2018-10-01T10:50:46.396Z',
      endDate: '2018-10-01T10:55:46.396Z'
    }

    it('Should make a simple search and not have results', async function () {
      const body = await command.searchVideos({ search: 'djidane'.repeat(50) })

      expect(body.total).to.equal(0)
      expect(body.data).to.have.lengthOf(0)
    })

    it('Should make a simple search and have results', async function () {
      const body = await command.searchVideos({ search: 'What is PeerTube' })

      expect(body.total).to.be.greaterThan(1)
    })

    it('Should make a simple search', async function () {
      await check(baseSearch)
    })

    it('Should search by start date', async function () {
      const search = { ...baseSearch, startDate: '2018-10-01T10:54:46.396Z' }
      await check(search, false)
    })

    it('Should search by tags', async function () {
      const search = { ...baseSearch, tagsAllOf: [ 'toto', 'framasoft' ] }
      await check(search, false)
    })

    it('Should search by duration', async function () {
      const search = { ...baseSearch, durationMin: 2000 }
      await check(search, false)
    })

    it('Should search by nsfw attribute', async function () {
      {
        const search = { ...baseSearch, nsfw: 'true' as BooleanBothQuery }
        await check(search, false)
      }

      {
        const search = { ...baseSearch, nsfw: 'false' as BooleanBothQuery }
        await check(search, true)
      }

      {
        const search = { ...baseSearch, nsfw: 'both' as BooleanBothQuery }
        await check(search, true)
      }
    })

    it('Should search by host', async function () {
      {
        const search = { ...baseSearch, host: 'example.com' }
        await check(search, false)
      }

      {
        const search = { ...baseSearch, host: 'framatube.org' }
        await check(search, true)
      }
    })

    it('Should search by uuids', async function () {
      const goodUUID = '9c9de5e8-0a1e-484a-b099-e80766180a6d'
      const goodShortUUID = 'kkGMgK9ZtnKfYAgnEtQxbv'
      const badUUID = 'c29c5b77-4a04-493d-96a9-2e9267e308f0'
      const badShortUUID = 'rP5RgUeX9XwTSrspCdkDej'

      {
        const uuidsMatrix = [
          [ goodUUID ],
          [ goodUUID, badShortUUID ],
          [ badShortUUID, goodShortUUID ],
          [ goodUUID, goodShortUUID ]
        ]

        for (const uuids of uuidsMatrix) {
          const search = { ...baseSearch, uuids }
          await check(search, true)
        }
      }

      {
        const uuidsMatrix = [
          [ badUUID ],
          [ badShortUUID ]
        ]

        for (const uuids of uuidsMatrix) {
          const search = { ...baseSearch, uuids }
          await check(search, false)
        }
      }
    })

    it('Should have a correct pagination', async function () {
      const search = {
        search: 'video',
        start: 0,
        count: 5
      }

      const body = await command.advancedVideoSearch({ search })

      expect(body.total).to.be.greaterThan(5)
      expect(body.data).to.have.lengthOf(5)
    })

    it('Should use the nsfw instance policy as default', async function () {
      let nsfwUUID: string

      {
        await server.config.updateCustomSubConfig({
          newConfig: {
            instance: { defaultNSFWPolicy: 'display' }
          }
        })

        const body = await command.searchVideos({ search: 'NSFW search index', sort: '-match' })
        expect(body.data).to.have.length.greaterThan(0)

        const video = body.data[0]
        expect(video.nsfw).to.be.true

        nsfwUUID = video.uuid
      }

      {
        await server.config.updateCustomSubConfig({
          newConfig: {
            instance: { defaultNSFWPolicy: 'do_not_list' }
          }
        })

        const body = await command.searchVideos({ search: 'NSFW search index', sort: '-match' })

        try {
          expect(body.data).to.have.lengthOf(0)
        } catch {
          const video = body.data[0]

          expect(video.uuid).not.equal(nsfwUUID)
        }
      }
    })
  })

  describe('Channels search', async function () {

    async function check (search: VideoChannelsSearchQuery, exists = true) {
      const body = await command.advancedChannelSearch({ search })

      if (exists === false) {
        expect(body.total).to.equal(0)
        expect(body.data).to.have.lengthOf(0)
        return
      }

      expect(body.total).to.be.greaterThan(0)
      expect(body.data).to.have.length.greaterThan(0)

      const videoChannel = body.data[0]
      expect(videoChannel.url).to.equal('https://framatube.org/video-channels/bf54d359-cfad-4935-9d45-9d6be93f63e8')
      expect(videoChannel.host).to.equal('framatube.org')
      // TODO: remove, deprecated in 4.2
      expect(videoChannel.avatar).to.exist
      expect(videoChannel.avatars.length).to.equal(2, 'Channel should have two avatar images')
      expect(videoChannel.displayName).to.exist

      expect(videoChannel.ownerAccount.url).to.equal('https://framatube.org/accounts/framasoft')
      expect(videoChannel.ownerAccount.name).to.equal('framasoft')
      expect(videoChannel.ownerAccount.host).to.equal('framatube.org')
      // TODO: remove, deprecated in 4.2
      expect(videoChannel.ownerAccount.avatar).to.exist
      expect(videoChannel.ownerAccount.avatars.length).to.equal(2, 'Account should have two avatar images')
    }

    it('Should make a simple search and not have results', async function () {
      const body = await command.searchChannels({ search: 'a'.repeat(500) })

      expect(body.total).to.equal(0)
      expect(body.data).to.have.lengthOf(0)
    })

    it('Should make a search and have results', async function () {
      await check({ search: 'Framasoft', sort: 'createdAt' }, true)
    })

    it('Should make host search and have appropriate results', async function () {
      await check({ search: 'Framasoft videos', host: 'example.com' }, false)
      await check({ search: 'Framasoft videos', host: 'framatube.org' }, true)
    })

    it('Should make handles search and have appropriate results', async function () {
      await check({ handles: [ 'bf54d359-cfad-4935-9d45-9d6be93f63e8@framatube.org' ] }, true)
      await check({ handles: [ 'jeanine', 'bf54d359-cfad-4935-9d45-9d6be93f63e8@framatube.org' ] }, true)
      await check({ handles: [ 'jeanine', 'chocobozzz_channel2@peertube2.cpy.re' ] }, false)
    })

    it('Should have a correct pagination', async function () {
      const body = await command.advancedChannelSearch({ search: { search: 'root', start: 0, count: 2 } })

      expect(body.total).to.be.greaterThan(2)
      expect(body.data).to.have.lengthOf(2)
    })
  })

  describe('Playlists search', async function () {

    async function check (search: VideoPlaylistsSearchQuery, exists = true) {
      const body = await command.advancedPlaylistSearch({ search })

      if (exists === false) {
        expect(body.total).to.equal(0)
        expect(body.data).to.have.lengthOf(0)
        return
      }

      expect(body.total).to.be.greaterThan(0)
      expect(body.data).to.have.length.greaterThan(0)

      const videoPlaylist = body.data[0]

      expect(videoPlaylist.url).to.equal('https://peertube2.cpy.re/videos/watch/playlist/73804a40-da9a-40c2-b1eb-2c6d9eec8f0a')
      expect(videoPlaylist.thumbnailUrl).to.exist
      expect(videoPlaylist.embedUrl).to.equal('https://peertube2.cpy.re/video-playlists/embed/73804a40-da9a-40c2-b1eb-2c6d9eec8f0a')

      expect(videoPlaylist.type.id).to.equal(VideoPlaylistType.REGULAR)
      expect(videoPlaylist.privacy.id).to.equal(VideoPlaylistPrivacy.PUBLIC)
      expect(videoPlaylist.videosLength).to.exist

      expect(videoPlaylist.createdAt).to.exist
      expect(videoPlaylist.updatedAt).to.exist

      expect(videoPlaylist.uuid).to.equal('73804a40-da9a-40c2-b1eb-2c6d9eec8f0a')
      expect(videoPlaylist.displayName).to.exist

      expect(videoPlaylist.ownerAccount.url).to.equal('https://peertube2.cpy.re/accounts/chocobozzz')
      expect(videoPlaylist.ownerAccount.name).to.equal('chocobozzz')
      expect(videoPlaylist.ownerAccount.host).to.equal('peertube2.cpy.re')
      // TODO: remove, deprecated in 4.2
      expect(videoPlaylist.ownerAccount.avatar).to.exist
      expect(videoPlaylist.ownerAccount.avatars.length).to.equal(2, 'Account should have two avatar images')

      expect(videoPlaylist.videoChannel.url).to.equal('https://peertube2.cpy.re/video-channels/chocobozzz_channel')
      expect(videoPlaylist.videoChannel.name).to.equal('chocobozzz_channel')
      expect(videoPlaylist.videoChannel.host).to.equal('peertube2.cpy.re')
      // TODO: remove, deprecated in 4.2
      expect(videoPlaylist.videoChannel.avatar).to.exist
      expect(videoPlaylist.videoChannel.avatars.length).to.equal(2, 'Channel should have two avatar images')
    }

    it('Should make a simple search and not have results', async function () {
      const body = await command.searchPlaylists({ search: 'a'.repeat(500) })

      expect(body.total).to.equal(0)
      expect(body.data).to.have.lengthOf(0)
    })

    it('Should make a search and have results', async function () {
      await check({ search: 'E2E playlist', sort: '-match' }, true)
    })

    it('Should make host search and have appropriate results', async function () {
      await check({ search: 'E2E playlist', host: 'example.com' }, false)
      await check({ search: 'E2E playlist', host: 'peertube2.cpy.re', sort: '-match' }, true)
    })

    it('Should make a search by uuids and have appropriate results', async function () {
      const goodUUID = '73804a40-da9a-40c2-b1eb-2c6d9eec8f0a'
      const goodShortUUID = 'fgei1ws1oa6FCaJ2qZPG29'
      const badUUID = 'c29c5b77-4a04-493d-96a9-2e9267e308f0'
      const badShortUUID = 'rP5RgUeX9XwTSrspCdkDej'

      {
        const uuidsMatrix = [
          [ goodUUID ],
          [ goodUUID, badShortUUID ],
          [ badShortUUID, goodShortUUID ],
          [ goodUUID, goodShortUUID ]
        ]

        for (const uuids of uuidsMatrix) {
          const search = { search: 'E2E playlist', sort: '-match', uuids }
          await check(search, true)
        }
      }

      {
        const uuidsMatrix = [
          [ badUUID ],
          [ badShortUUID ]
        ]

        for (const uuids of uuidsMatrix) {
          const search = { search: 'E2E playlist', sort: '-match', uuids }
          await check(search, false)
        }
      }
    })

    it('Should have a correct pagination', async function () {
      const body = await command.advancedChannelSearch({ search: { search: 'root', start: 0, count: 2 } })

      expect(body.total).to.be.greaterThan(2)
      expect(body.data).to.have.lengthOf(2)
    })
  })

  after(async function () {
    await cleanupTests([ server ])
  })
})