Files
pipu-music/services/api/internal/genius/metadata_test.go
T
mrleo1nidandClaude Sonnet 5.5 139dd226fa
ci / api / api (push) Successful in 27s
ci / db / db (push) Successful in 53s
feat(genius): fetch artist avatars by name and match names with transliteration suffix
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-28 13:50:55 +03:00

120 lines
4.8 KiB
Go

package genius
import (
"context"
"io"
"net/http"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func TestMatchSong(t *testing.T) {
songs := []Song{
{ID: 1, Title: "Alright (Live)", Artist: Artist{Name: "Kendrick Lamar"}},
{ID: 2, Title: "Alright!", Artist: Artist{Name: "Kendrick Lamar"}},
{ID: 3, Title: "Alright", Artist: Artist{Name: "Someone Else"}},
}
got, ok := matchSong(songs, "kendrick-lamar", "Alright")
require.True(t, ok)
require.Equal(t, int64(2), got.ID)
_, ok = matchSong(append(songs, Song{ID: 4, Title: "Alright", Artist: Artist{Name: "Kendrick Lamar"}}), "Kendrick Lamar", "Alright")
require.False(t, ok)
_, ok = matchSong(songs, "Kendrick Lamar", "Alright Remix")
require.False(t, ok)
}
func TestImageURL(t *testing.T) {
require.Equal(t, "https://images.genius.com/cover.jpg", imageURL("https://images.genius.com/cover.jpg"))
require.Equal(t, "https://images.rapgenius.com/a.png", imageURL("https://images.rapgenius.com/a.png"))
for _, raw := range []string{
"http://images.genius.com/a.jpg",
"https://evil.test/a.jpg",
"https://images.genius.com/default_avatar.png",
"https://user@images.genius.com/a.jpg",
"",
} {
require.Empty(t, imageURL(raw))
}
song := Song{
ImageURL: "https://images.genius.com/default_cover_image.png",
ThumbURL: "https://images.genius.com/song.jpg",
Artist: Artist{ImageURL: "https://images.genius.com/artist.jpg"},
}
song.Album.ImageURL = "https://images.genius.com/album.jpg"
songImage, artistImage, albumImage := songArtwork(song)
require.Equal(t, "https://images.genius.com/song.jpg", songImage)
require.Equal(t, "https://images.genius.com/artist.jpg", artistImage)
require.Equal(t, "https://images.genius.com/album.jpg", albumImage)
}
func TestSongAndArtist(t *testing.T) {
c := New("secret")
c.http.Transport = transportFunc(func(r *http.Request) (*http.Response, error) {
require.Equal(t, "Bearer secret", r.Header.Get("Authorization"))
body := `{"response":{"artist":{"id":9,"name":"Artist","image_url":"https://images.genius.com/artist.jpg"}}}`
if strings.Contains(r.URL.Path, "/songs/") {
require.Equal(t, "plain", r.URL.Query().Get("text_format"))
body = `{"response":{"song":{"id":12,"title":"Example","url":"https://genius.com/example-lyrics","song_art_image_url":"https://images.genius.com/song.jpg","primary_artist":{"id":9,"name":"Artist"},"album":{"name":"Album","cover_art_url":"https://images.genius.com/album.jpg"},"description":{"plain":"About the song"}}}}`
}
return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(body)), Header: make(http.Header)}, nil
})
song, err := c.Song(context.Background(), 12)
require.NoError(t, err)
require.Equal(t, "About the song", song.Description.Plain)
require.Equal(t, "Album", song.Album.Name)
artist, err := c.Artist(context.Background(), 9)
require.NoError(t, err)
require.Equal(t, "https://images.genius.com/artist.jpg", artist.ImageURL)
_, err = c.Song(context.Background(), 0)
require.Error(t, err)
}
func TestAnnotations(t *testing.T) {
c := New("secret")
pages := 0
one := `{"id":1,"fragment":"line","annotations":[{"body":{"plain":"means this"},"url":"https://genius.com/annotations/1","verified":true,"authors":[{"user":{"name":"Editor"}}]}]}`
full := `{"response":{"referents":[` + strings.TrimRight(strings.Repeat(one+",", 50), ",") + `]}}`
c.http.Transport = transportFunc(func(r *http.Request) (*http.Response, error) {
pages++
require.Equal(t, "plain", r.URL.Query().Get("text_format"))
body := full
if pages > 1 {
body = `{"response":{"referents":[` + one + `]}}`
}
return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(body)), Header: make(http.Header)}, nil
})
notes, err := c.Annotations(context.Background(), 12)
require.NoError(t, err)
require.Equal(t, 2, pages)
require.Len(t, notes, 50)
require.Equal(t, "line", notes[0].Fragment)
require.Equal(t, "means this", notes[0].Text)
require.Equal(t, "Editor", notes[0].Authors)
require.True(t, notes[0].Verified)
require.Equal(t, "https://genius.com/annotations/1", notes[0].Url)
}
type fakeArtists struct {
songs []Song
artist Artist
}
func (f fakeArtists) Search(context.Context, string) ([]Song, error) { return f.songs, nil }
func (f fakeArtists) Artist(context.Context, int64) (Artist, error) { return f.artist, nil }
func TestArtistImage(t *testing.T) {
ctx := context.Background()
src := fakeArtists{songs: []Song{
{Artist: Artist{ID: 1, Name: "Other", ImageURL: "https://images.genius.com/other.jpg"}},
{Artist: Artist{ID: 2, Name: "Некич (Nekich)"}},
}, artist: Artist{ID: 2, ImageURL: "https://images.genius.com/nekich.jpg"}}
img, err := artistImage(ctx, src, "некич")
require.NoError(t, err)
require.Equal(t, "https://images.genius.com/nekich.jpg", img)
img, err = artistImage(ctx, src, "Unknown")
require.NoError(t, err)
require.Empty(t, img)
}