1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { cleanupTests, flushAndRunServer, SearchCommand, ServerInfo, setAccessTokensToServers } from '@shared/extra-utils'
6 import { BooleanBothQuery, VideoPlaylistPrivacy, VideoPlaylistType, VideosSearchQuery } from '@shared/models'
8 const expect = chai.expect
10 describe('Test videos search', function () {
11 const localVideoName = 'local video' + new Date().toISOString()
13 let server: ServerInfo = null
14 let command: SearchCommand
16 before(async function () {
19 server = await flushAndRunServer(1)
21 await setAccessTokensToServers([ server ])
23 await server.videosCommand.upload({ attributes: { name: localVideoName } })
25 command = server.searchCommand
28 describe('Default search', async function () {
30 it('Should make a local videos search by default', async function () {
33 await server.configCommand.updateCustomSubConfig({
38 isDefaultSearch: false,
39 disableLocalSearch: false
45 const body = await command.searchVideos({ search: 'local video' })
47 expect(body.total).to.equal(1)
48 expect(body.data[0].name).to.equal(localVideoName)
51 it('Should make a local channels search by default', async function () {
52 const body = await command.searchChannels({ search: 'root' })
54 expect(body.total).to.equal(1)
55 expect(body.data[0].name).to.equal('root_channel')
56 expect(body.data[0].host).to.equal('localhost:' + server.port)
59 it('Should make an index videos search by default', async function () {
60 await server.configCommand.updateCustomSubConfig({
65 isDefaultSearch: true,
66 disableLocalSearch: false
72 const body = await command.searchVideos({ search: 'local video' })
73 expect(body.total).to.be.greaterThan(2)
76 it('Should make an index channels search by default', async function () {
77 const body = await command.searchChannels({ search: 'root' })
78 expect(body.total).to.be.greaterThan(2)
81 it('Should make an index videos search if local search is disabled', async function () {
82 await server.configCommand.updateCustomSubConfig({
87 isDefaultSearch: false,
88 disableLocalSearch: true
94 const body = await command.searchVideos({ search: 'local video' })
95 expect(body.total).to.be.greaterThan(2)
98 it('Should make an index channels search if local search is disabled', async function () {
99 const body = await command.searchChannels({ search: 'root' })
100 expect(body.total).to.be.greaterThan(2)
104 describe('Videos search', async function () {
106 it('Should make a simple search and not have results', async function () {
107 const body = await command.searchVideos({ search: 'djidane'.repeat(50) })
109 expect(body.total).to.equal(0)
110 expect(body.data).to.have.lengthOf(0)
113 it('Should make a simple search and have results', async function () {
114 const body = await command.searchVideos({ search: 'What is PeerTube' })
116 expect(body.total).to.be.greaterThan(1)
119 it('Should make a complex search', async function () {
121 async function check (search: VideosSearchQuery, exists = true) {
122 const body = await command.advancedVideoSearch({ search })
124 if (exists === false) {
125 expect(body.total).to.equal(0)
126 expect(body.data).to.have.lengthOf(0)
130 expect(body.total).to.equal(1)
131 expect(body.data).to.have.lengthOf(1)
133 const video = body.data[0]
135 expect(video.name).to.equal('What is PeerTube?')
136 expect(video.category.label).to.equal('Science & Technology')
137 expect(video.licence.label).to.equal('Attribution - Share Alike')
138 expect(video.privacy.label).to.equal('Public')
139 expect(video.duration).to.equal(113)
140 expect(video.thumbnailUrl.startsWith('https://framatube.org/static/thumbnails')).to.be.true
142 expect(video.account.host).to.equal('framatube.org')
143 expect(video.account.name).to.equal('framasoft')
144 expect(video.account.url).to.equal('https://framatube.org/accounts/framasoft')
145 expect(video.account.avatar).to.exist
147 expect(video.channel.host).to.equal('framatube.org')
148 expect(video.channel.name).to.equal('bf54d359-cfad-4935-9d45-9d6be93f63e8')
149 expect(video.channel.url).to.equal('https://framatube.org/video-channels/bf54d359-cfad-4935-9d45-9d6be93f63e8')
150 expect(video.channel.avatar).to.exist
153 const baseSearch: VideosSearchQuery = {
154 search: 'what is peertube',
157 categoryOneOf: [ 15 ],
159 tagsAllOf: [ 'framasoft', 'peertube' ],
160 startDate: '2018-10-01T10:50:46.396Z',
161 endDate: '2018-10-01T10:55:46.396Z'
165 await check(baseSearch)
169 const search = { ...baseSearch, startDate: '2018-10-01T10:54:46.396Z' }
170 await check(search, false)
174 const search = { ...baseSearch, tagsAllOf: [ 'toto', 'framasoft' ] }
175 await check(search, false)
179 const search = { ...baseSearch, durationMin: 2000 }
180 await check(search, false)
184 const search = { ...baseSearch, nsfw: 'true' as BooleanBothQuery }
185 await check(search, false)
189 const search = { ...baseSearch, nsfw: 'false' as BooleanBothQuery }
190 await check(search, true)
194 const search = { ...baseSearch, nsfw: 'both' as BooleanBothQuery }
195 await check(search, true)
199 it('Should have a correct pagination', async function () {
206 const body = await command.advancedVideoSearch({ search })
208 expect(body.total).to.be.greaterThan(5)
209 expect(body.data).to.have.lengthOf(5)
212 it('Should use the nsfw instance policy as default', async function () {
216 await server.configCommand.updateCustomSubConfig({
218 instance: { defaultNSFWPolicy: 'display' }
222 const body = await command.searchVideos({ search: 'NSFW search index', sort: '-match' })
223 expect(body.data).to.have.length.greaterThan(0)
225 const video = body.data[0]
226 expect(video.nsfw).to.be.true
228 nsfwUUID = video.uuid
232 await server.configCommand.updateCustomSubConfig({
234 instance: { defaultNSFWPolicy: 'do_not_list' }
238 const body = await command.searchVideos({ search: 'NSFW search index', sort: '-match' })
241 expect(body.data).to.have.lengthOf(0)
243 const video = body.data[0]
245 expect(video.uuid).not.equal(nsfwUUID)
251 describe('Channels search', async function () {
253 it('Should make a simple search and not have results', async function () {
254 const body = await command.searchChannels({ search: 'a'.repeat(500) })
256 expect(body.total).to.equal(0)
257 expect(body.data).to.have.lengthOf(0)
260 it('Should make a search and have results', async function () {
261 const body = await command.advancedChannelSearch({ search: { search: 'Framasoft', sort: 'createdAt' } })
263 expect(body.total).to.be.greaterThan(0)
264 expect(body.data).to.have.length.greaterThan(0)
266 const videoChannel = body.data[0]
267 expect(videoChannel.url).to.equal('https://framatube.org/video-channels/bf54d359-cfad-4935-9d45-9d6be93f63e8')
268 expect(videoChannel.host).to.equal('framatube.org')
269 expect(videoChannel.avatar).to.exist
270 expect(videoChannel.displayName).to.exist
272 expect(videoChannel.ownerAccount.url).to.equal('https://framatube.org/accounts/framasoft')
273 expect(videoChannel.ownerAccount.name).to.equal('framasoft')
274 expect(videoChannel.ownerAccount.host).to.equal('framatube.org')
275 expect(videoChannel.ownerAccount.avatar).to.exist
278 it('Should have a correct pagination', async function () {
279 const body = await command.advancedChannelSearch({ search: { search: 'root', start: 0, count: 2 } })
281 expect(body.total).to.be.greaterThan(2)
282 expect(body.data).to.have.lengthOf(2)
286 describe('Playlists search', async function () {
288 it('Should make a simple search and not have results', async function () {
289 const body = await command.searchPlaylists({ search: 'a'.repeat(500) })
291 expect(body.total).to.equal(0)
292 expect(body.data).to.have.lengthOf(0)
295 it('Should make a search and have results', async function () {
296 const body = await command.advancedPlaylistSearch({ search: { search: 'E2E playlist', sort: '-match' } })
298 expect(body.total).to.be.greaterThan(0)
299 expect(body.data).to.have.length.greaterThan(0)
301 const videoPlaylist = body.data[0]
303 expect(videoPlaylist.url).to.equal('https://peertube2.cpy.re/videos/watch/playlist/73804a40-da9a-40c2-b1eb-2c6d9eec8f0a')
304 expect(videoPlaylist.thumbnailUrl).to.exist
305 expect(videoPlaylist.embedUrl).to.equal('https://peertube2.cpy.re/video-playlists/embed/73804a40-da9a-40c2-b1eb-2c6d9eec8f0a')
307 expect(videoPlaylist.type.id).to.equal(VideoPlaylistType.REGULAR)
308 expect(videoPlaylist.privacy.id).to.equal(VideoPlaylistPrivacy.PUBLIC)
309 expect(videoPlaylist.videosLength).to.exist
311 expect(videoPlaylist.createdAt).to.exist
312 expect(videoPlaylist.updatedAt).to.exist
314 expect(videoPlaylist.uuid).to.equal('73804a40-da9a-40c2-b1eb-2c6d9eec8f0a')
315 expect(videoPlaylist.displayName).to.exist
317 expect(videoPlaylist.ownerAccount.url).to.equal('https://peertube2.cpy.re/accounts/chocobozzz')
318 expect(videoPlaylist.ownerAccount.name).to.equal('chocobozzz')
319 expect(videoPlaylist.ownerAccount.host).to.equal('peertube2.cpy.re')
320 expect(videoPlaylist.ownerAccount.avatar).to.exist
322 expect(videoPlaylist.videoChannel.url).to.equal('https://peertube2.cpy.re/video-channels/chocobozzz_channel')
323 expect(videoPlaylist.videoChannel.name).to.equal('chocobozzz_channel')
324 expect(videoPlaylist.videoChannel.host).to.equal('peertube2.cpy.re')
325 expect(videoPlaylist.videoChannel.avatar).to.exist
328 it('Should have a correct pagination', async function () {
329 const body = await command.advancedChannelSearch({ search: { search: 'root', start: 0, count: 2 } })
331 expect(body.total).to.be.greaterThan(2)
332 expect(body.data).to.have.lengthOf(2)
336 after(async function () {
337 await cleanupTests([ server ])