sonda/internal/images/commons_source_test.go
tgiachi 9323702b1e fix(jobs): a stop or a panic never wedges a job, a bad download never replaces a good picture
- A panic while counting no longer leaves the job marked as running; a panic in the
  work of one item is a failure of that item, and a panic in the list ends the job
  with a reason.
- A stop while the items are counted is recorded as a stop instead of a 500, and a
  count that fails leaves the status as the previous job left it.
- An artist that lost its MusicBrainz id since it was listed is skipped by the
  pictures job, as the spec says, instead of counting as a failure.
- A picture that cannot be used now (refused download, bytes that are not an image)
  is a failure when a good picture is stored, so a refresh never throws it away.
- The outcomes the artist jobs report share their constants in the jobs package.
- The spec no longer calls each job "the one request" that goes through the library.
- The test that waited with a sleep for the count now waits for a signal.
2026-11-12 10:09:46 +01:00

232 lines
7.7 KiB
Go

package images
import (
"context"
"errors"
"fmt"
"sync"
"testing"
"codeberg.org/squidd/sonda/internal/discovery"
"codeberg.org/squidd/sonda/internal/providers"
"codeberg.org/squidd/sonda/internal/providers/commons"
"codeberg.org/squidd/sonda/internal/providers/musicbrainz"
)
const mbid = "11111111-2222-4333-8444-555555555555"
type artistsFake struct{ artists map[int64]discovery.Artist }
func (f artistsFake) Artist(_ context.Context, id int64) (discovery.Artist, error) {
a, ok := f.artists[id]
if !ok {
return discovery.Artist{}, discovery.ErrNotFound
}
return a, nil
}
type switchesFake struct {
mu sync.Mutex
on map[string]bool
}
func (f *switchesFake) Active(_ context.Context, name string) (providers.Settings, bool, error) {
f.mu.Lock()
defer f.mu.Unlock()
return providers.Settings{Provider: name}, f.on[name], nil
}
func (f *switchesFake) set(name string, on bool) {
f.mu.Lock()
defer f.mu.Unlock()
f.on[name] = on
}
type mbFake struct {
item string
err error
calls int
got string
hook func()
}
func (f *mbFake) WikidataID(_ context.Context, id string) (string, error) {
f.calls++
f.got = id
if f.hook != nil {
f.hook()
}
return f.item, f.err
}
type commonsFake struct {
name string
nameErr, infoErr, loadErr error
file commons.File
data []byte
nameCalls, infoCalls, load int
loaded string
}
func (f *commonsFake) FileName(context.Context, string) (string, error) {
f.nameCalls++
return f.name, f.nameErr
}
func (f *commonsFake) Info(context.Context, string) (commons.File, error) {
f.infoCalls++
return f.file, f.infoErr
}
func (f *commonsFake) Download(_ context.Context, url string) ([]byte, error) {
f.load++
f.loaded = url
return f.data, f.loadErr
}
type chain struct {
src *CommonsSource
sw *switchesFake
mb *mbFake
cm *commonsFake
}
func newChain() *chain {
c := &chain{
sw: &switchesFake{on: map[string]bool{providers.Commons: true, providers.MusicBrainz: true}},
mb: &mbFake{item: "Q44190"},
cm: &commonsFake{
name: "Band.jpg",
file: commons.File{Name: "Band.jpg", PageURL: "https://commons.wikimedia.org/wiki/File:Band.jpg", ThumbURL: "https://thumb.wikimedia.org/x/960px-Band.jpg",
Author: "Raph", License: "CC BY 4.0", LicenseURL: "https://creativecommons.org/licenses/by/4.0"},
data: []byte("picture bytes"),
},
}
c.src = &CommonsSource{
Artists: artistsFake{map[int64]discovery.Artist{1: {ID: 1, Name: "With Id", MBID: mbid}, 2: {ID: 2, Name: "No Id"}}},
Settings: c.sw,
MusicBrainz: c.mb,
Commons: c.cm,
}
return c
}
func TestTheChainGivesTheFreePictureWithItsCredit(t *testing.T) {
c := newChain()
got, err := c.src.ArtistImage(context.Background(), 1)
if err != nil {
t.Fatal(err)
}
if string(got.Data) != "picture bytes" || got.Credit != (Credit{Author: "Raph", License: "CC BY 4.0",
LicenseURL: "https://creativecommons.org/licenses/by/4.0", PageURL: "https://commons.wikimedia.org/wiki/File:Band.jpg"}) {
t.Errorf("found = %+v", got)
}
if c.mb.got != mbid || c.cm.loaded != "https://thumb.wikimedia.org/x/960px-Band.jpg" {
t.Errorf("asked MusicBrainz about %q and downloaded %q", c.mb.got, c.cm.loaded)
}
}
func TestNothingIsAskedWhenAnythingNeededIsOffOrThereIsNoId(t *testing.T) {
for name, tt := range map[string]struct {
prepare func(*chain)
id int64
want error
}{
"commons off": {func(c *chain) { c.sw.set(providers.Commons, false) }, 1, ErrProviderDisabled},
"musicbrainz off": {func(c *chain) { c.sw.set(providers.MusicBrainz, false) }, 1, ErrProviderDisabled},
"no musicbrainz id": {func(*chain) {}, 2, ErrNoID},
"unknown artist": {func(*chain) {}, 99, ErrArtistNotFound},
} {
c := newChain()
tt.prepare(c)
if _, err := c.src.ArtistImage(context.Background(), tt.id); !errors.Is(err, tt.want) {
t.Errorf("%s: err = %v, want %v", name, err, tt.want)
}
if c.mb.calls+c.cm.nameCalls+c.cm.infoCalls+c.cm.load != 0 {
t.Errorf("%s: a provider was asked", name)
}
}
}
func TestWhereThereIsNoPictureTheAnswerIsNoImage(t *testing.T) {
for name, tt := range map[string]func(*chain){
"no wikidata link": func(c *chain) { c.mb.item = "" },
"musicbrainz does not know": func(c *chain) { c.mb.err = musicbrainz.ErrNotFound },
"no image on wikidata": func(c *chain) { c.cm.name = "" },
"wikidata does not know": func(c *chain) { c.cm.nameErr = commons.ErrNotFound },
"the file is gone": func(c *chain) { c.cm.infoErr = commons.ErrNotFound },
"not a free licence": func(c *chain) { c.cm.infoErr = commons.ErrNotFree },
"not a usable picture": func(c *chain) { c.cm.infoErr = commons.ErrUnusable },
"an address that is refused": func(c *chain) { c.cm.loadErr = commons.ErrBadRequest },
} {
c := newChain()
tt(c)
if _, err := c.src.ArtistImage(context.Background(), 1); !errors.Is(err, ErrNoImage) {
t.Errorf("%s: err = %v, want ErrNoImage", name, err)
}
}
}
func TestAFailureIsNotTakenForNoPicture(t *testing.T) {
boom := errors.New("boom")
for name, tt := range map[string]func(*chain){
"musicbrainz fails": func(c *chain) { c.mb.err = boom },
"wikidata fails": func(c *chain) { c.cm.nameErr = commons.ErrUnavailable },
"commons fails": func(c *chain) { c.cm.infoErr = commons.ErrRateLimited },
"the download fails": func(c *chain) { c.cm.loadErr = commons.ErrUnavailable },
} {
c := newChain()
tt(c)
_, err := c.src.ArtistImage(context.Background(), 1)
if !errors.Is(err, ErrSourceUnavailable) || errors.Is(err, ErrNoImage) {
t.Errorf("%s: err = %v, want ErrSourceUnavailable", name, err)
}
}
}
func TestSwitchedOffWhileItWasWorkingNothingIsReturned(t *testing.T) {
c := newChain()
c.mb.hook = func() { c.sw.set(providers.Commons, false) } // the admin took the data back
if _, err := c.src.ArtistImage(context.Background(), 1); !errors.Is(err, ErrProviderDisabled) {
t.Errorf("err = %v, want ErrProviderDisabled: a picture read before the switch must not be kept", err)
}
if c.cm.load != 0 {
t.Error("a picture was downloaded after the provider was switched off")
}
}
func TestAnOutageThatEndsInADeadlineIsStillAnOutage(t *testing.T) {
// The outbound client's own timeout wraps context.DeadlineExceeded inside ErrUnavailable. It
// is a failure of the provider (so an older picture can be served), not a cancellation.
slow := fmt.Errorf("%w: %w", commons.ErrUnavailable, context.DeadlineExceeded)
for name, tt := range map[string]func(*chain){
"musicbrainz": func(c *chain) { c.mb.err = fmt.Errorf("%w: %w", musicbrainz.ErrUnavailable, context.DeadlineExceeded) },
"wikidata": func(c *chain) { c.cm.nameErr = slow },
"commons": func(c *chain) { c.cm.infoErr = slow },
"the download": func(c *chain) { c.cm.loadErr = slow },
} {
c := newChain()
tt(c)
if _, err := c.src.ArtistImage(context.Background(), 1); !errors.Is(err, ErrSourceUnavailable) {
t.Errorf("%s: err = %v, want ErrSourceUnavailable", name, err)
}
}
}
func TestARequestThatTheCallerCancelledIsReturnedAsCancelled(t *testing.T) {
c := newChain()
ctx, cancel := context.WithCancel(context.Background())
c.mb.hook = cancel // the caller leaves while MusicBrainz is being asked
c.mb.err = context.Canceled
if _, err := c.src.ArtistImage(ctx, 1); !errors.Is(err, context.Canceled) || errors.Is(err, ErrSourceUnavailable) {
t.Errorf("err = %v, want the cancellation itself", err)
}
}
func TestAPictureThatCannotBeUsedFromTheHostIsNoPicture(t *testing.T) {
c := newChain()
c.cm.loadErr = commons.ErrUnusable // a redirect or a picture over the limit
if _, err := c.src.ArtistImage(context.Background(), 1); !errors.Is(err, ErrNoImage) || !errors.Is(err, ErrBadPicture) {
t.Errorf("err = %v, want ErrNoImage that says the picture itself was bad", err)
}
}