feat: initial commit
build / test (push) Successful in 9s
build / build (push) Successful in 10s
build / build-image (push) Successful in 1m4s

This commit is contained in:
2026-06-04 14:19:37 +02:00
commit cf961ab94f
28 changed files with 3036 additions and 0 deletions
+73
View File
@@ -0,0 +1,73 @@
package scoreboard
import (
"context"
"errors"
"testing"
"time"
)
func sample() []Entry {
return []Entry{
{Rank: 1, Username: "skav", Score: 26435},
{Rank: 2, Username: "Kkameleon", Score: 26000},
{Rank: 3, Username: "ENOENT", Score: 25900},
}
}
func TestStoreLookups(t *testing.T) {
s := NewStore()
if s.Ready() {
t.Fatal("empty store should not be ready")
}
s.Set(sample(), time.Now())
if !s.Ready() || s.Len() != 3 {
t.Fatalf("ready=%v len=%d", s.Ready(), s.Len())
}
if e, ok := s.ByRank(2); !ok || e.Username != "Kkameleon" {
t.Errorf("ByRank(2) = %+v ok=%v", e, ok)
}
if e, ok := s.ByUsername("SKAV"); !ok || e.Rank != 1 {
t.Errorf("ByUsername(SKAV) = %+v ok=%v (case-insensitive lookup failed)", e, ok)
}
if _, ok := s.ByUsername("nobody"); ok {
t.Error("ByUsername(nobody) should miss")
}
page, _ := s.List(1, 1)
if len(page) != 1 || page[0].Username != "Kkameleon" {
t.Errorf("List(1,1) = %+v", page)
}
if page, _ := s.List(10, 5); len(page) != 0 {
t.Errorf("List past end should be empty, got %+v", page)
}
}
func TestRefresherKeepsLastGood(t *testing.T) {
s := NewStore()
calls := 0
fetch := func(context.Context) ([]Entry, error) {
calls++
if calls == 1 {
return sample(), nil
}
return nil, errors.New("boom")
}
r := NewRefresher(s, fetch, time.Hour, nil)
if err := r.RefreshOnce(context.Background()); err != nil {
t.Fatalf("first refresh: %v", err)
}
if err := r.RefreshOnce(context.Background()); err == nil {
t.Fatal("second refresh should have failed")
}
// Last good snapshot must still be served.
if s.Len() != 3 {
t.Errorf("expected last good snapshot of 3, got %d", s.Len())
}
ok, fail, lastErr, _ := r.Stats()
if ok != 1 || fail != 1 || lastErr == nil {
t.Errorf("stats: ok=%d fail=%d lastErr=%v", ok, fail, lastErr)
}
}