feat: replace core mock services with test-specific implementations to avoid import cycles

This commit is contained in:
Deluan 2026-01-08 12:10:19 -05:00
parent d5458d3135
commit 181152aeb7
7 changed files with 47 additions and 78 deletions

View File

@ -1,55 +0,0 @@
package core
import (
"context"
"github.com/deluan/rest"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/tests"
)
// MockUserWrapper provides a simple wrapper around MockedUserRepo
// that implements the core.User interface for testing
type MockUserWrapper struct {
*tests.MockedUserRepo
}
// MockUserRestAdapter adapts MockedUserRepo to rest.Repository interface
type MockUserRestAdapter struct {
*tests.MockedUserRepo
}
// NewMockUserService creates a new mock user service for testing
func NewMockUserService() User {
repo := tests.CreateMockUserRepo()
return &MockUserWrapper{MockedUserRepo: repo}
}
func (m *MockUserWrapper) NewRepository(ctx context.Context) rest.Repository {
return &MockUserRestAdapter{MockedUserRepo: m.MockedUserRepo}
}
// rest.Repository interface implementation
func (a *MockUserRestAdapter) Count(options ...rest.QueryOptions) (int64, error) {
return a.CountAll()
}
func (a *MockUserRestAdapter) Read(id string) (interface{}, error) {
return a.Get(id)
}
func (a *MockUserRestAdapter) ReadAll(options ...rest.QueryOptions) (interface{}, error) {
return a.GetAll()
}
func (a *MockUserRestAdapter) EntityName() string {
return "user"
}
func (a *MockUserRestAdapter) NewInstance() interface{} {
return &model.User{}
}
var _ User = (*MockUserWrapper)(nil)
var _ rest.Repository = (*MockUserRestAdapter)(nil)

View File

