1 /* tslint:disable:no-unused-expression */
3 import * as magnetUtil from 'magnet-uri'
5 import { getVideo, killallServers, runServer, ServerInfo, uploadVideo } from '../../utils'
6 import { flushTests, setAccessTokensToServers } from '../../utils/index'
7 import { VideoDetails } from '../../../../shared/models/videos'
8 import * as WebTorrent from 'webtorrent'
10 describe('Test tracker', function () {
11 let server: ServerInfo
13 let goodMagnet: string
15 before(async function () {
19 server = await runServer(1)
20 await setAccessTokensToServers([ server ])
23 const res = await uploadVideo(server.url, server.accessToken, {})
24 const videoUUID = res.body.video.uuid
26 const resGet = await getVideo(server.url, videoUUID)
27 const video: VideoDetails = resGet.body
28 goodMagnet = video.files[0].magnetUri
30 const parsed = magnetUtil.decode(goodMagnet)
31 parsed.infoHash = '010597bb88b1968a5693a4fa8267c592ca65f2e9'
33 badMagnet = magnetUtil.encode(parsed)
37 it('Should return an error when adding an incorrect infohash', done => {
39 const webtorrent = new WebTorrent()
41 const torrent = webtorrent.add(badMagnet)
43 torrent.on('error', done)
44 torrent.on('warning', warn => {
45 const message = typeof warn === 'string' ? warn : warn.message
46 if (message.indexOf('Unknown infoHash ') !== -1) return done()
49 torrent.on('done', () => done(new Error('No error on infohash')))
52 it('Should succeed with the correct infohash', done => {
54 const webtorrent = new WebTorrent()
56 const torrent = webtorrent.add(goodMagnet)
58 torrent.on('error', done)
59 torrent.on('warning', warn => {
60 const message = typeof warn === 'string' ? warn : warn.message
61 if (message.indexOf('Unknown infoHash ') !== -1) return done(new Error('Error on infohash'))
64 torrent.on('done', done)
67 after(async function () {
68 killallServers([ server ])