1 | require('./common.js');
|
2 | var LastFmSession = require('lastfm/lastfm-session');
|
3 | var LastFmUpdate = require('lastfm/lastfm-update');
|
4 | var fakes = require("./fakes");
|
5 |
|
6 | (function() {
|
7 | describe("new LastFmUpdate")
|
8 | it("can have success and error handlers specified at creation", function() {
|
9 | var gently = new Gently();
|
10 | var lastfm = new LastFmNode();
|
11 | var update = new LastFmUpdate(lastfm, "method", new LastFmSession(lastfm, "user", "key"), {
|
12 | error: gently.expect(function error() {}),
|
13 | success: gently.expect(function success() {})
|
14 | });
|
15 | update.emit("error");
|
16 | update.emit("success");
|
17 | });
|
18 | })();
|
19 |
|
20 | (function() {
|
21 | var request, returndata, options, session, method, gently, lastfm, authorisedSession, requestError;
|
22 |
|
23 | function setupFixture() {
|
24 | request = new fakes.LastFmRequest();
|
25 | returndata;
|
26 | options = {};
|
27 | session = null;
|
28 | method = "";
|
29 | gently = new Gently();
|
30 | lastfm = new LastFmNode();
|
31 | authorisedSession = new LastFmSession(lastfm, "user", "key");
|
32 | requestError = null;
|
33 | }
|
34 |
|
35 | function whenWriteRequestReturns(data) {
|
36 | returndata = data;
|
37 | gently.expect(lastfm, "write", function(params, signed) {
|
38 | return request;
|
39 | });
|
40 | }
|
41 |
|
42 | function whenWriteRequestThrowsError(errorMessage) {
|
43 | requestError = errorMessage;
|
44 | gently.expect(lastfm, "write", function(params, signed) {
|
45 | return request;
|
46 | });
|
47 | }
|
48 |
|
49 | function andOptionsAre(setOptions) {
|
50 | options = setOptions;
|
51 | }
|
52 |
|
53 | function andMethodIs(setMethod) {
|
54 | method = setMethod;
|
55 | }
|
56 |
|
57 | function andSessionIs(setSession) {
|
58 | session = setSession;
|
59 | }
|
60 |
|
61 | function expectSuccess(assertions) {
|
62 | options.success = function(track) {
|
63 | if (assertions) {
|
64 | assertions(track);
|
65 | }
|
66 | };
|
67 | new LastFmUpdate(lastfm, method, session, options);
|
68 | request.emit("success", returndata);
|
69 | }
|
70 |
|
71 | function expectError(expectedError) {
|
72 | options.error = gently.expect(function(error) {
|
73 | assert.equal(expectedError, error.message);
|
74 | });
|
75 | new LastFmUpdate(lastfm, method, session, options);
|
76 | if (requestError) {
|
77 | request.emit("error", new Error(requestError));
|
78 | }
|
79 | else {
|
80 | request.emit("success", returndata);
|
81 | }
|
82 | }
|
83 |
|
84 | describe("update requests")
|
85 | before(function() {
|
86 | setupFixture();
|
87 | });
|
88 |
|
89 | it("fail when the session is not authorised", function() {
|
90 | var session = new LastFmSession();
|
91 | assert.throws(function() {
|
92 | new LastFmUpdate(lastfm, "method", session);
|
93 | });
|
94 | });
|
95 |
|
96 | it("sends a signed request", function() {
|
97 | gently.expect(lastfm, "write", function(params, signed) {
|
98 | assert.ok(signed);
|
99 | return request;
|
100 | });
|
101 | new LastFmUpdate(lastfm, "nowplaying", authorisedSession, { track: FakeTracks.RunToYourGrave });
|
102 | });
|
103 |
|
104 | it("emits error when problem updating", function() {
|
105 | whenWriteRequestReturns(FakeData.UpdateError);
|
106 | andMethodIs("nowplaying");
|
107 | andSessionIs(authorisedSession);
|
108 | andOptionsAre({
|
109 | track: FakeTracks.RunToYourGrave
|
110 | });
|
111 | expectError("Invalid method signature supplied");
|
112 | });
|
113 |
|
114 | describe("nowPlaying updates")
|
115 | before(function() {
|
116 | setupFixture();
|
117 | });
|
118 |
|
119 | it("uses updateNowPlaying method", function() {
|
120 | gently.expect(lastfm, "write", function(params) {
|
121 | assert.equal("track.updateNowPlaying", params.method);
|
122 | return request;
|
123 | });
|
124 | new LastFmUpdate(lastfm, "nowplaying", authorisedSession, {
|
125 | track: FakeTracks.RunToYourGrave
|
126 | });
|
127 | });
|
128 |
|
129 | it("sends required parameters", function() {
|
130 | gently.expect(lastfm, "write", function(params) {
|
131 | assert.equal("The Mae Shi", params.artist);
|
132 | assert.equal("Run To Your Grave", params.track);
|
133 | assert.equal("key", params.sk);
|
134 | return request;
|
135 | });
|
136 | new LastFmUpdate(lastfm, "nowplaying", authorisedSession, {
|
137 | track: FakeTracks.RunToYourGrave
|
138 | });
|
139 | });
|
140 |
|
141 | it("emits success when updated", function() {
|
142 | whenWriteRequestReturns(FakeData.UpdateNowPlayingSuccess);
|
143 | andMethodIs("nowplaying");
|
144 | andSessionIs(authorisedSession);
|
145 | andOptionsAre({
|
146 | track: FakeTracks.RunToYourGrave
|
147 | });
|
148 | expectSuccess(function(track) {
|
149 | assert.equal("Run To Your Grave", track.name);
|
150 | });
|
151 | });
|
152 |
|
153 | it("sends duration when supplied", function() {
|
154 | gently.expect(lastfm, "write", function(params) {
|
155 | assert.equal(232000, params.duration);
|
156 | return request;
|
157 | });
|
158 | new LastFmUpdate(lastfm, "nowplaying", authorisedSession, {
|
159 | track: FakeTracks.RunToYourGrave,
|
160 | duration: 232000
|
161 | });
|
162 | });
|
163 |
|
164 | it("bubbles up errors", function() {
|
165 | var errorMessage = "Bubbled error";
|
166 | whenWriteRequestThrowsError(errorMessage);
|
167 | andMethodIs("nowplaying");
|
168 | andSessionIs(authorisedSession);
|
169 | andOptionsAre({
|
170 | track: FakeTracks.RunToYourGrave,
|
171 | timestamp: 12345678
|
172 | });
|
173 | expectError(errorMessage);
|
174 | });
|
175 |
|
176 | describe("a scrobble request")
|
177 | before(function() {
|
178 | setupFixture();
|
179 | });
|
180 |
|
181 | it("emits error when no timestamp supplied", function() {
|
182 | new LastFmUpdate(lastfm, "scrobble", authorisedSession, {
|
183 | track: FakeTracks.RunToYourGrave,
|
184 | error: gently.expect(function error(error) {
|
185 | assert.equal("Timestamp is required for scrobbling", error.message);
|
186 | })
|
187 | });
|
188 | });
|
189 |
|
190 | it("uses scrobble method", function() {
|
191 | gently.expect(lastfm, "write", function(params) {
|
192 | assert.equal("track.scrobble", params.method);
|
193 | return request;
|
194 | });
|
195 | new LastFmUpdate(lastfm, "scrobble", authorisedSession, {
|
196 | track: FakeTracks.RunToYourGrave,
|
197 | timestamp: 12345678
|
198 | });
|
199 | });
|
200 |
|
201 | it("sends required parameters", function() {
|
202 | gently.expect(lastfm, "write", function(params) {
|
203 | assert.equal("The Mae Shi", params.artist);
|
204 | assert.equal("Run To Your Grave", params.track);
|
205 | assert.equal("key", params.sk);
|
206 | assert.equal(12345678, params.timestamp);
|
207 | return request;
|
208 | });
|
209 |
|
210 | new LastFmUpdate(lastfm, "scrobble", authorisedSession, {
|
211 | track: FakeTracks.RunToYourGrave,
|
212 | timestamp: 12345678
|
213 | });
|
214 | });
|
215 |
|
216 | it("emits success when updated", function() {
|
217 | whenWriteRequestReturns(FakeData.ScrobbleSuccess);
|
218 | andMethodIs("scrobble");
|
219 | andSessionIs(authorisedSession);
|
220 | andOptionsAre({
|
221 | track: FakeTracks.RunToYourGrave,
|
222 | timestamp: 12345678
|
223 | });
|
224 | expectSuccess(function(track) {
|
225 | assert.equal("Run To Your Grave", track.name);
|
226 | });
|
227 | });
|
228 |
|
229 | it("bubbles up errors", function() {
|
230 | var errorMessage = "Bubbled error";
|
231 | whenWriteRequestThrowsError(errorMessage);
|
232 | andMethodIs("scrobble");
|
233 | andSessionIs(authorisedSession);
|
234 | andOptionsAre({
|
235 | track: FakeTracks.RunToYourGrave,
|
236 | timestamp: 12345678
|
237 | });
|
238 | expectError(errorMessage);
|
239 | });
|
240 | })();
|