@ -10,7 +10,6 @@ import (
"github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/conf/configtest"
"github.com/navidrome/navidrome/consts"
"github.com/navidrome/navidrome/core"
"github.com/navidrome/navidrome/core/auth"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/server"
@ -29,7 +28,7 @@ var _ = Describe("Config API", func() {
conf.Server.DevUIShowConfig = true // Enable config endpoint for tests
ds = &tests.MockDataStore{}
auth.Init(ds)
nativeRouter := New(ds, nil, nil, nil, core.NewMockLibraryService(), core.NewMockUserService(), nil, nil)
nativeRouter := New(ds, nil, nil, nil, tests.NewMockLibraryService(), tests.NewMockUserService(), nil, nil)
router = server.JWTVerifier(nativeRouter)
// Create test users

View File

@ -11,7 +11,6 @@ import (
"github.com/navidrome/navidrome/conf/configtest"
"github.com/navidrome/navidrome/consts"
"github.com/navidrome/navidrome/core"
"github.com/navidrome/navidrome/core/auth"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/server"
@ -30,7 +29,7 @@ var _ = Describe("Library API", func() {
DeferCleanup(configtest.SetupConfig())
ds = &tests.MockDataStore{}
auth.Init(ds)
nativeRouter := New(ds, nil, nil, nil, core.NewMockLibraryService(), core.NewMockUserService(), nil, nil)
nativeRouter := New(ds, nil, nil, nil, tests.NewMockLibraryService(), tests.NewMockUserService(), nil, nil)
router = server.JWTVerifier(nativeRouter)
// Create test users

View File

@ -11,7 +11,6 @@ import (
"github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/conf/configtest"
"github.com/navidrome/navidrome/consts"
"github.com/navidrome/navidrome/core"
"github.com/navidrome/navidrome/core/auth"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/server"
@ -95,7 +94,7 @@ var _ = Describe("Song Endpoints", func() {
mfRepo.SetData(testSongs)
// Create the native API router and wrap it with the JWTVerifier middleware
nativeRouter := New(ds, nil, nil, nil, core.NewMockLibraryService(), core.NewMockUserService(), nil, nil)
nativeRouter := New(ds, nil, nil, nil, tests.NewMockLibraryService(), tests.NewMockUserService(), nil, nil)
router = server.JWTVerifier(nativeRouter)
w = httptest.NewRecorder()
})

View File

@ -11,7 +11,6 @@ import (
"github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/conf/configtest"
"github.com/navidrome/navidrome/consts"
"github.com/navidrome/navidrome/core"
"github.com/navidrome/navidrome/core/auth"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/model/request"
@ -34,7 +33,7 @@ var _ = Describe("Plugin API", func() {
ds = &tests.MockDataStore{}
mockManager = &tests.MockPluginManager{}
auth.Init(ds)
nativeRouter := New(ds, nil, nil, nil, core.NewMockLibraryService(), core.NewMockUserService(), nil, mockManager)
nativeRouter := New(ds, nil, nil, nil, tests.NewMockLibraryService(), tests.NewMockUserService(), nil, mockManager)
router = server.JWTVerifier(nativeRouter)
// Create test users

View File

@ -1,27 +1,28 @@
package core
package tests
import (
"context"
"github.com/deluan/rest"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/tests"
)
// MockLibraryWrapper provides a simple wrapper around MockLibraryRepo
// that implements the core.Library interface for testing
type MockLibraryWrapper struct {
*tests.MockLibraryRepo
// MockLibraryService provides a simple wrapper around MockLibraryRepo
// that implements the core.Library interface for testing.
// Returns concrete type to avoid import cycles - callers assign to core.Library.
type MockLibraryService struct {
*MockLibraryRepo
}
// MockLibraryRestAdapter adapts MockLibraryRepo to rest.Repository interface
type MockLibraryRestAdapter struct {
*tests.MockLibraryRepo
*MockLibraryRepo
}
// NewMockLibraryService creates a new mock library service for testing
func NewMockLibraryService() Library {
repo := &tests.MockLibraryRepo{
// NewMockLibraryService creates a new mock library service for testing.
// Returns concrete type - assign to core.Library at call site.
func NewMockLibraryService() *MockLibraryService {
repo := &MockLibraryRepo{
Data: make(map[int]model.Library),
}
// Set up default test data
@ -29,10 +30,10 @@ func NewMockLibraryService() Library {
{ID: 1, Name: "Test Library 1", Path: "/music/library1"},
{ID: 2, Name: "Test Library 2", Path: "/music/library2"},
})
return &MockLibraryWrapper{MockLibraryRepo: repo}
return &MockLibraryService{MockLibraryRepo: repo}
}
func (m *MockLibraryWrapper) NewRepository(ctx context.Context) rest.Repository {
func (m *MockLibraryService) NewRepository(ctx context.Context) rest.Repository {
return &MockLibraryRestAdapter{MockLibraryRepo: m.MockLibraryRepo}
}
@ -41,6 +42,3 @@ func (m *MockLibraryWrapper) NewRepository(ctx context.Context) rest.Repository
func (a *MockLibraryRestAdapter) Delete(id string) error {
return a.DeleteByStringID(id)
}
var _ Library = (*MockLibraryWrapper)(nil)
var _ rest.Repository = (*MockLibraryRestAdapter)(nil)

View File

@ -0,0 +1,30 @@
package tests
import (
"context"
"github.com/deluan/rest"
)
// MockUserService provides a simple wrapper around MockedUserRepo
// that implements the core.User interface for testing.
// Returns concrete type to avoid import cycles - callers assign to core.User.
type MockUserService struct {
*MockedUserRepo
}
// MockUserRestAdapter adapts MockedUserRepo to rest.Repository interface
type MockUserRestAdapter struct {
*MockedUserRepo
}
// NewMockUserService creates a new mock user service for testing.
// Returns concrete type - assign to core.User at call site.
func NewMockUserService() *MockUserService {
repo := CreateMockUserRepo()
return &MockUserService{MockedUserRepo: repo}
}
func (m *MockUserService) NewRepository(ctx context.Context) rest.Repository {
return &MockUserRestAdapter{MockedUserRepo: m.MockedUserRepo}
}