mirror of
https://github.com/navidrome/navidrome.git
synced 2026-08-31 07:30:32 +00:00
* feat(artwork): add a resolution chain trace collector * feat(artwork): trace the local priority chain * fix(artwork): record priority candidates the chain never evaluated * refactor(artwork): report never-evaluated candidates as skipped * feat(artwork): trace external agents at the gate seam * feat(artwork): add repository queries to enqueue by current source * feat(artwork): expose a tracing resolver for the CLI * feat(artwork): read a single queue row by item The explain CLI must report whether an item is queued, at what priority and when it retries; the queue repository could only be drained in eligibility batches, which cannot see a row that is still backing off. * feat(cli): add artwork explain Prints why an item has the artwork it has: the stored state, its queue row, the governing config, the resolver's priority-chain walk and the verdict. Offline by default so a diagnostic run cannot add load to an external provider; --live asks the agents for real. Playlists and radios do not walk a priority chain, so they report that instead of an empty chain table. * fix(artwork): trace an external tier that never reaches an agent A configured 'external' token vanished from the chain when no enabled agent provided images for that entity type, and for synthetic artists, leaving the trace unable to say whether the tier was even considered. * fix(cli): never state an artwork outcome the walk did not observe A transient external failure traced as 'error' fell through to 'not resolved', which is the most common state behind a missing-artwork report. It is now indeterminate, and an offline win that a skipped higher-priority external candidate could have taken says so instead of naming a winner the live chain might not pick. * feat(cli): add artwork refresh * feat(cli): add artwork reprocess Bulk re-enqueues artwork by kind and/or by the source an item currently resolves from, previewing the matched count and confirming before queueing. The preview counts with CountBySource (rows matched) and reports separately what EnqueueBySource inserted: its DO NOTHING conflict policy leaves an already-queued row untouched, so the two numbers differ and the output must not claim the skipped rows were re-queued. An unknown --source is rejected against the sources present in item_artwork, rather than silently matching nothing and printing a reassuring 0. * fix(cli): cover the reprocess selection rule and validate sources table-wide The reconciliation that makes --source alone target every kind was only exercised through runReprocess, which no test calls: mutating it to `all := reprocessAll` left the suite green. It is now reprocessSelectsAll, covered for all three selectors. Scoping source validation to the selected kinds made the same well-formed filter valid or invalid depending on which other kinds were selected, and its error read the same for a typo as for a source that simply does not apply to the chosen kind. Validation is now table-wide: a typo still aborts, while a valid-but-inapplicable source falls through to "Nothing matches". Also: the prompt now counts only the kinds that reach an external agent as external cost, and --dry-run on an empty selection reports a dry run. * fix(cli): cover the reprocess --yes guard and preview the external cost Mutating the --yes check to `if true` left the suite green, so the one bypass of the confirmation was unverified. The choice is now reprocessConfirm(yes, in), covered in both directions. The external estimate only reached the operator through the prompt, which --dry-run skips — hiding the number in the one mode that exists to show it before committing. The preview now carries it, and the prompt drops the clause when no lookup will be made. An empty selection says so again under --dry-run. * feat(artwork): add read-only queue and absent counters Both are needed by the artwork status CLI: a queue breakdown by kind and priority, and the absent totals split against the recheck cutoff. * feat(cli): add artwork status Reports the queue, where artwork currently resolves from, absent counts against the 24h recheck window, and the stored config fingerprint versus the current one — the line that turns 'why is my server re-resolving everything?' into one command. fingerprint() and staleAbsentAge are exported so the CLI reports the values backfill itself compares, instead of a second copy of the formula that can silently drift. * fix(cli): lead the artwork status backfill line with the queued backlog By the time anyone runs a diagnostic, backfill has usually already stored the new fingerprint, so 'up to date' was printed while thousands of items churned through external providers. The backlog is the finding; the fingerprint is context. Also echoes the config inputs the fingerprint covers, so a change can be traced to the setting that caused it, and pins the rendered rows: the Absent values, the queue TOTAL and a queue-scoped kind/priority pair were all unasserted, so kindName and priorityName were effectively untested. FingerprintInputs is now the single listing ConfigFingerprint hashes; a pinned hash proves the value did not change. * refactor(artwork): export the trace outcome vocabulary The CLI hardcoded the outcome literals and the "external:" prefix, so renaming a constant's value in core/artwork left cmd compiling and the suite green while `artwork explain` silently degraded its verdict. Renaming a value now fails the golden vocabulary test in core/artwork and the explainResult tests in cmd. * fix(cli): keep the re-enqueue warning when a backfill is already running A stale stored fingerprint with items already queued is the worst state the system can be in: a second full re-enqueue is pending on top of the one running. The line carried the weakest wording of the three, and was untested. * refactor(artwork): drop the unreachable breaker branch from the tracing gate --live wires the tracing gate straight to passthroughGate, so errBreakerOpen can never reach it; the test only passed by injecting a fake gate. * refactor(artwork): delete the never-emitted not-reached outcome Candidates after the winner are lower priority and say nothing about why a source won; the ones that matter sit above it and are already recorded. * refactor(artwork): make the trace nil-safe in one place only add already handles a nil trace, so record's own guard was dead; Steps was the odd one out and would panic where every other method tolerates nil. * refactor(artwork): export the trace types directly ChainTrace and TraceStep were unexported types re-exported through aliases, which existed only so the CLI had a name to refer to them by. The types are public API — Resolver.Steps returns []TraceStep and the CLI constructs a ChainTrace — so name them that way and drop the indirection. Encapsulation is unchanged: add, mu and steps stay unexported, so only this package can write a step. * refactor(cli): simplify parseArtworkKind with slices.Contains Replaces a nested loop and a manual append with slices.Contains and the repo's slice.Map helper. Same behaviour, same error message. * fix(cli): print the absent artwork source under the name --source accepts `artwork explain` rendered the stored empty source as "(absent)", while `artwork reprocess --source` only accepts "absent", so pasting what explain printed straight back into reprocess was rejected as an unknown source. * refactor(artwork): own the kind list and the chain predicate in the package Export RecheckKinds and add WalksPriorityChain so the CLI stops keeping its own copies of both, and unexport externalCandidate, which nothing outside the package consumes. * refactor(cli): drop the artwork command's duplicated state and formatting Reuse artwork.RecheckKinds and artwork.WalksPriorityChain, extract newTabWriter and externalEstimate, fold reprocessSelectsAll into selectedKinds, and derive the queue total and the walks-chain flag instead of carrying them in the report structs. * test(persistence): drop two artwork-queue specs that cannot fail One seeded hash and source together and then asserted the two counts agree, so its setup guaranteed the result; the other repeated the count-does-not- enqueue property already covered by the CountBySource spec. * refactor(artwork): rename Resolver to TracingResolver for clarity * fix(cli): count playlists in the artwork reprocess external estimate The estimate used WalksPriorityChain, which is true only for artist and album, so a playlist-only reprocess reported "External lookups: none" and the confirmation prompt dropped the external-cost warning. Playlists do reach the network: through the m3u ExternalImageURL fetch when EnableM3UExternalAlbumArt is on, and — verified by test — through the generated grid, whose tiles resolve album art via the full album priority chain. Adds artwork.MayFetchExternal, a config-aware predicate for "can this kind's resolver reach the network", and uses it for the estimate. WalksPriorityChain keeps its separate job of deciding whether explain prints a chain block. * fix(cli): estimate artwork reprocess external lookups per agent, not per item The reprocess prompt billed one external lookup per externally-capable item. fetchArtistImage/fetchAlbumImage try every enabled image agent and stop early only on a hit, and resolvePlaylist can fetch the m3u image and then resolve up to four sampled albums for the grid, each walking the album agents again. The number the operator confirmed could understate real provider traffic several fold, in the prompt whose whole job is to stop a provider flood. ExternalLookupsPerItem now multiplies by the visible image-agent count and adds the playlist grid factor. It stays a floor: the CLI never calls Manager.Start(), so the plugin registry is empty and plugin-provided agents are dropped by getEnabledAgentNames. On an install with 5 agents of which 3 are plugins the count is well under the truth, so the wording is now "at least N" rather than "up to N" — a zero visible count still bills one lookup for the same reason. Fixing the plugin visibility is out of scope: Manager.Start() needs a Subsonic router and writes to the DB via syncPlugins, breaking this command group's read-only guarantee. * fix(cli): state the artwork reprocess estimate as an estimate, not a bound Neither bound is true. A ceiling is false because plugin agents are invisible to a CLI that never starts the plugin manager, and a floor is false because a local hit ends the walk before any agent is asked and a hit on the first agent skips the rest. "at least N" traded one wrong claim for another. The line now names its blind spots instead: External lookups: ~340 estimated (plugin agents not counted; local hits may need fewer). The same line is reused in the confirmation prompt, and the zero case still reads "External lookups: none." with the prompt dropping the clause entirely. The count itself is unchanged. * fix(cli): account for every configured agent in artwork explain The Agents: line printed the raw config while the Chain only showed the agents the CLI could construct, with nothing explaining the gap: plugin agents are never registered in a CLI that does not start the plugin manager, and a built-in without credentials returns nil. Three of five agents could vanish, including ones ranked above the one shown. Also treat a live external error before the winning hit like the already-handled would-try case: the resolver serves such a hit provisionally and retries later, so the verdict is indeterminate. The Result line is still not qualified when an unavailable agent might have won; that needs agent ranking, and is left to the follow-up that makes the CLI load plugin agents for real. * fix(cli): do not call an external artwork win indeterminate explainResult qualified the verdict whenever an external OutcomeError appeared before the winning hit. When a later external agent returns an image, fetchArtistImage/fetchAlbumImage discard the earlier error, so extError is false: the worker settles the item and schedules no retry. Telling the operator it may resolve differently on a retry was wrong. The warning is only correct when a lower-priority local source won while an external error was recorded, which is the case that carries extError. * fix(cli): accept --source absent when nothing is currently absent validateSources checks the requested sources against the ones item_artwork actually uses, to catch a typo. The reserved empty source (spelled 'absent' on the CLI) is a valid filter even when it matches nothing, so a scheduled 'artwork reprocess --source absent --yes' stopped working the moment the library finished resolving. Treat it as intrinsically valid and let the existing zero-match path report it. * feat(artwork): explain disc and media file artwork from the CLI `artwork explain` rejected `dc` and `mf` because it validated against RecheckKinds, the list of kinds the backfill revisits. Those are different questions: a kind with no recheck path still has artwork someone can report as wrong. Disc artwork now walks DiscArtPriority under a trace, so explain reports which entry won and why the others lost, including entries that map to no source at all (external is unsupported, a disc with no subtitle, an album folder with no images). Media file artwork traces its single embedded candidate, separating "EnableMediaFileCoverArt is off" from "the track has no embedded art" — stored state cannot tell those apart. Each command now validates against the kinds it can actually serve: explain takes all six, refresh takes artwork.RefreshableKinds (which nativeapi now shares instead of keeping its own copy), reprocess still takes RecheckKinds. Disc artwork stays out of refresh: the worker cannot resolve it, so the queue row would be rejected on every drain. WalksPriorityChain becomes Explainable, and ResolveArtist/ResolveAlbum collapse into Resolve(kind, id). * refactor(artwork): one disc-artwork walk for serving and explain resolveDisc duplicated the loop selectImageReader already ran: try each source in priority order, take the first that yields an image. The serving path and the CLI diverged on two details as a result — only selectImageReader checked ctx between candidates and logged each attempt. Both now call discArtworkReader.selectImage, which takes the chainState the CLI already uses for the other kinds. The serving path passes an untraced one, whose nil trace makes recording a no-op. selectImageReader had no other caller and is gone. The disc tests move from fromDiscArtPriority to discCandidates, so they assert the skip reason for an entry that maps to no source rather than that it silently vanished, and cancellation mid-walk is now covered. * fix(artwork): reject a nil reader in the resize cache instead of panicking resizedItem.Reader closes what open() hands back, so an open() that reports "no image" as (nil, nil) rather than an error takes the request down with a nil-pointer panic. Every caller returns an error today, and no test covered it: the resolution e2e harness stubs the resize reader out entirely, so no e2e path reaches this code at all. Guard it and cover Reader directly. * refactor(artwork): move the keeps-state fact into core, drop a redundant guard keepsArtworkState lived in package cmd and re-derived by hand what RefreshableKinds already encodes: the same five-of-six kinds. It is now artwork.KeepsState, beside the list, with a test pinning the two together — nothing else stopped them drifting, and a drift would have explain report stored state for a kind that keeps none. serveDisc's closure also hand-rolled a nil-reader error that both consumers of open() now produce themselves: serveSource for a full-size request, resizedItem.Reader for a resized one. * fix(artwork): route disc candidates through the shared resolvers openCandidate ran its own source loop and threw the error away, so a disc track that exists but cannot be parsed traced as "miss" — indistinguishable from a track with no embedded art. fromTag and fromFFmpegTag already report that case as errSourceUnreadable; only this loop was discarding it. Telling those two apart is what the trace is for. Candidates now carry a resolve func instead of raw sources: embedded goes to resolveEmbedded, and the folder-backed entries to resolveFolderSource, extracted from resolveFolderFile so both callers classify an unopenable file the same way. openCandidate and its absolute-path special case go away with it. Disc's own fromExternalFile and fromDiscSubtitle still swallow open errors, so folder candidates cannot report unreadable yet; that is a change to their error contracts. * fix(artwork): report an unreadable local candidate as indeterminate processor.acquire treats resolution.localError exactly as it treats extError: a fault is not a definitive "no image", so it retries instead of settling absent. explainResult qualified only the external case, so a chain that ended on an unreadable local candidate printed "not resolved" — the one verdict that says the walk was conclusive. The qualification belongs only to the unresolved branch. chainState.try stamps extErr onto a hit and deliberately drops localErr, so an unreadable step followed by a hit is settled as found and must not carry a warning; a test pins that. Found by Codex on 5f65d7cfa.
540 lines
18 KiB
Go
540 lines
18 KiB
Go
package artwork
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/navidrome/navidrome/model"
|
|
"github.com/navidrome/navidrome/tests"
|
|
"github.com/navidrome/navidrome/utils/slice"
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
)
|
|
|
|
var _ = Describe("Disc Artwork Reader", func() {
|
|
Describe("extractDiscNumber", func() {
|
|
DescribeTable("extracts disc number from filename based on glob pattern",
|
|
func(pattern, filename string, expectedNum int, expectedOk bool) {
|
|
num, ok := extractDiscNumber(pattern, filename)
|
|
Expect(ok).To(Equal(expectedOk))
|
|
if expectedOk {
|
|
Expect(num).To(Equal(expectedNum))
|
|
}
|
|
},
|
|
// Standard disc patterns
|
|
Entry("disc1.jpg", "disc*.*", "disc1.jpg", 1, true),
|
|
Entry("disc2.png", "disc*.*", "disc2.png", 2, true),
|
|
Entry("disc01.jpg", "disc*.*", "disc01.jpg", 1, true),
|
|
Entry("disc02.png", "disc*.*", "disc02.png", 2, true),
|
|
Entry("disc10.jpg", "disc*.*", "disc10.jpg", 10, true),
|
|
|
|
// CD patterns
|
|
Entry("cd1.jpg", "cd*.*", "cd1.jpg", 1, true),
|
|
Entry("cd02.png", "cd*.*", "cd02.png", 2, true),
|
|
|
|
// No number in filename
|
|
Entry("disc.jpg has no number", "disc*.*", "disc.jpg", 0, false),
|
|
Entry("cd.jpg has no number", "cd*.*", "cd.jpg", 0, false),
|
|
|
|
// Extra text after number
|
|
Entry("disc2-bonus.jpg", "disc*.*", "disc2-bonus.jpg", 2, true),
|
|
Entry("disc01_front.png", "disc*.*", "disc01_front.png", 1, true),
|
|
|
|
// Case insensitive (filename already lowered by caller)
|
|
Entry("Disc1.jpg lowered", "disc*.*", "disc1.jpg", 1, true),
|
|
|
|
// HasPrefix guard: filename doesn't share the pattern's literal prefix
|
|
Entry("cover.jpg with disc*.* (no prefix match)", "disc*.*", "cover.jpg", 0, false),
|
|
|
|
// Pattern with no wildcard before dot
|
|
Entry("front1.jpg with front*.*", "front*.*", "front1.jpg", 1, true),
|
|
|
|
// '?' single-char wildcard
|
|
Entry("disc?.jpg with disc1.jpg", "disc?.jpg", "disc1.jpg", 1, true),
|
|
Entry("disc?.jpg with disc2.jpg", "disc?.jpg", "disc2.jpg", 2, true),
|
|
Entry("cd??.jpg with cd07.jpg", "cd??.jpg", "cd07.jpg", 7, true),
|
|
|
|
// '[...]' character class wildcard
|
|
Entry("cd[12].jpg with cd1.jpg", "cd[12].jpg", "cd1.jpg", 1, true),
|
|
Entry("cd[12].jpg with cd2.jpg", "cd[12].jpg", "cd2.jpg", 2, true),
|
|
Entry("disc[0-9].jpg with disc5.jpg", "disc[0-9].jpg", "disc5.jpg", 5, true),
|
|
|
|
// Literal pattern (no wildcard) returns false
|
|
Entry("shellac.png literal", "shellac.png", "shellac.png", 0, false),
|
|
)
|
|
})
|
|
|
|
Describe("fromExternalFile", func() {
|
|
var (
|
|
ctx context.Context
|
|
tmpDir string
|
|
)
|
|
|
|
BeforeEach(func() {
|
|
ctx = context.Background()
|
|
tmpDir = GinkgoT().TempDir()
|
|
})
|
|
|
|
// createFile creates the file on disk and returns its library-relative forward-slash path.
|
|
createFile := func(relPath string) string {
|
|
fullPath := filepath.Join(tmpDir, filepath.FromSlash(relPath))
|
|
Expect(os.MkdirAll(filepath.Dir(fullPath), 0755)).To(Succeed())
|
|
Expect(os.WriteFile(fullPath, []byte("image data"), 0600)).To(Succeed())
|
|
return relPath
|
|
}
|
|
|
|
// removeFile removes a library-relative file from disk.
|
|
removeFile := func(relPath string) {
|
|
Expect(os.Remove(filepath.Join(tmpDir, filepath.FromSlash(relPath)))).To(Succeed())
|
|
}
|
|
|
|
It("matches file with disc number in single-folder album", func() {
|
|
f1 := createFile("album/disc1.jpg")
|
|
f2 := createFile("album/disc2.jpg")
|
|
reader := &discArtworkReader{
|
|
discNumber: 1,
|
|
imgFiles: []string{f1, f2},
|
|
discFoldersRel: map[string]bool{"album": true},
|
|
lib: libraryView{FS: osDirFS{os.DirFS(tmpDir)}, absRoot: tmpDir},
|
|
}
|
|
|
|
sf := reader.fromExternalFile(ctx, "disc*.*")
|
|
r, path, err := sf()
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(r).ToNot(BeNil())
|
|
r.Close()
|
|
Expect(path).To(Equal(f1))
|
|
})
|
|
|
|
It("matches file without number in single-folder album (shared disc art)", func() {
|
|
f1 := createFile("album/cover.png")
|
|
reader := &discArtworkReader{
|
|
discNumber: 1,
|
|
imgFiles: []string{f1},
|
|
discFoldersRel: map[string]bool{"album": true},
|
|
lib: libraryView{FS: osDirFS{os.DirFS(tmpDir)}, absRoot: tmpDir},
|
|
}
|
|
|
|
sf := reader.fromExternalFile(ctx, "cover.*")
|
|
r, path, err := sf()
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(r).ToNot(BeNil())
|
|
r.Close()
|
|
Expect(path).To(Equal(f1))
|
|
})
|
|
|
|
It("returns shared disc art for every disc number in single-folder album", func() {
|
|
f1 := createFile("album/shellac.png")
|
|
makeReader := func(discNum int) *discArtworkReader {
|
|
return &discArtworkReader{
|
|
discNumber: discNum,
|
|
imgFiles: []string{f1},
|
|
discFoldersRel: map[string]bool{"album": true},
|
|
lib: libraryView{FS: osDirFS{os.DirFS(tmpDir)}, absRoot: tmpDir},
|
|
}
|
|
}
|
|
|
|
for _, disc := range []int{1, 2, 5} {
|
|
sf := makeReader(disc).fromExternalFile(ctx, "shellac.png")
|
|
r, path, err := sf()
|
|
Expect(err).ToNot(HaveOccurred(), "disc %d", disc)
|
|
Expect(r).ToNot(BeNil())
|
|
r.Close()
|
|
Expect(path).To(Equal(f1), "disc %d", disc)
|
|
}
|
|
})
|
|
|
|
It("numbered and unnumbered patterns both resolve against the same reader", func() {
|
|
f1 := createFile("album/cover.png")
|
|
f2 := createFile("album/disc1.jpg")
|
|
f3 := createFile("album/disc2.jpg")
|
|
reader := &discArtworkReader{
|
|
discNumber: 2,
|
|
imgFiles: []string{f1, f2, f3},
|
|
discFoldersRel: map[string]bool{"album": true},
|
|
lib: libraryView{FS: osDirFS{os.DirFS(tmpDir)}, absRoot: tmpDir},
|
|
}
|
|
|
|
sf := reader.fromExternalFile(ctx, "disc*.*")
|
|
r, path, err := sf()
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(r).ToNot(BeNil())
|
|
r.Close()
|
|
Expect(path).To(Equal(f3))
|
|
|
|
sf = reader.fromExternalFile(ctx, "cover.*")
|
|
r, path, err = sf()
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(r).ToNot(BeNil())
|
|
r.Close()
|
|
Expect(path).To(Equal(f1))
|
|
})
|
|
|
|
It("respects DiscArtPriority order when both numbered and unnumbered patterns match", func() {
|
|
f1 := createFile("album/cover.png")
|
|
f2 := createFile("album/disc1.jpg")
|
|
reader := &discArtworkReader{
|
|
discNumber: 1,
|
|
imgFiles: []string{f1, f2},
|
|
discFoldersRel: map[string]bool{"album": true},
|
|
lib: libraryView{FS: osDirFS{os.DirFS(tmpDir)}, absRoot: tmpDir},
|
|
}
|
|
|
|
cc := reader.discCandidates(ctx, nil, "disc*.*, cover.*")
|
|
Expect(cc).To(HaveLen(2))
|
|
res, ok := cc[0].resolve()
|
|
Expect(ok).To(BeTrue())
|
|
Expect(res.sourcePath).To(Equal(reader.lib.Abs(f2)))
|
|
res.reader.Close()
|
|
|
|
cc = reader.discCandidates(ctx, nil, "cover.*, disc*.*")
|
|
Expect(cc).To(HaveLen(2))
|
|
res, ok = cc[0].resolve()
|
|
Expect(ok).To(BeTrue())
|
|
Expect(res.sourcePath).To(Equal(reader.lib.Abs(f1)))
|
|
res.reader.Close()
|
|
})
|
|
|
|
DescribeTable("numbered match wins over shared fallback within a pattern",
|
|
func(discNumber, expectedIdx int) {
|
|
files := []string{
|
|
createFile("album/disc.jpg"),
|
|
createFile("album/disc1.jpg"),
|
|
createFile("album/disc2.jpg"),
|
|
}
|
|
reader := &discArtworkReader{
|
|
discNumber: discNumber,
|
|
imgFiles: files,
|
|
discFoldersRel: map[string]bool{"album": true},
|
|
lib: libraryView{FS: osDirFS{os.DirFS(tmpDir)}, absRoot: tmpDir},
|
|
}
|
|
|
|
sf := reader.fromExternalFile(ctx, "disc*.*")
|
|
r, path, err := sf()
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(r).ToNot(BeNil())
|
|
r.Close()
|
|
Expect(path).To(Equal(files[expectedIdx]))
|
|
},
|
|
Entry("disc 2 picks disc2.jpg over the shared disc.jpg", 2, 2),
|
|
Entry("disc 3 falls back to disc.jpg when no numbered match exists", 3, 0),
|
|
)
|
|
|
|
It("tries the next fallback candidate when the first one cannot be opened", func() {
|
|
f1 := createFile("album/cover.jpg")
|
|
f2 := createFile("album/cover.png")
|
|
// Remove f1 so Open will fail on it; f2 should still win.
|
|
removeFile(f1)
|
|
reader := &discArtworkReader{
|
|
discNumber: 1,
|
|
imgFiles: []string{f1, f2},
|
|
discFoldersRel: map[string]bool{"album": true},
|
|
lib: libraryView{FS: osDirFS{os.DirFS(tmpDir)}, absRoot: tmpDir},
|
|
}
|
|
|
|
sf := reader.fromExternalFile(ctx, "cover.*")
|
|
r, path, err := sf()
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(r).ToNot(BeNil())
|
|
r.Close()
|
|
Expect(path).To(Equal(f2))
|
|
})
|
|
|
|
It("keeps scanning literal-pattern matches so fallback retry still works", func() {
|
|
// Guards against an 'early break on first literal match' optimization.
|
|
// Multiple imgFiles entries can share a basename (symlinks, case-variant
|
|
// duplicates on case-sensitive filesystems). If the loop breaks after
|
|
// recording just the first, the fallback retry cannot recover when
|
|
// that first file is unreadable.
|
|
f1 := createFile("album/stale/cover.png")
|
|
f2 := createFile("album/cover.png")
|
|
removeFile(f1)
|
|
reader := &discArtworkReader{
|
|
discNumber: 1,
|
|
imgFiles: []string{f1, f2},
|
|
discFoldersRel: map[string]bool{
|
|
"album": true,
|
|
"album/stale": true,
|
|
},
|
|
isMultiFolder: true,
|
|
lib: libraryView{FS: osDirFS{os.DirFS(tmpDir)}, absRoot: tmpDir},
|
|
}
|
|
|
|
sf := reader.fromExternalFile(ctx, "cover.png")
|
|
r, path, err := sf()
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(r).ToNot(BeNil())
|
|
r.Close()
|
|
Expect(path).To(Equal(f2))
|
|
})
|
|
|
|
DescribeTable("filters by disc number for non-'*' wildcard patterns",
|
|
func(pattern string, discNumber, expectedIdx int) {
|
|
files := []string{
|
|
createFile("album/disc1.jpg"),
|
|
createFile("album/disc2.jpg"),
|
|
}
|
|
reader := &discArtworkReader{
|
|
discNumber: discNumber,
|
|
imgFiles: files,
|
|
discFoldersRel: map[string]bool{"album": true},
|
|
lib: libraryView{FS: osDirFS{os.DirFS(tmpDir)}, absRoot: tmpDir},
|
|
}
|
|
|
|
sf := reader.fromExternalFile(ctx, pattern)
|
|
r, path, err := sf()
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(r).ToNot(BeNil())
|
|
r.Close()
|
|
Expect(path).To(Equal(files[expectedIdx]))
|
|
},
|
|
Entry("disc?.jpg, target disc 1 → disc1.jpg", "disc?.jpg", 1, 0),
|
|
Entry("disc?.jpg, target disc 2 → disc2.jpg", "disc?.jpg", 2, 1),
|
|
Entry("disc[0-9].jpg, target disc 1 → disc1.jpg", "disc[0-9].jpg", 1, 0),
|
|
Entry("disc[0-9].jpg, target disc 2 → disc2.jpg", "disc[0-9].jpg", 2, 1),
|
|
)
|
|
|
|
It("matches file without number in multi-folder album by folder", func() {
|
|
f1 := createFile("album/cd1/disc.jpg")
|
|
f2 := createFile("album/cd2/disc.jpg")
|
|
reader := &discArtworkReader{
|
|
discNumber: 1,
|
|
imgFiles: []string{f1, f2},
|
|
discFoldersRel: map[string]bool{"album/cd1": true},
|
|
isMultiFolder: true,
|
|
lib: libraryView{FS: osDirFS{os.DirFS(tmpDir)}, absRoot: tmpDir},
|
|
}
|
|
|
|
sf := reader.fromExternalFile(ctx, "disc*.*")
|
|
r, path, err := sf()
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(r).ToNot(BeNil())
|
|
r.Close()
|
|
Expect(path).To(Equal(f1))
|
|
})
|
|
|
|
It("prefers disc number over folder when number is present", func() {
|
|
// disc2.jpg in cd1 folder should match disc 2, not disc 1
|
|
f1 := createFile("album/cd1/disc2.jpg")
|
|
reader := &discArtworkReader{
|
|
discNumber: 2,
|
|
imgFiles: []string{f1},
|
|
discFoldersRel: map[string]bool{"album/cd1": true},
|
|
isMultiFolder: true,
|
|
lib: libraryView{FS: osDirFS{os.DirFS(tmpDir)}, absRoot: tmpDir},
|
|
}
|
|
|
|
sf := reader.fromExternalFile(ctx, "disc*.*")
|
|
r, path, err := sf()
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(r).ToNot(BeNil())
|
|
r.Close()
|
|
Expect(path).To(Equal(f1))
|
|
})
|
|
|
|
It("does not match disc2.jpg when looking for disc 1", func() {
|
|
f1 := createFile("album/disc2.jpg")
|
|
reader := &discArtworkReader{
|
|
discNumber: 1,
|
|
imgFiles: []string{f1},
|
|
discFoldersRel: map[string]bool{"album": true},
|
|
lib: libraryView{FS: osDirFS{os.DirFS(tmpDir)}, absRoot: tmpDir},
|
|
}
|
|
|
|
sf := reader.fromExternalFile(ctx, "disc*.*")
|
|
r, _, _ := sf()
|
|
Expect(r).To(BeNil())
|
|
})
|
|
})
|
|
|
|
Describe("fromDiscSubtitle", func() {
|
|
var (
|
|
ctx context.Context
|
|
tmpDir string
|
|
)
|
|
|
|
BeforeEach(func() {
|
|
ctx = context.Background()
|
|
tmpDir = GinkgoT().TempDir()
|
|
})
|
|
|
|
createFile := func(relPath string) string {
|
|
fullPath := filepath.Join(tmpDir, filepath.FromSlash(relPath))
|
|
Expect(os.MkdirAll(filepath.Dir(fullPath), 0755)).To(Succeed())
|
|
Expect(os.WriteFile(fullPath, []byte("image data"), 0600)).To(Succeed())
|
|
return relPath
|
|
}
|
|
|
|
It("matches image file whose stem equals the disc subtitle (case-insensitive)", func() {
|
|
f1 := createFile("album/The Blue Disc.jpg")
|
|
reader := &discArtworkReader{
|
|
discNumber: 1,
|
|
imgFiles: []string{f1},
|
|
lib: libraryView{FS: osDirFS{os.DirFS(tmpDir)}, absRoot: tmpDir},
|
|
}
|
|
|
|
sf := reader.fromDiscSubtitle(ctx, "The Blue Disc")
|
|
r, path, err := sf()
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(r).ToNot(BeNil())
|
|
r.Close()
|
|
Expect(path).To(Equal(f1))
|
|
})
|
|
|
|
It("matches case-insensitively", func() {
|
|
f1 := createFile("album/bonus tracks.png")
|
|
reader := &discArtworkReader{
|
|
discNumber: 2,
|
|
imgFiles: []string{f1},
|
|
lib: libraryView{FS: osDirFS{os.DirFS(tmpDir)}, absRoot: tmpDir},
|
|
}
|
|
|
|
sf := reader.fromDiscSubtitle(ctx, "Bonus Tracks")
|
|
r, path, err := sf()
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(r).ToNot(BeNil())
|
|
r.Close()
|
|
Expect(path).To(Equal(f1))
|
|
})
|
|
|
|
It("returns error when no matching file found", func() {
|
|
f1 := createFile("album/cover.jpg")
|
|
reader := &discArtworkReader{
|
|
discNumber: 1,
|
|
imgFiles: []string{f1},
|
|
lib: libraryView{FS: osDirFS{os.DirFS(tmpDir)}, absRoot: tmpDir},
|
|
}
|
|
|
|
sf := reader.fromDiscSubtitle(ctx, "The Blue Disc")
|
|
_, _, err := sf()
|
|
Expect(err).To(HaveOccurred())
|
|
})
|
|
|
|
It("matches first file when multiple extensions exist", func() {
|
|
f1 := createFile("album/The Blue Disc.jpg")
|
|
f2 := createFile("album/The Blue Disc.png")
|
|
reader := &discArtworkReader{
|
|
discNumber: 1,
|
|
imgFiles: []string{f1, f2},
|
|
lib: libraryView{FS: osDirFS{os.DirFS(tmpDir)}, absRoot: tmpDir},
|
|
}
|
|
|
|
sf := reader.fromDiscSubtitle(ctx, "The Blue Disc")
|
|
r, path, err := sf()
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(r).ToNot(BeNil())
|
|
r.Close()
|
|
Expect(path).To(Equal(f1))
|
|
})
|
|
})
|
|
|
|
Describe("discArtworkReader", func() {
|
|
var (
|
|
reader *discArtworkReader
|
|
tmpDir string
|
|
)
|
|
|
|
BeforeEach(func() {
|
|
tmpDir = GinkgoT().TempDir()
|
|
reader = &discArtworkReader{
|
|
discNumber: 2,
|
|
isMultiFolder: true,
|
|
discFoldersRel: map[string]bool{"music/album/cd2": true},
|
|
imgFiles: []string{
|
|
"music/album/cd1/disc.jpg",
|
|
"music/album/cd2/disc.jpg",
|
|
"music/album/cd2/disc2.jpg",
|
|
},
|
|
firstTrackRel: "music/album/cd2/track1.flac",
|
|
lib: libraryView{FS: osDirFS{os.DirFS(tmpDir)}, absRoot: tmpDir},
|
|
}
|
|
})
|
|
|
|
Describe("selectImage", func() {
|
|
It("abandons the walk when the context is cancelled", func() {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
|
|
res, err := reader.selectImage(ctx, nil, "disc*.*, cover.*", &chainState{})
|
|
|
|
Expect(err).To(MatchError(context.Canceled))
|
|
Expect(res.reader).To(BeNil())
|
|
})
|
|
|
|
// "the track has no embedded art" and "the track is there but unreadable" are the two
|
|
// answers a wrong-artwork report needs told apart; only the second is worth retrying.
|
|
It("reports a track it cannot parse as unreadable, not as a miss", func() {
|
|
trace := &ChainTrace{}
|
|
track := filepath.Join(tmpDir, filepath.FromSlash(reader.firstTrackRel))
|
|
Expect(os.MkdirAll(filepath.Dir(track), 0755)).To(Succeed())
|
|
Expect(os.WriteFile(track, []byte("not audio"), 0600)).To(Succeed())
|
|
|
|
res, err := reader.selectImage(context.Background(), tests.NewMockFFmpeg(""), "embedded",
|
|
&chainState{trace: trace})
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(res.localError).To(BeTrue())
|
|
Expect(trace.Steps()).To(Equal([]TraceStep{{Candidate: "embedded", Outcome: OutcomeUnreadable}}))
|
|
})
|
|
|
|
It("reports a disc with no tracks to read as a miss", func() {
|
|
trace := &ChainTrace{}
|
|
reader.firstTrackRel = ""
|
|
|
|
res, err := reader.selectImage(context.Background(), tests.NewMockFFmpeg(""), "embedded",
|
|
&chainState{trace: trace})
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(res.localError).To(BeFalse(), "there was nothing to read, so nothing failed to read")
|
|
Expect(trace.Steps()).To(Equal([]TraceStep{{Candidate: "embedded", Outcome: OutcomeMiss}}))
|
|
})
|
|
})
|
|
|
|
Describe("discCandidates", func() {
|
|
It("returns a resolvable candidate for glob patterns", func() {
|
|
cc := reader.discCandidates(context.Background(), nil, "disc*.*")
|
|
Expect(cc).To(HaveLen(1))
|
|
Expect(cc[0].resolve).ToNot(BeNil())
|
|
})
|
|
|
|
It("returns one candidate per entry, in order", func() {
|
|
cc := reader.discCandidates(context.Background(), nil, "disc*.*, cd*.*, embedded")
|
|
Expect(slice.Map(cc, func(c discCandidate) string { return c.pattern })).
|
|
To(Equal([]string{"disc*.*", "cd*.*", "embedded"}))
|
|
})
|
|
|
|
It("skips an empty entry rather than building a glob that matches nothing", func() {
|
|
cc := reader.discCandidates(context.Background(), nil, "disc*.*,")
|
|
Expect(cc).To(HaveLen(1))
|
|
})
|
|
|
|
// The skip reasons below are what `artwork explain` prints, so an entry that maps to no
|
|
// source must say why instead of vanishing from the walk.
|
|
DescribeTable("keeps an entry that maps to no source, with its reason",
|
|
func(setup func(), priority, reason string) {
|
|
setup()
|
|
cc := reader.discCandidates(context.Background(), nil, priority)
|
|
Expect(cc).To(HaveLen(1))
|
|
Expect(cc[0].resolve).To(BeNil())
|
|
Expect(cc[0].skip).To(Equal(reason))
|
|
},
|
|
Entry("external is unsupported", func() {}, "external",
|
|
"external sources are not supported for disc artwork"),
|
|
Entry("no images in the album folder", func() { reader.imgFiles = nil }, "disc*.*",
|
|
"no images in album folder"),
|
|
Entry("the disc has no subtitle",
|
|
func() { reader.album = model.Album{Discs: model.Discs{2: ""}} }, "discsubtitle",
|
|
"disc has no subtitle"),
|
|
)
|
|
|
|
It("returns source func for discsubtitle pattern", func() {
|
|
reader.album = model.Album{Discs: model.Discs{2: "Bonus Tracks"}}
|
|
cc := reader.discCandidates(context.Background(), nil, "discsubtitle")
|
|
Expect(cc).To(HaveLen(1))
|
|
Expect(cc[0].resolve).ToNot(BeNil())
|
|
})
|
|
})
|
|
})
|
|
})
|