]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/search/search-videos.ts
Adapt CLI to new commands
[github/Chocobozzz/PeerTube.git] / server / tests / api / search / search-videos.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import * as chai from 'chai'
5 import {
6 cleanupTests,
7 flushAndRunServer,
8 SearchCommand,
9 ServerInfo,
10 setAccessTokensToServers,
11 setDefaultVideoChannel,
12 stopFfmpeg,
13 uploadVideo,
14 wait
15 } from '@shared/extra-utils'
16 import { VideoPrivacy } from '@shared/models'
17
18 const expect = chai.expect
19
20 describe('Test videos search', function () {
21 let server: ServerInfo = null
22 let startDate: string
23 let videoUUID: string
24
25 let command: SearchCommand
26
27 before(async function () {
28 this.timeout(60000)
29
30 server = await flushAndRunServer(1)
31
32 await setAccessTokensToServers([ server ])
33 await setDefaultVideoChannel([ server ])
34
35 {
36 const attributes1 = {
37 name: '1111 2222 3333',
38 fixture: '60fps_720p_small.mp4', // 2 seconds
39 category: 1,
40 licence: 1,
41 nsfw: false,
42 language: 'fr'
43 }
44 await uploadVideo(server.url, server.accessToken, attributes1)
45
46 const attributes2 = { ...attributes1, name: attributes1.name + ' - 2', fixture: 'video_short.mp4' }
47 await uploadVideo(server.url, server.accessToken, attributes2)
48
49 {
50 const attributes3 = { ...attributes1, name: attributes1.name + ' - 3', language: undefined }
51 const res = await uploadVideo(server.url, server.accessToken, attributes3)
52 const videoId = res.body.video.id
53 videoUUID = res.body.video.uuid
54
55 await server.captionsCommand.createVideoCaption({
56 language: 'en',
57 videoId,
58 fixture: 'subtitle-good2.vtt',
59 mimeType: 'application/octet-stream'
60 })
61
62 await server.captionsCommand.createVideoCaption({
63 language: 'aa',
64 videoId,
65 fixture: 'subtitle-good2.vtt',
66 mimeType: 'application/octet-stream'
67 })
68 }
69
70 const attributes4 = { ...attributes1, name: attributes1.name + ' - 4', language: 'pl', nsfw: true }
71 await uploadVideo(server.url, server.accessToken, attributes4)
72
73 await wait(1000)
74
75 startDate = new Date().toISOString()
76
77 const attributes5 = { ...attributes1, name: attributes1.name + ' - 5', licence: 2, language: undefined }
78 await uploadVideo(server.url, server.accessToken, attributes5)
79
80 const attributes6 = { ...attributes1, name: attributes1.name + ' - 6', tags: [ 't1', 't2' ] }
81 await uploadVideo(server.url, server.accessToken, attributes6)
82
83 const attributes7 = { ...attributes1, name: attributes1.name + ' - 7', originallyPublishedAt: '2019-02-12T09:58:08.286Z' }
84 await uploadVideo(server.url, server.accessToken, attributes7)
85
86 const attributes8 = { ...attributes1, name: attributes1.name + ' - 8', licence: 4 }
87 await uploadVideo(server.url, server.accessToken, attributes8)
88 }
89
90 {
91 const attributes = {
92 name: '3333 4444 5555',
93 fixture: 'video_short.mp4',
94 category: 2,
95 licence: 2,
96 language: 'en'
97 }
98 await uploadVideo(server.url, server.accessToken, attributes)
99
100 await uploadVideo(server.url, server.accessToken, { ...attributes, name: attributes.name + ' duplicate' })
101 }
102
103 {
104 const attributes = {
105 name: '6666 7777 8888',
106 fixture: 'video_short.mp4',
107 category: 3,
108 licence: 3,
109 language: 'pl'
110 }
111 await uploadVideo(server.url, server.accessToken, attributes)
112 }
113
114 {
115 const attributes1 = {
116 name: '9999',
117 tags: [ 'aaaa', 'bbbb', 'cccc' ],
118 category: 1
119 }
120 await uploadVideo(server.url, server.accessToken, attributes1)
121 await uploadVideo(server.url, server.accessToken, { ...attributes1, category: 2 })
122
123 await uploadVideo(server.url, server.accessToken, { ...attributes1, tags: [ 'cccc', 'dddd' ] })
124 await uploadVideo(server.url, server.accessToken, { ...attributes1, tags: [ 'eeee', 'ffff' ] })
125 }
126
127 {
128 const attributes1 = {
129 name: 'aaaa 2',
130 category: 1
131 }
132 await uploadVideo(server.url, server.accessToken, attributes1)
133 await uploadVideo(server.url, server.accessToken, { ...attributes1, category: 2 })
134 }
135
136 command = server.searchCommand
137 })
138
139 it('Should make a simple search and not have results', async function () {
140 const body = await command.searchVideos({ search: 'abc' })
141
142 expect(body.total).to.equal(0)
143 expect(body.data).to.have.lengthOf(0)
144 })
145
146 it('Should make a simple search and have results', async function () {
147 const body = await command.searchVideos({ search: '4444 5555 duplicate' })
148
149 expect(body.total).to.equal(2)
150
151 const videos = body.data
152 expect(videos).to.have.lengthOf(2)
153
154 // bestmatch
155 expect(videos[0].name).to.equal('3333 4444 5555 duplicate')
156 expect(videos[1].name).to.equal('3333 4444 5555')
157 })
158
159 it('Should make a search on tags too, and have results', async function () {
160 const search = {
161 search: 'aaaa',
162 categoryOneOf: [ 1 ]
163 }
164 const body = await command.advancedVideoSearch({ search })
165
166 expect(body.total).to.equal(2)
167
168 const videos = body.data
169 expect(videos).to.have.lengthOf(2)
170
171 // bestmatch
172 expect(videos[0].name).to.equal('aaaa 2')
173 expect(videos[1].name).to.equal('9999')
174 })
175
176 it('Should filter on tags without a search', async function () {
177 const search = {
178 tagsAllOf: [ 'bbbb' ]
179 }
180 const body = await command.advancedVideoSearch({ search })
181
182 expect(body.total).to.equal(2)
183
184 const videos = body.data
185 expect(videos).to.have.lengthOf(2)
186
187 expect(videos[0].name).to.equal('9999')
188 expect(videos[1].name).to.equal('9999')
189 })
190
191 it('Should filter on category without a search', async function () {
192 const search = {
193 categoryOneOf: [ 3 ]
194 }
195 const body = await command.advancedVideoSearch({ search: search })
196
197 expect(body.total).to.equal(1)
198
199 const videos = body.data
200 expect(videos).to.have.lengthOf(1)
201
202 expect(videos[0].name).to.equal('6666 7777 8888')
203 })
204
205 it('Should search by tags (one of)', async function () {
206 const query = {
207 search: '9999',
208 categoryOneOf: [ 1 ],
209 tagsOneOf: [ 'aAaa', 'ffff' ]
210 }
211
212 {
213 const body = await command.advancedVideoSearch({ search: query })
214 expect(body.total).to.equal(2)
215 }
216
217 {
218 const body = await command.advancedVideoSearch({ search: { ...query, tagsOneOf: [ 'blabla' ] } })
219 expect(body.total).to.equal(0)
220 }
221 })
222
223 it('Should search by tags (all of)', async function () {
224 const query = {
225 search: '9999',
226 categoryOneOf: [ 1 ],
227 tagsAllOf: [ 'CCcc' ]
228 }
229
230 {
231 const body = await command.advancedVideoSearch({ search: query })
232 expect(body.total).to.equal(2)
233 }
234
235 {
236 const body = await command.advancedVideoSearch({ search: { ...query, tagsAllOf: [ 'blAbla' ] } })
237 expect(body.total).to.equal(0)
238 }
239
240 {
241 const body = await command.advancedVideoSearch({ search: { ...query, tagsAllOf: [ 'bbbb', 'CCCC' ] } })
242 expect(body.total).to.equal(1)
243 }
244 })
245
246 it('Should search by category', async function () {
247 const query = {
248 search: '6666',
249 categoryOneOf: [ 3 ]
250 }
251
252 {
253 const body = await command.advancedVideoSearch({ search: query })
254 expect(body.total).to.equal(1)
255 expect(body.data[0].name).to.equal('6666 7777 8888')
256 }
257
258 {
259 const body = await command.advancedVideoSearch({ search: { ...query, categoryOneOf: [ 2 ] } })
260 expect(body.total).to.equal(0)
261 }
262 })
263
264 it('Should search by licence', async function () {
265 const query = {
266 search: '4444 5555',
267 licenceOneOf: [ 2 ]
268 }
269
270 {
271 const body = await command.advancedVideoSearch({ search: query })
272 expect(body.total).to.equal(2)
273 expect(body.data[0].name).to.equal('3333 4444 5555')
274 expect(body.data[1].name).to.equal('3333 4444 5555 duplicate')
275 }
276
277 {
278 const body = await command.advancedVideoSearch({ search: { ...query, licenceOneOf: [ 3 ] } })
279 expect(body.total).to.equal(0)
280 }
281 })
282
283 it('Should search by languages', async function () {
284 const query = {
285 search: '1111 2222 3333',
286 languageOneOf: [ 'pl', 'en' ]
287 }
288
289 {
290 const body = await command.advancedVideoSearch({ search: query })
291 expect(body.total).to.equal(2)
292 expect(body.data[0].name).to.equal('1111 2222 3333 - 3')
293 expect(body.data[1].name).to.equal('1111 2222 3333 - 4')
294 }
295
296 {
297 const body = await command.advancedVideoSearch({ search: { ...query, languageOneOf: [ 'pl', 'en', '_unknown' ] } })
298 expect(body.total).to.equal(3)
299 expect(body.data[0].name).to.equal('1111 2222 3333 - 3')
300 expect(body.data[1].name).to.equal('1111 2222 3333 - 4')
301 expect(body.data[2].name).to.equal('1111 2222 3333 - 5')
302 }
303
304 {
305 const body = await command.advancedVideoSearch({ search: { ...query, languageOneOf: [ 'eo' ] } })
306 expect(body.total).to.equal(0)
307 }
308 })
309
310 it('Should search by start date', async function () {
311 const query = {
312 search: '1111 2222 3333',
313 startDate
314 }
315
316 const body = await command.advancedVideoSearch({ search: query })
317 expect(body.total).to.equal(4)
318
319 const videos = body.data
320 expect(videos[0].name).to.equal('1111 2222 3333 - 5')
321 expect(videos[1].name).to.equal('1111 2222 3333 - 6')
322 expect(videos[2].name).to.equal('1111 2222 3333 - 7')
323 expect(videos[3].name).to.equal('1111 2222 3333 - 8')
324 })
325
326 it('Should make an advanced search', async function () {
327 const query = {
328 search: '1111 2222 3333',
329 languageOneOf: [ 'pl', 'fr' ],
330 durationMax: 4,
331 nsfw: 'false' as 'false',
332 licenceOneOf: [ 1, 4 ]
333 }
334
335 const body = await command.advancedVideoSearch({ search: query })
336 expect(body.total).to.equal(4)
337
338 const videos = body.data
339 expect(videos[0].name).to.equal('1111 2222 3333')
340 expect(videos[1].name).to.equal('1111 2222 3333 - 6')
341 expect(videos[2].name).to.equal('1111 2222 3333 - 7')
342 expect(videos[3].name).to.equal('1111 2222 3333 - 8')
343 })
344
345 it('Should make an advanced search and sort results', async function () {
346 const query = {
347 search: '1111 2222 3333',
348 languageOneOf: [ 'pl', 'fr' ],
349 durationMax: 4,
350 nsfw: 'false' as 'false',
351 licenceOneOf: [ 1, 4 ],
352 sort: '-name'
353 }
354
355 const body = await command.advancedVideoSearch({ search: query })
356 expect(body.total).to.equal(4)
357
358 const videos = body.data
359 expect(videos[0].name).to.equal('1111 2222 3333 - 8')
360 expect(videos[1].name).to.equal('1111 2222 3333 - 7')
361 expect(videos[2].name).to.equal('1111 2222 3333 - 6')
362 expect(videos[3].name).to.equal('1111 2222 3333')
363 })
364
365 it('Should make an advanced search and only show the first result', async function () {
366 const query = {
367 search: '1111 2222 3333',
368 languageOneOf: [ 'pl', 'fr' ],
369 durationMax: 4,
370 nsfw: 'false' as 'false',
371 licenceOneOf: [ 1, 4 ],
372 sort: '-name',
373 start: 0,
374 count: 1
375 }
376
377 const body = await command.advancedVideoSearch({ search: query })
378 expect(body.total).to.equal(4)
379
380 const videos = body.data
381 expect(videos[0].name).to.equal('1111 2222 3333 - 8')
382 })
383
384 it('Should make an advanced search and only show the last result', async function () {
385 const query = {
386 search: '1111 2222 3333',
387 languageOneOf: [ 'pl', 'fr' ],
388 durationMax: 4,
389 nsfw: 'false' as 'false',
390 licenceOneOf: [ 1, 4 ],
391 sort: '-name',
392 start: 3,
393 count: 1
394 }
395
396 const body = await command.advancedVideoSearch({ search: query })
397 expect(body.total).to.equal(4)
398
399 const videos = body.data
400 expect(videos[0].name).to.equal('1111 2222 3333')
401 })
402
403 it('Should search on originally published date', async function () {
404 const baseQuery = {
405 search: '1111 2222 3333',
406 languageOneOf: [ 'pl', 'fr' ],
407 durationMax: 4,
408 nsfw: 'false' as 'false',
409 licenceOneOf: [ 1, 4 ]
410 }
411
412 {
413 const query = { ...baseQuery, originallyPublishedStartDate: '2019-02-11T09:58:08.286Z' }
414 const body = await command.advancedVideoSearch({ search: query })
415
416 expect(body.total).to.equal(1)
417 expect(body.data[0].name).to.equal('1111 2222 3333 - 7')
418 }
419
420 {
421 const query = { ...baseQuery, originallyPublishedEndDate: '2019-03-11T09:58:08.286Z' }
422 const body = await command.advancedVideoSearch({ search: query })
423
424 expect(body.total).to.equal(1)
425 expect(body.data[0].name).to.equal('1111 2222 3333 - 7')
426 }
427
428 {
429 const query = { ...baseQuery, originallyPublishedEndDate: '2019-01-11T09:58:08.286Z' }
430 const body = await command.advancedVideoSearch({ search: query })
431
432 expect(body.total).to.equal(0)
433 }
434
435 {
436 const query = { ...baseQuery, originallyPublishedStartDate: '2019-03-11T09:58:08.286Z' }
437 const body = await command.advancedVideoSearch({ search: query })
438
439 expect(body.total).to.equal(0)
440 }
441
442 {
443 const query = {
444 ...baseQuery,
445 originallyPublishedStartDate: '2019-01-11T09:58:08.286Z',
446 originallyPublishedEndDate: '2019-01-10T09:58:08.286Z'
447 }
448 const body = await command.advancedVideoSearch({ search: query })
449
450 expect(body.total).to.equal(0)
451 }
452
453 {
454 const query = {
455 ...baseQuery,
456 originallyPublishedStartDate: '2019-01-11T09:58:08.286Z',
457 originallyPublishedEndDate: '2019-04-11T09:58:08.286Z'
458 }
459 const body = await command.advancedVideoSearch({ search: query })
460
461 expect(body.total).to.equal(1)
462 expect(body.data[0].name).to.equal('1111 2222 3333 - 7')
463 }
464 })
465
466 it('Should search by UUID', async function () {
467 const search = videoUUID
468 const body = await command.advancedVideoSearch({ search: { search } })
469
470 expect(body.total).to.equal(1)
471 expect(body.data[0].name).to.equal('1111 2222 3333 - 3')
472 })
473
474 it('Should search by live', async function () {
475 this.timeout(30000)
476
477 {
478 const newConfig = {
479 search: {
480 searchIndex: { enabled: false }
481 },
482 live: { enabled: true }
483 }
484 await server.configCommand.updateCustomSubConfig({ newConfig })
485 }
486
487 {
488 const body = await command.advancedVideoSearch({ search: { isLive: true } })
489
490 expect(body.total).to.equal(0)
491 expect(body.data).to.have.lengthOf(0)
492 }
493
494 {
495 const liveCommand = server.liveCommand
496
497 const liveAttributes = { name: 'live', privacy: VideoPrivacy.PUBLIC, channelId: server.videoChannel.id }
498 const live = await liveCommand.create({ fields: liveAttributes })
499
500 const ffmpegCommand = await liveCommand.sendRTMPStreamInVideo({ videoId: live.id })
501 await liveCommand.waitUntilPublished({ videoId: live.id })
502
503 const body = await command.advancedVideoSearch({ search: { isLive: true } })
504
505 expect(body.total).to.equal(1)
506 expect(body.data[0].name).to.equal('live')
507
508 await stopFfmpeg(ffmpegCommand)
509 }
510 })
511
512 after(async function () {
513 await cleanupTests([ server ])
514 })
515 })