mirror of
https://github.com/navidrome/navidrome.git
synced 2026-11-23 10:29:46 +01:00
Smart playlists imported from .nsp files showed 0 tracks until their owner opened them, because tracks are only materialized on read. The scanner now queues every imported smart playlist that has no evaluation yet (new, or its file changed) and evaluates them as the last scan step, after GC and stats, so the rules see the final library state. Unchanged files keep their evaluation and are not re-run. The new PlaylistRepository.Evaluate runs the refresh as the playlist owner, ignoring the caller's visibility and the refresh delay, inside a transaction so readers never see an emptied playlist. Evaluating inside the scan also covers the default out-of-process scanner. Evaluation errors are logged and do not fail the scan; the playlist is still evaluated on its next read. Fixes #4539
167 lines
4.5 KiB
Go
167 lines
4.5 KiB
Go
// nolint unused
|
|
package scanner
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"sync/atomic"
|
|
|
|
ppl "github.com/google/go-pipeline/pkg/pipeline"
|
|
"github.com/navidrome/navidrome/tests"
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
)
|
|
|
|
var _ = Describe("libraryRelativePath", func() {
|
|
// Paths are built with filepath so the "absolute" cases stay absolute on every OS
|
|
// (a Unix-style "/foo" is not absolute on Windows).
|
|
libRoot, _ := filepath.Abs(filepath.Join("jukebox", "collection"))
|
|
outside, _ := filepath.Abs(filepath.Join("somewhere", "else"))
|
|
|
|
It("returns a relative path unchanged", func() {
|
|
Expect(libraryRelativePath(libRoot, "_Collection")).To(Equal("_Collection"))
|
|
})
|
|
|
|
It("rebases an absolute target when the library root is relative", func() {
|
|
cwd, err := os.Getwd()
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(libraryRelativePath(filepath.Join("music", "library"), filepath.Join(cwd, "music", "library", "rock"))).To(Equal("rock"))
|
|
})
|
|
|
|
It("rebases an absolute path that equals the library root to '.'", func() {
|
|
Expect(libraryRelativePath(libRoot, libRoot)).To(Equal("."))
|
|
})
|
|
|
|
It("rebases an absolute path under the library root", func() {
|
|
Expect(libraryRelativePath(libRoot, filepath.Join(libRoot, "_Collection"))).To(Equal("_Collection"))
|
|
})
|
|
|
|
It("handles a trailing slash on the library path", func() {
|
|
Expect(libraryRelativePath(libRoot+string(filepath.Separator), filepath.Join(libRoot, "_Collection"))).To(Equal("_Collection"))
|
|
})
|
|
|
|
It("leaves an absolute path outside the library root unchanged", func() {
|
|
Expect(libraryRelativePath(libRoot, outside)).To(Equal(outside))
|
|
})
|
|
|
|
It("returns an empty path unchanged", func() {
|
|
Expect(libraryRelativePath(libRoot, "")).To(Equal(""))
|
|
})
|
|
})
|
|
|
|
type mockPhase struct {
|
|
num int
|
|
produceFunc func() ppl.Producer[int]
|
|
stagesFunc func() []ppl.Stage[int]
|
|
finalizeFunc func(error) error
|
|
descriptionFn func() string
|
|
}
|
|
|
|
func (m *mockPhase) producer() ppl.Producer[int] {
|
|
return m.produceFunc()
|
|
}
|
|
|
|
func (m *mockPhase) stages() []ppl.Stage[int] {
|
|
return m.stagesFunc()
|
|
}
|
|
|
|
func (m *mockPhase) finalize(err error) error {
|
|
return m.finalizeFunc(err)
|
|
}
|
|
|
|
func (m *mockPhase) description() string {
|
|
return m.descriptionFn()
|
|
}
|
|
|
|
var _ = Describe("runPhase", func() {
|
|
var (
|
|
ctx context.Context
|
|
phaseNum int
|
|
phase *mockPhase
|
|
sum atomic.Int32
|
|
)
|
|
|
|
BeforeEach(func() {
|
|
ctx = context.Background()
|
|
phaseNum = 1
|
|
phase = &mockPhase{
|
|
num: 3,
|
|
produceFunc: func() ppl.Producer[int] {
|
|
return ppl.NewProducer(func(put func(int)) error {
|
|
for i := 1; i <= phase.num; i++ {
|
|
put(i)
|
|
}
|
|
return nil
|
|
})
|
|
},
|
|
stagesFunc: func() []ppl.Stage[int] {
|
|
return []ppl.Stage[int]{ppl.NewStage(func(i int) (int, error) {
|
|
sum.Add(int32(i))
|
|
return i, nil
|
|
})}
|
|
},
|
|
finalizeFunc: func(err error) error {
|
|
return err
|
|
},
|
|
descriptionFn: func() string {
|
|
return "Mock Phase"
|
|
},
|
|
}
|
|
})
|
|
|
|
It("should run the phase successfully", func() {
|
|
err := runPhase(ctx, phaseNum, phase)()
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(sum.Load()).To(Equal(int32(1 * 2 * 3)))
|
|
})
|
|
|
|
It("should log an error if the phase fails", func() {
|
|
phase.finalizeFunc = func(err error) error {
|
|
return errors.New("finalize error")
|
|
}
|
|
err := runPhase(ctx, phaseNum, phase)()
|
|
Expect(err).To(HaveOccurred())
|
|
Expect(err.Error()).To(ContainSubstring("finalize error"))
|
|
})
|
|
|
|
It("should count the tasks", func() {
|
|
counter, countStageFn := countTasks[int]()
|
|
phase.stagesFunc = func() []ppl.Stage[int] {
|
|
return []ppl.Stage[int]{ppl.NewStage(countStageFn, ppl.Name("count tasks"))}
|
|
}
|
|
err := runPhase(ctx, phaseNum, phase)()
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(counter.Load()).To(Equal(int64(3)))
|
|
})
|
|
})
|
|
|
|
var _ = Describe("runEvaluateSmartPlaylists", func() {
|
|
var ctx context.Context
|
|
var plsRepo *tests.MockPlaylistRepo
|
|
var s *scannerImpl
|
|
var state *scanState
|
|
|
|
BeforeEach(func() {
|
|
ctx = GinkgoT().Context()
|
|
plsRepo = tests.CreateMockPlaylistRepo()
|
|
s = &scannerImpl{ds: &tests.MockDataStore{MockedPlaylist: plsRepo}}
|
|
state = &scanState{}
|
|
})
|
|
|
|
It("evaluates every queued smart playlist", func() {
|
|
state.queueSmartPlaylist("p1")
|
|
state.queueSmartPlaylist("p2")
|
|
|
|
Expect(s.runEvaluateSmartPlaylists(ctx, state)()).To(Succeed())
|
|
Expect(plsRepo.Evaluated).To(Equal([]string{"p1", "p2"}))
|
|
})
|
|
|
|
It("does not fail the scan when an evaluation fails", func() {
|
|
plsRepo.SetError(true)
|
|
state.queueSmartPlaylist("p1")
|
|
|
|
Expect(s.runEvaluateSmartPlaylists(ctx, state)()).To(Succeed())
|
|
})
|
|
})
|