sonda/internal/images/artist_test.go
tgiachi d5ed0f2e9c feat(images): album covers from the Cover Art Archive
- The scan reads the MusicBrainz release group id of an album from the tags of its
  files (MUSICBRAINZ_RELEASEGROUPID, a vorbis comment or an ID3 frame) and stores it in
  albums.mbid: it is taken only when the file has an album and exactly one valid id, an
  album keeps the one it has, and an id that another album owns is not given twice. A full
  rescan reads it for music that was already indexed.
- New provider "coverartarchive" (off by default, no key, contact in the User-Agent). When
  an album has no picture in the user's own files, which always come first, its cover is
  asked from the archive by that id and never by a title: only the id leaves the server.
  The same GET /albums/{id}/cover; the record keeps the source. An outage is a 502
  provider_unavailable and is not remembered, and an older cover is served in its place.
- The archive answers with three redirects (coverartarchive.org, archive.org, a data node
  whose name changes). The outbound client can now follow redirects, but only the ones the
  caller allows, up to a limit; here that is https on archive.org and its subdomains.
- A purge counter now guards the covers too: a read that started before the provider's data
  was taken back is not kept. Removing the provider's data forgets the covers that came from
  it and deletes their files, and leaves the ones from the user's files.
- Spec, contract and HTTP tests, README updated; the facts of the real archive are written down.

Closes #43
2026-11-12 09:09:31 +01:00

322 lines
11 KiB
Go

