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.
163 lines
5 KiB
Go
163 lines
5 KiB
Go
package about
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.kmalloc.org/squid/sonda/internal/discovery"
|
|
"git.kmalloc.org/squid/sonda/internal/jobs"
|
|
)
|
|
|
|
func TestEnsuringAFreshDescriptionAsksNobodyUnlessARefreshIsWanted(t *testing.T) {
|
|
r := newRig(true)
|
|
for i := 0; i < 2; i++ {
|
|
res, err := r.svc.Ensure(context.Background(), 1, false)
|
|
if err != nil || res.Status != discovery.StatusOK || res.Description == nil {
|
|
t.Fatalf("result = %+v, err = %v", res, err)
|
|
}
|
|
}
|
|
calls := r.src.total()
|
|
if calls != 3 { // the id, the pages and one summary, once
|
|
t.Fatalf("the source was asked %d times, want 3", calls)
|
|
}
|
|
r.src.summaries["it"] = standard("it", "Q44190", "Un testo nuovo.")
|
|
res, err := r.svc.Ensure(context.Background(), 1, true)
|
|
if err != nil || res.Description == nil || res.Description.Text != "Un testo nuovo." {
|
|
t.Fatalf("refresh: result = %+v, err = %v", res, err)
|
|
}
|
|
if got := r.src.total(); got != 2*calls {
|
|
t.Errorf("a refresh asked the source %d times in all, want %d", got, 2*calls)
|
|
}
|
|
}
|
|
|
|
func TestARefreshThatFailsKeepsTheStoredDescription(t *testing.T) {
|
|
r := newRig(true)
|
|
r.get(t, 1)
|
|
r.src.qidErr = errors.New("wikidata is down")
|
|
res, err := r.svc.Ensure(context.Background(), 1, true)
|
|
if err != nil || res.Status != discovery.StatusError || res.Description == nil || res.Description.Language != "it" {
|
|
t.Errorf("result = %+v, err = %v, want an error status that still carries the stored text", res, err)
|
|
}
|
|
if got := r.store.snaps[1]; got.None || got.Description.Language != "it" {
|
|
t.Errorf("stored = %+v, want it untouched", got)
|
|
}
|
|
}
|
|
|
|
func TestARefreshWithTheProviderOffAsksNobodyAndKeepsTheDescription(t *testing.T) {
|
|
r := newRig(true)
|
|
r.get(t, 1)
|
|
r.sw.set("wikipedia", false)
|
|
before := r.src.total()
|
|
res, err := r.svc.Ensure(context.Background(), 1, true)
|
|
if err != nil || res.Status != discovery.StatusDisabled || res.Description == nil {
|
|
t.Errorf("result = %+v, err = %v", res, err)
|
|
}
|
|
if r.src.total() != before {
|
|
t.Error("the source was asked although the provider is off")
|
|
}
|
|
}
|
|
|
|
// ---- the job ----
|
|
|
|
type fakeLister struct {
|
|
mu sync.Mutex
|
|
ids []int64
|
|
}
|
|
|
|
func (f *fakeLister) DescriptionsToBuild(_ context.Context, after int64, limit int, _ bool, _ time.Time) ([]int64, error) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
var out []int64
|
|
for _, id := range f.ids {
|
|
if id > after && len(out) < limit {
|
|
out = append(out, id)
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func (f *fakeLister) CountDescriptionsToBuild(context.Context, bool, time.Time) (int, error) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
return len(f.ids), nil
|
|
}
|
|
|
|
func waitDone(t *testing.T, j *jobs.Job) jobs.Status {
|
|
t.Helper()
|
|
deadline := time.Now().Add(20 * time.Second)
|
|
for {
|
|
if st := j.Status(); !st.Running {
|
|
return st
|
|
}
|
|
if time.Now().After(deadline) {
|
|
t.Fatal("the job did not finish")
|
|
}
|
|
time.Sleep(5 * time.Millisecond)
|
|
}
|
|
}
|
|
|
|
func jobRig(t *testing.T, ids ...int64) (*rig, *jobs.Job) {
|
|
t.Helper()
|
|
r := newRig(true)
|
|
for _, id := range ids {
|
|
r.store.artists[id] = discovery.Artist{ID: id, Name: "Artist", MBID: mbid}
|
|
}
|
|
return r, NewJob(r.svc, &fakeLister{ids: ids})
|
|
}
|
|
|
|
func TestTheDescriptionsJobFindsTheDescriptionsAndCountsWhatHasNone(t *testing.T) {
|
|
r, job := jobRig(t, 11, 12, 13)
|
|
r.src.summaries["it"] = Summary{Type: "disambiguation"}
|
|
r.src.summaries["en"] = Summary{Type: "disambiguation"}
|
|
// The pages are not articles, so there is no description for any of them.
|
|
if err := job.Start(context.Background(), jobs.Options{}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
st := waitDone(t, job)
|
|
if st.Last == nil || st.Last.Checked != 3 || st.Last.Outcomes[OutcomeNone] != 3 || st.Last.Failed != 0 {
|
|
t.Fatalf("status = %+v", st)
|
|
}
|
|
_, job2 := jobRig(t, 11, 12)
|
|
_ = job2.Start(context.Background(), jobs.Options{})
|
|
st = waitDone(t, job2)
|
|
if st.Last.Outcomes[OutcomeFound] != 2 {
|
|
t.Errorf("summary = %+v, want two found", *st.Last)
|
|
}
|
|
}
|
|
|
|
func TestTheDescriptionsJobEndsAtOnceWhenAProviderIsOff(t *testing.T) {
|
|
r, job := jobRig(t, 11, 12, 13)
|
|
r.sw.set("musicbrainz", false)
|
|
_ = job.Start(context.Background(), jobs.Options{})
|
|
st := waitDone(t, job)
|
|
if st.LastError == "" || st.Last.Checked != 0 || st.Last.Failed != 0 {
|
|
t.Errorf("status = %+v, want it to end saying a provider is off", st)
|
|
}
|
|
if r.src.total() != 0 {
|
|
t.Error("the source was asked although a provider is off")
|
|
}
|
|
}
|
|
|
|
func TestTheDescriptionsJobGivesUpWhenWikipediaKeepsFailing(t *testing.T) {
|
|
r, job := jobRig(t, 11, 12, 13, 14, 15, 16, 17)
|
|
r.src.qidErr = errors.New("down")
|
|
_ = job.Start(context.Background(), jobs.Options{})
|
|
st := waitDone(t, job)
|
|
if st.Last.Failed != jobs.MaxConsecutiveFailures || st.LastError == "" {
|
|
t.Errorf("status = %+v", st)
|
|
}
|
|
}
|
|
|
|
func TestAnArtistThatWentOrLostItsIdIsSkippedByTheDescriptionsJob(t *testing.T) {
|
|
r, job := jobRig(t, 11, 12)
|
|
delete(r.store.artists, 11)
|
|
r.store.artists[12] = discovery.Artist{ID: 12, Name: "No Id Any More"}
|
|
_ = job.Start(context.Background(), jobs.Options{})
|
|
st := waitDone(t, job)
|
|
if st.Last.Skipped != 2 || st.Last.Failed != 0 || st.LastError != "" {
|
|
t.Errorf("status = %+v", st)
|
|
}
|
|
}
|