Migrating hosting from Codeberg to git.kmalloc.org. Renames the module path in go.mod, every internal import, and the OCI image source label in the Dockerfile. Build and full test suite verified green after the rename.
356 lines
12 KiB
Go
356 lines
12 KiB
Go
package images
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"slices"
|
|
"testing"
|
|
|
|
"git.kmalloc.org/squid/sonda/internal/providers"
|
|
"git.kmalloc.org/squid/sonda/internal/providers/coverart"
|
|
"git.kmalloc.org/squid/sonda/internal/providers/musicbrainz"
|
|
)
|
|
|
|
const releaseGroup = "b1392450-e666-3926-a536-22c65f834433"
|
|
|
|
type albumsFake struct {
|
|
ids map[int64]string
|
|
names map[int64][2]string // artist, title
|
|
}
|
|
|
|
func (f albumsFake) AlbumNames(_ context.Context, id int64) (string, string, error) {
|
|
if _, ok := f.ids[id]; !ok {
|
|
return "", "", ErrAlbumNotFound
|
|
}
|
|
n := f.names[id]
|
|
return n[0], n[1], nil
|
|
}
|
|
|
|
func (f albumsFake) AlbumMBID(_ context.Context, id int64) (string, error) {
|
|
m, ok := f.ids[id]
|
|
if !ok {
|
|
return "", ErrAlbumNotFound
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
type coverFake struct {
|
|
data []byte
|
|
err error
|
|
calls int
|
|
got string
|
|
hook func()
|
|
}
|
|
|
|
func (f *coverFake) Front(_ context.Context, id string) ([]byte, error) {
|
|
f.calls++
|
|
f.got = id
|
|
if f.hook != nil {
|
|
f.hook()
|
|
}
|
|
return f.data, f.err
|
|
}
|
|
|
|
type archive struct {
|
|
src *CoverArtSource
|
|
sw *switchesFake
|
|
cv *coverFake
|
|
}
|
|
|
|
func newArchive() *archive {
|
|
a := &archive{
|
|
sw: &switchesFake{on: map[string]bool{providers.CoverArtArchive: true}},
|
|
cv: &coverFake{data: []byte("cover bytes")},
|
|
}
|
|
a.src = &CoverArtSource{Albums: albumsFake{ids: map[int64]string{1: releaseGroup, 2: ""}}, Settings: a.sw, Client: a.cv}
|
|
return a
|
|
}
|
|
|
|
func TestTheArchiveGivesTheCoverByReleaseGroup(t *testing.T) {
|
|
a := newArchive()
|
|
got, err := a.src.AlbumImage(context.Background(), 1)
|
|
if err != nil || string(got.Data) != "cover bytes" || got.Credit != (Credit{}) {
|
|
t.Fatalf("got %+v err %v", got, err)
|
|
}
|
|
if a.cv.got != releaseGroup {
|
|
t.Errorf("asked for %q, want only the release group id", a.cv.got)
|
|
}
|
|
}
|
|
|
|
func TestNothingIsAskedWhenTheProviderIsOffOrTheAlbumHasNoId(t *testing.T) {
|
|
for name, tt := range map[string]struct {
|
|
prepare func(*archive)
|
|
id int64
|
|
want error
|
|
}{
|
|
"provider off": {func(a *archive) { a.sw.set(providers.CoverArtArchive, false) }, 1, ErrProviderDisabled},
|
|
"no id": {func(*archive) {}, 2, ErrNoID},
|
|
"unknown album": {func(*archive) {}, 99, ErrAlbumNotFound},
|
|
} {
|
|
a := newArchive()
|
|
tt.prepare(a)
|
|
if _, err := a.src.AlbumImage(context.Background(), tt.id); !errors.Is(err, tt.want) {
|
|
t.Errorf("%s: err = %v, want %v", name, err, tt.want)
|
|
}
|
|
if a.cv.calls != 0 {
|
|
t.Errorf("%s: the archive was asked", name)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestWhereTheArchiveHasNoCoverTheAnswerIsNoImage(t *testing.T) {
|
|
for name, why := range map[string]error{
|
|
"no cover": coverart.ErrNotFound, "bad id": coverart.ErrBadRequest, "unusable": coverart.ErrUnusable,
|
|
} {
|
|
a := newArchive()
|
|
a.cv.err = why
|
|
if _, err := a.src.AlbumImage(context.Background(), 1); !errors.Is(err, ErrNoImage) {
|
|
t.Errorf("%s: err = %v, want ErrNoImage", name, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAnArchiveFailureIsAnOutageEvenWhenItEndsInADeadline(t *testing.T) {
|
|
for name, why := range map[string]error{
|
|
"unavailable": coverart.ErrUnavailable,
|
|
"rate limited": coverart.ErrRateLimited,
|
|
"a deadline": fmt.Errorf("%w: %w", coverart.ErrUnavailable, context.DeadlineExceeded),
|
|
} {
|
|
a := newArchive()
|
|
a.cv.err = why
|
|
if _, err := a.src.AlbumImage(context.Background(), 1); !errors.Is(err, ErrSourceUnavailable) {
|
|
t.Errorf("%s: err = %v, want ErrSourceUnavailable", name, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSwitchedOffWhileTheCoverWasComingNothingIsReturned(t *testing.T) {
|
|
a := newArchive()
|
|
a.cv.hook = func() { a.sw.set(providers.CoverArtArchive, false) }
|
|
if _, err := a.src.AlbumImage(context.Background(), 1); !errors.Is(err, ErrProviderDisabled) {
|
|
t.Errorf("err = %v, want ErrProviderDisabled", err)
|
|
}
|
|
}
|
|
|
|
func TestACallerWhoLeavesGetsTheCancellation(t *testing.T) {
|
|
a := newArchive()
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
a.cv.hook = cancel
|
|
a.cv.err = context.Canceled
|
|
if _, err := a.src.AlbumImage(ctx, 1); !errors.Is(err, context.Canceled) || errors.Is(err, ErrSourceUnavailable) {
|
|
t.Errorf("err = %v", err)
|
|
}
|
|
}
|
|
|
|
// ---- albums without an id: looked up in MusicBrainz by the artist and the title ----
|
|
|
|
type finderFake struct {
|
|
id string
|
|
answers map[string]string // by title: when set, a title it does not list has no answer
|
|
err error
|
|
calls int
|
|
artist string
|
|
title string
|
|
titles []string
|
|
}
|
|
|
|
func (f *finderFake) FindReleaseGroup(_ context.Context, artist, title string) (string, error) {
|
|
f.calls++
|
|
f.artist, f.title = artist, title
|
|
f.titles = append(f.titles, title)
|
|
if f.err != nil {
|
|
return "", f.err
|
|
}
|
|
if f.answers != nil {
|
|
return f.answers[title], nil
|
|
}
|
|
return f.id, nil
|
|
}
|
|
|
|
// searching is an archive whose source may look an album up: album 2 has no id and a name,
|
|
// album 3 has no id and no artist.
|
|
func searching() (*archive, *finderFake) {
|
|
a := newArchive()
|
|
a.sw.set(providers.MusicBrainz, true)
|
|
f := &finderFake{id: releaseGroup}
|
|
a.src.Albums = albumsFake{
|
|
ids: map[int64]string{1: releaseGroup, 2: "", 3: "", 4: "", 5: ""},
|
|
names: map[int64][2]string{
|
|
1: {"Radiohead", "The Bends"}, 2: {"Radiohead", "OK Computer"}, 3: {"", "Untitled"},
|
|
4: {"Aerosmith", "Alive II (disc 2)"}, 5: {"Pearl Jam", "Ten"},
|
|
},
|
|
}
|
|
a.src.Finder = f
|
|
return a, f
|
|
}
|
|
|
|
func TestAnAlbumWithNoIdIsLookedUpByArtistAndTitle(t *testing.T) {
|
|
a, f := searching()
|
|
got, err := a.src.AlbumImage(context.Background(), 2)
|
|
if err != nil || string(got.Data) != "cover bytes" {
|
|
t.Fatalf("got %+v err %v", got, err)
|
|
}
|
|
if f.artist != "Radiohead" || f.title != "OK Computer" {
|
|
t.Errorf("looked up %q / %q", f.artist, f.title)
|
|
}
|
|
if a.cv.got != releaseGroup {
|
|
t.Errorf("the archive was asked for %q, want the id that was found", a.cv.got)
|
|
}
|
|
}
|
|
|
|
func TestNothingIsLookedUpWhenTheIdIsKnownOrAProviderIsOffOrTheArtistIsMissing(t *testing.T) {
|
|
for name, tt := range map[string]struct {
|
|
prepare func(*archive)
|
|
id int64
|
|
want error
|
|
}{
|
|
"the files have the id": {func(*archive) {}, 1, nil},
|
|
"musicbrainz is off": {func(a *archive) { a.sw.set(providers.MusicBrainz, false) }, 2, ErrNoID},
|
|
"the archive is off": {func(a *archive) { a.sw.set(providers.CoverArtArchive, false) }, 2, ErrProviderDisabled},
|
|
"the album has no artist": {func(*archive) {}, 3, ErrNoID},
|
|
"the album does not exist": {func(*archive) {}, 99, ErrAlbumNotFound},
|
|
} {
|
|
a, f := searching()
|
|
tt.prepare(a)
|
|
if _, err := a.src.AlbumImage(context.Background(), tt.id); !errors.Is(err, tt.want) {
|
|
t.Errorf("%s: err = %v, want %v", name, err, tt.want)
|
|
}
|
|
if f.calls != 0 {
|
|
t.Errorf("%s: MusicBrainz was asked", name)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestWithoutAFinderTheAlbumIsNeverLookedUp(t *testing.T) {
|
|
a, _ := searching()
|
|
a.src.Finder = nil
|
|
if _, err := a.src.AlbumImage(context.Background(), 2); !errors.Is(err, ErrNoID) {
|
|
t.Errorf("err = %v, want ErrNoID", err)
|
|
}
|
|
}
|
|
|
|
func TestWhenNoResultIsSureThereIsNoCover(t *testing.T) {
|
|
a, f := searching()
|
|
f.id = ""
|
|
if _, err := a.src.AlbumImage(context.Background(), 2); !errors.Is(err, ErrNoImage) {
|
|
t.Errorf("err = %v, want ErrNoImage", err)
|
|
}
|
|
if a.cv.calls != 0 {
|
|
t.Error("the archive was asked without an id")
|
|
}
|
|
}
|
|
|
|
func TestAMusicBrainzFailureIsAnOutage(t *testing.T) {
|
|
for name, why := range map[string]error{
|
|
"unavailable": musicbrainz.ErrUnavailable,
|
|
"rate limited": musicbrainz.ErrRateLimited,
|
|
"unreadable": musicbrainz.ErrBadResponse,
|
|
"a deadline": fmt.Errorf("%w: %w", musicbrainz.ErrUnavailable, context.DeadlineExceeded),
|
|
} {
|
|
a, f := searching()
|
|
f.err = why
|
|
if _, err := a.src.AlbumImage(context.Background(), 2); !errors.Is(err, ErrSourceUnavailable) {
|
|
t.Errorf("%s: err = %v, want ErrSourceUnavailable", name, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestACallerWhoLeavesDuringTheLookupGetsTheCancellation(t *testing.T) {
|
|
a, f := searching()
|
|
f.err = context.Canceled
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
if _, err := a.src.AlbumImage(ctx, 2); !errors.Is(err, context.Canceled) || errors.Is(err, ErrSourceUnavailable) {
|
|
t.Errorf("err = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestSwitchedOffDuringTheLookupNothingIsReturned(t *testing.T) {
|
|
a, _ := searching()
|
|
a.cv.hook = func() { a.sw.set(providers.CoverArtArchive, false) }
|
|
if _, err := a.src.AlbumImage(context.Background(), 2); !errors.Is(err, ErrProviderDisabled) {
|
|
t.Errorf("err = %v, want ErrProviderDisabled", err)
|
|
}
|
|
}
|
|
|
|
func TestADiscOrEditionSuffixIsTriedWithoutItWhenTheFullTitleHasNoSureResult(t *testing.T) {
|
|
a, f := searching()
|
|
f.answers = map[string]string{"Alive II": releaseGroup}
|
|
got, err := a.src.AlbumImage(context.Background(), 4)
|
|
if err != nil || string(got.Data) != "cover bytes" {
|
|
t.Fatalf("got %+v err %v", got, err)
|
|
}
|
|
if want := []string{"Alive II (disc 2)", "Alive II"}; !slices.Equal(f.titles, want) {
|
|
t.Errorf("titles asked = %q, want %q", f.titles, want)
|
|
}
|
|
if a.cv.got != releaseGroup {
|
|
t.Errorf("the archive was asked for %q", a.cv.got)
|
|
}
|
|
}
|
|
|
|
func TestTheFullTitleComesFirstAndIsEnoughWhenItIsSure(t *testing.T) {
|
|
a, f := searching()
|
|
f.answers = map[string]string{"Alive II (disc 2)": releaseGroup, "Alive II": "cccccccc-1111-4222-8333-444444444444"}
|
|
if _, err := a.src.AlbumImage(context.Background(), 4); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if want := []string{"Alive II (disc 2)"}; !slices.Equal(f.titles, want) {
|
|
t.Errorf("titles asked = %q, want only the full one", f.titles)
|
|
}
|
|
if a.cv.got != releaseGroup {
|
|
t.Errorf("the archive was asked for %q", a.cv.got)
|
|
}
|
|
}
|
|
|
|
func TestATitleWithoutASuffixIsAskedOnce(t *testing.T) {
|
|
a, f := searching()
|
|
f.id = ""
|
|
if _, err := a.src.AlbumImage(context.Background(), 5); !errors.Is(err, ErrNoImage) {
|
|
t.Errorf("err = %v, want ErrNoImage", err)
|
|
}
|
|
if f.calls != 1 {
|
|
t.Errorf("MusicBrainz was asked %d times, want once", f.calls)
|
|
}
|
|
}
|
|
|
|
func TestWhenNeitherTitleHasASureResultThereIsNoCoverAndAFailureStops(t *testing.T) {
|
|
a, f := searching()
|
|
f.id = ""
|
|
if _, err := a.src.AlbumImage(context.Background(), 4); !errors.Is(err, ErrNoImage) || f.calls != 2 {
|
|
t.Errorf("err = %v after %d calls, want ErrNoImage after 2", err, f.calls)
|
|
}
|
|
a, f = searching()
|
|
f.err = musicbrainz.ErrUnavailable
|
|
if _, err := a.src.AlbumImage(context.Background(), 4); !errors.Is(err, ErrSourceUnavailable) || f.calls != 1 {
|
|
t.Errorf("err = %v after %d calls, want an outage after 1", err, f.calls)
|
|
}
|
|
}
|
|
|
|
func TestBaseTitleTakesOffTheDiscAndEditionNotes(t *testing.T) {
|
|
for in, want := range map[string]string{
|
|
"Alive II (disc 2)": "Alive II",
|
|
"Crossroads (Disc 2)": "Crossroads",
|
|
"The Boogie House Tapes (Disc 1": "The Boogie House Tapes",
|
|
"Ummagumma (disc 1: Live Album)": "Ummagumma",
|
|
"Seventeen Seconds (bonus disc: Rarities 1979-1980)": "Seventeen Seconds",
|
|
"Pornography (bonus disc: Rarit": "Pornography",
|
|
"Exciter (bonus disc)": "Exciter",
|
|
"A-Ha Shake Heartbreak (Bonus CD)": "A-Ha Shake Heartbreak",
|
|
"Live At Wembley Stadium (2011 Deluxe Remaster)": "Live At Wembley Stadium",
|
|
"Extreme Behavior (Special Edit": "Extreme Behavior",
|
|
"Album [Remastered]": "Album",
|
|
"Album (Deluxe Edition) (disc 2)": "Album",
|
|
"Soundtrack to the Apocalypse (disc 5: Live at The Grove in Anaheim, CA 5/2/02)": "Soundtrack to the Apocalypse",
|
|
// What is part of the title stays.
|
|
"Cobras And Fire (The Mastermind Redux)": "Cobras And Fire (The Mastermind Redux)",
|
|
"This Type Of Thinking (Could Do Us In)": "This Type Of Thinking (Could Do Us In)",
|
|
"Ten": "Ten",
|
|
"(disc 1)": "(disc 1)", // nothing would be left
|
|
"Disc One": "Disc One", // not in brackets
|
|
} {
|
|
if got := baseTitle(in); got != want {
|
|
t.Errorf("baseTitle(%q) = %q, want %q", in, got, want)
|
|
}
|
|
}
|
|
}
|