2014-03-26 08:17:59 +00:00
|
|
|
var Client = require('../').Client
|
|
|
|
var fs = require('fs')
|
|
|
|
var parseTorrent = require('parse-torrent')
|
|
|
|
var test = require('tape')
|
|
|
|
|
|
|
|
var torrent = fs.readFileSync(__dirname + '/torrents/bitlove-intro.torrent')
|
|
|
|
var parsedTorrent = parseTorrent(torrent)
|
|
|
|
var peerId = new Buffer('01234567890123456789')
|
2014-05-09 06:38:41 +00:00
|
|
|
var announceUrl = 'http://t.bitlove.org/announce' // TODO: shouldn't rely on an external server!
|
2014-03-26 08:17:59 +00:00
|
|
|
var port = 6881
|
|
|
|
|
|
|
|
test('client.start()', function (t) {
|
|
|
|
t.plan(4)
|
|
|
|
|
|
|
|
var client = new Client(peerId, port, parsedTorrent)
|
|
|
|
|
|
|
|
client.on('error', function (err) {
|
2014-03-28 04:29:00 +00:00
|
|
|
t.error(err)
|
2014-03-26 08:17:59 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
client.once('update', function (data) {
|
2014-05-09 06:38:41 +00:00
|
|
|
t.equal(data.announce, announceUrl)
|
2014-03-26 08:17:59 +00:00
|
|
|
t.equal(typeof data.complete, 'number')
|
|
|
|
t.equal(typeof data.incomplete, 'number')
|
|
|
|
})
|
|
|
|
|
|
|
|
client.once('peer', function (addr) {
|
2014-05-09 06:38:41 +00:00
|
|
|
t.pass('there is at least one peer')
|
2014-03-26 08:17:59 +00:00
|
|
|
client.stop()
|
|
|
|
})
|
|
|
|
|
|
|
|
client.start()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('client.stop()', function (t) {
|
2014-03-27 07:17:49 +00:00
|
|
|
t.plan(4)
|
2014-03-26 08:17:59 +00:00
|
|
|
|
|
|
|
var client = new Client(peerId, port, parsedTorrent)
|
|
|
|
|
|
|
|
client.on('error', function (err) {
|
2014-03-28 04:29:00 +00:00
|
|
|
t.error(err)
|
2014-03-26 08:17:59 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
client.start()
|
|
|
|
|
|
|
|
setTimeout(function () {
|
|
|
|
client.stop()
|
|
|
|
|
|
|
|
client.once('update', function (data) {
|
|
|
|
// receive one final update after calling stop
|
2014-05-09 06:38:41 +00:00
|
|
|
t.equal(data.announce, announceUrl)
|
2014-03-26 08:17:59 +00:00
|
|
|
t.equal(typeof data.complete, 'number')
|
|
|
|
t.equal(typeof data.incomplete, 'number')
|
|
|
|
})
|
2014-03-27 07:17:49 +00:00
|
|
|
|
|
|
|
client.once('peer', function () {
|
|
|
|
t.pass('should get more peers on stop()')
|
|
|
|
})
|
2014-03-26 08:17:59 +00:00
|
|
|
}, 1000)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('client.update()', function (t) {
|
|
|
|
t.plan(3)
|
|
|
|
|
|
|
|
var client = new Client(peerId, port, parsedTorrent, { interval: 5000 })
|
|
|
|
|
|
|
|
client.on('error', function (err) {
|
2014-03-28 04:29:00 +00:00
|
|
|
t.error(err)
|
2014-03-26 08:17:59 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
client.start()
|
|
|
|
|
|
|
|
client.once('update', function () {
|
|
|
|
|
|
|
|
client.once('update', function (data) {
|
|
|
|
// received an update!
|
2014-05-09 06:38:41 +00:00
|
|
|
t.equal(data.announce, announceUrl)
|
2014-03-26 08:17:59 +00:00
|
|
|
t.equal(typeof data.complete, 'number')
|
|
|
|
t.equal(typeof data.incomplete, 'number')
|
|
|
|
client.stop()
|
|
|
|
})
|
|
|
|
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-05-09 06:38:41 +00:00
|
|
|
test('client.scrape()', function (t) {
|
|
|
|
t.plan(4)
|
|
|
|
|
|
|
|
var client = new Client(peerId, port, parsedTorrent)
|
|
|
|
|
|
|
|
client.on('error', function (err) {
|
|
|
|
t.error(err)
|
|
|
|
})
|
|
|
|
|
|
|
|
client.once('scrape', function (data) {
|
|
|
|
t.equal(data.announce, announceUrl)
|
|
|
|
t.equal(typeof data.complete, 'number')
|
|
|
|
t.equal(typeof data.incomplete, 'number')
|
|
|
|
t.equal(typeof data.downloaded, 'number')
|
|
|
|
})
|
2014-05-12 00:32:57 +00:00
|
|
|
|
2014-05-09 06:38:41 +00:00
|
|
|
client.scrape()
|
|
|
|
})
|
|
|
|
|
2014-03-26 08:17:59 +00:00
|
|
|
// TODO: add test where tracker doesn't support compact
|