Skip to content

Commit

Permalink
chore: Added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
svrooij committed Oct 29, 2020
1 parent feeca71 commit d8ee722
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/helpers/tts-helper.ts
Expand Up @@ -37,7 +37,7 @@ export default class TtsHelper {
return data.cdnUri || data.uri;
}

static async TtsOptionsToNotification(options: PlayTtsOptions): Promise<PlayNotificationOptions | undefined> {
static async TtsOptionsToNotification(options: PlayTtsOptions): Promise<PlayNotificationOptions> {
const endpoint = options.endpoint ?? process.env.SONOS_TTS_ENDPOINT;
if (endpoint === undefined) {
throw new Error('TTS endpoint is required, check the documentation.');
Expand Down
3 changes: 0 additions & 3 deletions src/sonos-device.ts
Expand Up @@ -422,9 +422,6 @@ export default class SonosDevice extends SonosDeviceBase {
this.debug('PlayTTS(%o)', options);

const notificationOptions = await TtsHelper.TtsOptionsToNotification(options);
if (typeof (notificationOptions) === 'undefined') {
return false;
}

return await this.PlayNotification(notificationOptions);
}
Expand Down
45 changes: 45 additions & 0 deletions tests/helpers/metadata-helper.test.ts
Expand Up @@ -27,6 +27,12 @@ describe('MetadataHelper', () => {
expect(track).to.be.an('object');
});

it('Guess metadata for Spotify user playlist', () => {
const uri = 'spotify:user:37i9dQZF1DX4WYpdgoIcn6'
const track = MetadataHelper.GuessTrack(uri);
expect(track).to.be.an('object');
});

it('Produces same metadata as legacy for Spotify track', () => {
const track = MetadataHelper.GuessTrack(spotifyTrack);
expect(track).to.be.an('object');
Expand All @@ -35,6 +41,12 @@ describe('MetadataHelper', () => {
const metadata = MetadataHelper.TrackToMetaData(track);
assert.equal(metadata, legacyMetadataObject.metadata);
});

it('Returns undefined for unsupported uri', () => {
const uri = 'fake:music:service'
const track = MetadataHelper.GuessTrack(uri);
expect(track).to.be.undefined;
});
});

describe('GuessTrackAndMetadata', () => {
Expand Down Expand Up @@ -68,6 +80,39 @@ describe('MetadataHelper', () => {
});
})

describe('ParseDIDLTrack', () => {
it('parsed r:streamContent correctly 2', () => {
const artist = 'Guus Meeuwis';
const title = 'Brabant'
const id = 'FAKE_ITEM_ID'
const didl = {
_id: id,
'r:streamContent': `${artist} - ${title}`
}
const result = MetadataHelper.ParseDIDLTrack(didl, 'fake_host')
expect(result).to.be.an('object');
expect(result).to.have.nested.property('Artist', artist);
expect(result).to.have.nested.property('Title', title);
expect(result).to.have.nested.property('ItemId', id);
})

it('parsed r:streamContent and r:radioShowMd correctly', () => {
const artist = 'Guus Meeuwis';
const title = 'Brabant'
const id = 'FAKE_ITEM_ID'
const didl = {
_id: id,
'r:streamContent': artist,
'r:radioShowMd': title
}
const result = MetadataHelper.ParseDIDLTrack(didl, 'fake_host')
expect(result).to.be.an('object');
expect(result).to.have.nested.property('Artist', artist);
expect(result).to.have.nested.property('Title', title);
expect(result).to.have.nested.property('ItemId', id);
})
})

describe('TrackToMetaData', () => {
it('returns emtpy string when track undefined', () => {
const result = MetadataHelper.TrackToMetaData(undefined);
Expand Down
15 changes: 15 additions & 0 deletions tests/services/alarm-clock.service.test.ts
Expand Up @@ -31,6 +31,21 @@ describe('AlarmClockService', () => {
});
});

describe('DestroyAlarm', () => {
it('sends the correct request', async () => {
TestHelpers.mockRequest('/AlarmClock/Control',
'"urn:schemas-upnp-org:service:AlarmClock:1#DestroyAlarm"',
'<u:DestroyAlarm xmlns:u="urn:schemas-upnp-org:service:AlarmClock:1"><ID>1</ID></u:DestroyAlarm>',
'DestroyAlarmResponse',
'AlarmClock');

const service = new AlarmClockService(TestHelpers.testHost, 1400);
const result = await service.DestroyAlarm({ ID: 1 });
expect(result).to.be.true;

})
})

describe('GetTimeZone', () => {
it('works', async () => {
TestHelpers.mockRequest('/AlarmClock/Control',
Expand Down

0 comments on commit d8ee722

Please sign in to comment.