1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { wait } from '@shared/core-utils'
6 import { VideoPrivacy } from '@shared/models'
12 setAccessTokensToServers,
13 setDefaultAccountAvatar,
14 setDefaultVideoChannel,
16 } from '@shared/server-commands'
18 const expect = chai.expect
20 describe('Test ActivityPub videos search', function () {
21 let servers: PeerTubeServer[]
22 let videoServer1UUID: string
23 let videoServer2UUID: string
25 let command: SearchCommand
27 before(async function () {
30 servers = await createMultipleServers(2)
32 await setAccessTokensToServers(servers)
33 await setDefaultVideoChannel(servers)
34 await setDefaultAccountAvatar(servers)
37 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video 1 on server 1' } })
38 videoServer1UUID = uuid
42 const { uuid } = await servers[1].videos.upload({ attributes: { name: 'video 1 on server 2' } })
43 videoServer2UUID = uuid
46 await waitJobs(servers)
48 command = servers[0].search
51 it('Should not find a remote video', async function () {
53 const search = servers[1].url + '/videos/watch/43'
54 const body = await command.searchVideos({ search, token: servers[0].accessToken })
56 expect(body.total).to.equal(0)
57 expect(body.data).to.be.an('array')
58 expect(body.data).to.have.lengthOf(0)
63 const search = servers[1].url + '/videos/watch/' + videoServer2UUID
64 const body = await command.searchVideos({ search })
66 expect(body.total).to.equal(0)
67 expect(body.data).to.be.an('array')
68 expect(body.data).to.have.lengthOf(0)
72 it('Should search a local video', async function () {
73 const search = servers[0].url + '/videos/watch/' + videoServer1UUID
74 const body = await command.searchVideos({ search })
76 expect(body.total).to.equal(1)
77 expect(body.data).to.be.an('array')
78 expect(body.data).to.have.lengthOf(1)
79 expect(body.data[0].name).to.equal('video 1 on server 1')
82 it('Should search a local video with an alternative URL', async function () {
83 const search = servers[0].url + '/w/' + videoServer1UUID
84 const body1 = await command.searchVideos({ search })
85 const body2 = await command.searchVideos({ search, token: servers[0].accessToken })
87 for (const body of [ body1, body2 ]) {
88 expect(body.total).to.equal(1)
89 expect(body.data).to.be.an('array')
90 expect(body.data).to.have.lengthOf(1)
91 expect(body.data[0].name).to.equal('video 1 on server 1')
95 it('Should search a local video with a query in URL', async function () {
97 servers[0].url + '/w/' + videoServer1UUID,
98 servers[0].url + '/videos/watch/' + videoServer1UUID
101 for (const search of searches) {
102 for (const token of [ undefined, servers[0].accessToken ]) {
103 const body = await command.searchVideos({ search: search + '?startTime=4', token })
105 expect(body.total).to.equal(1)
106 expect(body.data).to.be.an('array')
107 expect(body.data).to.have.lengthOf(1)
108 expect(body.data[0].name).to.equal('video 1 on server 1')
113 it('Should search a remote video', async function () {
115 servers[1].url + '/w/' + videoServer2UUID,
116 servers[1].url + '/videos/watch/' + videoServer2UUID
119 for (const search of searches) {
120 const body = await command.searchVideos({ search, token: servers[0].accessToken })
122 expect(body.total).to.equal(1)
123 expect(body.data).to.be.an('array')
124 expect(body.data).to.have.lengthOf(1)
125 expect(body.data[0].name).to.equal('video 1 on server 2')
129 it('Should not list this remote video', async function () {
130 const { total, data } = await servers[0].videos.list()
131 expect(total).to.equal(1)
132 expect(data).to.have.lengthOf(1)
133 expect(data[0].name).to.equal('video 1 on server 1')
136 it('Should update video of server 2, and refresh it on server 1', async function () {
139 const channelAttributes = {
140 name: 'super_channel',
141 displayName: 'super channel'
143 const created = await servers[1].channels.create({ attributes: channelAttributes })
144 const videoChannelId = created.id
148 tag: [ 'tag1', 'tag2' ],
149 privacy: VideoPrivacy.UNLISTED,
150 channelId: videoChannelId
152 await servers[1].videos.update({ id: videoServer2UUID, attributes })
154 await waitJobs(servers)
158 // Will run refresh async
159 const search = servers[1].url + '/videos/watch/' + videoServer2UUID
160 await command.searchVideos({ search, token: servers[0].accessToken })
165 const body = await command.searchVideos({ search, token: servers[0].accessToken })
166 expect(body.total).to.equal(1)
167 expect(body.data).to.have.lengthOf(1)
169 const video = body.data[0]
170 expect(video.name).to.equal('updated')
171 expect(video.channel.name).to.equal('super_channel')
172 expect(video.privacy.id).to.equal(VideoPrivacy.UNLISTED)
175 it('Should delete video of server 2, and delete it on server 1', async function () {
178 await servers[1].videos.remove({ id: videoServer2UUID })
180 await waitJobs(servers)
184 // Will run refresh async
185 const search = servers[1].url + '/videos/watch/' + videoServer2UUID
186 await command.searchVideos({ search, token: servers[0].accessToken })
191 const body = await command.searchVideos({ search, token: servers[0].accessToken })
192 expect(body.total).to.equal(0)
193 expect(body.data).to.have.lengthOf(0)
196 after(async function () {
197 await cleanupTests(servers)