UNPKG

8.77 kBJavaScriptView Raw
1require("./common.js");
2
3var RecentTracksParser = require("lastfm/recenttracks-parser");
4var RecentTracksStream = require("lastfm/recenttracks-stream");
5var fakes = require("./fakes");
6
7(function() {
8 var gently, lastfm, trackStream;
9
10 describe("a new stream instance");
11
12 before(function() {
13 gently = new Gently();
14 lastfm = new LastFmNode();
15 trackStream = new RecentTracksStream(lastfm, "username");
16 });
17
18 it("configures user", function() {
19 assert.equal("username", trackStream.user);
20 });
21
22 it("checks every ten seconds", function() {
23 assert.equal(10, trackStream.rate);
24 });
25
26 it("accepts listeners", function() {
27 trackStream.addListener("event", function() {});
28 });
29
30 it("is not streaming", function() {
31 var isStreaming = trackStream.isStreaming;
32 assert.ok(!trackStream.isStreaming);
33 });
34
35 it("event handlers can be specified in options (deprecated)", function() {
36 var handlers = {};
37
38 gently.expect(handlers, "error");
39 gently.expect(handlers, "lastPlayed");
40 gently.expect(handlers, "nowPlaying");
41 gently.expect(handlers, "stoppedPlaying");
42 gently.expect(handlers, "scrobbled");
43
44 var trackStream = new RecentTracksStream(lastfm, "username", {
45 error: handlers.error,
46 lastPlayed: handlers.lastPlayed,
47 nowPlaying: handlers.nowPlaying,
48 stoppedPlaying: handlers.stoppedPlaying,
49 scrobbled: handlers.scrobbled
50 });
51
52 trackStream.emit("error");
53 trackStream.emit("lastPlayed");
54 trackStream.emit("nowPlaying");
55 trackStream.emit("stoppedPlaying");
56 trackStream.emit("scrobbled");
57 });
58
59 it("event handlers can be specified in options", function() {
60 var handlers = {};
61
62 gently.expect(handlers, "error");
63 gently.expect(handlers, "lastPlayed");
64 gently.expect(handlers, "nowPlaying");
65 gently.expect(handlers, "stoppedPlaying");
66 gently.expect(handlers, "scrobbled");
67
68 var trackStream = new RecentTracksStream(lastfm, "username", {
69 handlers: {
70 error: handlers.error,
71 lastPlayed: handlers.lastPlayed,
72 nowPlaying: handlers.nowPlaying,
73 stoppedPlaying: handlers.stoppedPlaying,
74 scrobbled: handlers.scrobbled
75 }
76 });
77
78 trackStream.emit("error");
79 trackStream.emit("lastPlayed");
80 trackStream.emit("nowPlaying");
81 trackStream.emit("stoppedPlaying");
82 trackStream.emit("scrobbled");
83 });
84})();
85
86(function() {
87 var parser, lastfm, trackStream, gently;
88
89 describe("An active stream");
90
91 before(function() {
92 parser = new RecentTracksParser();
93 lastfm = new LastFmNode();
94 trackStream = new RecentTracksStream(lastfm, "username", { parser: parser });
95 gently = new Gently();
96 });
97
98 it("bubbles errors", function() {
99 gently.expect(trackStream, "emit", function(event) {
100 assert.equal("error", event);
101 });
102 parser.emit("error", new Error());
103 });
104
105 it("emits last played when track received", function() {
106 gently.expect(trackStream, "emit", function(event, track) {
107 assert.equal("lastPlayed", event);
108 assert.equal("Lamb and the Lion", track.name);
109 });
110 parser.emit("track", FakeTracks.LambAndTheLion);
111 });
112
113 it("emits now playing if track flagged now playing", function() {
114 gently.expect(trackStream, "emit", function(event, track) {
115 assert.equal("nowPlaying", event);
116 assert.equal("Run To Your Grave", track.name);
117 });
118 parser.emit("track", FakeTracks.RunToYourGrave_NP);
119 });
120
121 it("emits now playing and last played if both received", function() {
122 gently.expect(trackStream, "emit", function(event, track) {
123 assert.equal("nowPlaying", event);
124 assert.equal("Theme Song", track.name);
125 });
126 gently.expect(trackStream, "emit", function(event, track) {
127 assert.equal("lastPlayed", event);
128 assert.equal("Over The Moon", track.name);
129 });
130 parser.emit("track", FakeTracks.NowPlayingAndScrobbled);
131 });
132
133 it("does not re-emit lastPlayed on receipt of same track", function() {
134 gently.expect(trackStream, "emit", 1, function(event, track) {
135 assert.equal("lastPlayed", event);
136 assert.equal("Lamb and the Lion", track.name);
137 });
138 parser.emit("track", FakeTracks.LambAndTheLion);
139 parser.emit("track", FakeTracks.LambAndTheLion);
140 });
141
142 it("does not re-emit nowPlaying on receipt of same track", function() {
143 gently.expect(trackStream, "emit", 1, function(event, track) {
144 assert.equal("nowPlaying", event);
145 assert.equal("Run To Your Grave", track.name);
146 });
147 parser.emit("track", FakeTracks.RunToYourGrave_NP);
148 parser.emit("track", FakeTracks.RunToYourGrave_NP);
149 });
150
151 it("emits stoppedPlaying track when now playing stops", function() {
152 gently.expect(trackStream, "emit", function(event, track) {
153 assert.equal("lastPlayed", event);
154 assert.equal("Run To Your Grave", track.name);
155 });
156 gently.expect(trackStream, "emit", function(event, track) {
157 assert.equal("nowPlaying", event);
158 assert.equal("Run To Your Grave", track.name);
159 });
160 gently.expect(trackStream, "emit", function(event, track) {
161 assert.equal("stoppedPlaying", event);
162 assert.equal("Run To Your Grave", track.name);
163 });
164 parser.emit("track", FakeTracks.RunToYourGrave);
165 parser.emit("track", FakeTracks.RunToYourGrave_NP);
166 parser.emit("track", FakeTracks.RunToYourGrave);
167 });
168
169 it("emits scrobbled when last play changes", function() {
170 gently.expect(trackStream, "emit", function(event, track) {
171 assert.equal("lastPlayed", event);
172 assert.equal("Lamb and the Lion", track.name);
173 });
174 gently.expect(trackStream, "emit", function(event, track) {
175 assert.equal("nowPlaying", event);
176 assert.equal("Run To Your Grave", track.name);
177 });
178 gently.expect(trackStream, "emit", function(event, track) {
179 assert.equal("scrobbled", event);
180 assert.equal("Run To Your Grave", track.name);
181 });
182 gently.expect(trackStream, "emit", function(event, track) {
183 assert.equal("stoppedPlaying", event);
184 assert.equal("Run To Your Grave", track.name);
185 });
186 parser.emit("track", FakeTracks.LambAndTheLion);
187 parser.emit("track", FakeTracks.RunToYourGrave_NP);
188 parser.emit("track", FakeTracks.RunToYourGrave);
189 });
190
191 it("emits nowPlaying when track same as lastPlayed", function() {
192 gently.expect(trackStream, "emit", function(event, track) {
193 assert.equal("lastPlayed", event);
194 assert.equal("Run To Your Grave", track.name);
195 });
196 gently.expect(trackStream, "emit", function(event, track) {
197 assert.equal("nowPlaying", event);
198 assert.equal("Run To Your Grave", track.name);
199 });
200 parser.emit("track", FakeTracks.RunToYourGrave);
201 parser.emit("track", FakeTracks.RunToYourGrave_NP);
202 });
203})();
204
205(function() {
206 var lastfm, gently, request;
207
208 describe("Streaming")
209
210 before(function() {
211 lastfm = new LastFmNode();
212 gently = new Gently();
213 request = new fakes.LastFmRequest();
214 });
215
216 it("starts and stops streaming when requested", function() {
217 gently.expect(lastfm, "request", 1, function(method, params) {
218 return request;
219 });
220 var trackStream = new RecentTracksStream(lastfm);
221 trackStream.start();
222 trackStream.stop();
223 assert.ok(!trackStream.isStreaming);
224 });
225
226 it("starts automatically when autostart set to true", function() {
227 gently.expect(lastfm, "request", function() {
228 return request;
229 });
230 var trackStream = new RecentTracksStream(lastfm, "username", { autostart: true} );
231 assert.ok(trackStream.isStreaming);
232 trackStream.stop();
233 });
234
235 it("calls user.getrecenttracks method for user", function() {
236 gently.expect(lastfm, "request", function(method, params) {
237 assert.equal("user.getrecenttracks", method);
238 assert.equal("username", params.user);
239 return request;
240 });
241 var trackStream = new RecentTracksStream(lastfm, "username", { autostart: true} );
242 trackStream.stop();
243 });
244
245 it("only fetches most recent track", function() {
246 gently.expect(lastfm, "request", function(method, params) {
247 assert.equal(1, params.limit);
248 return request;
249 });
250 var trackStream = new RecentTracksStream(lastfm, "username", { autostart: true} );
251 trackStream.stop();
252 });
253
254 it("bubbles up errors", function() {
255 var errorMessage = "Bubbled error";
256 gently.expect(lastfm, "request", function() {
257 return request;
258 });
259 var trackStream = new RecentTracksStream(lastfm, "username", { autostart:true });
260 gently.expect(trackStream, "emit", function(event, error) {
261 assert.equal(errorMessage, error.message);
262 });
263 request.emit("error", new Error(errorMessage));
264 trackStream.stop();
265 });
266})();