sonda/internal/about/source_test.go
tgiachi 48cb903115 feat(about): artist descriptions from Wikipedia
- GET /artists/{id}/description shows what Wikipedia says about an artist: a short
  text with its page, a link and the licence (CC BY-SA 4.0).
- New provider "wikipedia" (off by default, no key, contact in the User-Agent, which
  Wikimedia requires). The way to the text is a chain of ids, never a name: the
  artist's MusicBrainz id, the Wikidata item MusicBrainz links to it, the Wikipedia
  page Wikidata names in the first configured language that has one. A page is used
  only if it is an article about that same item.
- [wikipedia] languages in the config (one to five codes of two or three lower case
  letters, checked before they are sent: an unknown site makes Wikidata answer with
  every language).
- Stored 30 days (a day when there was nothing to find), served stale if Wikipedia
  fails, removed with the provider's data (the purge now reports descriptions).
- MusicBrainz client: WikidataID. Wikipedia client for Wikidata and the summary,
  which also reads Wikidata's errors that come with HTTP 200.
- artist_descriptions table (migration 0014, SQLite and PostgreSQL).
- Spec, contract test, README and example config updated.

Closes #39
2026-11-11 21:36:27 +01:00

77 lines
3 KiB
Go

package about
import (
"context"
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
"codeberg.org/squidd/sonda/internal/providers/musicbrainz"
"codeberg.org/squidd/sonda/internal/providers/outbound"
"codeberg.org/squidd/sonda/internal/providers/wikipedia"
)
// fakeWeb answers for MusicBrainz, Wikidata and Wikipedia on one server.
func fakeWeb(t *testing.T, wikidataLink string) WikipediaSource {
t.Helper()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case strings.HasPrefix(r.URL.Path, "/ws/2/artist/"+mbid):
rels := `[]`
if wikidataLink != "" {
rels = `[{"type":"wikidata","url":{"resource":"` + wikidataLink + `"}}]`
}
_, _ = w.Write([]byte(`{"id":"` + mbid + `","relations":` + rels + `}`))
case strings.HasPrefix(r.URL.Path, "/ws/2/artist/"):
http.NotFound(w, r)
case r.URL.Path == "/w/api.php":
_, _ = w.Write([]byte(`{"entities":{"Q44190":{"sitelinks":{"itwiki":{"title":"Radiohead"},"enwiki":{"title":"Radiohead"}}}}}`))
case r.URL.Path == "/it/summary/Radiohead":
_, _ = w.Write([]byte(`{"type":"standard","title":"Radiohead","lang":"it","extract":"Testo.","description":"gruppo","wikibase_item":"Q44190","content_urls":{"desktop":{"page":"https://it.wikipedia.org/wiki/Radiohead"}}}`))
default:
http.NotFound(w, r)
}
}))
t.Cleanup(srv.Close)
client := outbound.New(outbound.Options{UserAgent: "Sonda/test ( https://example.org )", Retries: outbound.NoRetries})
return WikipediaSource{
MusicBrainz: musicbrainz.New(client, srv.URL+"/ws/2/"),
Wikipedia: wikipedia.New(client, srv.URL+"/w/api.php", srv.URL+"/%s/summary/"),
}
}
func TestTheRealClientsFollowTheChain(t *testing.T) {
src := fakeWeb(t, "https://www.wikidata.org/wiki/Q44190")
ctx := context.Background()
item, err := src.WikidataID(ctx, mbid)
if err != nil || item != "Q44190" {
t.Fatalf("item = %q err = %v", item, err)
}
pages, err := src.Pages(ctx, item, []string{"it", "en"})
if err != nil || pages["it"] != "Radiohead" || pages["en"] != "Radiohead" {
t.Fatalf("pages = %v err = %v", pages, err)
}
sum, err := src.Summary(ctx, "it", "Radiohead")
if err != nil || sum.WikibaseItem != "Q44190" || sum.Extract != "Testo." || sum.Type != "standard" || sum.Description != "gruppo" {
t.Fatalf("summary = %+v err = %v", sum, err)
}
}
func TestWhatThereIsNothingToFindIsNotAFailure(t *testing.T) {
ctx := context.Background()
if item, err := fakeWeb(t, "").WikidataID(ctx, mbid); err != nil || item != "" {
t.Errorf("no link: %q %v", item, err)
}
src := fakeWeb(t, "https://www.wikidata.org/wiki/Q44190")
if _, err := src.WikidataID(ctx, "22222222-2222-4333-8444-555555555555"); !errors.Is(err, ErrSourceNotFound) {
t.Errorf("unknown artist: %v", err)
}
if _, err := src.WikidataID(ctx, "not an id"); !errors.Is(err, ErrSourceNotFound) {
t.Errorf("a bad id: %v", err)
}
if _, err := src.Summary(ctx, "it", "No such page"); !errors.Is(err, ErrSourceNotFound) {
t.Errorf("no page: %v", err)
}
}