Four extractions, each chosen because the two halves have different callers,
different failure modes or different testing needs. Not a line-count exercise.
internal/home out of internal/media
- media serves item RESOURCES (detail, file, cover, progress); home computes
AGGREGATES (the five shelves). The 2328-line package was doing both.
- media keeps POST /items/{id}/view because it is item-scoped and must stay in
the package that authorizes the item; it imports home.RecordView for that one
thing. home does not import media.
- Verified live: all five shelves non-empty, and two consecutive /home calls
return different explore ids, which is the non-determinism the Explore rewrite
exists for.
internal/scanner out of internal/library
- The scanner walks the filesystem and reconciles; library CRUD is an admin HTTP
surface. Different callers (a job vs a request), different failure modes.
- The registry deliberately STAYED in internal/library: three of its four callers
are not the scanner (modules register types, handlers serve the list, metadata
reads the group mode, subsonic calls CleanTitle). It is a property of the kind,
not of the scan. The one genuinely scanner-owned helper became
library.ExtensionsFor.
- Two tests moved with the code rather than being rewritten; they now exercise
the boundary by calling library.ListMissing across it.
- Verified live: first scan updates, rescan reports unchanged=N, so change
detection still short-circuits.
internal/providers out of internal/metadata
- Providers talk to third-party APIs; the enrich job owns the matching policy and
the database writes. The split's whole point is that a provider is testable
WITHOUT a database, so a DB-free test was added and OpenLibrary gained a
BaseURL override to make it possible.
- providers imports zero internal packages — a true leaf.
- The matching policy (searchTitle, artistHint, pickBest) is untouched. It exists
because real false matches were found.
dto packages out of internal/jellyfin and internal/subsonic
- The wire types and their mapping are now separate from the handler logic, so
the contract with clients we do not control is reviewable on its own.
- Both dto packages are PURE: zero references to sql.DB or net/http, verified by
grep. That is what makes them reviewable.
- No field name, tag or error code changed.
internal/arch did its job: it failed the build twice during this phase, once for
each new package that was not yet placed in a layer. That is the rule working,
not an obstacle.
Verified: gofmt clean, build exit 0, vet exit 0, 23 packages pass, svelte-check
0/0, and end to end against a running server — home shelves, a queued scan
reporting unchanged, Jellyfin /Items, and Subsonic ping.
Process note: one agent (dto) stopped mid-task and left the tree red — the dto
packages unlayered and its test still referencing constants that had moved. The
arch test caught it immediately. I finished it by hand: exported constant
references in the test, and both dto packages placed at layer 3.
Still open from Phase 1: internal/jellyfin has no test files, catalog is
item-centric while Subsonic is file-first, and subsonic loadAlbum now scopes to
the item's own library.
100 lines
3 KiB
Go
100 lines
3 KiB
Go
package providers
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// TestOpenLibrarySearchDBFree is the point of the split: a provider is an HTTP
|
|
// client, so it is exercisable end to end against a stub server with no
|
|
// database anywhere in sight.
|
|
func TestOpenLibrarySearchDBFree(t *testing.T) {
|
|
var gotTitle, gotAuthor, gotLimit, gotUA string
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/search.json" {
|
|
t.Errorf("path = %q, want /search.json", r.URL.Path)
|
|
}
|
|
q := r.URL.Query()
|
|
gotTitle, gotAuthor, gotLimit = q.Get("title"), q.Get("author"), q.Get("limit")
|
|
gotUA = r.Header.Get("User-Agent")
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{"docs":[
|
|
{"key":"/works/OL45804W","title":"The Hobbit","author_name":["J.R.R. Tolkien"],
|
|
"first_publish_year":1937,"publisher":["Allen & Unwin"],"language":["eng"],
|
|
"cover_i":8225261,"isbn":["9780261102217"]},
|
|
{"key":"/works/OL1W","title":"","author_name":["Nobody"]}
|
|
]}`))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
p := NewOpenLibrary()
|
|
p.BaseURL = srv.URL
|
|
|
|
cands, err := p.Search(context.Background(), Query{Title: "The Hobbit", Author: "Tolkien"})
|
|
if err != nil {
|
|
t.Fatalf("Search: %v", err)
|
|
}
|
|
// The untitled doc is dropped rather than surfaced as an empty candidate.
|
|
if len(cands) != 1 {
|
|
t.Fatalf("got %d candidates, want 1", len(cands))
|
|
}
|
|
|
|
c := cands[0]
|
|
if c.Provider != "openlibrary" || c.ExternalID != "/works/OL45804W" {
|
|
t.Errorf("provider id = %s/%s", c.Provider, c.ExternalID)
|
|
}
|
|
if c.Title != "The Hobbit" || c.Year != 1937 {
|
|
t.Errorf("title/year = %q/%d", c.Title, c.Year)
|
|
}
|
|
if len(c.Authors) != 1 || c.Authors[0] != "J.R.R. Tolkien" {
|
|
t.Errorf("Authors = %v", c.Authors)
|
|
}
|
|
if c.Publisher != "Allen & Unwin" || c.Language != "eng" {
|
|
t.Errorf("publisher/language = %q/%q", c.Publisher, c.Language)
|
|
}
|
|
if c.ISBN != "9780261102217" {
|
|
t.Errorf("ISBN = %q", c.ISBN)
|
|
}
|
|
if !strings.Contains(c.CoverURL, "covers.openlibrary.org/b/id/8225261") {
|
|
t.Errorf("CoverURL = %q", c.CoverURL)
|
|
}
|
|
|
|
if gotTitle != "The Hobbit" || gotAuthor != "Tolkien" {
|
|
t.Errorf("query title/author = %q/%q", gotTitle, gotAuthor)
|
|
}
|
|
if gotLimit != "5" {
|
|
t.Errorf("limit = %q, want 5", gotLimit)
|
|
}
|
|
if gotUA == "" {
|
|
t.Error("no User-Agent sent; Open Library asks callers to identify themselves")
|
|
}
|
|
}
|
|
|
|
func TestOpenLibrarySkipsRequestWithoutATerm(t *testing.T) {
|
|
p := NewOpenLibrary()
|
|
cands, err := p.Search(context.Background(), Query{})
|
|
if err != nil {
|
|
t.Fatalf("Search: %v", err)
|
|
}
|
|
if len(cands) != 0 {
|
|
t.Errorf("got %d candidates for an empty query, want 0", len(cands))
|
|
}
|
|
}
|
|
|
|
func TestOpenLibraryReportsHTTPFailure(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusTooManyRequests)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
p := NewOpenLibrary()
|
|
p.BaseURL = srv.URL
|
|
|
|
if _, err := p.Search(context.Background(), Query{Title: "The Hobbit"}); err == nil {
|
|
t.Fatal("expected an error for a 429 response")
|
|
}
|
|
}
|