]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/search/search-index.ts
Introduce search command
[github/Chocobozzz/PeerTube.git] / server / tests / api / search / search-index.ts
CommitLineData
3521ab8f
C
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3import 'mocha'
4import * as chai from 'chai'
5import {
6 cleanupTests,
7 flushAndRunServer,
37a44fc9 8 immutableAssign,
af971e06 9 SearchCommand,
3521ab8f
C
10 ServerInfo,
11 setAccessTokensToServers,
12 updateCustomSubConfig,
37a44fc9 13 uploadVideo
af971e06
C
14} from '@shared/extra-utils'
15import { VideoPlaylistPrivacy, VideoPlaylistType, VideosSearchQuery } from '@shared/models'
3521ab8f
C
16
17const expect = chai.expect
18
19describe('Test videos search', function () {
3521ab8f
C
20 const localVideoName = 'local video' + new Date().toISOString()
21
af971e06
C
22 let server: ServerInfo = null
23 let command: SearchCommand
24
3521ab8f
C
25 before(async function () {
26 this.timeout(30000)
27
28 server = await flushAndRunServer(1)
29
30 await setAccessTokensToServers([ server ])
31
32 await uploadVideo(server.url, server.accessToken, { name: localVideoName })
af971e06
C
33
34 command = server.searchCommand
3521ab8f
C
35 })
36
37 describe('Default search', async function () {
38
39 it('Should make a local videos search by default', async function () {
40 this.timeout(10000)
41
42 await updateCustomSubConfig(server.url, server.accessToken, {
43 search: {
44 searchIndex: {
45 enabled: true,
46 isDefaultSearch: false,
47 disableLocalSearch: false
48 }
49 }
50 })
51
af971e06 52 const body = await command.searchVideos({ search: 'local video' })
3521ab8f 53
af971e06
C
54 expect(body.total).to.equal(1)
55 expect(body.data[0].name).to.equal(localVideoName)
3521ab8f
C
56 })
57
58 it('Should make a local channels search by default', async function () {
af971e06 59 const body = await command.searchChannels({ search: 'root' })
3521ab8f 60
af971e06
C
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)
3521ab8f
C
64 })
65
66 it('Should make an index videos search by default', async function () {
67 await updateCustomSubConfig(server.url, server.accessToken, {
68 search: {
69 searchIndex: {
70 enabled: true,
71 isDefaultSearch: true,
72 disableLocalSearch: false
73 }
74 }
75 })
76
af971e06
C
77 const body = await command.searchVideos({ search: 'local video' })
78 expect(body.total).to.be.greaterThan(2)
3521ab8f
C
79 })
80
81 it('Should make an index channels search by default', async function () {
af971e06
C
82 const body = await command.searchChannels({ search: 'root' })
83 expect(body.total).to.be.greaterThan(2)
3521ab8f
C
84 })
85
86 it('Should make an index videos search if local search is disabled', async function () {
87 await updateCustomSubConfig(server.url, server.accessToken, {
88 search: {
89 searchIndex: {
90 enabled: true,
91 isDefaultSearch: false,
92 disableLocalSearch: true
93 }
94 }
95 })
96
af971e06
C
97 const body = await command.searchVideos({ search: 'local video' })
98 expect(body.total).to.be.greaterThan(2)
3521ab8f
C
99 })
100
101 it('Should make an index channels search if local search is disabled', async function () {
af971e06
C
102 const body = await command.searchChannels({ search: 'root' })
103 expect(body.total).to.be.greaterThan(2)
3521ab8f
C
104 })
105 })
106
107 describe('Videos search', async function () {
108
109 it('Should make a simple search and not have results', async function () {
af971e06 110 const body = await command.searchVideos({ search: 'djidane'.repeat(50) })
3521ab8f 111
af971e06
C
112 expect(body.total).to.equal(0)
113 expect(body.data).to.have.lengthOf(0)
3521ab8f
C
114 })
115
116 it('Should make a simple search and have results', async function () {
af971e06 117 const body = await command.searchVideos({ search: 'What is PeerTube' })
3521ab8f 118
af971e06 119 expect(body.total).to.be.greaterThan(1)
3521ab8f
C
120 })
121
122 it('Should make a complex search', async function () {
123
124 async function check (search: VideosSearchQuery, exists = true) {
af971e06 125 const body = await command.advancedVideoSearch({ search })
3521ab8f
C
126
127 if (exists === false) {
af971e06
C
128 expect(body.total).to.equal(0)
129 expect(body.data).to.have.lengthOf(0)
3521ab8f
C
130 return
131 }
132
af971e06
C
133 expect(body.total).to.equal(1)
134 expect(body.data).to.have.lengthOf(1)
3521ab8f 135
af971e06 136 const video = body.data[0]
3521ab8f
C
137
138 expect(video.name).to.equal('What is PeerTube?')
139 expect(video.category.label).to.equal('Science & Technology')
140 expect(video.licence.label).to.equal('Attribution - Share Alike')
141 expect(video.privacy.label).to.equal('Public')
142 expect(video.duration).to.equal(113)
143 expect(video.thumbnailUrl.startsWith('https://framatube.org/static/thumbnails')).to.be.true
144
145 expect(video.account.host).to.equal('framatube.org')
146 expect(video.account.name).to.equal('framasoft')
147 expect(video.account.url).to.equal('https://framatube.org/accounts/framasoft')
148 expect(video.account.avatar).to.exist
149
150 expect(video.channel.host).to.equal('framatube.org')
151 expect(video.channel.name).to.equal('bf54d359-cfad-4935-9d45-9d6be93f63e8')
152 expect(video.channel.url).to.equal('https://framatube.org/video-channels/bf54d359-cfad-4935-9d45-9d6be93f63e8')
153 expect(video.channel.avatar).to.exist
154 }
155
156 const baseSearch: VideosSearchQuery = {
157 search: 'what is peertube',
158 start: 0,
159 count: 2,
160 categoryOneOf: [ 15 ],
161 licenceOneOf: [ 2 ],
162 tagsAllOf: [ 'framasoft', 'peertube' ],
163 startDate: '2018-10-01T10:50:46.396Z',
164 endDate: '2018-10-01T10:55:46.396Z'
165 }
166
167 {
168 await check(baseSearch)
169 }
170
171 {
172 const search = immutableAssign(baseSearch, { startDate: '2018-10-01T10:54:46.396Z' })
173 await check(search, false)
174 }
175
176 {
177 const search = immutableAssign(baseSearch, { tagsAllOf: [ 'toto', 'framasoft' ] })
178 await check(search, false)
179 }
180
181 {
182 const search = immutableAssign(baseSearch, { durationMin: 2000 })
183 await check(search, false)
184 }
185
186 {
187 const search = immutableAssign(baseSearch, { nsfw: 'true' })
188 await check(search, false)
189 }
190
191 {
192 const search = immutableAssign(baseSearch, { nsfw: 'false' })
193 await check(search, true)
194 }
195
196 {
197 const search = immutableAssign(baseSearch, { nsfw: 'both' })
198 await check(search, true)
199 }
200 })
201
202 it('Should have a correct pagination', async function () {
203 const search = {
204 search: 'video',
205 start: 0,
206 count: 5
207 }
208
af971e06 209 const body = await command.advancedVideoSearch({ search })
3521ab8f 210
af971e06
C
211 expect(body.total).to.be.greaterThan(5)
212 expect(body.data).to.have.lengthOf(5)
3521ab8f 213 })
1a40132c
C
214
215 it('Should use the nsfw instance policy as default', async function () {
216 let nsfwUUID: string
217
218 {
219 await updateCustomSubConfig(server.url, server.accessToken, { instance: { defaultNSFWPolicy: 'display' } })
220
af971e06
C
221 const body = await command.searchVideos({ search: 'NSFW search index', sort: '-match' })
222 expect(body.data).to.have.length.greaterThan(0)
1a40132c 223
af971e06 224 const video = body.data[0]
1a40132c
C
225 expect(video.nsfw).to.be.true
226
227 nsfwUUID = video.uuid
228 }
229
230 {
231 await updateCustomSubConfig(server.url, server.accessToken, { instance: { defaultNSFWPolicy: 'do_not_list' } })
232
af971e06 233 const body = await command.searchVideos({ search: 'NSFW search index', sort: '-match' })
1a40132c
C
234
235 try {
af971e06
C
236 expect(body.data).to.have.lengthOf(0)
237 } catch {
238 const video = body.data[0]
1a40132c
C
239
240 expect(video.uuid).not.equal(nsfwUUID)
241 }
242 }
243 })
3521ab8f
C
244 })
245
246 describe('Channels search', async function () {
247
248 it('Should make a simple search and not have results', async function () {
af971e06 249 const body = await command.searchChannels({ search: 'a'.repeat(500) })
3521ab8f 250
af971e06
C
251 expect(body.total).to.equal(0)
252 expect(body.data).to.have.lengthOf(0)
3521ab8f
C
253 })
254
255 it('Should make a search and have results', async function () {
af971e06 256 const body = await command.advancedChannelSearch({ search: { search: 'Framasoft', sort: 'createdAt' } })
3521ab8f 257
af971e06
C
258 expect(body.total).to.be.greaterThan(0)
259 expect(body.data).to.have.length.greaterThan(0)
3521ab8f 260
af971e06 261 const videoChannel = body.data[0]
3521ab8f
C
262 expect(videoChannel.url).to.equal('https://framatube.org/video-channels/bf54d359-cfad-4935-9d45-9d6be93f63e8')
263 expect(videoChannel.host).to.equal('framatube.org')
264 expect(videoChannel.avatar).to.exist
265 expect(videoChannel.displayName).to.exist
266
267 expect(videoChannel.ownerAccount.url).to.equal('https://framatube.org/accounts/framasoft')
268 expect(videoChannel.ownerAccount.name).to.equal('framasoft')
269 expect(videoChannel.ownerAccount.host).to.equal('framatube.org')
270 expect(videoChannel.ownerAccount.avatar).to.exist
271 })
272
273 it('Should have a correct pagination', async function () {
af971e06 274 const body = await command.advancedChannelSearch({ search: { search: 'root', start: 0, count: 2 } })
3521ab8f 275
af971e06
C
276 expect(body.total).to.be.greaterThan(2)
277 expect(body.data).to.have.lengthOf(2)
3521ab8f
C
278 })
279 })
280
37a44fc9
C
281 describe('Playlists search', async function () {
282
283 it('Should make a simple search and not have results', async function () {
af971e06 284 const body = await command.searchPlaylists({ search: 'a'.repeat(500) })
37a44fc9 285
af971e06
C
286 expect(body.total).to.equal(0)
287 expect(body.data).to.have.lengthOf(0)
37a44fc9
C
288 })
289
290 it('Should make a search and have results', async function () {
af971e06 291 const body = await command.advancedPlaylistSearch({ search: { search: 'E2E playlist', sort: '-match' } })
37a44fc9 292
af971e06
C
293 expect(body.total).to.be.greaterThan(0)
294 expect(body.data).to.have.length.greaterThan(0)
37a44fc9 295
af971e06 296 const videoPlaylist = body.data[0]
37a44fc9
C
297
298 expect(videoPlaylist.url).to.equal('https://peertube2.cpy.re/videos/watch/playlist/73804a40-da9a-40c2-b1eb-2c6d9eec8f0a')
299 expect(videoPlaylist.thumbnailUrl).to.exist
300 expect(videoPlaylist.embedUrl).to.equal('https://peertube2.cpy.re/video-playlists/embed/73804a40-da9a-40c2-b1eb-2c6d9eec8f0a')
301
302 expect(videoPlaylist.type.id).to.equal(VideoPlaylistType.REGULAR)
303 expect(videoPlaylist.privacy.id).to.equal(VideoPlaylistPrivacy.PUBLIC)
304 expect(videoPlaylist.videosLength).to.exist
305
306 expect(videoPlaylist.createdAt).to.exist
307 expect(videoPlaylist.updatedAt).to.exist
308
309 expect(videoPlaylist.uuid).to.equal('73804a40-da9a-40c2-b1eb-2c6d9eec8f0a')
310 expect(videoPlaylist.displayName).to.exist
311
312 expect(videoPlaylist.ownerAccount.url).to.equal('https://peertube2.cpy.re/accounts/chocobozzz')
313 expect(videoPlaylist.ownerAccount.name).to.equal('chocobozzz')
314 expect(videoPlaylist.ownerAccount.host).to.equal('peertube2.cpy.re')
315 expect(videoPlaylist.ownerAccount.avatar).to.exist
316
317 expect(videoPlaylist.videoChannel.url).to.equal('https://peertube2.cpy.re/video-channels/chocobozzz_channel')
318 expect(videoPlaylist.videoChannel.name).to.equal('chocobozzz_channel')
319 expect(videoPlaylist.videoChannel.host).to.equal('peertube2.cpy.re')
320 expect(videoPlaylist.videoChannel.avatar).to.exist
321 })
322
323 it('Should have a correct pagination', async function () {
af971e06 324 const body = await command.advancedChannelSearch({ search: { search: 'root', start: 0, count: 2 } })
37a44fc9 325
af971e06
C
326 expect(body.total).to.be.greaterThan(2)
327 expect(body.data).to.have.lengthOf(2)
37a44fc9
C
328 })
329 })
330
3521ab8f
C
331 after(async function () {
332 await cleanupTests([ server ])
333 })
334})