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.
111 lines
5.1 KiB
Go
111 lines
5.1 KiB
Go
package about
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"git.kmalloc.org/squid/sonda/internal/discovery"
|
|
"git.kmalloc.org/squid/sonda/internal/providers"
|
|
"git.kmalloc.org/squid/sonda/internal/providers/musicbrainz"
|
|
"git.kmalloc.org/squid/sonda/internal/providers/outbound"
|
|
"git.kmalloc.org/squid/sonda/internal/providers/wikipedia"
|
|
)
|
|
|
|
// fakeWeb answers for MusicBrainz, Wikidata and Wikipedia on one server.
|
|
func fakeWeb(t *testing.T, wikidataLink string) WikipediaSource {
|
|
t.Helper()
|
|
return fakeWebWith(t, wikidataLink, "ok")
|
|
}
|
|
|
|
// fakeWebWith can make the Italian page a redirect ("redirect") or a page with no text ("noextract").
|
|
func fakeWebWith(t *testing.T, wikidataLink, italian 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":{"id":"Q44190","sitelinks":{"itwiki":{"title":"Radiohead"},"enwiki":{"title":"Radiohead"}}}}}`))
|
|
case r.URL.Path == "/it/summary/Radiohead" && italian == "redirect":
|
|
http.Redirect(w, r, "https://evil.example/", http.StatusFound)
|
|
case r.URL.Path == "/it/summary/Radiohead" && italian == "noextract":
|
|
_, _ = w.Write([]byte(`{"type":"no-extract","title":"Radiohead","lang":"it","extract":"","wikibase_item":"Q44190","content_urls":{"desktop":{"page":"https://it.wikipedia.org/wiki/Radiohead"}}}`))
|
|
case r.URL.Path == "/en/summary/Radiohead":
|
|
_, _ = w.Write([]byte(`{"type":"standard","title":"Radiohead","lang":"en","extract":"Text.","description":"band","wikibase_item":"Q44190","content_urls":{"desktop":{"page":"https://en.wikipedia.org/wiki/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)
|
|
}
|
|
resolved, pages, err := src.Pages(ctx, item, []string{"it", "en"})
|
|
if err != nil || resolved != "Q44190" || pages["it"] != "Radiohead" || pages["en"] != "Radiohead" {
|
|
t.Fatalf("item %q pages = %v err = %v", resolved, 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)
|
|
}
|
|
}
|
|
|
|
// With the real clients, a page that cannot be used (a redirect that is never followed, a
|
|
// page with no text) is an answer: the chain goes on to the next language, and the outcome
|
|
// is stored, so the same page is not asked about again at every view.
|
|
func TestAPageThatCannotBeUsedFallsThroughToTheNextLanguage(t *testing.T) {
|
|
for _, italian := range []string{"redirect", "noextract"} {
|
|
store := newStore()
|
|
svc := NewService(store, fakeWebWith(t, "https://www.wikidata.org/wiki/Q44190", italian),
|
|
&switches{m: map[string]bool{providers.Wikipedia: true, providers.MusicBrainz: true}}, []string{"it", "en"}, zap.NewNop())
|
|
res, err := svc.Description(context.Background(), 1)
|
|
if err != nil || res.Status != discovery.StatusOK || res.Description == nil || res.Description.Language != "en" {
|
|
t.Errorf("Italian page %s: result = %+v err = %v, want the English description", italian, res, err)
|
|
}
|
|
if len(store.saves) != 1 {
|
|
t.Errorf("Italian page %s: %d saves, want the outcome stored once", italian, len(store.saves))
|
|
}
|
|
}
|
|
}
|