1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { cleanupTests, createSingleServer, PeerTubeServer, SearchCommand, setAccessTokensToServers } from '@shared/server-commands'
8 VideoChannelsSearchQuery,
10 VideoPlaylistsSearchQuery,
13 } from '@shared/models'
15 const expect = chai.expect
17 describe('Test index search', function () {
18 const localVideoName = 'local video' + new Date().toISOString()
20 let server: PeerTubeServer = null
21 let command: SearchCommand
23 before(async function () {
26 server = await createSingleServer(1)
28 await setAccessTokensToServers([ server ])
30 await server.videos.upload({ attributes: { name: localVideoName } })
32 command = server.search
35 describe('Default search', async function () {
37 it('Should make a local videos search by default', async function () {
40 await server.config.updateCustomSubConfig({
45 isDefaultSearch: false,
46 disableLocalSearch: false
52 const body = await command.searchVideos({ search: 'local video' })
54 expect(body.total).to.equal(1)
55 expect(body.data[0].name).to.equal(localVideoName)
58 it('Should make a local channels search by default', async function () {
59 const body = await command.searchChannels({ search: 'root' })
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)
66 it('Should make an index videos search by default', async function () {
67 await server.config.updateCustomSubConfig({
72 isDefaultSearch: true,
73 disableLocalSearch: false
79 const body = await command.searchVideos({ search: 'local video' })
80 expect(body.total).to.be.greaterThan(2)
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)
88 it('Should make an index videos search if local search is disabled', async function () {
89 await server.config.updateCustomSubConfig({
94 isDefaultSearch: false,
95 disableLocalSearch: true
101 const body = await command.searchVideos({ search: 'local video' })
102 expect(body.total).to.be.greaterThan(2)
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)
111 describe('Videos search', async function () {
113 async function check (search: VideosSearchQuery, exists = true) {
114 const body = await command.advancedVideoSearch({ search })
116 if (exists === false) {
117 expect(body.total).to.equal(0)
118 expect(body.data).to.have.lengthOf(0)
122 expect(body.total).to.equal(1)
123 expect(body.data).to.have.lengthOf(1)
125 const video = body.data[0]
127 expect(video.name).to.equal('What is PeerTube?')
128 expect(video.category.label).to.equal('Science & Technology')
129 expect(video.licence.label).to.equal('Attribution - Share Alike')
130 expect(video.privacy.label).to.equal('Public')
131 expect(video.duration).to.equal(113)
132 expect(video.thumbnailUrl.startsWith('https://framatube.org/static/thumbnails')).to.be.true
134 expect(video.account.host).to.equal('framatube.org')
135 expect(video.account.name).to.equal('framasoft')
136 expect(video.account.url).to.equal('https://framatube.org/accounts/framasoft')
137 // TODO: remove, deprecated in 4.2
138 expect(video.account.avatar).to.exist
139 expect(video.account.avatars.length).to.equal(1, 'Account should have one avatar image')
141 expect(video.channel.host).to.equal('framatube.org')
142 expect(video.channel.name).to.equal('joinpeertube')
143 expect(video.channel.url).to.equal('https://framatube.org/video-channels/joinpeertube')
144 // TODO: remove, deprecated in 4.2
145 expect(video.channel.avatar).to.exist
146 expect(video.channel.avatars.length).to.equal(1, 'Channel should have one avatar image')
149 const baseSearch: VideosSearchQuery = {
150 search: 'what is peertube',
153 categoryOneOf: [ 15 ],
155 tagsAllOf: [ 'framasoft', 'peertube' ],
156 startDate: '2018-10-01T10:50:46.396Z',
157 endDate: '2018-10-01T10:55:46.396Z'
160 it('Should make a simple search and not have results', async function () {
161 const body = await command.searchVideos({ search: 'djidane'.repeat(50) })
163 expect(body.total).to.equal(0)
164 expect(body.data).to.have.lengthOf(0)
167 it('Should make a simple search and have results', async function () {
168 const body = await command.searchVideos({ search: 'What is PeerTube' })
170 expect(body.total).to.be.greaterThan(1)
173 it('Should make a simple search', async function () {
174 await check(baseSearch)
177 it('Should search by start date', async function () {
178 const search = { ...baseSearch, startDate: '2018-10-01T10:54:46.396Z' }
179 await check(search, false)
182 it('Should search by tags', async function () {
183 const search = { ...baseSearch, tagsAllOf: [ 'toto', 'framasoft' ] }
184 await check(search, false)
187 it('Should search by duration', async function () {
188 const search = { ...baseSearch, durationMin: 2000 }
189 await check(search, false)
192 it('Should search by nsfw attribute', async function () {
194 const search = { ...baseSearch, nsfw: 'true' as BooleanBothQuery }
195 await check(search, false)
199 const search = { ...baseSearch, nsfw: 'false' as BooleanBothQuery }
200 await check(search, true)
204 const search = { ...baseSearch, nsfw: 'both' as BooleanBothQuery }
205 await check(search, true)
209 it('Should search by host', async function () {
211 const search = { ...baseSearch, host: 'example.com' }
212 await check(search, false)
216 const search = { ...baseSearch, host: 'framatube.org' }
217 await check(search, true)
221 it('Should search by uuids', async function () {
222 const goodUUID = '9c9de5e8-0a1e-484a-b099-e80766180a6d'
223 const goodShortUUID = 'kkGMgK9ZtnKfYAgnEtQxbv'
224 const badUUID = 'c29c5b77-4a04-493d-96a9-2e9267e308f0'
225 const badShortUUID = 'rP5RgUeX9XwTSrspCdkDej'
228 const uuidsMatrix = [
230 [ goodUUID, badShortUUID ],
231 [ badShortUUID, goodShortUUID ],
232 [ goodUUID, goodShortUUID ]
235 for (const uuids of uuidsMatrix) {
236 const search = { ...baseSearch, uuids }
237 await check(search, true)
242 const uuidsMatrix = [
247 for (const uuids of uuidsMatrix) {
248 const search = { ...baseSearch, uuids }
249 await check(search, false)
254 it('Should have a correct pagination', async function () {
261 const body = await command.advancedVideoSearch({ search })
263 expect(body.total).to.be.greaterThan(5)
264 expect(body.data).to.have.lengthOf(5)
267 it('Should use the nsfw instance policy as default', async function () {
271 await server.config.updateCustomSubConfig({
273 instance: { defaultNSFWPolicy: 'display' }
277 const body = await command.searchVideos({ search: 'NSFW search index', sort: '-match' })
278 expect(body.data).to.have.length.greaterThan(0)
280 const video = body.data[0]
281 expect(video.nsfw).to.be.true
283 nsfwUUID = video.uuid
287 await server.config.updateCustomSubConfig({
289 instance: { defaultNSFWPolicy: 'do_not_list' }
293 const body = await command.searchVideos({ search: 'NSFW search index', sort: '-match' })
296 expect(body.data).to.have.lengthOf(0)
298 const video = body.data[0]
300 expect(video.uuid).not.equal(nsfwUUID)
306 describe('Channels search', async function () {
308 async function check (search: VideoChannelsSearchQuery, exists = true) {
309 const body = await command.advancedChannelSearch({ search })
311 if (exists === false) {
312 expect(body.total).to.equal(0)
313 expect(body.data).to.have.lengthOf(0)
317 expect(body.total).to.be.greaterThan(0)
318 expect(body.data).to.have.length.greaterThan(0)
320 const videoChannel = body.data[0]
321 expect(videoChannel.url).to.equal('https://framatube.org/video-channels/bf54d359-cfad-4935-9d45-9d6be93f63e8')
322 expect(videoChannel.host).to.equal('framatube.org')
323 // TODO: remove, deprecated in 4.2
324 expect(videoChannel.avatar).to.exist
325 expect(videoChannel.avatars.length).to.equal(1, 'Channel should have two avatar images')
326 expect(videoChannel.displayName).to.exist
328 expect(videoChannel.ownerAccount.url).to.equal('https://framatube.org/accounts/framasoft')
329 expect(videoChannel.ownerAccount.name).to.equal('framasoft')
330 expect(videoChannel.ownerAccount.host).to.equal('framatube.org')
331 // TODO: remove, deprecated in 4.2
332 expect(videoChannel.ownerAccount.avatar).to.exist
333 expect(videoChannel.ownerAccount.avatars.length).to.equal(1, 'Account should have two avatar images')
336 it('Should make a simple search and not have results', async function () {
337 const body = await command.searchChannels({ search: 'a'.repeat(500) })
339 expect(body.total).to.equal(0)
340 expect(body.data).to.have.lengthOf(0)
343 it('Should make a search and have results', async function () {
344 await check({ search: 'Framasoft', sort: 'createdAt' }, true)
347 it('Should make host search and have appropriate results', async function () {
348 await check({ search: 'Framasoft', host: 'example.com' }, false)
349 await check({ search: 'Framasoft', host: 'framatube.org' }, true)
352 it('Should make handles search and have appropriate results', async function () {
353 await check({ handles: [ 'bf54d359-cfad-4935-9d45-9d6be93f63e8@framatube.org' ] }, true)
354 await check({ handles: [ 'jeanine', 'bf54d359-cfad-4935-9d45-9d6be93f63e8@framatube.org' ] }, true)
355 await check({ handles: [ 'jeanine', 'chocobozzz_channel2@peertube2.cpy.re' ] }, false)
358 it('Should have a correct pagination', async function () {
359 const body = await command.advancedChannelSearch({ search: { search: 'root', start: 0, count: 2 } })
361 expect(body.total).to.be.greaterThan(2)
362 expect(body.data).to.have.lengthOf(2)
366 describe('Playlists search', async function () {
368 async function check (search: VideoPlaylistsSearchQuery, exists = true) {
369 const body = await command.advancedPlaylistSearch({ search })
371 if (exists === false) {
372 expect(body.total).to.equal(0)
373 expect(body.data).to.have.lengthOf(0)
377 expect(body.total).to.be.greaterThan(0)
378 expect(body.data).to.have.length.greaterThan(0)
380 const videoPlaylist = body.data[0]
382 expect(videoPlaylist.url).to.equal('https://peertube2.cpy.re/videos/watch/playlist/73804a40-da9a-40c2-b1eb-2c6d9eec8f0a')
383 expect(videoPlaylist.thumbnailUrl).to.exist
384 expect(videoPlaylist.embedUrl).to.equal('https://peertube2.cpy.re/video-playlists/embed/73804a40-da9a-40c2-b1eb-2c6d9eec8f0a')
386 expect(videoPlaylist.type.id).to.equal(VideoPlaylistType.REGULAR)
387 expect(videoPlaylist.privacy.id).to.equal(VideoPlaylistPrivacy.PUBLIC)
388 expect(videoPlaylist.videosLength).to.exist
390 expect(videoPlaylist.createdAt).to.exist
391 expect(videoPlaylist.updatedAt).to.exist
393 expect(videoPlaylist.uuid).to.equal('73804a40-da9a-40c2-b1eb-2c6d9eec8f0a')
394 expect(videoPlaylist.displayName).to.exist
396 expect(videoPlaylist.ownerAccount.url).to.equal('https://peertube2.cpy.re/accounts/chocobozzz')
397 expect(videoPlaylist.ownerAccount.name).to.equal('chocobozzz')
398 expect(videoPlaylist.ownerAccount.host).to.equal('peertube2.cpy.re')
399 // TODO: remove, deprecated in 4.2
400 expect(videoPlaylist.ownerAccount.avatar).to.exist
401 expect(videoPlaylist.ownerAccount.avatars.length).to.equal(2, 'Account should have two avatar images')
403 expect(videoPlaylist.videoChannel.url).to.equal('https://peertube2.cpy.re/video-channels/chocobozzz_channel')
404 expect(videoPlaylist.videoChannel.name).to.equal('chocobozzz_channel')
405 expect(videoPlaylist.videoChannel.host).to.equal('peertube2.cpy.re')
406 // TODO: remove, deprecated in 4.2
407 expect(videoPlaylist.videoChannel.avatar).to.exist
408 expect(videoPlaylist.videoChannel.avatars.length).to.equal(2, 'Channel should have two avatar images')
411 it('Should make a simple search and not have results', async function () {
412 const body = await command.searchPlaylists({ search: 'a'.repeat(500) })
414 expect(body.total).to.equal(0)
415 expect(body.data).to.have.lengthOf(0)
418 it('Should make a search and have results', async function () {
419 await check({ search: 'E2E playlist', sort: '-match' }, true)
422 it('Should make host search and have appropriate results', async function () {
423 await check({ search: 'E2E playlist', host: 'example.com' }, false)
424 await check({ search: 'E2E playlist', host: 'peertube2.cpy.re', sort: '-match' }, true)
427 it('Should make a search by uuids and have appropriate results', async function () {
428 const goodUUID = '73804a40-da9a-40c2-b1eb-2c6d9eec8f0a'
429 const goodShortUUID = 'fgei1ws1oa6FCaJ2qZPG29'
430 const badUUID = 'c29c5b77-4a04-493d-96a9-2e9267e308f0'
431 const badShortUUID = 'rP5RgUeX9XwTSrspCdkDej'
434 const uuidsMatrix = [
436 [ goodUUID, badShortUUID ],
437 [ badShortUUID, goodShortUUID ],
438 [ goodUUID, goodShortUUID ]
441 for (const uuids of uuidsMatrix) {
442 const search = { search: 'E2E playlist', sort: '-match', uuids }
443 await check(search, true)
448 const uuidsMatrix = [
453 for (const uuids of uuidsMatrix) {
454 const search = { search: 'E2E playlist', sort: '-match', uuids }
455 await check(search, false)
460 it('Should have a correct pagination', async function () {
461 const body = await command.advancedChannelSearch({ search: { search: 'root', start: 0, count: 2 } })
463 expect(body.total).to.be.greaterThan(2)
464 expect(body.data).to.have.lengthOf(2)
468 after(async function () {
469 await cleanupTests([ server ])