package images
import (
"bytes"
"context"
"errors"
"image"
"image/color"
"io"
"os"
"path/filepath"
"sync"
"sync/atomic"
"testing"
"time"
)
type fakeArtists struct {
mu sync.Mutex
found Found
err error
calls int32
gate chan struct{}
gate2 bool // the gate is never opened for this call: it waits for the context instead
}
func (f *fakeArtists) ArtistImage(ctx context.Context, id int64) (Found, error) {
atomic.AddInt32(&f.calls, 1)
f.mu.Lock()
gate, found, err := f.gate, f.found, f.err
f.mu.Unlock()
if gate != nil {
if f.gate2 {
select {
case <-gate:
case <-ctx.Done():
return Found{}, ctx.Err()
}
} else {
<-gate
}
}
return found, err
}
func (f *fakeArtists) set(found Found, err error) {
f.mu.Lock()
defer f.mu.Unlock()
f.found, f.err = found, err
}
var credit = Credit{Author: "Raph_PH", License: "CC BY 4.0", LicenseURL: "https://creativecommons.org/licenses/by/4.0", PageURL: "https://commons.wikimedia.org/wiki/File:Band.jpg"}
type artistRig struct {
*rig
src *fakeArtists
}
func newArtistRig(t *testing.T) *artistRig {
t.Helper()
r := &artistRig{rig: newRig(t), src: &fakeArtists{}}
r.src.found = Found{Data: jpegBytes(t, 1500, 1000, color.RGBA{30, 30, 200, 255}), Credit: credit}
r.svc.SetArtistSource(r.src)
return r
}
func (r *artistRig) get(t *testing.T, id int64, size Size) (*Cover, []byte) {
t.Helper()
c, err := r.svc.ArtistImage(context.Background(), id, size)
if err != nil {
t.Fatalf("ArtistImage: %v", err)
}
defer c.File.Close()
b, _ := io.ReadAll(c.File)
return c, b
}
func TestAnArtistPictureIsBuiltOnceStoredAndCredited(t *testing.T) {
r := newArtistRig(t)
c, big := r.get(t, 7, Large)
cfg, format, err := image.DecodeConfig(bytes.NewReader(big))
if err != nil || format != "jpeg" || cfg.Width != 1000 || cfg.Height != 666 {
t.Errorf("large = %v %s %v, want a jpeg 1000 wide", cfg, format, err)
}
if c.Credit != credit {
t.Errorf("credit = %+v, want %+v", c.Credit, credit)
}
if _, small := r.get(t, 7, Small); len(small) == 0 {
t.Error("no small picture")
}
rec := r.store.recs["artist/7"]
if rec.Status != StatusOK || rec.Source != SourceCommons || rec.Credit != credit || rec.ETag == "" {
t.Errorf("record = %+v", rec)
}
if n := atomic.LoadInt32(&r.src.calls); n != 1 {
t.Errorf("the provider was asked %d times, want 1: the picture is stored", n)
}
for _, size := range []string{"7-large.jpg", "7-small.jpg"} {
info, err := os.Stat(filepath.Join(r.data, "images", "artist", size))
if err != nil || info.Mode().Perm()&0o077 != 0 {
t.Errorf("%s: %v %v, want a private file under images/artist", size, info, err)
}
}
}
func TestAnArtistWithNoFreePictureIsRememberedForAWeek(t *testing.T) {
r := newArtistRig(t)
r.src.set(Found{}, ErrNoImage)
if _, err := r.svc.ArtistImage(context.Background(), 7, Large); !errors.Is(err, ErrNoImage) {
t.Fatalf("err = %v", err)
}
if rec := r.store.recs["artist/7"]; rec.Status != StatusNone {
t.Fatalf("record = %+v", rec)
}
for i := 0; i < 5; i++ {
if _, err := r.svc.ArtistImage(context.Background(), 7, Large); !errors.Is(err, ErrNoImage) {
t.Fatalf("err = %v", err)
}
}
if n := atomic.LoadInt32(&r.src.calls); n != 1 {
t.Errorf("asked %d times within the week, want 1", n)
}
r.now = r.now.Add(NoImageTTL + time.Hour)
r.src.set(Found{Data: jpegBytes(t, 300, 300, color.White), Credit: credit}, nil)
if _, b := r.get(t, 7, Large); len(b) == 0 {
t.Error("a picture that appeared was not found after the week")
}
}
func TestSomethingThatIsNotAPictureCountsAsNoPicture(t *testing.T) {
r := newArtistRig(t)
r.src.set(Found{Data: []byte("<html>not a picture</html>"), Credit: credit}, nil)
if _, err := r.svc.ArtistImage(context.Background(), 7, Large); !errors.Is(err, ErrNoImage) {
t.Errorf("err = %v, want ErrNoImage", err)
}
if rec := r.store.recs["artist/7"]; rec.Status != StatusNone {
t.Errorf("record = %+v", rec)
}
}
func TestWhatIsNotAnAnswerAboutThePictureIsNotRemembered(t *testing.T) {
for _, want := range []error{ErrProviderDisabled, ErrNoID, ErrSourceUnavailable, ErrArtistNotFound} {
r := newArtistRig(t)
r.src.set(Found{}, want)
if _, err := r.svc.ArtistImage(context.Background(), 7, Large); !errors.Is(err, want) {
t.Errorf("err = %v, want %v", err, want)
}
if len(r.store.recs) != 0 {
t.Errorf("%v: records = %v, want none: the next request must ask again", want, r.store.recs)
}
}
// No source at all is a provider that is off.
r := newRig(t)
if _, err := r.svc.ArtistImage(context.Background(), 7, Large); !errors.Is(err, ErrProviderDisabled) {
t.Errorf("no source: err = %v", err)
}
}
func TestAnOldPictureIsKeptWhenTheProviderCannotBeAsked(t *testing.T) {
for _, why := range []error{ErrProviderDisabled, ErrSourceUnavailable} {
r := newArtistRig(t)
_, first := r.get(t, 7, Large)
r.now = r.now.Add(ArtistTTL + time.Hour)
r.src.set(Found{}, why)
c, again := r.get(t, 7, Large)
if !bytes.Equal(first, again) || c.Credit != credit {
t.Errorf("%v: the stored picture was not served, or lost its credit", why)
}
}
// A picture that was changed is fetched again after a month.
r := newArtistRig(t)
_, first := r.get(t, 7, Large)
r.now = r.now.Add(ArtistTTL + time.Hour)
r.src.set(Found{Data: jpegBytes(t, 800, 800, color.RGBA{200, 0, 0, 255}), Credit: Credit{Author: "Someone", License: "CC0", PageURL: "https://commons.wikimedia.org/wiki/File:New.jpg"}}, nil)
c, second := r.get(t, 7, Large)
if bytes.Equal(first, second) || c.Credit.Author != "Someone" {
t.Error("the new picture and its credit were not taken after a month")
}
}
func TestACoverAndAnArtistPictureWithTheSameIdDoNotMix(t *testing.T) {
r := newArtistRig(t)
r.write(t, "Album One/cover.jpg", jpegBytes(t, 400, 400, color.RGBA{200, 0, 0, 255}))
_, artist := r.get(t, 1, Large)
album := r.rig.read(t, 1, Large)
if bytes.Equal(artist, album) {
t.Error("the picture of artist 1 and the cover of album 1 are the same file")
}
if len(r.store.recs) != 2 {
t.Errorf("records = %v, want one for the artist and one for the album", r.store.recs)
}
}
func TestManyAsksForAnArtistPictureAskTheProviderOnce(t *testing.T) {
r := newArtistRig(t)
r.src.gate = make(chan struct{})
var wg sync.WaitGroup
for i := 0; i < 15; i++ {
wg.Add(1)
go func() {
defer wg.Done()
if c, err := r.svc.ArtistImage(context.Background(), 7, Large); err != nil {
t.Errorf("err = %v", err)
} else {
c.File.Close()
}
}()
}
time.Sleep(80 * time.Millisecond)
close(r.src.gate)
wg.Wait()
if n := atomic.LoadInt32(&r.src.calls); n != 1 {
t.Errorf("asked %d times, want 1", n)
}
}
func TestAPurgeDuringTheReadIsNotUndoneByIt(t *testing.T) {
r := newArtistRig(t)
r.src.gate = make(chan struct{})
done := make(chan error, 1)
go func() { _, err := r.svc.ArtistImage(context.Background(), 7, Large); done <- err }()
time.Sleep(50 * time.Millisecond) // the read has the picture and is about to keep it
r.svc.BeginPurge() // the admin takes the provider's data back...
r.src.set(Found{}, ErrProviderDisabled) // ...and switches it off
close(r.src.gate)
if err := <-done; !errors.Is(err, ErrProviderDisabled) {
t.Fatalf("err = %v, want the read to be refused once the provider is off", err)
}
if len(r.store.recs) != 0 {
t.Errorf("records = %v: what the read found before the purge came back", r.store.recs)
}
if entries, _ := os.ReadDir(filepath.Join(r.data, "images", "artist")); len(entries) != 0 {
t.Errorf("%d files left by a read that started before the purge", len(entries))
}
}
func TestAReadThatStartedBeforeAPurgeStartsAgainIfTheProviderIsStillOn(t *testing.T) {
r := newArtistRig(t)
r.src.gate = make(chan struct{})
done := make(chan error, 1)
go func() { _, err := r.svc.ArtistImage(context.Background(), 7, Large); done <- err }()
time.Sleep(50 * time.Millisecond)
r.svc.BeginPurge()
close(r.src.gate)
if err := <-done; err != nil {
t.Fatalf("err = %v", err)
}
if n := atomic.LoadInt32(&r.src.calls); n != 2 {
t.Errorf("the provider was asked %d times, want 2: the first answer was from before the purge", n)
}
if rec := r.store.recs["artist/7"]; rec.Status != StatusOK {
t.Errorf("record = %+v", rec)
}
}
func TestABuildThatTakesTooLongIsAnOutageNotAnInternalError(t *testing.T) {
r := newArtistRig(t)
_, old := r.get(t, 7, Large) // a picture is stored
r.now = r.now.Add(ArtistTTL + time.Hour)
r.svc.buildTimeout = 40 * time.Millisecond
r.src.gate = make(chan struct{}) // never opened during the build
r.src.gate2 = true
c, again := r.get(t, 7, Large)
if !bytes.Equal(old, again) || c.Credit != credit {
t.Error("the old picture was not served when the build timed out")
}
// With nothing stored the caller is told it is an outage (502), not a failure of ours (500).
r2 := newArtistRig(t)
r2.svc.buildTimeout = 40 * time.Millisecond
r2.src.gate = make(chan struct{})
r2.src.gate2 = true
if _, err := r2.svc.ArtistImage(context.Background(), 8, Large); !errors.Is(err, ErrSourceUnavailable) {
t.Errorf("err = %v, want ErrSourceUnavailable", err)
}
close(r.src.gate)
close(r2.src.gate)
}
func TestAHugePictureOfAnArtistIsRefused(t *testing.T) {
r := newArtistRig(t)
r.src.set(Found{Data: jpegBytes(t, 2500, 2500, color.White), Credit: credit}, nil) // 6 million pixels
if _, err := r.svc.ArtistImage(context.Background(), 7, Large); !errors.Is(err, ErrNoImage) {
t.Errorf("err = %v, want ErrNoImage: a copy of 960 pixels is never that big", err)
}
// The same picture is fine as the cover of an album (the limit there is larger).
if _, err := Process(r.src.found.Data); err != nil {
t.Errorf("Process: %v", err)
}
}
func TestPurgingTheArtistPicturesRemovesTheFilesAndOnlyThose(t *testing.T) {
r := newArtistRig(t)
r.get(t, 7, Large)
r.get(t, 8, Large)
r.write(t, "Album One/cover.jpg", jpegBytes(t, 400, 400, color.White))
r.rig.read(t, 1, Large)
n, err := r.svc.RemoveArtistFiles()
if err != nil || n != 4 {
t.Fatalf("removed %d files err = %v, want 4 (two sizes of two pictures)", n, err)
}
if _, err := os.Stat(filepath.Join(r.data, "images", "artist")); !os.IsNotExist(err) {
t.Error("the artist folder is still there")
}
if _, err := os.Stat(filepath.Join(r.data, "images", "album", "1-large.jpg")); err != nil {
t.Errorf("a cover was removed with them: %v", err)
}
// Nothing to remove is not an error.
if n, err := r.svc.RemoveArtistFiles(); err != nil || n != 0 {
t.Errorf("again: %d %v", n, err)
}
}
func TestACreditIsPartOfTheETag(t *testing.T) {
r := newArtistRig(t)
c1, _ := r.get(t, 7, Large)
r.store.clear("artist", 7)
r.src.set(Found{Data: r.src.found.Data, Credit: Credit{Author: "Other", License: "CC0", PageURL: "https://commons.wikimedia.org/wiki/File:Other.jpg"}}, nil)
c2, _ := r.get(t, 7, Large)
if c1.ETag == c2.ETag {
t.Error("the picture is the same but its credit changed: the etag must change too, or a client keeps the old credit")
}
}