forked from phts/nodejs-torrent-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtracker-service.test.ts
More file actions
138 lines (119 loc) · 4.16 KB
/
Copy pathtracker-service.test.ts
File metadata and controls
138 lines (119 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
'use strict';
var expect = require('chai').expect;
var sinon = require('sinon');
var createRequestParams = require('./helpers/create-request-params');
describe('TrackerService', function () {
var TrackerService = require('../app/tracker-service').default,
MemoryTorrentStore = require('../app/memory-torrent-store').default,
Torrent = require('../app/torrent').default,
trackerService,
torrentStub,
memoryTorrentStoreStub,
params,
output;
beforeEach(function () {
torrentStub = sinon.createStubInstance(Torrent);
torrentStub.getPeers = function () {
return 'peers';
};
torrentStub.getComplete = function () {
return 'complete';
};
torrentStub.getIncomplete = function () {
return 'incomplete';
};
memoryTorrentStoreStub = sinon.createStubInstance(MemoryTorrentStore);
memoryTorrentStoreStub.getTorrent = function () {
return torrentStub;
};
params = createRequestParams();
trackerService = new TrackerService();
trackerService.torrentStore = memoryTorrentStoreStub;
});
describe('#announce', function () {
function returnsObject() {
it('returns object', function () {
expect(output).to.be.an('object');
});
describe('[peers]', function () {
it('contains a list of peers', function () {
expect(output.peers).to.equal('peers');
});
});
describe('[complete]', function () {
it('contains a list of seeders', function () {
expect(output.complete).to.equal('complete');
});
});
describe('[incomplete]', function () {
it('contains a list of leechers', function () {
expect(output.incomplete).to.equal('incomplete');
});
});
}
function behavesLikeEventNotSpecified() {
it('sets peer\'s data only once', function () {
expect(torrentStub.setPeer.callCount).to.equal(1);
});
['peerId', 'ip', 'port', 'left'].forEach(function (testParam) {
it('updates the peer\'s ' + testParam, function () {
var expObj = {};
expObj[testParam] = params[testParam];
expect(torrentStub.setPeer.getCall(0).args[0]).to.include(expObj);
});
});
it('saves updated torrent', function () {
expect(memoryTorrentStoreStub.saveTorrent.callCount).to.equal(1);
expect(memoryTorrentStoreStub.saveTorrent.calledAfter(torrentStub.setPeer)).to.be.true;
expect(memoryTorrentStoreStub.saveTorrent.calledWith(torrentStub)).to.be.true;
});
}
beforeEach(function () {
params
.withPeerId('myPeerId')
.withIp('11.22.33.44')
.withPort(6666)
.withLeft(27);
});
describe("when `event` === stopped", function() {
beforeEach(function () {
params.withEvent('stopped');
output = trackerService.announce(params);
});
returnsObject();
it('unregisters the peer from the torrent', function () {
expect(torrentStub.removePeer.callCount).to.equal(1);
expect(torrentStub.removePeer.calledWith('myPeerId')).to.be.true;
});
it('saves updated torrent', function () {
expect(memoryTorrentStoreStub.saveTorrent.callCount).to.equal(1);
expect(memoryTorrentStoreStub.saveTorrent.calledAfter(torrentStub.removePeer)).to.be.true;
expect(memoryTorrentStoreStub.saveTorrent.calledWith(torrentStub)).to.be.true;
});
});
describe("when `event` is not specified", function () {
beforeEach(function () {
params.withEvent(undefined);
output = trackerService.announce(params);
});
returnsObject();
behavesLikeEventNotSpecified();
});
describe("when `event` === started", function () {
beforeEach(function () {
params.withEvent('started');
output = trackerService.announce(params);
});
returnsObject();
behavesLikeEventNotSpecified();
});
describe("when `event` === completed", function () {
beforeEach(function () {
params.withEvent('completed');
output = trackerService.announce(params);
});
returnsObject();
behavesLikeEventNotSpecified();
});
});
});