diff --git a/src/backend/tests/plays/spotifyCurrentPlaybackState.json b/src/backend/tests/plays/spotifyCurrentPlaybackState.json new file mode 100644 index 00000000..760aa788 --- /dev/null +++ b/src/backend/tests/plays/spotifyCurrentPlaybackState.json @@ -0,0 +1,491 @@ +{ + "device": { + "id": "10ef8b79eb62f6fe99b53988c7bb3a84b358e88b", + "is_active": true, + "is_private_session": false, + "is_restricted": false, + "name": "foxx-arch", + "supports_volume": true, + "type": "Computer", + "volume_percent": 59 + }, + "shuffle_state": false, + "smart_shuffle": false, + "repeat_state": "off", + "timestamp": 1734358194901, + "context": { + "external_urls": { + "spotify": "https://open.spotify.com/album/4r9HEszOAbfhRfT1n1QPLF" + }, + "href": "https://api.spotify.com/v1/albums/4r9HEszOAbfhRfT1n1QPLF", + "type": "album", + "uri": "spotify:album:4r9HEszOAbfhRfT1n1QPLF" + }, + "progress_ms": 78823, + "item": { + "album": { + "album_type": "album", + "artists": [ + { + "external_urls": { + "spotify": "https://open.spotify.com/artist/4D2G48IdJKhcdZ5c1dqp5Z" + }, + "href": "https://api.spotify.com/v1/artists/4D2G48IdJKhcdZ5c1dqp5Z", + "id": "4D2G48IdJKhcdZ5c1dqp5Z", + "name": "Dubmood", + "type": "artist", + "uri": "spotify:artist:4D2G48IdJKhcdZ5c1dqp5Z" + } + ], + "available_markets": [ + "AR", + "AU", + "AT", + "BE", + "BO", + "BR", + "BG", + "CA", + "CL", + "CO", + "CR", + "CY", + "CZ", + "DK", + "DO", + "DE", + "EC", + "EE", + "SV", + "FI", + "FR", + "GR", + "GT", + "HN", + "HK", + "HU", + "IS", + "IE", + "IT", + "LV", + "LT", + "LU", + "MY", + "MT", + "MX", + "NL", + "NZ", + "NI", + "NO", + "PA", + "PY", + "PE", + "PH", + "PL", + "PT", + "SG", + "SK", + "ES", + "SE", + "CH", + "TW", + "TR", + "UY", + "US", + "GB", + "AD", + "LI", + "MC", + "ID", + "JP", + "TH", + "VN", + "RO", + "IL", + "ZA", + "SA", + "AE", + "BH", + "QA", + "OM", + "KW", + "EG", + "MA", + "DZ", + "TN", + "LB", + "JO", + "PS", + "IN", + "BY", + "KZ", + "MD", + "UA", + "AL", + "BA", + "HR", + "ME", + "MK", + "RS", + "SI", + "KR", + "BD", + "PK", + "LK", + "GH", + "KE", + "NG", + "TZ", + "UG", + "AG", + "AM", + "BS", + "BB", + "BZ", + "BT", + "BW", + "BF", + "CV", + "CW", + "DM", + "FJ", + "GM", + "GE", + "GD", + "GW", + "GY", + "HT", + "JM", + "KI", + "LS", + "LR", + "MW", + "MV", + "ML", + "MH", + "FM", + "NA", + "NR", + "NE", + "PW", + "PG", + "PR", + "WS", + "SM", + "ST", + "SN", + "SC", + "SL", + "SB", + "KN", + "LC", + "VC", + "SR", + "TL", + "TO", + "TT", + "TV", + "VU", + "AZ", + "BN", + "BI", + "KH", + "CM", + "TD", + "KM", + "GQ", + "SZ", + "GA", + "GN", + "KG", + "LA", + "MO", + "MR", + "MN", + "NP", + "RW", + "TG", + "UZ", + "ZW", + "BJ", + "MG", + "MU", + "MZ", + "AO", + "CI", + "DJ", + "ZM", + "CD", + "CG", + "IQ", + "LY", + "TJ", + "VE", + "ET", + "XK" + ], + "external_urls": { + "spotify": "https://open.spotify.com/album/4r9HEszOAbfhRfT1n1QPLF" + }, + "href": "https://api.spotify.com/v1/albums/4r9HEszOAbfhRfT1n1QPLF", + "id": "4r9HEszOAbfhRfT1n1QPLF", + "images": [ + { + "height": 640, + "url": "https://i.scdn.co/image/ab67616d0000b273ba89e34c27a792ffe9c8dcb2", + "width": 640 + }, + { + "height": 300, + "url": "https://i.scdn.co/image/ab67616d00001e02ba89e34c27a792ffe9c8dcb2", + "width": 300 + }, + { + "height": 64, + "url": "https://i.scdn.co/image/ab67616d00004851ba89e34c27a792ffe9c8dcb2", + "width": 64 + } + ], + "name": "Bloodbags And Downtube Shifters", + "release_date": "2020-08-28", + "release_date_precision": "day", + "total_tracks": 13, + "type": "album", + "uri": "spotify:album:4r9HEszOAbfhRfT1n1QPLF" + }, + "artists": [ + { + "external_urls": { + "spotify": "https://open.spotify.com/artist/4D2G48IdJKhcdZ5c1dqp5Z" + }, + "href": "https://api.spotify.com/v1/artists/4D2G48IdJKhcdZ5c1dqp5Z", + "id": "4D2G48IdJKhcdZ5c1dqp5Z", + "name": "Dubmood", + "type": "artist", + "uri": "spotify:artist:4D2G48IdJKhcdZ5c1dqp5Z" + }, + { + "external_urls": { + "spotify": "https://open.spotify.com/artist/77s5NAGQbxu8oLstaqSwHE" + }, + "href": "https://api.spotify.com/v1/artists/77s5NAGQbxu8oLstaqSwHE", + "id": "77s5NAGQbxu8oLstaqSwHE", + "name": "MASTER BOOT RECORD", + "type": "artist", + "uri": "spotify:artist:77s5NAGQbxu8oLstaqSwHE" + } + ], + "available_markets": [ + "AR", + "AU", + "AT", + "BE", + "BO", + "BR", + "BG", + "CA", + "CL", + "CO", + "CR", + "CY", + "CZ", + "DK", + "DO", + "DE", + "EC", + "EE", + "SV", + "FI", + "FR", + "GR", + "GT", + "HN", + "HK", + "HU", + "IS", + "IE", + "IT", + "LV", + "LT", + "LU", + "MY", + "MT", + "MX", + "NL", + "NZ", + "NI", + "NO", + "PA", + "PY", + "PE", + "PH", + "PL", + "PT", + "SG", + "SK", + "ES", + "SE", + "CH", + "TW", + "TR", + "UY", + "US", + "GB", + "AD", + "LI", + "MC", + "ID", + "JP", + "TH", + "VN", + "RO", + "IL", + "ZA", + "SA", + "AE", + "BH", + "QA", + "OM", + "KW", + "EG", + "MA", + "DZ", + "TN", + "LB", + "JO", + "PS", + "IN", + "BY", + "KZ", + "MD", + "UA", + "AL", + "BA", + "HR", + "ME", + "MK", + "RS", + "SI", + "KR", + "BD", + "PK", + "LK", + "GH", + "KE", + "NG", + "TZ", + "UG", + "AG", + "AM", + "BS", + "BB", + "BZ", + "BT", + "BW", + "BF", + "CV", + "CW", + "DM", + "FJ", + "GM", + "GE", + "GD", + "GW", + "GY", + "HT", + "JM", + "KI", + "LS", + "LR", + "MW", + "MV", + "ML", + "MH", + "FM", + "NA", + "NR", + "NE", + "PW", + "PG", + "PR", + "WS", + "SM", + "ST", + "SN", + "SC", + "SL", + "SB", + "KN", + "LC", + "VC", + "SR", + "TL", + "TO", + "TT", + "TV", + "VU", + "AZ", + "BN", + "BI", + "KH", + "CM", + "TD", + "KM", + "GQ", + "SZ", + "GA", + "GN", + "KG", + "LA", + "MO", + "MR", + "MN", + "NP", + "RW", + "TG", + "UZ", + "ZW", + "BJ", + "MG", + "MU", + "MZ", + "AO", + "CI", + "DJ", + "ZM", + "CD", + "CG", + "IQ", + "LY", + "TJ", + "VE", + "ET", + "XK" + ], + "disc_number": 1, + "duration_ms": 213539, + "explicit": false, + "external_ids": { + "isrc": "FR9W12915571" + }, + "external_urls": { + "spotify": "https://open.spotify.com/track/0dYovyBKizxbMla12ofzBr" + }, + "href": "https://api.spotify.com/v1/tracks/0dYovyBKizxbMla12ofzBr", + "id": "0dYovyBKizxbMla12ofzBr", + "is_local": false, + "name": "The Sandpits Of Zonhoven", + "popularity": 15, + "preview_url": null, + "track_number": 5, + "type": "track", + "uri": "spotify:track:0dYovyBKizxbMla12ofzBr" + }, + "currently_playing_type": "track", + "actions": { + "disallows": { + "resuming": true + } + }, + "is_playing": true +} \ No newline at end of file diff --git a/src/backend/tests/source/source.test.ts b/src/backend/tests/source/source.test.ts index 6058897b..f2ae8d84 100644 --- a/src/backend/tests/source/source.test.ts +++ b/src/backend/tests/source/source.test.ts @@ -4,9 +4,12 @@ import asPromised from 'chai-as-promised'; import EventEmitter from "events"; import { after, before, describe, it } from 'mocha'; import pEvent from "p-event"; +import clone from 'clone'; import { PlayObject } from "../../../core/Atomic.js"; import { generatePlay } from "../utils/PlayTestUtils.js"; import { TestSource } from "./TestSource.js"; +import spotifyPayload from '../plays/spotifyCurrentPlaybackState.json'; +import SpotifySource from "../../sources/SpotifySource.js"; chai.use(asPromised); @@ -125,3 +128,35 @@ describe('Sources use transform plays correctly', function () { expect(source.discover([newScrobble]).length).is.eq(1); }); }) + + +describe('Sources correctly parse incoming payloads', function () { + + it('Spotify parses payload with no album artists correctly', function() { + const noAAPayload = clone(spotifyPayload) + noAAPayload.item.album.artists = []; + const play = SpotifySource.formatPlayObj(noAAPayload as SpotifyApi.CurrentPlaybackResponse); + expect(play.data.track).eq('The Sandpits Of Zonhoven'); + expect(play.data.album).eq('Bloodbags And Downtube Shifters'); + expect(play.data.artists).eql(['Dubmood', 'MASTER BOOT RECORD']); + expect(play.data.albumArtists).to.be.empty; + }); + + it('Spotify parses payload with different album artists correctly', function() { + const play = SpotifySource.formatPlayObj(spotifyPayload as SpotifyApi.CurrentPlaybackResponse); + expect(play.data.track).eq('The Sandpits Of Zonhoven'); + expect(play.data.album).eq('Bloodbags And Downtube Shifters'); + expect(play.data.artists).eql(['Dubmood', 'MASTER BOOT RECORD']); + expect(play.data.albumArtists).eql(['Dubmood']); + }); + + it('Spotify parses payload with identical album artists correctly', function() { + const identicalArtistsPayload = clone(spotifyPayload) + identicalArtistsPayload.item.album.artists = identicalArtistsPayload.item.artists; + const identicalArtistsPlay = SpotifySource.formatPlayObj(identicalArtistsPayload as SpotifyApi.CurrentPlaybackResponse); + expect(identicalArtistsPlay.data.track).eq('The Sandpits Of Zonhoven'); + expect(identicalArtistsPlay.data.album).eq('Bloodbags And Downtube Shifters'); + expect(identicalArtistsPlay.data.artists).eql(['Dubmood', 'MASTER BOOT RECORD']); + expect(identicalArtistsPlay.data.albumArtists).to.be.empty; + }); +}); \ No newline at end of file