UNPKG

7.51 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("requests recent tracks", function() {
36 assert.equal("user.getrecenttracks", trackStream.params.method);
37 });
38
39 it("only requests the most recent track", function() {
40 assert.equal(1, trackStream.params.limit);
41 });
42
43 it("event handlers can be specified in options", function() {
44 var handlers = {};
45
46 gently.expect(handlers, "error");
47 gently.expect(handlers, "lastPlayed");
48 gently.expect(handlers, "nowPlaying");
49 gently.expect(handlers, "stoppedPlaying");
50 gently.expect(handlers, "scrobbled");
51
52 var trackStream = new RecentTracksStream(lastfm, "username", {
53 error: handlers.error,
54 lastPlayed: handlers.lastPlayed,
55 nowPlaying: handlers.nowPlaying,
56 stoppedPlaying: handlers.stoppedPlaying,
57 scrobbled: handlers.scrobbled
58 });
59
60 trackStream.emit("error");
61 trackStream.emit("lastPlayed");
62 trackStream.emit("nowPlaying");
63 trackStream.emit("stoppedPlaying");
64 trackStream.emit("scrobbled");
65 });
66})();
67
68(function() {
69 var parser, lastfm, trackStream, gently;
70
71 describe("An active stream");
72
73 before(function() {
74 parser = new RecentTracksParser();
75 lastfm = new LastFmNode();
76 trackStream = new RecentTracksStream(lastfm, "username", { parser: parser });
77 gently = new Gently();
78 });
79
80 it("bubbles errors", function() {
81 gently.expect(trackStream, "emit", function(event) {
82 assert.equal("error", event);
83 });
84 parser.emit("error", new Error());
85 });
86
87 it("emits last played when track received", function() {
88 gently.expect(trackStream, "emit", function(event, track) {
89 assert.equal("lastPlayed", event);
90 assert.equal("Lamb and the Lion", track.name);
91 });
92 parser.emit("track", FakeTracks.LambAndTheLion);
93 });
94
95 it("emits now playing if track flagged now playing", function() {
96 gently.expect(trackStream, "emit", function(event, track) {
97 assert.equal("nowPlaying", event);
98 assert.equal("Run To Your Grave", track.name);
99 });
100 parser.emit("track", FakeTracks.RunToYourGrave_NP);
101 });
102
103 it("emits now playing and last played if both received", function() {
104 gently.expect(trackStream, "emit", function(event, track) {
105 assert.equal("nowPlaying", event);
106 assert.equal("Theme Song", track.name);
107 });
108 gently.expect(trackStream, "emit", function(event, track) {
109 assert.equal("lastPlayed", event);
110 assert.equal("Over The Moon", track.name);
111 });
112 parser.emit("track", FakeTracks.NowPlayingAndScrobbled);
113 });
114
115 it("does not re-emit lastPlayed on receipt of same track", function() {
116 gently.expect(trackStream, "emit", 1, function(event, track) {
117 assert.equal("lastPlayed", event);
118 assert.equal("Lamb and the Lion", track.name);
119 });
120 parser.emit("track", FakeTracks.LambAndTheLion);
121 parser.emit("track", FakeTracks.LambAndTheLion);
122 });
123
124 it("does not re-emit nowPlaying on receipt of same track", function() {
125 gently.expect(trackStream, "emit", 1, function(event, track) {
126 assert.equal("nowPlaying", event);
127 assert.equal("Run To Your Grave", track.name);
128 });
129 parser.emit("track", FakeTracks.RunToYourGrave_NP);
130 parser.emit("track", FakeTracks.RunToYourGrave_NP);
131 });
132
133 it("emits stoppedPlaying track when now playing stops", function() {
134 gently.expect(trackStream, "emit", function(event, track) {
135 assert.equal("lastPlayed", event);
136 assert.equal("Run To Your Grave", track.name);
137 });
138 gently.expect(trackStream, "emit", function(event, track) {
139 assert.equal("nowPlaying", event);
140 assert.equal("Run To Your Grave", track.name);
141 });
142 gently.expect(trackStream, "emit", function(event, track) {
143 assert.equal("stoppedPlaying", event);
144 assert.equal("Run To Your Grave", track.name);
145 });
146 parser.emit("track", FakeTracks.RunToYourGrave);
147 parser.emit("track", FakeTracks.RunToYourGrave_NP);
148 parser.emit("track", FakeTracks.RunToYourGrave);
149 });
150
151 it("emits scrobbled when last play changes", function() {
152 gently.expect(trackStream, "emit", function(event, track) {
153 assert.equal("lastPlayed", event);
154 assert.equal("Lamb and the Lion", 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("scrobbled", event);
162 assert.equal("Run To Your Grave", track.name);
163 });
164 gently.expect(trackStream, "emit", function(event, track) {
165 assert.equal("stoppedPlaying", event);
166 assert.equal("Run To Your Grave", track.name);
167 });
168 parser.emit("track", FakeTracks.LambAndTheLion);
169 parser.emit("track", FakeTracks.RunToYourGrave_NP);
170 parser.emit("track", FakeTracks.RunToYourGrave);
171 });
172
173 it("emits nowPlaying when track same as lastPlayed", function() {
174 gently.expect(trackStream, "emit", function(event, track) {
175 assert.equal("lastPlayed", event);
176 assert.equal("Run To Your Grave", track.name);
177 });
178 gently.expect(trackStream, "emit", function(event, track) {
179 assert.equal("nowPlaying", event);
180 assert.equal("Run To Your Grave", track.name);
181 });
182 parser.emit("track", FakeTracks.RunToYourGrave);
183 parser.emit("track", FakeTracks.RunToYourGrave_NP);
184 });
185})();
186
187(function() {
188 var lastfm, gently, request;
189
190 describe("Streaming")
191
192 before(function() {
193 lastfm = new LastFmNode();
194 gently = new Gently();
195 request = new fakes.LastFmRequest();
196 });
197
198 it("starts and stops streaming when requested", function() {
199 gently.expect(lastfm, "read", 1, function(params, signed, callback) {
200 return request;
201 });
202 var trackStream = new RecentTracksStream(lastfm);
203 trackStream.start();
204 trackStream.stop();
205 assert.ok(!trackStream.isStreaming);
206 });
207
208 it("starts automatically when autostart set to true", function() {
209 gently.expect(lastfm, "read", function() {
210 return request;
211 });
212 var trackStream = new RecentTracksStream(lastfm, "username", { autostart: true} );
213 assert.ok(trackStream.isStreaming);
214 trackStream.stop();
215 });
216
217 it("bubbles up errors", function() {
218 var errorMessage = "Bubbled error";
219 gently.expect(lastfm, "read", function() {
220 return request;
221 });
222 var trackStream = new RecentTracksStream(lastfm, "username", { autostart:true });
223 gently.expect(trackStream, "emit", function(event, error) {
224 assert.equal(errorMessage, error.message);
225 });
226 request.emit("error", new Error(errorMessage));
227 trackStream.stop();
228 });
229})();