1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } from 'chai'
6 VideoChannelsSearchQuery,
8 VideoPlaylistsSearchQuery,
11 } from '@shared/models'
12 import { cleanupTests, createSingleServer, PeerTubeServer, SearchCommand, setAccessTokensToServers } from '@shared/server-commands'
14 describe('Test index search', function () {
15 const localVideoName = 'local video' + new Date().toISOString()
17 let server: PeerTubeServer = null
18 let command: SearchCommand
20 before(async function () {
23 server = await createSingleServer(1)
25 await setAccessTokensToServers([ server ])
27 await server.videos.upload({ attributes: { name: localVideoName } })
29 command = server.search
32 describe('Default search', async function () {
34 it('Should make a local videos search by default', async function () {
37 await server.config.updateCustomSubConfig({
42 isDefaultSearch: false,
43 disableLocalSearch: false
49 const body = await command.searchVideos({ search: 'local video' })
51 expect(body.total).to.equal(1)
52 expect(body.data[0].name).to.equal(localVideoName)
55 it('Should make a local channels search by default', async function () {
56 const body = await command.searchChannels({ search: 'root' })
58 expect(body.total).to.equal(1)
59 expect(body.data[0].name).to.equal('root_channel')
60 expect(body.data[0].host).to.equal('localhost:' + server.port)
63 it('Should make an index videos search by default', async function () {
64 await server.config.updateCustomSubConfig({
69 isDefaultSearch: true,
70 disableLocalSearch: false
76 const body = await command.searchVideos({ search: 'local video' })
77 expect(body.total).to.be.greaterThan(2)
80 it('Should make an index channels search by default', async function () {
81 const body = await command.searchChannels({ search: 'root' })
82 expect(body.total).to.be.greaterThan(2)
85 it('Should make an index videos search if local search is disabled', async function () {
86 await server.config.updateCustomSubConfig({
91 isDefaultSearch: false,
92 disableLocalSearch: true
98 const body = await command.searchVideos({ search: 'local video' })
99 expect(body.total).to.be.greaterThan(2)
102 it('Should make an index channels search if local search is disabled', async function () {
103 const body = await command.searchChannels({ search: 'root' })
104 expect(body.total).to.be.greaterThan(2)
108 describe('Videos search', async function () {
110 async function check (search: VideosSearchQuery, exists = true) {
111 const body = await command.advancedVideoSearch({ search })
113 if (exists === false) {
114 expect(body.total).to.equal(0)
115 expect(body.data).to.have.lengthOf(0)
119 expect(body.total).to.equal(1)
120 expect(body.data).to.have.lengthOf(1)
122 const video = body.data[0]
124 expect(video.name).to.equal('What is PeerTube?')
125 expect(video.category.label).to.equal('Science & Technology')
126 expect(video.licence.label).to.equal('Attribution - Share Alike')
127 expect(video.privacy.label).to.equal('Public')
128 expect(video.duration).to.equal(113)
129 expect(video.thumbnailUrl.startsWith('https://framatube.org/static/thumbnails')).to.be.true
131 expect(video.account.host).to.equal('framatube.org')
132 expect(video.account.name).to.equal('framasoft')
133 expect(video.account.url).to.equal('https://framatube.org/accounts/framasoft')
134 // TODO: remove, deprecated in 4.2
135 expect(video.account.avatar).to.exist
136 expect(video.account.avatars.length).to.equal(2, 'Account should have one avatar image')
138 expect(video.channel.host).to.equal('framatube.org')
139 expect(video.channel.name).to.equal('joinpeertube')
140 expect(video.channel.url).to.equal('https://framatube.org/video-channels/joinpeertube')
141 // TODO: remove, deprecated in 4.2
142 expect(video.channel.avatar).to.exist
143 expect(video.channel.avatars.length).to.equal(2, 'Channel should have one avatar image')
146 const baseSearch: VideosSearchQuery = {
147 search: 'what is peertube',
150 categoryOneOf: [ 15 ],
152 tagsAllOf: [ 'framasoft', 'peertube' ],
153 startDate: '2018-10-01T10:50:46.396Z',
154 endDate: '2018-10-01T10:55:46.396Z'
157 it('Should make a simple search and not have results', async function () {
158 const body = await command.searchVideos({ search: 'djidane'.repeat(50) })
160 expect(body.total).to.equal(0)
161 expect(body.data).to.have.lengthOf(0)
164 it('Should make a simple search and have results', async function () {
165 const body = await command.searchVideos({ search: 'What is PeerTube' })
167 expect(body.total).to.be.greaterThan(1)
170 it('Should make a simple search', async function () {
171 await check(baseSearch)
174 it('Should search by start date', async function () {
175 const search = { ...baseSearch, startDate: '2018-10-01T10:54:46.396Z' }
176 await check(search, false)
179 it('Should search by tags', async function () {
180 const search = { ...baseSearch, tagsAllOf: [ 'toto', 'framasoft' ] }
181 await check(search, false)
184 it('Should search by duration', async function () {
185 const search = { ...baseSearch, durationMin: 2000 }
186 await check(search, false)
189 it('Should search by nsfw attribute', async function () {
191 const search = { ...baseSearch, nsfw: 'true' as BooleanBothQuery }
192 await check(search, false)
196 const search = { ...baseSearch, nsfw: 'false' as BooleanBothQuery }
197 await check(search, true)
201 const search = { ...baseSearch, nsfw: 'both' as BooleanBothQuery }
202 await check(search, true)
206 it('Should search by host', async function () {
208 const search = { ...baseSearch, host: 'example.com' }
209 await check(search, false)
213 const search = { ...baseSearch, host: 'framatube.org' }
214 await check(search, true)
218 it('Should search by uuids', async function () {
219 const goodUUID = '9c9de5e8-0a1e-484a-b099-e80766180a6d'
220 const goodShortUUID = 'kkGMgK9ZtnKfYAgnEtQxbv'
221 const badUUID = 'c29c5b77-4a04-493d-96a9-2e9267e308f0'
222 const badShortUUID = 'rP5RgUeX9XwTSrspCdkDej'
225 const uuidsMatrix = [
227 [ goodUUID, badShortUUID ],
228 [ badShortUUID, goodShortUUID ],
229 [ goodUUID, goodShortUUID ]
232 for (const uuids of uuidsMatrix) {
233 const search = { ...baseSearch, uuids }
234 await check(search, true)
239 const uuidsMatrix = [
244 for (const uuids of uuidsMatrix) {
245 const search = { ...baseSearch, uuids }
246 await check(search, false)
251 it('Should have a correct pagination', async function () {
258 const body = await command.advancedVideoSearch({ search })
260 expect(body.total).to.be.greaterThan(5)
261 expect(body.data).to.have.lengthOf(5)
264 it('Should use the nsfw instance policy as default', async function () {
268 await server.config.updateCustomSubConfig({
270 instance: { defaultNSFWPolicy: 'display' }
274 const body = await command.searchVideos({ search: 'NSFW search index', sort: '-match' })
275 expect(body.data).to.have.length.greaterThan(0)
277 const video = body.data[0]
278 expect(video.nsfw).to.be.true
280 nsfwUUID = video.uuid
284 await server.config.updateCustomSubConfig({
286 instance: { defaultNSFWPolicy: 'do_not_list' }
290 const body = await command.searchVideos({ search: 'NSFW search index', sort: '-match' })
293 expect(body.data).to.have.lengthOf(0)
295 const video = body.data[0]
297 expect(video.uuid).not.equal(nsfwUUID)
303 describe('Channels search', async function () {
305 async function check (search: VideoChannelsSearchQuery, exists = true) {
306 const body = await command.advancedChannelSearch({ search })
308 if (exists === false) {
309 expect(body.total).to.equal(0)
310 expect(body.data).to.have.lengthOf(0)
314 expect(body.total).to.be.greaterThan(0)
315 expect(body.data).to.have.length.greaterThan(0)
317 const videoChannel = body.data[0]
318 expect(videoChannel.url).to.equal('https://framatube.org/video-channels/bf54d359-cfad-4935-9d45-9d6be93f63e8')
319 expect(videoChannel.host).to.equal('framatube.org')
320 // TODO: remove, deprecated in 4.2
321 expect(videoChannel.avatar).to.exist
322 expect(videoChannel.avatars.length).to.equal(2, 'Channel should have two avatar images')
323 expect(videoChannel.displayName).to.exist
325 expect(videoChannel.ownerAccount.url).to.equal('https://framatube.org/accounts/framasoft')
326 expect(videoChannel.ownerAccount.name).to.equal('framasoft')
327 expect(videoChannel.ownerAccount.host).to.equal('framatube.org')
328 // TODO: remove, deprecated in 4.2
329 expect(videoChannel.ownerAccount.avatar).to.exist
330 expect(videoChannel.ownerAccount.avatars.length).to.equal(2, 'Account should have two avatar images')
333 it('Should make a simple search and not have results', async function () {
334 const body = await command.searchChannels({ search: 'a'.repeat(500) })
336 expect(body.total).to.equal(0)
337 expect(body.data).to.have.lengthOf(0)
340 it('Should make a search and have results', async function () {
341 await check({ search: 'Framasoft', sort: 'createdAt' }, true)
344 it('Should make host search and have appropriate results', async function () {
345 await check({ search: 'Framasoft videos', host: 'example.com' }, false)
346 await check({ search: 'Framasoft videos', host: 'framatube.org' }, true)
349 it('Should make handles search and have appropriate results', async function () {
350 await check({ handles: [ 'bf54d359-cfad-4935-9d45-9d6be93f63e8@framatube.org' ] }, true)
351 await check({ handles: [ 'jeanine', 'bf54d359-cfad-4935-9d45-9d6be93f63e8@framatube.org' ] }, true)
352 await check({ handles: [ 'jeanine', 'chocobozzz_channel2@peertube2.cpy.re' ] }, false)
355 it('Should have a correct pagination', async function () {
356 const body = await command.advancedChannelSearch({ search: { search: 'root', start: 0, count: 2 } })
358 expect(body.total).to.be.greaterThan(2)
359 expect(body.data).to.have.lengthOf(2)
363 describe('Playlists search', async function () {
365 async function check (search: VideoPlaylistsSearchQuery, exists = true) {
366 const body = await command.advancedPlaylistSearch({ search })
368 if (exists === false) {
369 expect(body.total).to.equal(0)
370 expect(body.data).to.have.lengthOf(0)
374 expect(body.total).to.be.greaterThan(0)
375 expect(body.data).to.have.length.greaterThan(0)
377 const videoPlaylist = body.data[0]
379 expect(videoPlaylist.url).to.equal('https://peertube2.cpy.re/videos/watch/playlist/73804a40-da9a-40c2-b1eb-2c6d9eec8f0a')
380 expect(videoPlaylist.thumbnailUrl).to.exist
381 expect(videoPlaylist.embedUrl).to.equal('https://peertube2.cpy.re/video-playlists/embed/73804a40-da9a-40c2-b1eb-2c6d9eec8f0a')
383 expect(videoPlaylist.type.id).to.equal(VideoPlaylistType.REGULAR)
384 expect(videoPlaylist.privacy.id).to.equal(VideoPlaylistPrivacy.PUBLIC)
385 expect(videoPlaylist.videosLength).to.exist
387 expect(videoPlaylist.createdAt).to.exist
388 expect(videoPlaylist.updatedAt).to.exist
390 expect(videoPlaylist.uuid).to.equal('73804a40-da9a-40c2-b1eb-2c6d9eec8f0a')
391 expect(videoPlaylist.displayName).to.exist
393 expect(videoPlaylist.ownerAccount.url).to.equal('https://peertube2.cpy.re/accounts/chocobozzz')
394 expect(videoPlaylist.ownerAccount.name).to.equal('chocobozzz')
395 expect(videoPlaylist.ownerAccount.host).to.equal('peertube2.cpy.re')
396 // TODO: remove, deprecated in 4.2
397 expect(videoPlaylist.ownerAccount.avatar).to.exist
398 expect(videoPlaylist.ownerAccount.avatars.length).to.equal(2, 'Account should have two avatar images')
400 expect(videoPlaylist.videoChannel.url).to.equal('https://peertube2.cpy.re/video-channels/chocobozzz_channel')
401 expect(videoPlaylist.videoChannel.name).to.equal('chocobozzz_channel')
402 expect(videoPlaylist.videoChannel.host).to.equal('peertube2.cpy.re')
403 // TODO: remove, deprecated in 4.2
404 expect(videoPlaylist.videoChannel.avatar).to.exist
405 expect(videoPlaylist.videoChannel.avatars.length).to.equal(2, 'Channel should have two avatar images')
408 it('Should make a simple search and not have results', async function () {
409 const body = await command.searchPlaylists({ search: 'a'.repeat(500) })
411 expect(body.total).to.equal(0)
412 expect(body.data).to.have.lengthOf(0)
415 it('Should make a search and have results', async function () {
416 await check({ search: 'E2E playlist', sort: '-match' }, true)
419 it('Should make host search and have appropriate results', async function () {
420 await check({ search: 'E2E playlist', host: 'example.com' }, false)
421 await check({ search: 'E2E playlist', host: 'peertube2.cpy.re', sort: '-match' }, true)
424 it('Should make a search by uuids and have appropriate results', async function () {
425 const goodUUID = '73804a40-da9a-40c2-b1eb-2c6d9eec8f0a'
426 const goodShortUUID = 'fgei1ws1oa6FCaJ2qZPG29'
427 const badUUID = 'c29c5b77-4a04-493d-96a9-2e9267e308f0'
428 const badShortUUID = 'rP5RgUeX9XwTSrspCdkDej'
431 const uuidsMatrix = [
433 [ goodUUID, badShortUUID ],
434 [ badShortUUID, goodShortUUID ],
435 [ goodUUID, goodShortUUID ]
438 for (const uuids of uuidsMatrix) {
439 const search = { search: 'E2E playlist', sort: '-match', uuids }
440 await check(search, true)
445 const uuidsMatrix = [
450 for (const uuids of uuidsMatrix) {
451 const search = { search: 'E2E playlist', sort: '-match', uuids }
452 await check(search, false)
457 it('Should have a correct pagination', async function () {
458 const body = await command.advancedChannelSearch({ search: { search: 'root', start: 0, count: 2 } })
460 expect(body.total).to.be.greaterThan(2)
461 expect(body.data).to.have.lengthOf(2)
465 after(async function () {
466 await cleanupTests([ server ])