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" "net/url"
"github.com/navidrome/navidrome/conf" "github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/conf/configtest"
"github.com/navidrome/navidrome/core/agents" "github.com/navidrome/navidrome/core/agents"
. "github.com/navidrome/navidrome/core/extdata" . "github.com/navidrome/navidrome/core/extdata"
"github.com/navidrome/navidrome/model" "github.com/navidrome/navidrome/model"
@ -24,12 +25,10 @@ var _ = Describe("Provider - AlbumImage", func() {
var mockAlbumAgent *mockAlbumInfoAgent var mockAlbumAgent *mockAlbumInfoAgent
var agentsCombined *mockAgents var agentsCombined *mockAgents
var ctx context.Context var ctx context.Context
var cancel context.CancelFunc
var originalAgentsConfig string
BeforeEach(func() { BeforeEach(func() {
ctx, cancel = context.WithCancel(context.Background()) ctx = GinkgoT().Context()
originalAgentsConfig = conf.Server.Agents DeferCleanup(configtest.SetupConfig())
conf.Server.Agents = "mockAlbum" // Configure mock agent conf.Server.Agents = "mockAlbum" // Configure mock agent
mockArtistRepo = newMockArtistRepo() mockArtistRepo = newMockArtistRepo()
@ -51,9 +50,6 @@ var _ = Describe("Provider - AlbumImage", func() {
provider = NewProvider(ds, agentsCombined) provider = NewProvider(ds, agentsCombined)
// Default mocks // 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) // Mocks for GetEntityByID sequence (initial failed lookups)
mockArtistRepo.On("Get", "album-1").Return(nil, model.ErrNotFound).Once() mockArtistRepo.On("Get", "album-1").Return(nil, model.ErrNotFound).Once()
mockArtistRepo.On("Get", "mf-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() mockArtistRepo.On("Get", "not-found").Return(nil, model.ErrNotFound).Maybe()
mockAlbumRepo.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() 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() { It("returns the largest image URL when successful", func() {
// Arrange // Arrange
mockArtistRepo.On("Get", "album-1").Return(nil, model.ErrNotFound).Once() // Expect GetEntityByID sequence 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() { It("returns context error if context is canceled", func() {
// Arrange // Arrange
cctx, cancelCtx := context.WithCancel(context.Background()) cctx, cancelCtx := context.WithCancel(ctx)
// Mock the necessary DB calls *before* canceling the context // Mock the necessary DB calls *before* canceling the context
mockArtistRepo.On("Get", "album-1").Return(nil, model.ErrNotFound).Once() 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() 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") mockMediaFileRepo.AssertCalled(GinkgoT(), "Get", "not-found")
mockAlbumAgent.AssertNotCalled(GinkgoT(), "GetAlbumInfo", mock.Anything, mock.Anything, mock.Anything, mock.Anything) mockAlbumAgent.AssertNotCalled(GinkgoT(), "GetAlbumInfo", mock.Anything, mock.Anything, mock.Anything, mock.Anything)
}) })
})
}) })
// mockAlbumInfoAgent implementation // mockAlbumInfoAgent implementation

View file

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

View file

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

View file

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

View file

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

View file

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