- comicvine provider for comics. ComicVine has no keyless tier, so the provider is only registered when VELLUM_COMICVINE_API_KEY is set; without it the server logs a note and comic enrichment completes as a no-op rather than erroring. - BaseURL is injectable so the response parsing is covered by a test against a local server: candidate mapping, the required User-Agent, and the status_code=100 invalid-key case (ComicVine returns HTTP 200 for that). - descriptions are stripped of ComicVine's embedded HTML before storage. Verified against a fake ComicVine server, and live that a comics library enriches cleanly with no key configured.
116 lines
3 KiB
Go
116 lines
3 KiB
Go
package metadata
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestComicVineSearch(t *testing.T) {
|
|
var gotPath, gotAPIKey, gotUA string
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
gotPath = r.URL.Path
|
|
gotAPIKey = r.URL.Query().Get("api_key")
|
|
gotUA = r.Header.Get("User-Agent")
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{
|
|
"status_code": 1,
|
|
"results": [
|
|
{
|
|
"id": 796,
|
|
"name": "Saga",
|
|
"start_year": "2012",
|
|
"description": "<p>A space opera.</p>",
|
|
"publisher": {"name": "Image Comics"},
|
|
"image": {"medium_url": "https://example.test/saga.jpg", "small_url": ""}
|
|
}
|
|
]
|
|
}`))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
p := NewComicVine("secret-key")
|
|
p.BaseURL = srv.URL
|
|
|
|
cands, err := p.Search(context.Background(), Query{Title: "Saga"})
|
|
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 != "Saga" {
|
|
t.Errorf("Title = %q", c.Title)
|
|
}
|
|
if c.ExternalID != "796" {
|
|
t.Errorf("ExternalID = %q", c.ExternalID)
|
|
}
|
|
if c.Year != 2012 {
|
|
t.Errorf("Year = %d, want 2012", c.Year)
|
|
}
|
|
if c.Publisher != "Image Comics" {
|
|
t.Errorf("Publisher = %q", c.Publisher)
|
|
}
|
|
if c.CoverURL != "https://example.test/saga.jpg" {
|
|
t.Errorf("CoverURL = %q", c.CoverURL)
|
|
}
|
|
if c.Description != "A space opera." {
|
|
t.Errorf("Description = %q (HTML should be stripped)", c.Description)
|
|
}
|
|
|
|
if gotPath != "/search/" {
|
|
t.Errorf("request path = %q, want /search/", gotPath)
|
|
}
|
|
if gotAPIKey != "secret-key" {
|
|
t.Errorf("api_key = %q, want secret-key", gotAPIKey)
|
|
}
|
|
if gotUA == "" {
|
|
t.Error("no User-Agent sent; ComicVine rejects those")
|
|
}
|
|
}
|
|
|
|
func TestComicVineRejectsBadAPIKey(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
_, _ = w.Write([]byte(`{"status_code": 100, "error": "Invalid API Key", "results": []}`))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
p := NewComicVine("wrong")
|
|
p.BaseURL = srv.URL
|
|
|
|
if _, err := p.Search(context.Background(), Query{Title: "Saga"}); err == nil {
|
|
t.Fatal("expected an error for status_code 100")
|
|
} else if !strings.Contains(err.Error(), "API key") {
|
|
t.Errorf("error %q should mention the API key", err)
|
|
}
|
|
}
|
|
|
|
func TestComicVineWithoutKeyDoesNothing(t *testing.T) {
|
|
p := NewComicVine("")
|
|
cands, err := p.Search(context.Background(), Query{Title: "Saga"})
|
|
if err != nil {
|
|
t.Fatalf("Search: %v", err)
|
|
}
|
|
if len(cands) != 0 {
|
|
t.Errorf("got %d candidates with no key, want 0", len(cands))
|
|
}
|
|
}
|
|
|
|
func TestStripHTML(t *testing.T) {
|
|
cases := map[string]string{
|
|
"<p>A space opera.</p>": "A space opera.",
|
|
"plain text": "plain text",
|
|
"<b>Bold</b> and <i>italic</i>.": "Bold and italic.",
|
|
"": "",
|
|
}
|
|
for in, want := range cases {
|
|
if got := stripHTML(in); got != want {
|
|
t.Errorf("stripHTML(%q) = %q, want %q", in, got, want)
|
|
}
|
|
}
|
|
}
|