Skip to content

Commit 14d3e45

Browse files
committed
feat: Add seekForward and seekBackward
1 parent 39346e3 commit 14d3e45

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

lib/spotify-node-applescript.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ var scripts = {
3636
'tell application "Spotify" to next track',
3737
previous:
3838
'tell application "Spotify" to previous track',
39+
seekForward:
40+
'tell application "Spotify" to set player position to (player position + %s)',
41+
seekBackward:
42+
'tell application "Spotify" to set player position to (player position - %s)',
3943
jumpTo:
4044
'tell application "Spotify" to set player position to %s',
4145
isRunning:
@@ -151,6 +155,14 @@ exports.previous = function (callback) {
151155
return execScript('previous', callback);
152156
};
153157

158+
exports.seekForward = function (seconds, callback) {
159+
return execScript('seekForward', seconds, callback);
160+
};
161+
162+
exports.seekBackward = function (seconds, callback) {
163+
return execScript('seekBackward', seconds, callback);
164+
};
165+
154166
exports.jumpTo = function (position, callback) {
155167
return execScript('jumpTo', position, callback);
156168
};

test/test.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var spotify = require('../lib/spotify-node-applescript.js');
22
var expect = require('chai').expect;
33

44
var COLDPLAY_TROUBLE_ID = 'spotify:track:0R8P9KfGJCDULmlEoBagcO'
5+
var COLDPLAY_TROUBLE_PLAYTIME_SEC = 273
56

67
describe('Spotify Controller', function () {
78

@@ -206,6 +207,60 @@ describe('Spotify Controller', function () {
206207
});
207208
});
208209

210+
// Seek forward and backward
211+
212+
it('should seek forward', function (done) {
213+
// first do jumpTo 0 in case position is at the max
214+
spotify.jumpTo(0, function () {
215+
spotify.seekForward(5, function () {
216+
spotify.getState(function (error, state) {
217+
expect(state.position).to.be.gte(5);
218+
done();
219+
});
220+
});
221+
});
222+
});
223+
224+
it('should seek backward', function (done) {
225+
// first do jumpTo 15 in case position is at 0
226+
spotify.jumpTo(15, function () {
227+
spotify.seekBackward(5, function () {
228+
spotify.getState(function (error, state) {
229+
expect(state.position).to.be.gte(10);
230+
done();
231+
});
232+
});
233+
});
234+
});
235+
236+
it('should set position to 0 on seeking foward exceeding max', function (done) {
237+
// first do jumpTo to emulate overflow when seeking forward
238+
spotify.jumpTo(COLDPLAY_TROUBLE_PLAYTIME_SEC - 3, function () {
239+
spotify.getState(function (error, state) {
240+
spotify.seekForward(5, function () {
241+
spotify.getState(function (error, state) {
242+
expect(state.position).to.equal(0);
243+
done();
244+
});
245+
});
246+
});
247+
});
248+
});
249+
250+
it('should set position to 0 on seeking foward exceeding min', function (done) {
251+
// first do jumpTo to emulate overflow when seeking backward
252+
spotify.jumpTo(2, function () {
253+
spotify.getState(function (error, state) {
254+
spotify.seekBackward(5, function () {
255+
spotify.getState(function (error, state) {
256+
expect(state.position).to.equal(0);
257+
done();
258+
});
259+
});
260+
});
261+
});
262+
});
263+
209264
// State retrieval
210265

211266
it('should return current track', function (done) {

0 commit comments

Comments
 (0)