scotty/internal/backends/spotify/spotify_test.go
2025-04-03 15:08:02 +02:00

82 lines
3 KiB
Go

/*
Copyright © 2023 Philipp Wolfer <phw@uploadedlobster.com>
This file is part of Scotty.
Scotty is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later version.
Scotty is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
Scotty. If not, see <https://www.gnu.org/licenses/>.
*/
package spotify_test
import (
_ "embed"
"encoding/json"
"testing"
"time"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uploadedlobster.com/mbtypes"
"go.uploadedlobster.com/scotty/internal/backends/spotify"
"go.uploadedlobster.com/scotty/internal/config"
)
var (
//go:embed testdata/listen.json
testListen []byte
//go:embed testdata/track.json
testTrack []byte
)
func TestFromConfig(t *testing.T) {
c := viper.New()
c.Set("client-id", "someclientid")
c.Set("client-secret", "someclientsecret")
service := config.NewServiceConfig("test", c)
backend := (&spotify.SpotifyApiBackend{}).FromConfig(&service)
assert.IsType(t, &spotify.SpotifyApiBackend{}, backend)
}
func TestSpotifyListenAsListen(t *testing.T) {
spListen := spotify.Listen{}
err := json.Unmarshal(testListen, &spListen)
require.NoError(t, err)
listen := spListen.AsListen()
listenedAt, _ := time.Parse(time.RFC3339, "2023-11-21T15:24:33.361Z")
assert.Equal(t, listenedAt, listen.ListenedAt)
assert.Equal(t, time.Duration(413826*time.Millisecond), listen.Duration)
assert.Equal(t, "Oweynagat", listen.TrackName)
assert.Equal(t, "Here Now, There Then", listen.ReleaseName)
assert.Equal(t, []string{"Dool"}, listen.ArtistNames)
assert.Equal(t, 5, listen.TrackNumber)
assert.Equal(t, 1, listen.DiscNumber)
assert.Equal(t, mbtypes.ISRC("DES561620801"), listen.ISRC)
info := listen.AdditionalInfo
assert.Equal(t, "spotify.com", info["music_service"])
assert.Equal(t, "https://open.spotify.com/track/2JKUgGuXK3dEvyuIJ4Yj2V", info["origin_url"])
assert.Equal(t, "https://open.spotify.com/track/2JKUgGuXK3dEvyuIJ4Yj2V", info["spotify_id"])
assert.Equal(t, "https://open.spotify.com/album/5U1umzRH4EONHWsFgPtRbA", info["spotify_album_id"])
assert.Equal(t, []string{"https://open.spotify.com/artist/101HSR6JTJqe3DBh6rb8kz"}, info["spotify_artist_ids"])
assert.Equal(t, []string{"https://open.spotify.com/artist/101HSR6JTJqe3DBh6rb8kz"}, info["spotify_album_artist_ids"])
}
func TestSavedTrackAsLove(t *testing.T) {
track := spotify.SavedTrack{}
err := json.Unmarshal(testTrack, &track)
require.NoError(t, err)
love := track.AsLove()
created, _ := time.Parse(time.RFC3339, "2022-02-13T21:46:08Z")
assert.Equal(t, created, love.Created)
assert.Equal(t, time.Duration(187680*time.Millisecond), love.Duration)
assert.Equal(t, "Death to the Holy", love.TrackName)
}