Vellum/internal/metadata/igdb_test.go
mithral b82090ed29 Phase 7: games — ROM scanning, platforms, cloud saves, EmulatorJS
The RomM replacement.

- registry: a games media type with ~35 ROM extensions across NES, SNES, GB/GBA,
  N64, NDS, Genesis/Master System/Game Gear, PC Engine, Neo Geo Pocket,
  WonderSwan, Atari, Coleco, Intellivision, and PlayStation.
- games: platform detection from the file extension, mapping each to an
  EmulatorJS core. Platforms with no browser core are recognised but marked
  not-playable so the UI offers a download instead of a broken player.
- migration 0005: game_saves keyed by (user, item, slot). Save states are user
  data, not media — two people playing the same ROM keep their own progress.
- save endpoints with a 16 MiB cap and a validated slot name; the upload is
  read through a LimitReader so an unbounded body is refused rather than
  buffered.
- igdb provider: Twitch client-credentials flow with the token cached for its
  ~2 month lifetime, Apicalypse query body, and cover URLs normalised from
  protocol-relative t_thumb to https t_cover_big.
- searchTitle: strips "(USA)", "[!]", "(Rev 1)" and similar release tags before
  searching. "Sonic the Hedgehog (USA)" matched nothing; "Sonic the Hedgehog"
  matches immediately. Stored titles keep the original.
- web: GamePlayer embeds EmulatorJS and adds Load/Save-to-server buttons.

Verified live against IGDB: Sonic matched (id 316900) with real cover art; a
4 KB save uploaded, listed, downloaded byte-identical, and deleted.
2026-10-09 21:54:49 -04:00

95 lines
2.7 KiB
Go

package metadata
import (
"context"
"io"
"net/http"
"net/http/httptest"
"strings"
"sync/atomic"
"testing"
)
func TestIGDBSearch(t *testing.T) {
var tokenHits int32
tokenSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
atomic.AddInt32(&tokenHits, 1)
if r.URL.Query().Get("grant_type") != "client_credentials" {
t.Errorf("grant_type = %q", r.URL.Query().Get("grant_type"))
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"access_token":"tok-123","expires_in":5184000}`))
}))
defer tokenSrv.Close()
var gotBody, gotClientID, gotAuth string
apiSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, _ := io.ReadAll(r.Body)
gotBody = string(b)
gotClientID = r.Header.Get("Client-ID")
gotAuth = r.Header.Get("Authorization")
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`[
{
"id": 1029,
"name": "Sonic the Hedgehog",
"summary": "A fast blue hedgehog.",
"first_release_date": 677635200,
"cover": {"url": "//images.igdb.com/igdb/image/upload/t_thumb/co1234.jpg"}
}
]`))
}))
defer apiSrv.Close()
p := NewIGDB("client-id", "client-secret")
p.TokenURL = tokenSrv.URL
p.BaseURL = apiSrv.URL
cands, err := p.Search(context.Background(), Query{Title: "Sonic"})
if err != nil {
t.Fatalf("Search: %v", err)
}
if len(cands) != 1 {
t.Fatalf("got %d candidates, want 1", len(cands))
}
c := cands[0]
if c.Title != "Sonic the Hedgehog" {
t.Errorf("Title = %q", c.Title)
}
if c.ExternalID != "1029" {
t.Errorf("ExternalID = %q", c.ExternalID)
}
if c.Year != 1991 {
t.Errorf("Year = %d, want 1991", c.Year)
}
if c.CoverURL != "https://images.igdb.com/igdb/image/upload/t_cover_big/co1234.jpg" {
t.Errorf("CoverURL = %q (want https and t_cover_big)", c.CoverURL)
}
if !strings.Contains(gotBody, `search "Sonic"`) {
t.Errorf("query body = %q", gotBody)
}
if gotClientID != "client-id" || gotAuth != "Bearer tok-123" {
t.Errorf("headers: Client-ID=%q Authorization=%q", gotClientID, gotAuth)
}
// A second search must reuse the cached token, not re-authenticate.
if _, err := p.Search(context.Background(), Query{Title: "Sonic 2"}); err != nil {
t.Fatal(err)
}
if n := atomic.LoadInt32(&tokenHits); n != 1 {
t.Errorf("token endpoint hit %d times, want 1 (token should be cached)", n)
}
}
func TestIGDBWithoutCredentialsDoesNothing(t *testing.T) {
p := NewIGDB("", "")
cands, err := p.Search(context.Background(), Query{Title: "Sonic"})
if err != nil {
t.Fatalf("Search: %v", err)
}
if len(cands) != 0 {
t.Errorf("got %d candidates with no credentials, want 0", len(cands))
}
}