1 | require("./common.js");
|
2 |
|
3 | var _ = require("underscore")
|
4 | , RecentTracksStream = require("../lib/lastfm/recenttracks-stream")
|
5 | , LastFmRequest = require("../lib/lastfm/lastfm-request")
|
6 | , fakes = require("./fakes");
|
7 |
|
8 | (function() {
|
9 | var gently, lastfm, trackStream;
|
10 |
|
11 | describe("a new stream instance");
|
12 |
|
13 | before(function() {
|
14 | gently = new Gently();
|
15 | lastfm = new LastFmNode();
|
16 | trackStream = new RecentTracksStream(lastfm, "username");
|
17 | });
|
18 |
|
19 | it("accepts listeners", function() {
|
20 | trackStream.addListener("event", function() {});
|
21 | });
|
22 |
|
23 | it("is not streaming", function() {
|
24 | assert.ok(!trackStream.isStreaming());
|
25 | });
|
26 |
|
27 | it("event handlers can be specified in options", function() {
|
28 | var handlers = {};
|
29 |
|
30 | gently.expect(handlers, "error");
|
31 | gently.expect(handlers, "lastPlayed");
|
32 | gently.expect(handlers, "nowPlaying");
|
33 | gently.expect(handlers, "stoppedPlaying");
|
34 | gently.expect(handlers, "scrobbled");
|
35 |
|
36 | var trackStream = new RecentTracksStream(lastfm, "username", {
|
37 | handlers: {
|
38 | error: handlers.error,
|
39 | lastPlayed: handlers.lastPlayed,
|
40 | nowPlaying: handlers.nowPlaying,
|
41 | stoppedPlaying: handlers.stoppedPlaying,
|
42 | scrobbled: handlers.scrobbled
|
43 | }
|
44 | });
|
45 |
|
46 | trackStream.emit("error");
|
47 | trackStream.emit("lastPlayed");
|
48 | trackStream.emit("nowPlaying");
|
49 | trackStream.emit("stoppedPlaying");
|
50 | trackStream.emit("scrobbled");
|
51 | });
|
52 | })();
|
53 |
|
54 | (function() {
|
55 | var requestEmits = [],
|
56 | previousEmits = [];
|
57 |
|
58 | function ifRequestHasPreviouslyEmit(emits) {
|
59 | previousEmits = emits;
|
60 | }
|
61 |
|
62 | function whenRequestEmits(count, event, object) {
|
63 | if (typeof count !== "number") {
|
64 | object = event;
|
65 | event = count;
|
66 | count = 1;
|
67 | }
|
68 | if (typeof event !== "string") {
|
69 | object = event;
|
70 | event = "success";
|
71 | }
|
72 | requestEmits = [event, object, count];
|
73 | }
|
74 |
|
75 | function expectStreamToEmit(count, expectation) {
|
76 | if (typeof count === "function") {
|
77 | expectation = count;
|
78 | count = 1;
|
79 | }
|
80 | var lastfm = new LastFmNode(),
|
81 | connection = new fakes.Client(80, lastfm.host),
|
82 | request = new fakes.LastFmRequest(),
|
83 | gently = new Gently();
|
84 |
|
85 | gently.expect(lastfm, "request", function() {
|
86 | return request;
|
87 | });
|
88 |
|
89 | var trackStream = new RecentTracksStream(lastfm, "username");
|
90 | trackStream.start();
|
91 | trackStream.stop();
|
92 | for(var index = 0; index < previousEmits.length; index++) {
|
93 | request.emit("success", previousEmits[index]);
|
94 | }
|
95 | gently.expect(trackStream, "emit", count, expectation);
|
96 | for(var times = 0; times < requestEmits[2]; times++) {
|
97 | request.emit(requestEmits[0], requestEmits[1]);
|
98 | }
|
99 | }
|
100 |
|
101 | describe("An active stream");
|
102 |
|
103 | before(function() {
|
104 | previousEmits = [];
|
105 | requestEmits = [];
|
106 | });
|
107 |
|
108 | it("bubbles errors", function() {
|
109 | whenRequestEmits("error", { error: 1, message: "An error" });
|
110 | expectStreamToEmit(function(event, error) {
|
111 | assert.equal("error", event);
|
112 | assert.equal("An error", error.message);
|
113 | });
|
114 | });
|
115 |
|
116 | it("emits last played when track received", function() {
|
117 | whenRequestEmits({ recenttracks: { track:
|
118 | FakeTracks.LambAndTheLion
|
119 | } });
|
120 | expectStreamToEmit(function(event, track) {
|
121 | assert.equal("lastPlayed", event);
|
122 | assert.equal("Lamb and the Lion", track.name);
|
123 | });
|
124 | });
|
125 |
|
126 | it("emits now playing if track flagged now playing", function() {
|
127 | whenRequestEmits({
|
128 | recenttracks: { track: FakeTracks.RunToYourGrave_NP }
|
129 | });
|
130 | expectStreamToEmit(function(event, track) {
|
131 | assert.equal("nowPlaying", event);
|
132 | assert.equal("Run To Your Grave", track.name);
|
133 | });
|
134 | });
|
135 |
|
136 | it("emits now playing and last played if both received", function() {
|
137 | var count = 0;
|
138 | whenRequestEmits({
|
139 | recenttracks: { track: FakeTracks.NowPlayingAndScrobbled }
|
140 | });
|
141 | expectStreamToEmit(2, function(event, track) {
|
142 | if (count == 0) {
|
143 | assert.equal("nowPlaying", event);
|
144 | assert.equal("Theme Song", track.name);
|
145 | }
|
146 | else {
|
147 | assert.equal("lastPlayed", event);
|
148 | assert.equal("Over The Moon", track.name);
|
149 | }
|
150 | count++;
|
151 | });
|
152 | });
|
153 |
|
154 | it("does not re-emit lastPlayed on receipt of same track", function() {
|
155 | whenRequestEmits(2, {
|
156 | recenttracks: { track: FakeTracks.LambAndTheLion }
|
157 | });
|
158 | expectStreamToEmit(1, function(event, track) {
|
159 | assert.equal("lastPlayed", event);
|
160 | assert.equal("Lamb and the Lion", track.name);
|
161 | });
|
162 | });
|
163 |
|
164 | it("does not re-emit nowPlaying on receipt of same track", function() {
|
165 | whenRequestEmits(2, {
|
166 | recenttracks: { track: FakeTracks.RunToYourGrave_NP }
|
167 | });
|
168 | expectStreamToEmit(1, function(event, track) {
|
169 | assert.equal("nowPlaying", event);
|
170 | assert.equal("Run To Your Grave", track.name);
|
171 | });
|
172 | });
|
173 |
|
174 | it("emits stoppedPlaying track when now playing stops", function() {
|
175 | ifRequestHasPreviouslyEmit([
|
176 | { recenttracks: { track: FakeTracks.RunToYourGrave } },
|
177 | { recenttracks: { track: FakeTracks.RunToYourGrave_NP } }
|
178 | ]);
|
179 | whenRequestEmits({
|
180 | recenttracks: { track: FakeTracks.RunToYourGrave }
|
181 | });
|
182 | expectStreamToEmit(function(event, track) {
|
183 | assert.equal("stoppedPlaying", event);
|
184 | assert.equal("Run To Your Grave", track.name);
|
185 | });
|
186 | });
|
187 |
|
188 | it("emits scrobbled when last play changes", function() {
|
189 | ifRequestHasPreviouslyEmit([
|
190 | { recenttracks: { track: FakeTracks.LambAndTheLion } },
|
191 | { recenttracks: { track: FakeTracks.RunToYourGrave_NP } }
|
192 | ]);
|
193 | whenRequestEmits({
|
194 | recenttracks: { track: FakeTracks.RunToYourGrave }
|
195 | });
|
196 | expectStreamToEmit(function(event, track) {
|
197 | assert.equal("scrobbled", event);
|
198 | assert.equal("Run To Your Grave", track.name);
|
199 | });
|
200 | });
|
201 |
|
202 | it("emits nowPlaying when track same as lastPlayed", function() {
|
203 | ifRequestHasPreviouslyEmit([
|
204 | { recenttracks: { track: FakeTracks.RunToYourGrave } }
|
205 | ]);
|
206 | whenRequestEmits({
|
207 | recenttracks: { track: FakeTracks.RunToYourGrave_NP }
|
208 | });
|
209 | expectStreamToEmit(function(event, track) {
|
210 | assert.equal("nowPlaying", event);
|
211 | assert.equal("Run To Your Grave", track.name);
|
212 | });
|
213 | });
|
214 |
|
215 | it("emits error when unexpected item is received", function() {
|
216 | whenRequestEmits({
|
217 | something: "we've never seen before"
|
218 | });
|
219 | expectStreamToEmit(function(event, error) {
|
220 | assert.equal("error", event);
|
221 | assert.equal("Unexpected response", error.message);
|
222 | });
|
223 | });
|
224 | })();
|
225 |
|
226 | (function() {
|
227 | var lastfm, gently, request;
|
228 |
|
229 | describe("Streaming")
|
230 |
|
231 | before(function() {
|
232 | lastfm = new LastFmNode();
|
233 | gently = new Gently();
|
234 | request = new fakes.LastFmRequest();
|
235 | });
|
236 |
|
237 | it("starts and stops streaming when requested", function() {
|
238 | gently.expect(lastfm, "request", 1, function(method, params) {
|
239 | return request;
|
240 | });
|
241 | var trackStream = new RecentTracksStream(lastfm);
|
242 | trackStream.start();
|
243 | trackStream.stop();
|
244 | assert.ok(!trackStream.isStreaming());
|
245 | });
|
246 |
|
247 | it("starts automatically when autostart set to true", function() {
|
248 | gently.expect(lastfm, "request", function() {
|
249 | return request;
|
250 | });
|
251 | var trackStream = new RecentTracksStream(lastfm, "username", { autostart: true} );
|
252 | assert.ok(trackStream.isStreaming());
|
253 | trackStream.stop();
|
254 | });
|
255 |
|
256 | it("calls user.getrecenttracks method for user", function() {
|
257 | gently.expect(lastfm, "request", function(method, params) {
|
258 | assert.equal("user.getrecenttracks", method);
|
259 | assert.equal("username", params.user);
|
260 | return request;
|
261 | });
|
262 | var trackStream = new RecentTracksStream(lastfm, "username", { autostart: true} );
|
263 | trackStream.stop();
|
264 | });
|
265 |
|
266 | it("only fetches most recent track", function() {
|
267 | gently.expect(lastfm, "request", function(method, params) {
|
268 | assert.equal(1, params.limit);
|
269 | return request;
|
270 | });
|
271 | var trackStream = new RecentTracksStream(lastfm, "username", { autostart: true} );
|
272 | trackStream.stop();
|
273 | });
|
274 |
|
275 | it("bubbles up errors", function() {
|
276 | var errorMessage = "Bubbled error";
|
277 | gently.expect(lastfm, "request", function() {
|
278 | return request;
|
279 | });
|
280 | var trackStream = new RecentTracksStream(lastfm, "username", { autostart:true });
|
281 | gently.expect(trackStream, "emit", function(event, error) {
|
282 | assert.equal(errorMessage, error.message);
|
283 | });
|
284 | request.emit("error", new Error(errorMessage));
|
285 | trackStream.stop();
|
286 | });
|
287 | })();
|
288 |
|
289 | (function() {
|
290 | var lastfm, gently;
|
291 |
|
292 | describe("Streaming")
|
293 |
|
294 | var tmpScheduleFn;
|
295 | before(function() {
|
296 | tmpScheduleFn = RecentTracksStream.prototype.scheduleCallback;
|
297 | lastfm = new LastFmNode();
|
298 | gently = new Gently();
|
299 | });
|
300 |
|
301 | after(function() {
|
302 | RecentTracksStream.prototype.scheduleCallback = tmpScheduleFn;
|
303 | });
|
304 |
|
305 | it("queries API every 10 seconds", function() {
|
306 | var trackStream = new RecentTracksStream(lastfm, "username");
|
307 | var count = 0;
|
308 | RecentTracksStream.prototype.scheduleCallback = function(callback, delay) {
|
309 | count++;
|
310 | if (count === 10) {
|
311 | trackStream.stop();
|
312 | }
|
313 | assert.ok(delay, 10000);
|
314 | gently.expect(lastfm, "request", function(method, params) {
|
315 | return new fakes.LastFmRequest();
|
316 | });
|
317 | callback();
|
318 | };
|
319 | gently.expect(lastfm, "request", function(method, params) {
|
320 | return new fakes.LastFmRequest();
|
321 | });
|
322 | trackStream.start();
|
323 | });
|
324 | })();
|