1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
10 setAccessTokensToServers,
11 updateCustomSubConfig,
15 } from '../../../../shared/extra-utils'
16 import { searchVideoChannel, advancedVideoChannelSearch } from '@shared/extra-utils/search/video-channels'
17 import { VideosSearchQuery, Video, VideoChannel } from '@shared/models'
19 const expect = chai.expect
21 describe('Test videos search', function () {
22 let server: ServerInfo = null
23 const localVideoName = 'local video' + new Date().toISOString()
25 before(async function () {
28 server = await flushAndRunServer(1)
30 await setAccessTokensToServers([ server ])
32 await uploadVideo(server.url, server.accessToken, { name: localVideoName })
35 describe('Default search', async function () {
37 it('Should make a local videos search by default', async function () {
40 await updateCustomSubConfig(server.url, server.accessToken, {
44 isDefaultSearch: false,
45 disableLocalSearch: false
50 const res = await searchVideo(server.url, 'local video')
52 expect(res.body.total).to.equal(1)
53 expect(res.body.data[0].name).to.equal(localVideoName)
56 it('Should make a local channels search by default', async function () {
57 const res = await searchVideoChannel(server.url, 'root')
59 expect(res.body.total).to.equal(1)
60 expect(res.body.data[0].name).to.equal('root_channel')
61 expect(res.body.data[0].host).to.equal('localhost:' + server.port)
64 it('Should make an index videos search by default', async function () {
65 await updateCustomSubConfig(server.url, server.accessToken, {
69 isDefaultSearch: true,
70 disableLocalSearch: false
75 const res = await searchVideo(server.url, 'local video')
76 expect(res.body.total).to.be.greaterThan(2)
79 it('Should make an index channels search by default', async function () {
80 const res = await searchVideoChannel(server.url, 'root')
81 expect(res.body.total).to.be.greaterThan(2)
84 it('Should make an index videos search if local search is disabled', async function () {
85 await updateCustomSubConfig(server.url, server.accessToken, {
89 isDefaultSearch: false,
90 disableLocalSearch: true
95 const res = await searchVideo(server.url, 'local video')
96 expect(res.body.total).to.be.greaterThan(2)
99 it('Should make an index channels search if local search is disabled', async function () {
100 const res = await searchVideoChannel(server.url, 'root')
101 expect(res.body.total).to.be.greaterThan(2)
105 describe('Videos search', async function () {
107 it('Should make a simple search and not have results', async function () {
108 const res = await searchVideo(server.url, 'a'.repeat(500))
110 expect(res.body.total).to.equal(0)
111 expect(res.body.data).to.have.lengthOf(0)
114 it('Should make a simple search and have results', async function () {
115 const res = await searchVideo(server.url, 'What is PeerTube')
117 expect(res.body.total).to.be.greaterThan(1)
120 it('Should make a complex search', async function () {
122 async function check (search: VideosSearchQuery, exists = true) {
123 const res = await advancedVideosSearch(server.url, search)
125 if (exists === false) {
126 expect(res.body.total).to.equal(0)
127 expect(res.body.data).to.have.lengthOf(0)
131 expect(res.body.total).to.equal(1)
132 expect(res.body.data).to.have.lengthOf(1)
134 const video: Video = res.body.data[0]
136 expect(video.name).to.equal('What is PeerTube?')
137 expect(video.category.label).to.equal('Science & Technology')
138 expect(video.licence.label).to.equal('Attribution - Share Alike')
139 expect(video.privacy.label).to.equal('Public')
140 expect(video.duration).to.equal(113)
141 expect(video.thumbnailUrl.startsWith('https://framatube.org/static/thumbnails')).to.be.true
143 expect(video.account.host).to.equal('framatube.org')
144 expect(video.account.name).to.equal('framasoft')
145 expect(video.account.url).to.equal('https://framatube.org/accounts/framasoft')
146 expect(video.account.avatar).to.exist
148 expect(video.channel.host).to.equal('framatube.org')
149 expect(video.channel.name).to.equal('bf54d359-cfad-4935-9d45-9d6be93f63e8')
150 expect(video.channel.url).to.equal('https://framatube.org/video-channels/bf54d359-cfad-4935-9d45-9d6be93f63e8')
151 expect(video.channel.avatar).to.exist
154 const baseSearch: VideosSearchQuery = {
155 search: 'what is peertube',
158 categoryOneOf: [ 15 ],
160 tagsAllOf: [ 'framasoft', 'peertube' ],
161 startDate: '2018-10-01T10:50:46.396Z',
162 endDate: '2018-10-01T10:55:46.396Z'
166 await check(baseSearch)
170 const search = immutableAssign(baseSearch, { startDate: '2018-10-01T10:54:46.396Z' })
171 await check(search, false)
175 const search = immutableAssign(baseSearch, { tagsAllOf: [ 'toto', 'framasoft' ] })
176 await check(search, false)
180 const search = immutableAssign(baseSearch, { durationMin: 2000 })
181 await check(search, false)
185 const search = immutableAssign(baseSearch, { nsfw: 'true' })
186 await check(search, false)
190 const search = immutableAssign(baseSearch, { nsfw: 'false' })
191 await check(search, true)
195 const search = immutableAssign(baseSearch, { nsfw: 'both' })
196 await check(search, true)
200 it('Should have a correct pagination', async function () {
207 const res = await advancedVideosSearch(server.url, search)
209 expect(res.body.total).to.be.greaterThan(5)
210 expect(res.body.data).to.have.lengthOf(5)
213 it('Should use the nsfw instance policy as default', async function () {
217 await updateCustomSubConfig(server.url, server.accessToken, { instance: { defaultNSFWPolicy: 'display' } })
219 const res = await searchVideo(server.url, 'NSFW search index')
220 const video = res.body.data[0] as Video
222 expect(res.body.data).to.have.length.greaterThan(0)
223 expect(video.nsfw).to.be.true
225 nsfwUUID = video.uuid
229 await updateCustomSubConfig(server.url, server.accessToken, { instance: { defaultNSFWPolicy: 'do_not_list' } })
231 const res = await searchVideo(server.url, 'NSFW search index')
234 expect(res.body.data).to.have.lengthOf(0)
237 const video = res.body.data[0] as Video
239 expect(video.uuid).not.equal(nsfwUUID)
245 describe('Channels search', async function () {
247 it('Should make a simple search and not have results', async function () {
248 const res = await searchVideoChannel(server.url, 'a'.repeat(500))
250 expect(res.body.total).to.equal(0)
251 expect(res.body.data).to.have.lengthOf(0)
254 it('Should make a search and have results', async function () {
255 const res = await advancedVideoChannelSearch(server.url, { search: 'Framasoft', sort: 'createdAt' })
257 expect(res.body.total).to.be.greaterThan(0)
258 expect(res.body.data).to.have.length.greaterThan(0)
260 const videoChannel: VideoChannel = res.body.data[0]
261 expect(videoChannel.url).to.equal('https://framatube.org/video-channels/bf54d359-cfad-4935-9d45-9d6be93f63e8')
262 expect(videoChannel.host).to.equal('framatube.org')
263 expect(videoChannel.avatar).to.exist
264 expect(videoChannel.displayName).to.exist
266 expect(videoChannel.ownerAccount.url).to.equal('https://framatube.org/accounts/framasoft')
267 expect(videoChannel.ownerAccount.name).to.equal('framasoft')
268 expect(videoChannel.ownerAccount.host).to.equal('framatube.org')
269 expect(videoChannel.ownerAccount.avatar).to.exist
272 it('Should have a correct pagination', async function () {
273 const res = await advancedVideoChannelSearch(server.url, { search: 'root', start: 0, count: 2 })
275 expect(res.body.total).to.be.greaterThan(2)
276 expect(res.body.data).to.have.lengthOf(2)
280 after(async function () {
281 await cleanupTests([ server ])