1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } 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 describe('Test ActivityPub videos search', function () {
18 let servers: PeerTubeServer[]
19 let videoServer1UUID: string
20 let videoServer2UUID: string
22 let command: SearchCommand
24 before(async function () {
27 servers = await createMultipleServers(2)
29 await setAccessTokensToServers(servers)
30 await setDefaultVideoChannel(servers)
31 await setDefaultAccountAvatar(servers)
34 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video 1 on server 1' } })
35 videoServer1UUID = uuid
39 const { uuid } = await servers[1].videos.upload({ attributes: { name: 'video 1 on server 2' } })
40 videoServer2UUID = uuid
43 await waitJobs(servers)
45 command = servers[0].search
48 it('Should not find a remote video', async function () {
50 const search = servers[1].url + '/videos/watch/43'
51 const body = await command.searchVideos({ search, token: servers[0].accessToken })
53 expect(body.total).to.equal(0)
54 expect(body.data).to.be.an('array')
55 expect(body.data).to.have.lengthOf(0)
60 const search = servers[1].url + '/videos/watch/' + videoServer2UUID
61 const body = await command.searchVideos({ search })
63 expect(body.total).to.equal(0)
64 expect(body.data).to.be.an('array')
65 expect(body.data).to.have.lengthOf(0)
69 it('Should search a local video', async function () {
70 const search = servers[0].url + '/videos/watch/' + videoServer1UUID
71 const body = await command.searchVideos({ search })
73 expect(body.total).to.equal(1)
74 expect(body.data).to.be.an('array')
75 expect(body.data).to.have.lengthOf(1)
76 expect(body.data[0].name).to.equal('video 1 on server 1')
79 it('Should search a local video with an alternative URL', async function () {
80 const search = servers[0].url + '/w/' + videoServer1UUID
81 const body1 = await command.searchVideos({ search })
82 const body2 = await command.searchVideos({ search, token: servers[0].accessToken })
84 for (const body of [ body1, body2 ]) {
85 expect(body.total).to.equal(1)
86 expect(body.data).to.be.an('array')
87 expect(body.data).to.have.lengthOf(1)
88 expect(body.data[0].name).to.equal('video 1 on server 1')
92 it('Should search a local video with a query in URL', async function () {
94 servers[0].url + '/w/' + videoServer1UUID,
95 servers[0].url + '/videos/watch/' + videoServer1UUID
98 for (const search of searches) {
99 for (const token of [ undefined, servers[0].accessToken ]) {
100 const body = await command.searchVideos({ search: search + '?startTime=4', token })
102 expect(body.total).to.equal(1)
103 expect(body.data).to.be.an('array')
104 expect(body.data).to.have.lengthOf(1)
105 expect(body.data[0].name).to.equal('video 1 on server 1')
110 it('Should search a remote video', async function () {
112 servers[1].url + '/w/' + videoServer2UUID,
113 servers[1].url + '/videos/watch/' + videoServer2UUID
116 for (const search of searches) {
117 const body = await command.searchVideos({ search, token: servers[0].accessToken })
119 expect(body.total).to.equal(1)
120 expect(body.data).to.be.an('array')
121 expect(body.data).to.have.lengthOf(1)
122 expect(body.data[0].name).to.equal('video 1 on server 2')
126 it('Should not list this remote video', async function () {
127 const { total, data } = await servers[0].videos.list()
128 expect(total).to.equal(1)
129 expect(data).to.have.lengthOf(1)
130 expect(data[0].name).to.equal('video 1 on server 1')
133 it('Should update video of server 2, and refresh it on server 1', async function () {
136 const channelAttributes = {
137 name: 'super_channel',
138 displayName: 'super channel'
140 const created = await servers[1].channels.create({ attributes: channelAttributes })
141 const videoChannelId = created.id
145 tag: [ 'tag1', 'tag2' ],
146 privacy: VideoPrivacy.UNLISTED,
147 channelId: videoChannelId
149 await servers[1].videos.update({ id: videoServer2UUID, attributes })
151 await waitJobs(servers)
155 // Will run refresh async
156 const search = servers[1].url + '/videos/watch/' + videoServer2UUID
157 await command.searchVideos({ search, token: servers[0].accessToken })
162 const body = await command.searchVideos({ search, token: servers[0].accessToken })
163 expect(body.total).to.equal(1)
164 expect(body.data).to.have.lengthOf(1)
166 const video = body.data[0]
167 expect(video.name).to.equal('updated')
168 expect(video.channel.name).to.equal('super_channel')
169 expect(video.privacy.id).to.equal(VideoPrivacy.UNLISTED)
172 it('Should delete video of server 2, and delete it on server 1', async function () {
175 await servers[1].videos.remove({ id: videoServer2UUID })
177 await waitJobs(servers)
181 // Will run refresh async
182 const search = servers[1].url + '/videos/watch/' + videoServer2UUID
183 await command.searchVideos({ search, token: servers[0].accessToken })
188 const body = await command.searchVideos({ search, token: servers[0].accessToken })
189 expect(body.total).to.equal(0)
190 expect(body.data).to.have.lengthOf(0)
193 after(async function () {
194 await cleanupTests(servers)