sonda/internal/spotifyimport/match_test.go
tgiachi c08a234159 feat(import): bring my Spotify history and playlists in
I want my listening history and my playlists from Spotify in Sonda, so that Mixes, Discover and the smart playlists know what I listened to before.

- POST /me/import/spotify takes the zip of a Spotify data export, or the JSON files in it (extended streaming history, the older StreamingHistory files, the playlists), as a multipart form. A request may be up to 512 MB; the zip is limited in how much it may inflate and how many files it may hold, and only the history and playlist files are opened.
- Everything is read on the server and nothing is sent anywhere. The files are not kept, and of each play only the song, artist, album, time and how it ended are read: never the IP addresses, the country or the platform.
- A play is matched to a song of the library by artist and title (no case, accents or punctuation; a "Remastered" note on either side; any credited artist; a compilation by its album). A match is recorded as a listen at the time of the play: complete, skip or play. Nothing is posted to Mastodon.
- Importing the same files again records nothing twice, and the same play in the two shapes of history file is counted once. One import per person at a time.
- A playlist becomes a playlist of mine with the songs that were found; one with a name I already have is left alone, and one with none of its songs found is not made.
- The report counts what was added, what was already there and what is not in the library, and lists the missing artists, most played first.
- Settings gets an Import from Spotify section; the API client can send files.
- English and Italian.
2026-11-14 16:25:58 +01:00

106 lines
4.3 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package spotifyimport
import (
"strings"
"testing"
)
// key is a stand-in for the key the server uses to compare names: lower case, no accents, no punctuation.
func key(s string) string {
r := strings.NewReplacer("ö", "o", "é", "e", ".", "", ",", "", "'", "", "’", "", "!", "", "-", " ")
return strings.Join(strings.Fields(r.Replace(strings.ToLower(s))), " ")
}
func library() *Index {
return NewIndex(key, []Candidate{
{TrackID: 1, Title: "Karma Police", Album: "OK Computer", Artists: []string{"Radiohead"}},
{TrackID: 2, Title: "Karma Police", Album: "OK Computer OKNOTOK", Artists: []string{"Radiohead"}},
{TrackID: 3, Title: "Hyperballad", Album: "Post", Artists: []string{"Björk"}},
{TrackID: 4, Title: "Heroes (2017 Remaster)", Album: "Heroes", Artists: []string{"David Bowie"}},
{TrackID: 5, Title: "Love Will Tear Us Apart", Album: "Substance", Artists: []string{"Joy Division"}},
{TrackID: 6, Title: "Under Pressure", Album: "Hot Space", Artists: []string{"Queen", "David Bowie"}},
{TrackID: 7, Title: "Paranoid Android", Album: "Now That's What I Call Music", Artists: []string{"Radiohead"}},
{TrackID: 8, Title: "Wonderwall", Album: "Greatest Hits", Artists: []string{"Oasis"}},
})
}
func TestAnExactArtistAndTitleMatches(t *testing.T) {
ix := library()
for _, tc := range []struct {
artist, title, album string
want int64
}{
{"Radiohead", "Karma Police", "OK Computer", 1},
{"radiohead", "KARMA POLICE", "", 1}, // case, and no album to tell the two apart: the first
{"Bjork", "Hyperballad", "Post", 3}, // accents do not matter
{"Joy Division", "Love Will Tear Us Apart!", "", 5},
} {
if id, ok := ix.Find(tc.artist, tc.title, tc.album); !ok || id != tc.want {
t.Errorf("Find(%q, %q, %q) = %d, %v, want %d", tc.artist, tc.title, tc.album, id, ok, tc.want)
}
}
}
func TestTheAlbumTellsTwoVersionsApart(t *testing.T) {
if id, ok := library().Find("Radiohead", "Karma Police", "OK Computer OKNOTOK"); !ok || id != 2 {
t.Errorf("= %d, %v, want the song of the album that was asked", id, ok)
}
}
func TestARemasterSuffixDoesNotMatter(t *testing.T) {
ix := library()
for _, title := range []string{"Heroes - 2017 Remaster", "Heroes (Remastered 2017)", "Heroes - Remastered", "Heroes"} {
if id, ok := ix.Find("David Bowie", title, ""); !ok || id != 4 {
t.Errorf("Find(%q) = %d, %v, want 4", title, id, ok)
}
}
// The other way round: the library has the plain title and Spotify the remaster.
if id, ok := ix.Find("Queen", "Under Pressure - Remastered 2011", ""); !ok || id != 6 {
t.Errorf("a remastered title for a plain one = %d, %v, want 6", id, ok)
}
}
func TestAnyOfTheCreditedArtistsMatches(t *testing.T) {
ix := library()
for _, artist := range []string{"Queen", "David Bowie"} {
if id, ok := ix.Find(artist, "Under Pressure", ""); !ok || id != 6 {
t.Errorf("Find(%q) = %d, %v, want 6", artist, id, ok)
}
}
}
func TestACompilationIsFoundByItsAlbumWhenTheArtistIsVariousArtists(t *testing.T) {
if id, ok := library().Find("Various Artists", "Paranoid Android", "Now That's What I Call Music"); !ok || id != 7 {
t.Errorf("= %d, %v, want 7", id, ok)
}
// ...but not by the album alone when it does not hold the song.
if _, ok := library().Find("Various Artists", "Something Else", "Now That's What I Call Music"); ok {
t.Error("a song that the album does not hold was found")
}
}
func TestWhatIsNotInTheLibraryIsNotFound(t *testing.T) {
ix := library()
for _, tc := range [][2]string{{"Radiohead", "Creep"}, {"Blur", "Karma Police"}, {"", "Karma Police"}, {"Radiohead", ""}} {
if id, ok := ix.Find(tc[0], tc[1], ""); ok {
t.Errorf("Find(%q, %q) = %d, want nothing", tc[0], tc[1], id)
}
}
}
func TestTheBaseOfATitle(t *testing.T) {
for in, want := range map[string]string{
"Heroes - 2017 Remaster": "Heroes",
"Heroes - Remastered 2017": "Heroes",
"Heroes (Remastered)": "Heroes",
"Heroes [2017 Remaster]": "Heroes",
"Heroes - Mono": "Heroes - Mono", // only a remaster is taken off
"Heroes (Live)": "Heroes (Live)",
"The Remaster of Remasters": "The Remaster of Remasters",
"Song (feat. X) - 2011 Remaster": "Song (feat. X)",
} {
if got := baseTitle(in); got != want {
t.Errorf("baseTitle(%q) = %q, want %q", in, got, want)
}
}
}