Files
silo-server/internal/metadata/nfo_mixed_library_test.go
91e1164090 feat(metadata): local NFO metadata and sidecar artwork (builtin chain provider) (#390)
* feat(metadata): register builtin NFO provider and broaden parsing

Phases A and B of the #216 local-NFO work, implemented test-first.

Registration & hint-first identity (Phase A):
- Migration seeds a reserved kind='builtin' silo.builtin installation
  and an 'nfo' metadata capability (default_enabled=false, priority 1
  for movie/series) with a partial unique index and documented Down.
- In-process builtin provider registry (internal/metadata/builtin.go);
  buildProviders returns the registered provider for builtin rows.
- Guard rails keep the reserved row out of every plugin surface (user
  plugin-settings, installations list, image resolvers, preload,
  auto-update, store Delete, mutation handlers -> 409); silo.builtin is
  a reserved manifest id.
- Startup sync materializes legacy content_level='' chains per level,
  then appends builtin capabilities disabled via
  AppendProviderToAllChains (idempotent); resolveEnabledProvidersBy
  priority now respects default_enabled=false.
- NFO uniqueids seed the trusted-hint machinery via IdentityHintProvider
  with per-mode conflict policy (stored IDs win on scheduled refresh,
  NFO wins on manual refresh, Identify skips NFO); ID-less candidates
  are excluded from provider-priority tie-breaks and nfo never counts
  as corroboration.
- Web chain-editor empty-state gate is now server-derived so builtin
  providers are reachable on plugin-less servers.

Parser breadth & sidecar hardening (Phase B):
- Parser covers the practical Kodi/Jellyfin field set for <movie> and
  <tvshow>: original title, tagline, runtime, dates, content rating,
  genres/studios/countries/tags, multi-source ratings with scale
  normalization, cast with roles/order, director/credits. Empty
  collections stay nil so merge early-returns apply.
- findNFO parses candidates and falls through on read/parse failure or
  root-type mismatch, so a stray movie.nfo cannot shadow tvshow.nfo;
  GetMetadata gains the same ContentType guard Search has.
- New FieldReleaseDates lock gates Year/ReleaseDate/First+LastAirDate
  in merge (Go) and the edit-metadata dialog (web), closing the gap
  where a manual refresh re-applied NFO dates over admin corrections.
- Merge-contract tests pin NFO fill semantics, genres whole-list
  first-provider-wins, and NFO edits propagating on manual refresh only.
- Docs: new admin wiki page (supported fields, merge semantics,
  naming-supplies-structure contract), index bullet, sidecar wording
  revision, v1-scope feature-detection note.

Zero behavior change while the provider is disabled (default); pinned
by CI-mode and DB-gated test suites.

Part of #216

AI-use disclosure: implemented with Claude Code (Fable 5) via
spec-driven TDD and agent-assisted implementation.

* feat(metadata): ingest local sidecar artwork and read series-depth NFO

Phases C and D of the #216 local-NFO work, implemented test-first, plus
the mixed-library use-case pins. Together these deliver the headline
case: a series absent from every remote database (e.g. a fitness
library) scans into a fully presented show -> named seasons -> titled
episodes tree from NFO files and sidecar art alone.

Local sidecar artwork through the S3 image cache (Phase C):
- The NFO provider implements ImageProvider: poster/backdrop/logo
  sidecar discovery with a fixed precedence map, symlink/non-regular
  rejection, an 8 MiB cap, and file:// source URLs at rating 0. Generic
  filenames apply only via the sidecar search paths, so a shared
  folder.jpg in a flat multi-movie directory applies to none.
- file:// becomes a live local source scheme: routed into *_source_path
  (never *_path), accepted by every image enqueue gate, attributed as
  provider "local", excluded from cached-path detection.
- The image-cache processor caches local files with lexical-on-logical
  confinement to the library roots, open-handle reads with re-checks,
  the same variant widths as remote art, and stable (7-day) failure
  classification. Keys land under
  local/{contentType}/{contentID}/{hash8}/{imageType}; superseded
  prefixes are cleaned on re-cache and item deletion.
- applyIfBetter gains a local exemption so rating-0 local art can fill
  matched items without being stickily displaced; ImageRequest carries
  additive sidecar path context.

Series depth (Phase D):
- SeasonsRequest/EpisodesRequest carry additive local path context
  (series roots, per-season directories, per-episode file paths),
  derived from naming at match time and reconstructed on refresh.
- season.nfo supplies season name/plot; NFO season numbers are advisory
  (directory-derived number wins with a Warn - naming owns structure).
  <episodedetails> gains aired/runtime/ratings; <basename>.nfo titles
  episodes and <basename>-thumb.ext supplies thumbs; filename SxxEyy
  wins over NFO numbers.
- Episode NFOs work without a season.nfo (provider seasons unioned with
  on-disk seasons); SynthesizeFallbackEpisodes always runs after persist
  so NFO-less episodes keep synthesized rows. Season/episode file:// art
  rides the Phase C pipeline unchanged.
- Migration adds season:1/episode:1 to the builtin NFO capability's
  default_priority (still default_enabled=false).

Mixed sports-library use case (tests only, no product change):
- Pins the classification contract for one library holding movie-shaped
  and show-shaped content (WWE PPV events as movies next to a "WWE
  SmackDown" show, NASCAR/F1/FIFA with partial TVDB/TMDB data): naming
  decides movie-vs-series per file before any provider runs; the NFO
  supplies metadata/identity but never flips type (ContentType guard);
  the per-root Type override is the correction path.
- NFO-driven type classification at scan time is recorded as an explicit
  deferred open question.

Part of #216

AI-use disclosure: implemented with Claude Code (Fable 5) via
spec-driven TDD and agent-assisted implementation.

* docs(metadata): document local NFO metadata architecture

Add a single as-built architecture page
(docs/architecture/local-nfo-metadata.md) for the #216 local-NFO
feature: the builtin registration model, hint-first identity semantics,
the file:// -> S3 artwork pipeline and its deployment constraint, series
depth, the mixed-library classification contract, and known limitations.

This replaces the working implementation plan, the per-phase specs, and
the narrow sidecar-artwork note, which were planning drafts and are left
untracked; admin-facing behavior remains in the wiki.

Part of #216

AI-use disclosure: planned, drafted, and consolidated with Claude Code
(Fable 5) using multi-agent exploration and adversarial review.

* fix(metadata): address PR review findings on NFO builtin provider

Fold in the valid, low-risk fixes surfaced by automated review on #390:

- imagecache: extract validateCacheRequest so CacheBytes (the local
  sidecar season/episode path) enforces the same episode-requires-season
  guard as Cache, preventing distinct episodes' art from colliding under
  one S3 key.
- image_cache_processor: close the sidecar symlink-swap window by
  rejecting the opened handle unless os.SameFile matches the Lstat'd
  file, so a leaf swapped to a symlink can't pull an out-of-root target
  into the public cache.
- plugins: guard the reserved builtin installation row in the store's
  Update, matching Delete, so its version/enabled/capabilities can never
  be rewritten even if a mutation slips past the HTTP layer.
- cmd/silo: bound SyncBuiltinProviderChains with a 30s timeout so a stuck
  DB round-trip fails fast at startup instead of hanging.
- metadata: panic instead of silently no-op'ing on an invalid
  RegisterBuiltinProvider call (init-time programmer error).
- docs: correct the media-folder-and-naming NFO paragraph to state
  season/episode NFOs and sidecar artwork are actively read.

---------

Co-authored-by: Quick104 <31828688+Quick104@users.noreply.github.com>
2026-07-16 17:55:36 -04:00

395 lines
15 KiB
Go

package metadata_test
// Mixed sports library tests (plan §2a win 3): one library of type "mixed"
// holding WWE pay-per-view events as movies alongside "WWE SmackDown" as a
// show, metadata driven mainly by NFO (sports content often has no TVDB/TMDB
// presence). The classification contract: naming decides movie-vs-series per
// file before any metadata provider runs; NFOs supply metadata and identity,
// never type (the provider's ContentType guard ignores a tvshow.nfo next to
// a movie-classified file). These tests reuse the NFOSeriesHarness bridge
// and the real built-in NFO provider, mirroring the fitness-library tests.
import (
"context"
"path/filepath"
"strings"
"sync"
"testing"
"github.com/Silo-Server/silo-server/internal/metadata"
"github.com/Silo-Server/silo-server/internal/metadata/nfo"
"github.com/Silo-Server/silo-server/internal/naming"
)
// ---------------------------------------------------------------------------
// (a) Classification pin: naming decides type in a mixed library.
// ---------------------------------------------------------------------------
// Sports-flavored classification cases for the mixed default branch
// (internal/naming/filename.go): SxxEyy/season-folder routes to the series
// lane, everything else to the movie lane. NFO contents never participate.
func TestMixedLibrary_NamingDecidesType(t *testing.T) {
t.Parallel()
cases := []struct {
name string
path string
wantType string
wantSeason int
wantEpisode int
}{
{
name: "PPV event in its own movie folder",
path: "/sports/WWE/WrestleMania 41 (2025)/WrestleMania 41 (2025).mkv",
wantType: "movie",
},
{
name: "bare PPV event file defaults to the movie lane",
path: "/sports/WWE/Royal Rumble 2025.mkv",
wantType: "movie",
},
{
name: "weekly show with season folder and SxxEyy",
path: "/sports/WWE/WWE SmackDown/Season 27/WWE SmackDown S27E15.mkv",
wantType: "series",
wantSeason: 27,
wantEpisode: 15,
},
{
name: "SxxEyy without a season folder still routes to series",
path: "/sports/WWE/WWE SmackDown/WWE SmackDown S27E15.mkv",
wantType: "series",
wantSeason: 27,
wantEpisode: 15,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
ctx := naming.ResolvePathContext(tc.path, "mixed")
if ctx.Type != tc.wantType {
t.Fatalf("ResolvePathContext(%q, mixed).Type = %q, want %q", tc.path, ctx.Type, tc.wantType)
}
if ctx.SeasonNum != tc.wantSeason || ctx.EpisodeNum != tc.wantEpisode {
t.Errorf("season/episode = S%dE%d, want S%dE%d",
ctx.SeasonNum, ctx.EpisodeNum, tc.wantSeason, tc.wantEpisode)
}
})
}
}
// ---------------------------------------------------------------------------
// (b) Movie lane: an NFO-only PPV event.
// ---------------------------------------------------------------------------
func TestMixedLibrary_MovieLane_NFOOnlyPPV(t *testing.T) {
root := filepath.Join(t.TempDir(), "WWE", "WrestleMania 41 (2025)")
movieFile := filepath.Join(root, "WrestleMania 41 (2025).mkv")
writeFixtureFile(t, movieFile, "video")
writeFixtureFile(t, filepath.Join(root, "movie.nfo"),
`<movie><title>WrestleMania 41</title><plot>Two nights from Las Vegas.</plot><year>2025</year></movie>`)
writeFixtureFile(t, filepath.Join(root, "poster.jpg"), "ppv-poster")
x := metadata.NewNFOSeriesHarness()
ctx := context.Background()
const movieID = "local-wm41"
if err := x.SeedMovieSkeleton(movieID, "WrestleMania 41"); err != nil {
t.Fatalf("seed skeleton: %v", err)
}
result, err := x.Service().ProcessWithProviders(ctx, metadata.ProcessRequest{
ContentID: movieID,
Hints: &metadata.MatchHints{
Title: "WrestleMania 41",
Year: 2025,
Type: "movie",
FilePath: movieFile,
RepresentativeFilePath: movieFile,
AllGroupFilePaths: []string{movieFile},
PrimarySidecarSearchPaths: []string{root},
},
Language: "en",
Mode: metadata.ModeInitialMatch,
}, []metadata.Provider{nfo.NewProvider()})
if err != nil {
t.Fatalf("ProcessWithProviders: %v", err)
}
if result == nil || !result.Updated {
t.Fatalf("result = %#v, want Updated=true", result)
}
item, err := x.Item(movieID)
if err != nil {
t.Fatalf("PPV movie not found under local id: %v", err)
}
if item.Status != "matched" {
t.Errorf("item status = %q, want matched", item.Status)
}
if item.Type != "movie" {
t.Errorf("item type = %q, want movie (NFO must not flip type)", item.Type)
}
if item.Title != "WrestleMania 41" || item.Overview != "Two nights from Las Vegas." {
t.Errorf("item title/overview = %q/%q", item.Title, item.Overview)
}
if item.TmdbID != "" || item.TvdbID != "" || item.ImdbID != "" {
t.Errorf("title-only PPV must carry no remote ids, got tmdb=%q tvdb=%q imdb=%q",
item.TmdbID, item.TvdbID, item.ImdbID)
}
if want := "file://" + filepath.Join(root, "poster.jpg"); item.PosterSourcePath != want {
t.Errorf("item PosterSourcePath = %q, want %q", item.PosterSourcePath, want)
}
jobs := x.EnqueuedImageJobs()
found := false
for _, job := range jobs {
if job.SourcePath == "file://"+filepath.Join(root, "poster.jpg") {
found = true
if job.TargetType != metadata.ImageCacheTargetItem || job.ProviderID != "local" {
t.Errorf("poster job = target %q provider %q, want item/local", job.TargetType, job.ProviderID)
}
}
if !strings.HasPrefix(job.SourcePath, "file://") {
t.Errorf("enqueued non-local source %q", job.SourcePath)
}
}
if !found {
t.Errorf("PPV poster not enqueued as a file:// local source (jobs: %#v)", jobs)
}
}
// ---------------------------------------------------------------------------
// (c) Series lane: the weekly show next to the PPVs, full NFO depth.
// ---------------------------------------------------------------------------
func TestMixedLibrary_SeriesLane_WeeklyShow(t *testing.T) {
root := filepath.Join(t.TempDir(), "WWE", "WWE SmackDown")
writeFixtureFile(t, filepath.Join(root, "tvshow.nfo"),
`<tvshow><title>WWE SmackDown</title><plot>Friday night wrestling.</plot></tvshow>`)
s27 := filepath.Join(root, "Season 27")
writeFixtureFile(t, filepath.Join(s27, "season.nfo"),
`<season><title>SmackDown 2025</title><seasonnumber>27</seasonnumber></season>`)
writeFixtureFile(t, filepath.Join(s27, "poster.jpg"), "s27-poster")
episodeFile := filepath.Join(s27, "WWE SmackDown S27E15.mkv")
writeFixtureFile(t, episodeFile, "video")
writeFixtureFile(t, filepath.Join(s27, "WWE SmackDown S27E15.nfo"),
`<episodedetails><title>SmackDown: April 11, 2025</title><plot>Go-home show before Mania.</plot></episodedetails>`)
writeFixtureFile(t, filepath.Join(s27, "WWE SmackDown S27E15-thumb.jpg"), "e15-thumb")
x := metadata.NewNFOSeriesHarness()
ctx := context.Background()
const seriesID = "local-smackdown"
if err := x.SeedSeriesSkeleton(seriesID, "WWE SmackDown"); err != nil {
t.Fatalf("seed skeleton: %v", err)
}
result, err := x.Service().ProcessWithProviders(ctx, metadata.ProcessRequest{
ContentID: seriesID,
Hints: &metadata.MatchHints{
Title: "WWE SmackDown",
Type: "series",
FilePath: episodeFile,
RepresentativeFilePath: episodeFile,
AllGroupFilePaths: []string{episodeFile},
PrimarySidecarSearchPaths: []string{root},
},
Language: "en",
Mode: metadata.ModeInitialMatch,
}, []metadata.Provider{nfo.NewProvider()})
if err != nil {
t.Fatalf("ProcessWithProviders: %v", err)
}
if result == nil || !result.Updated {
t.Fatalf("result = %#v, want Updated=true", result)
}
item, err := x.Item(seriesID)
if err != nil {
t.Fatalf("show not found under local id: %v", err)
}
if item.Type != "series" || item.Title != "WWE SmackDown" {
t.Errorf("item = type %q title %q, want series / WWE SmackDown", item.Type, item.Title)
}
seasons := x.Seasons(seriesID)
if len(seasons) != 1 || seasons[0].SeasonNumber != 27 || seasons[0].Title != "SmackDown 2025" {
t.Fatalf("seasons = %#v, want season 27 titled SmackDown 2025", seasons)
}
if want := "file://" + filepath.Join(s27, "poster.jpg"); seasons[0].PosterSourcePath != want {
t.Errorf("season PosterSourcePath = %q, want %q", seasons[0].PosterSourcePath, want)
}
episodes := x.Episodes(seriesID)
if len(episodes) != 1 || episodes[0].SeasonNumber != 27 || episodes[0].EpisodeNumber != 15 {
t.Fatalf("episodes = %#v, want S27E15", episodes)
}
if episodes[0].Title != "SmackDown: April 11, 2025" || episodes[0].Overview != "Go-home show before Mania." {
t.Errorf("episode title/plot = %q/%q", episodes[0].Title, episodes[0].Overview)
}
if want := "file://" + filepath.Join(s27, "WWE SmackDown S27E15-thumb.jpg"); episodes[0].StillSourcePath != want {
t.Errorf("episode StillSourcePath = %q, want %q", episodes[0].StillSourcePath, want)
}
}
// ---------------------------------------------------------------------------
// (d) Cross-type guard: a tvshow.nfo next to a movie-classified file.
// ---------------------------------------------------------------------------
// A tvshow.nfo adjacent to a movie-classified PPV file must be ignored by
// the ContentType guard: no series metadata is injected into the movie, and
// with nothing else to match against the item stays an unmatched skeleton
// (pipeline-level mirror of the Phase B provider guard tests).
func TestMixedLibrary_TVShowNFONextToMovieIgnored(t *testing.T) {
root := filepath.Join(t.TempDir(), "WWE", "WrestleMania 41 (2025)")
movieFile := filepath.Join(root, "WrestleMania 41 (2025).mkv")
writeFixtureFile(t, movieFile, "video")
// Only a series-shaped NFO — e.g. an export tool misplaced it.
writeFixtureFile(t, filepath.Join(root, "tvshow.nfo"),
`<tvshow><title>WWE SmackDown</title><plot>Wrong sidecar.</plot></tvshow>`)
x := metadata.NewNFOSeriesHarness()
ctx := context.Background()
const movieID = "local-wm41-guard"
if err := x.SeedMovieSkeleton(movieID, "WrestleMania 41"); err != nil {
t.Fatalf("seed skeleton: %v", err)
}
result, err := x.Service().ProcessWithProviders(ctx, metadata.ProcessRequest{
ContentID: movieID,
Hints: &metadata.MatchHints{
Title: "WrestleMania 41",
Year: 2025,
Type: "movie",
FilePath: movieFile,
RepresentativeFilePath: movieFile,
AllGroupFilePaths: []string{movieFile},
PrimarySidecarSearchPaths: []string{root},
},
Language: "en",
Mode: metadata.ModeInitialMatch,
}, []metadata.Provider{nfo.NewProvider()})
if err != nil {
t.Fatalf("ProcessWithProviders: %v", err)
}
if result != nil && result.Updated {
t.Fatalf("result = %#v, want Updated=false (mismatched NFO must not produce a match)", result)
}
item, err := x.Item(movieID)
if err != nil {
t.Fatalf("load skeleton: %v", err)
}
if item.Type != "movie" {
t.Errorf("item type = %q, want movie (tvshow.nfo must not flip type)", item.Type)
}
if item.Title != "WrestleMania 41" {
t.Errorf("item title = %q, want untouched WrestleMania 41 (no series title injection)", item.Title)
}
if item.Overview != "" {
t.Errorf("item overview = %q, want empty (series plot must not be injected)", item.Overview)
}
if item.Status != "pending_match" {
t.Errorf("item status = %q, want pending_match (skeleton unchanged)", item.Status)
}
}
// ---------------------------------------------------------------------------
// (e) Mixed identity: an ID-bearing PPV next to NFO-only ones.
// ---------------------------------------------------------------------------
// remoteMovieStubProvider is a canned remote movie provider that records the
// provider IDs it was asked to fetch metadata for (Phase A harness shape).
type remoteMovieStubProvider struct {
mu sync.Mutex
slug string
metadata *metadata.MetadataResult
lastMetaIDs map[string]string
}
func (p *remoteMovieStubProvider) Slug() string { return p.slug }
func (p *remoteMovieStubProvider) Name() string { return p.slug }
func (p *remoteMovieStubProvider) ForTypes() []string { return []string{"movie", "series"} }
func (p *remoteMovieStubProvider) Search(_ context.Context, _ metadata.SearchQuery) ([]metadata.SearchResult, error) {
return nil, nil // remote search knows nothing about obscure sports events
}
func (p *remoteMovieStubProvider) GetMetadata(_ context.Context, req metadata.MetadataRequest) (*metadata.MetadataResult, error) {
p.mu.Lock()
defer p.mu.Unlock()
p.lastMetaIDs = make(map[string]string, len(req.ProviderIDs))
for k, v := range req.ProviderIDs {
p.lastMetaIDs[k] = v
}
if p.metadata != nil {
cp := *p.metadata
return &cp, nil
}
return &metadata.MetadataResult{}, nil
}
func (p *remoteMovieStubProvider) lastMetadataIDs() map[string]string {
p.mu.Lock()
defer p.mu.Unlock()
return p.lastMetaIDs
}
// A PPV whose movie.nfo carries a tmdb <uniqueid> anchors a remote match via
// the trusted-hint phase even when remote search-by-title finds nothing —
// remote-capable events enrich fully while NFO-only events (case b) fall
// back locally, side by side in the same mixed library.
func TestMixedLibrary_PPVWithUniqueIDAnchorsRemoteMatch(t *testing.T) {
root := filepath.Join(t.TempDir(), "WWE", "WrestleMania 41 (2025)")
movieFile := filepath.Join(root, "WrestleMania 41 (2025).mkv")
writeFixtureFile(t, movieFile, "video")
writeFixtureFile(t, filepath.Join(root, "movie.nfo"),
`<movie><title>WrestleMania 41</title><year>2025</year><uniqueid type="tmdb">424242</uniqueid></movie>`)
x := metadata.NewNFOSeriesHarness()
ctx := context.Background()
remote := &remoteMovieStubProvider{
slug: "tmdb",
metadata: &metadata.MetadataResult{
HasMetadata: true,
Title: "WrestleMania 41",
Year: 2025,
Overview: "From TMDB.",
ProviderIDs: map[string]string{"tmdb": "424242"},
},
}
result, err := x.Service().ProcessWithProviders(ctx, metadata.ProcessRequest{
Hints: &metadata.MatchHints{
Title: "WrestleMania 41",
Year: 2025,
Type: "movie",
FilePath: movieFile,
RepresentativeFilePath: movieFile,
AllGroupFilePaths: []string{movieFile},
PrimarySidecarSearchPaths: []string{root},
},
Language: "en",
Mode: metadata.ModeInitialMatch,
}, []metadata.Provider{nfo.NewProvider(), remote})
if err != nil {
t.Fatalf("ProcessWithProviders: %v", err)
}
if result == nil || !result.Updated {
t.Fatalf("result = %#v, want Updated=true", result)
}
if got := remote.lastMetadataIDs()["tmdb"]; got != "424242" {
t.Errorf("remote GetMetadata tmdb id = %q, want 424242 (NFO uniqueid must anchor identity)", got)
}
item, err := x.Item(result.ContentID)
if err != nil {
t.Fatalf("load matched item: %v", err)
}
if item.TmdbID != "424242" {
t.Errorf("item tmdb id = %q, want 424242", item.TmdbID)
}
if item.Overview != "From TMDB." {
t.Errorf("item overview = %q, want remote enrichment applied", item.Overview)
}
}