navidrome/scanner/phase_1_folders_test.go
Deluan 8b5e50a1aa feat(scanner): mirror album re-PID flow for artists
When PID.Artist spec changes, existing artist IDs differ from newly-computed
ones. Without remapping, all annotations (starred, ratings, play counts) become
orphaned. This change builds an artistIDMap during phase 1 and reassigns
annotations on persist, mirroring the existing albumIDMap flow.

The migration logic is gated on the previous spec being non-empty and different
from the current spec, so the default "name" spec (byte-identical to the
historical hardcoded artistID) triggers no remapping on upgrade.
2026-05-24 19:42:04 -03:00

49 lines
1.4 KiB
Go

package scanner
import (
"context"
"github.com/navidrome/navidrome/conf/configtest"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/tests"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("phaseFolders", func() {
Describe("Artist re-PID migration", func() {
var (
artRepo *tests.MockArtistRepo
p *phaseFolders
)
BeforeEach(func() {
DeferCleanup(configtest.SetupConfig())
artRepo = tests.CreateMockArtistRepo()
if artRepo.ReassignAnnotationCalls == nil {
artRepo.ReassignAnnotationCalls = map[string]string{}
}
p = &phaseFolders{ctx: context.Background(), state: &scanState{}}
})
It("calls ReassignAnnotation when persisting an artist whose ID changed", func() {
oldID := "old-id"
newID := "new-id"
idMap := map[string]string{newID: oldID}
artist := model.Artist{ID: newID, Name: "Foo"}
Expect(p.persistArtist(artRepo, &artist, idMap)).To(Succeed())
Expect(artRepo.ReassignAnnotationCalls).To(HaveKeyWithValue(oldID, newID))
// Mapping should be removed after successful reassignment
Expect(idMap).ToNot(HaveKey(newID))
})
It("does not call ReassignAnnotation when no mapping exists", func() {
idMap := map[string]string{}
artist := model.Artist{ID: "some-id", Name: "Foo"}
Expect(p.persistArtist(artRepo, &artist, idMap)).To(Succeed())
Expect(artRepo.ReassignAnnotationCalls).To(BeEmpty())
})
})
})