1 /* tslint:disable:no-unused-expression */
3 import * as magnetUtil from 'magnet-uri'
4 import * as chai from 'chai'
6 import { getVideo, killallServers, runServer, ServerInfo, uploadVideo } from '../../utils'
7 import { flushTests, setAccessTokensToServers } from '../../utils/index'
8 import { VideoDetails } from '../../../../shared/models/videos'
9 import * as WebTorrent from 'webtorrent'
11 describe('Test tracker', function () {
12 let server: ServerInfo
14 let goodMagnet: string
16 before(async function () {
20 server = await runServer(1)
21 await setAccessTokensToServers([ server ])
24 const res = await uploadVideo(server.url, server.accessToken, {})
25 const videoUUID = res.body.video.uuid
27 const resGet = await getVideo(server.url, videoUUID)
28 const video: VideoDetails = resGet.body
29 goodMagnet = video.files[0].magnetUri
31 const parsed = magnetUtil.decode(goodMagnet)
32 parsed.infoHash = '010597bb88b1968a5693a4fa8267c592ca65f2e9'
34 badMagnet = magnetUtil.encode(parsed)
38 it('Should return an error when adding an incorrect infohash', done => {
40 const webtorrent = new WebTorrent()
42 const torrent = webtorrent.add(badMagnet)
44 torrent.on('error', done)
45 torrent.on('warning', warn => {
46 const message = typeof warn === 'string' ? warn : warn.message
47 if (message.indexOf('Unknown infoHash ') !== -1) return done()
50 torrent.on('done', () => done(new Error('No error on infohash')))
53 it('Should succeed with the correct infohash', done => {
55 const webtorrent = new WebTorrent()
57 const torrent = webtorrent.add(goodMagnet)
59 torrent.on('error', done)
60 torrent.on('warning', warn => {
61 const message = typeof warn === 'string' ? warn : warn.message
62 if (message.indexOf('Unknown infoHash ') !== -1) return done(new Error('Error on infohash'))
65 torrent.on('done', done)
68 after(async function () {
69 killallServers([ server ])