Signed-off-by: Deluan <deluan@navidrome.org>
This commit is contained in:
Deluan 2025-03-30 19:22:16 -04:00
parent 074b4937da
commit 582e4801b4
6 changed files with 587 additions and 634 deletions

View file

@ -6,6 +6,7 @@ import (
"net/url"
"github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/conf/configtest"
"github.com/navidrome/navidrome/core/agents"
. "github.com/navidrome/navidrome/core/extdata"
"github.com/navidrome/navidrome/model"
@ -24,12 +25,10 @@ var _ = Describe("Provider - AlbumImage", func() {
var mockAlbumAgent *mockAlbumInfoAgent
var agentsCombined *mockAgents
var ctx context.Context
var cancel context.CancelFunc
var originalAgentsConfig string
BeforeEach(func() {
ctx, cancel = context.WithCancel(context.Background())
originalAgentsConfig = conf.Server.Agents
ctx = GinkgoT().Context()
DeferCleanup(configtest.SetupConfig())
conf.Server.Agents = "mockAlbum" // Configure mock agent
mockArtistRepo = newMockArtistRepo()
@ -51,9 +50,6 @@ var _ = Describe("Provider - AlbumImage", func() {
provider = NewProvider(ds, agentsCombined)
// Default mocks
// Removed: mockAlbumRepo.On("Get", "album-1").Return(&model.Album{ID: "album-1", Name: "Album One", AlbumArtistID: "artist-1"}, nil).Maybe()
// Removed: mockMediaFileRepo.On("Get", "mf-1").Return(&model.MediaFile{ID: "mf-1", Title: "Track One", ArtistID: "artist-1", AlbumID: "album-1"}, nil).Maybe()
// Mocks for GetEntityByID sequence (initial failed lookups)
mockArtistRepo.On("Get", "album-1").Return(nil, model.ErrNotFound).Once()
mockArtistRepo.On("Get", "mf-1").Return(nil, model.ErrNotFound).Once()
@ -63,29 +59,8 @@ var _ = Describe("Provider - AlbumImage", func() {
mockArtistRepo.On("Get", "not-found").Return(nil, model.ErrNotFound).Maybe()
mockAlbumRepo.On("Get", "not-found").Return(nil, model.ErrNotFound).Maybe()
mockMediaFileRepo.On("Get", "not-found").Return(nil, model.ErrNotFound).Maybe()
// Default agent response removed - tests will define their own expectations
// mockAlbumAgent.On("GetAlbumInfo", mock.Anything, "Album One", "", "").
// Return(&agents.AlbumInfo{
// Images: []agents.ExternalImage{
// {URL: "http://example.com/large.jpg", Size: 1000},
// {URL: "http://example.com/medium.jpg", Size: 500},
// {URL: "http://example.com/small.jpg", Size: 200},
// },
// }, nil).Maybe()
})
AfterEach(func() {
cancel() // Restore context cancellation
conf.Server.Agents = originalAgentsConfig // Restore original agent config
// Removed mock assertions - rely on Once() in tests and outcome validation
// mockArtistRepo.AssertExpectations(GinkgoT())
// mockAlbumRepo.AssertExpectations(GinkgoT())
// mockMediaFileRepo.AssertExpectations(GinkgoT())
// mockAgent.AssertExpectations(GinkgoT())
})
Describe("AlbumImage", func() {
It("returns the largest image URL when successful", func() {
// Arrange
mockArtistRepo.On("Get", "album-1").Return(nil, model.ErrNotFound).Once() // Expect GetEntityByID sequence
@ -183,7 +158,7 @@ var _ = Describe("Provider - AlbumImage", func() {
It("returns context error if context is canceled", func() {
// Arrange
cctx, cancelCtx := context.WithCancel(context.Background())
cctx, cancelCtx := context.WithCancel(ctx)
// Mock the necessary DB calls *before* canceling the context
mockArtistRepo.On("Get", "album-1").Return(nil, model.ErrNotFound).Once()
mockAlbumRepo.On("Get", "album-1").Return(&model.Album{ID: "album-1", Name: "Album One", AlbumArtistID: "artist-1"}, nil).Once()
@ -297,7 +272,6 @@ var _ = Describe("Provider - AlbumImage", func() {
mockMediaFileRepo.AssertCalled(GinkgoT(), "Get", "not-found")
mockAlbumAgent.AssertNotCalled(GinkgoT(), "GetAlbumInfo", mock.Anything, mock.Anything, mock.Anything, mock.Anything)
})
})
})
// mockAlbumInfoAgent implementation

View file

@ -6,6 +6,7 @@ import (
"net/url"
"github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/conf/configtest"
"github.com/navidrome/navidrome/core/agents"
. "github.com/navidrome/navidrome/core/extdata"
"github.com/navidrome/navidrome/model"
@ -24,13 +25,11 @@ var _ = Describe("Provider - ArtistImage", func() {
var mockImageAgent *mockArtistImageAgent
var agentsCombined *mockAgents
var ctx context.Context
var cancel context.CancelFunc
var originalAgentsConfig string
BeforeEach(func() {
ctx, cancel = context.WithCancel(context.Background())
originalAgentsConfig = conf.Server.Agents
DeferCleanup(configtest.SetupConfig())
conf.Server.Agents = "mockImage" // Configure only the mock agent
ctx = GinkgoT().Context()
mockArtistRepo = newMockArtistRepo()
mockAlbumRepo = newMockAlbumRepo()
@ -70,15 +69,12 @@ var _ = Describe("Provider - ArtistImage", func() {
})
AfterEach(func() {
cancel() // Ensure context is cancelled
conf.Server.Agents = originalAgentsConfig
mockArtistRepo.AssertExpectations(GinkgoT())
mockAlbumRepo.AssertExpectations(GinkgoT())
mockMediaFileRepo.AssertExpectations(GinkgoT())
mockImageAgent.AssertExpectations(GinkgoT())
})
Describe("ArtistImage", func() {
It("returns the largest image URL when successful", func() {
// Arrange
expectedURL, _ := url.Parse("http://example.com/large.jpg")
@ -269,7 +265,6 @@ var _ = Describe("Provider - ArtistImage", func() {
mockArtistRepo.AssertCalled(GinkgoT(), "Get", "artist-1")
mockImageAgent.AssertCalled(GinkgoT(), "GetArtistImages", ctx, "artist-1", "Artist One", "")
})
})
})
// mockArtistImageAgent implementation using testify/mock

View file

@ -4,7 +4,6 @@ import (
"context"
"errors"
"github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/core/agents"
. "github.com/navidrome/navidrome/core/extdata"
"github.com/navidrome/navidrome/model"
@ -24,11 +23,9 @@ var _ = Describe("Provider - SimilarSongs", func() {
var artistRepo *mockArtistRepo
var mediaFileRepo *mockMediaFileRepo
var ctx context.Context
var originalAgentsConfig string
BeforeEach(func() {
ctx = context.Background()
originalAgentsConfig = conf.Server.Agents
ctx = GinkgoT().Context()
artistRepo = newMockArtistRepo()
mediaFileRepo = newMockMediaFileRepo()
@ -50,11 +47,6 @@ var _ = Describe("Provider - SimilarSongs", func() {
provider = NewProvider(ds, agentsCombined)
})
AfterEach(func() {
conf.Server.Agents = originalAgentsConfig
})
Describe("SimilarSongs", func() {
It("returns similar songs from main artist and similar artists", func() {
artist1 := model.Artist{ID: "artist-1", Name: "Artist One"}
similarArtist := model.Artist{ID: "artist-3", Name: "Similar Artist"}
@ -203,5 +195,4 @@ var _ = Describe("Provider - SimilarSongs", func() {
Expect(songs).To(HaveLen(1))
Expect(songs[0].ID).To(BeElementOf("song-1", "song-2"))
})
})
})

View file

@ -4,7 +4,6 @@ import (
"context"
"errors"
"github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/core/agents"
_ "github.com/navidrome/navidrome/core/agents/lastfm"
_ "github.com/navidrome/navidrome/core/agents/listenbrainz"
@ -24,12 +23,10 @@ var _ = Describe("Provider - TopSongs", func() {
mediaFileRepo *mockMediaFileRepo // From provider_helper_test.go
ag *mockAgents // Consolidated mock from export_test.go
ctx context.Context
originalAgentsConfig string
)
BeforeEach(func() {
ctx = context.Background()
originalAgentsConfig = conf.Server.Agents
ctx = GinkgoT().Context()
artistRepo = newMockArtistRepo() // Use helper mock
mediaFileRepo = newMockMediaFileRepo() // Use helper mock
@ -45,11 +42,6 @@ var _ = Describe("Provider - TopSongs", func() {
p = NewProvider(ds, ag)
})
AfterEach(func() {
conf.Server.Agents = originalAgentsConfig
})
Describe("TopSongs", func() {
BeforeEach(func() {
// Setup expectations in individual tests
})
@ -198,5 +190,4 @@ var _ = Describe("Provider - TopSongs", func() {
artistRepo.AssertExpectations(GinkgoT())
ag.AssertExpectations(GinkgoT())
})
})
})

View file

@ -31,7 +31,7 @@ var _ = Describe("Provider - UpdateAlbumInfo", func() {
)
BeforeEach(func() {
ctx = context.Background()
ctx = GinkgoT().Context()
ds = new(tests.MockDataStore)
ag = new(mockAgents)
p = extdata.NewProvider(ds, ag)

View file

@ -6,6 +6,7 @@ import (
"time"
"github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/conf/configtest"
"github.com/navidrome/navidrome/core/agents"
"github.com/navidrome/navidrome/core/extdata"
"github.com/navidrome/navidrome/log"
@ -31,12 +32,13 @@ var _ = Describe("Provider - UpdateArtistInfo", func() {
)
BeforeEach(func() {
ctx = context.Background()
DeferCleanup(configtest.SetupConfig())
conf.Server.DevArtistInfoTimeToLive = 1 * time.Hour
ctx = GinkgoT().Context()
ds = new(tests.MockDataStore)
ag = new(mockAgents)
p = extdata.NewProvider(ds, ag)
mockArtistRepo = ds.Artist(ctx).(*tests.MockArtistRepo)
conf.Server.DevArtistInfoTimeToLive = 1 * time.Hour
})
It("returns error when artist is not found", func() {