Files
silo-server/internal/metadata/local_image_source_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

232 lines
7.6 KiB
Go

package metadata
import (
"context"
"testing"
"github.com/Silo-Server/silo-server/internal/models"
)
func TestIsLocalImageSourcePath(t *testing.T) {
cases := []struct {
path string
want bool
}{
{"file:///media/movies/Film/poster.jpg", true},
{"FILE:///media/movies/Film/poster.jpg", true},
{" file:///media/poster.jpg ", true},
{"https://image.tmdb.org/t/p/original/a.jpg", false},
{"tvdb://banners/a.jpg", false},
{"local/movies/abc/poster/original.webp", false},
{"tmdb/movies/550/poster/original.webp", false},
{"", false},
}
for _, tc := range cases {
if got := isLocalImageSourcePath(tc.path); got != tc.want {
t.Errorf("isLocalImageSourcePath(%q) = %v, want %v", tc.path, got, tc.want)
}
}
}
func TestIsCachedImagePathExcludesLocalSources(t *testing.T) {
if isCachedImagePath("file:///media/movies/Film/poster.jpg") {
t.Fatal("file:// source must not be treated as a cached image path")
}
if !isCachedImagePath("local/movies/abc/deadbeef/poster/original.webp") {
t.Fatal("local/ cached key must remain a cached image path")
}
if !isCachedImagePath("tmdb/movies/550/poster/original.webp") {
t.Fatal("remote cached key must remain a cached image path")
}
}
func TestPreserveCachedArtworkRoutesLocalSource(t *testing.T) {
// No prior cached art: the file:// source must never land in *_path; the
// pre-cache window keeps the path empty until the cache job completes.
path, thumb, source := preserveCachedArtwork(
"file:///media/movies/Film/poster.jpg", "", "", "", "",
)
if path != "" {
t.Fatalf("path = %q, want empty during pre-cache window", path)
}
if thumb != "" {
t.Fatalf("thumb = %q, want empty", thumb)
}
if source != "file:///media/movies/Film/poster.jpg" {
t.Fatalf("source = %q", source)
}
// Prior cached art keeps serving while the local source waits to cache.
path, thumb, source = preserveCachedArtwork(
"file:///media/movies/Film/poster.jpg", "",
"tmdb/movies/550/poster/original.webp", "https://image.tmdb.org/a.jpg", "th",
)
if path != "tmdb/movies/550/poster/original.webp" {
t.Fatalf("path = %q, want prior cached key", path)
}
if thumb != "th" {
t.Fatalf("thumb = %q", thumb)
}
if source != "file:///media/movies/Film/poster.jpg" {
t.Fatalf("source = %q", source)
}
}
func TestPrepareItemImagesForQueueRoutesLocalSource(t *testing.T) {
item := &models.MediaItem{
ContentID: "movie-1",
Type: "movie",
PosterPath: "file:///media/movies/Film/poster.jpg",
}
prepareItemImagesForQueue(item, nil)
if item.PosterPath != "" {
t.Fatalf("PosterPath = %q, want empty during pre-cache window", item.PosterPath)
}
if item.PosterSourcePath != "file:///media/movies/Film/poster.jpg" {
t.Fatalf("PosterSourcePath = %q", item.PosterSourcePath)
}
}
func TestApplyBestImagesLocalCandidateAlwaysApplies(t *testing.T) {
item := &models.MediaItem{PosterPath: "tmdb/movies/550/poster/original.webp"}
applyBestImages(item, []RemoteImage{
{ProviderID: "nfo", URL: "file:///media/movies/Film/poster.jpg", Type: ImagePoster, Rating: 0},
}, MergeFillEmpty, "en")
if item.PosterPath != "file:///media/movies/Film/poster.jpg" {
t.Fatalf("PosterPath = %q, want local candidate to apply over existing art", item.PosterPath)
}
}
func TestApplyBestImagesRemoteSelectionUnchanged(t *testing.T) {
// Regression pins: a rating-0 remote candidate still cannot displace
// existing art in fill mode, while a rated one still can.
item := &models.MediaItem{PosterPath: "tmdb/movies/550/poster/original.webp"}
applyBestImages(item, []RemoteImage{
{ProviderID: "tmdb", URL: "https://image.tmdb.org/zero.jpg", Type: ImagePoster, Rating: 0},
}, MergeFillEmpty, "en")
if item.PosterPath != "tmdb/movies/550/poster/original.webp" {
t.Fatalf("PosterPath = %q, rating-0 remote must not displace existing art", item.PosterPath)
}
applyBestImages(item, []RemoteImage{
{ProviderID: "tmdb", URL: "https://image.tmdb.org/rated.jpg", Type: ImagePoster, Rating: 7.5},
}, MergeFillEmpty, "en")
if item.PosterPath != "https://image.tmdb.org/rated.jpg" {
t.Fatalf("PosterPath = %q, rated remote candidate must still apply", item.PosterPath)
}
empty := &models.MediaItem{}
applyBestImages(empty, []RemoteImage{
{ProviderID: "tmdb", URL: "https://image.tmdb.org/zero.jpg", Type: ImagePoster, Rating: 0},
}, MergeFillEmpty, "en")
if empty.PosterPath != "https://image.tmdb.org/zero.jpg" {
t.Fatalf("PosterPath = %q, rating-0 remote must still fill empty art", empty.PosterPath)
}
}
func TestBuildItemLocalizationRecordSkipsLocalCandidates(t *testing.T) {
// Language-neutral local files must not duplicate into per-language rows.
loc := buildItemLocalizationRecord(
nil,
"movie-1",
"de",
"movie",
&MetadataResult{},
[]RemoteImage{
{ProviderID: "nfo", URL: "file:///media/movies/Film/poster.jpg", Type: ImagePoster},
},
MergeFillEmpty,
"de",
false,
)
if loc.PosterPath != "" || loc.PosterSourcePath != "" {
t.Fatalf("localization picked up local candidate: path=%q source=%q", loc.PosterPath, loc.PosterSourcePath)
}
}
func TestItemImageCacheAttributionLocalSource(t *testing.T) {
item := &models.MediaItem{ContentID: "movie-1", TmdbID: "550"}
providerID, providerContentID := itemImageCacheAttribution(
item,
map[string]string{"tmdb": "550"},
nil,
"file:///media/movies/Film/poster.jpg",
)
if providerID != "local" {
t.Fatalf("providerID = %q, want local", providerID)
}
if providerContentID != "movie-1" {
t.Fatalf("providerContentID = %q, want the item content ID", providerContentID)
}
}
func TestEnqueueItemImagesAcceptsLocalSource(t *testing.T) {
service := &MetadataService{}
enqueuer := &recordingImageCacheJobEnqueuer{}
service.SetAutoCacheImages(true)
service.SetImageCacheJobEnqueuer(enqueuer)
item := &models.MediaItem{
ContentID: "movie-1",
Type: "movie",
PosterSourcePath: "file:///media/movies/Film/poster.jpg",
}
service.enqueueItemImages(context.Background(), item, nil, nil)
if len(enqueuer.inputs) != 1 {
t.Fatalf("enqueued %d jobs, want 1", len(enqueuer.inputs))
}
in := enqueuer.inputs[0]
if in.SourcePath != "file:///media/movies/Film/poster.jpg" {
t.Fatalf("SourcePath = %q", in.SourcePath)
}
if in.ProviderID != "local" {
t.Fatalf("ProviderID = %q, want local", in.ProviderID)
}
if in.ProviderContentID != "movie-1" {
t.Fatalf("ProviderContentID = %q", in.ProviderContentID)
}
}
func TestNormalizeImageCacheJobInputAcceptsLocalSource(t *testing.T) {
in, ok := normalizeImageCacheJobInput(EnqueueImageCacheJobInput{
TargetType: ImageCacheTargetItem,
TargetContentID: "movie-1",
SourcePath: "file:///media/movies/Film/poster.jpg",
ContentType: "movies",
})
if !ok {
t.Fatal("local file:// source must be accepted")
}
if in.ProviderID != "local" {
t.Fatalf("ProviderID = %q, want local", in.ProviderID)
}
// The other system schemes stay rejected.
for _, rejected := range []string{
"s3://bucket/key.jpg",
"local://x.jpg",
"upload://x.jpg",
"generated://x.jpg",
"tmdb/movies/550/poster/original.webp",
} {
if _, ok := normalizeImageCacheJobInput(EnqueueImageCacheJobInput{SourcePath: rejected}); ok {
t.Errorf("source %q must stay rejected", rejected)
}
}
}
func TestIsStableProviderImageFailureLocalClasses(t *testing.T) {
for _, text := range []string{
"local image missing: /media/movies/Film/poster.jpg",
"local image forbidden: /media/movies/Film/poster.jpg",
"local image path outside library roots: /etc/passwd",
"unexpected status 404",
} {
if !isStableProviderImageFailure(text) {
t.Errorf("expected stable failure classification for %q", text)
}
}
if isStableProviderImageFailure("local image read failed: io timeout") {
t.Error("transient local read errors must keep the normal backoff")
}
}