1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import * as chai from 'chai'
4 import { wait } from '@shared/core-utils'
5 import { VideoPrivacy } from '@shared/models'
11 setAccessTokensToServers,
12 setDefaultAccountAvatar,
13 setDefaultVideoChannel,
15 } from '@shared/server-commands'
17 const expect = chai.expect
19 describe('Test ActivityPub videos search', function () {
20 let servers: PeerTubeServer[]
21 let videoServer1UUID: string
22 let videoServer2UUID: string
24 let command: SearchCommand
26 before(async function () {
29 servers = await createMultipleServers(2)
31 await setAccessTokensToServers(servers)
32 await setDefaultVideoChannel(servers)
33 await setDefaultAccountAvatar(servers)
36 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video 1 on server 1' } })
37 videoServer1UUID = uuid
41 const { uuid } = await servers[1].videos.upload({ attributes: { name: 'video 1 on server 2' } })
42 videoServer2UUID = uuid
45 await waitJobs(servers)
47 command = servers[0].search
50 it('Should not find a remote video', async function () {
52 const search = servers[1].url + '/videos/watch/43'
53 const body = await command.searchVideos({ search, token: servers[0].accessToken })
55 expect(body.total).to.equal(0)
56 expect(body.data).to.be.an('array')
57 expect(body.data).to.have.lengthOf(0)
62 const search = servers[1].url + '/videos/watch/' + videoServer2UUID
63 const body = await command.searchVideos({ search })
65 expect(body.total).to.equal(0)
66 expect(body.data).to.be.an('array')
67 expect(body.data).to.have.lengthOf(0)
71 it('Should search a local video', async function () {
72 const search = servers[0].url + '/videos/watch/' + videoServer1UUID
73 const body = await command.searchVideos({ search })
75 expect(body.total).to.equal(1)
76 expect(body.data).to.be.an('array')
77 expect(body.data).to.have.lengthOf(1)
78 expect(body.data[0].name).to.equal('video 1 on server 1')
81 it('Should search a local video with an alternative URL', async function () {
82 const search = servers[0].url + '/w/' + videoServer1UUID
83 const body1 = await command.searchVideos({ search })
84 const body2 = await command.searchVideos({ search, token: servers[0].accessToken })
86 for (const body of [ body1, body2 ]) {
87 expect(body.total).to.equal(1)
88 expect(body.data).to.be.an('array')
89 expect(body.data).to.have.lengthOf(1)
90 expect(body.data[0].name).to.equal('video 1 on server 1')
94 it('Should search a local video with a query in URL', async function () {
96 servers[0].url + '/w/' + videoServer1UUID,
97 servers[0].url + '/videos/watch/' + videoServer1UUID
100 for (const search of searches) {
101 for (const token of [ undefined, servers[0].accessToken ]) {
102 const body = await command.searchVideos({ search: search + '?startTime=4', token })
104 expect(body.total).to.equal(1)
105 expect(body.data).to.be.an('array')
106 expect(body.data).to.have.lengthOf(1)
107 expect(body.data[0].name).to.equal('video 1 on server 1')
112 it('Should search a remote video', async function () {
114 servers[1].url + '/w/' + videoServer2UUID,
115 servers[1].url + '/videos/watch/' + videoServer2UUID
118 for (const search of searches) {
119 const body = await command.searchVideos({ search, token: servers[0].accessToken })
121 expect(body.total).to.equal(1)
122 expect(body.data).to.be.an('array')
123 expect(body.data).to.have.lengthOf(1)
124 expect(body.data[0].name).to.equal('video 1 on server 2')
128 it('Should not list this remote video', async function () {
129 const { total, data } = await servers[0].videos.list()
130 expect(total).to.equal(1)
131 expect(data).to.have.lengthOf(1)
132 expect(data[0].name).to.equal('video 1 on server 1')
135 it('Should update video of server 2, and refresh it on server 1', async function () {
138 const channelAttributes = {
139 name: 'super_channel',
140 displayName: 'super channel'
142 const created = await servers[1].channels.create({ attributes: channelAttributes })
143 const videoChannelId = created.id
147 tag: [ 'tag1', 'tag2' ],
148 privacy: VideoPrivacy.UNLISTED,
149 channelId: videoChannelId
151 await servers[1].videos.update({ id: videoServer2UUID, attributes })
153 await waitJobs(servers)
157 // Will run refresh async
158 const search = servers[1].url + '/videos/watch/' + videoServer2UUID
159 await command.searchVideos({ search, token: servers[0].accessToken })
164 const body = await command.searchVideos({ search, token: servers[0].accessToken })
165 expect(body.total).to.equal(1)
166 expect(body.data).to.have.lengthOf(1)
168 const video = body.data[0]
169 expect(video.name).to.equal('updated')
170 expect(video.channel.name).to.equal('super_channel')
171 expect(video.privacy.id).to.equal(VideoPrivacy.UNLISTED)
174 it('Should delete video of server 2, and delete it on server 1', async function () {
177 await servers[1].videos.remove({ id: videoServer2UUID })
179 await waitJobs(servers)
183 // Will run refresh async
184 const search = servers[1].url + '/videos/watch/' + videoServer2UUID
185 await command.searchVideos({ search, token: servers[0].accessToken })
190 const body = await command.searchVideos({ search, token: servers[0].accessToken })
191 expect(body.total).to.equal(0)
192 expect(body.data).to.have.lengthOf(0)
195 after(async function () {
196 await cleanupTests(servers)