refactor(tests): enhance test infrastructure with library/collection helpers

- Add LibraryTestData struct to TestDeviceSetup
- Implement CreateLibrary() for proper library creation in tests
- Implement CreateCollection() for test collection support
- Improve test isolation with dedicated library creation

This provides a more robust foundation for integration tests that need
proper library management support.
This commit is contained in:
2026-02-13 20:04:47 -05:00
parent 8ed0bdb040
commit 368c790c67
11 changed files with 849 additions and 556 deletions
+56 -2
View File
@@ -46,9 +46,16 @@ type TestDeviceSetup struct {
Config *config.Config
User UserTestData
Device DeviceTestData
Library LibraryTestData
UserToken string
}
type LibraryTestData struct {
ID string
Name string
Type string
}
type UserTestData struct {
ID uuid.UUID
Email string
@@ -300,6 +307,55 @@ func (s *TestDeviceSetup) CreateDevice(t *testing.T, deviceName, deviceType, dev
}
}
// CreateLibrary creates a test library for the TestDeviceSetup
func (s *TestDeviceSetup) CreateLibrary(t *testing.T, name, libraryType string) string {
ctx := context.Background()
// Get the library_type_id for the specified type
libraryTypeRow, err := s.DB.GetLibraryTypeByName(ctx, libraryType)
require.NoError(t, err, "Should find library type")
pgUserID := pgtype.UUID{Bytes: [16]byte(s.User.ID), Valid: true}
library, err := s.DB.CreateLibrary(ctx, database.CreateLibraryParams{
Name: name,
Description: pgtype.Text{String: "Test library description", Valid: true},
LibraryTypeID: libraryTypeRow.ID,
CreatedByAdminID: pgUserID,
})
require.NoError(t, err, "Should create library")
libraryUUID, err := uuid.FromBytes(library.ID.Bytes[0:16])
require.NoError(t, err, "Should parse library ID")
s.Library = LibraryTestData{
ID: libraryUUID.String(),
Name: name,
Type: libraryType,
}
return libraryUUID.String()
}
// CreateCollection creates a test collection for the TestDeviceSetup
func (s *TestDeviceSetup) CreateCollection(t *testing.T, name string) string {
ctx := context.Background()
pgUserID := pgtype.UUID{Bytes: [16]byte(s.User.ID), Valid: true}
collection, err := s.DB.CreateCollection(ctx, database.CreateCollectionParams{
UserID: pgUserID,
Name: name,
Description: pgtype.Text{String: "Test collection description", Valid: true},
Color: pgtype.Text{String: "#FF5733", Valid: true},
Icon: pgtype.Text{String: "folder", Valid: true},
})
require.NoError(t, err, "Should create collection")
collectionUUID, err := uuid.FromBytes(collection.ID.Bytes[0:16])
require.NoError(t, err, "Should parse collection ID")
return collectionUUID.String()
}
// setupTestServer creates a test server with a test database
// Returns: *TestServerSetup with automatic cleanup via t.Cleanup
func setupTestServer(t *testing.T) *TestServerSetup {
@@ -355,7 +411,6 @@ func setupTestServer(t *testing.T) *TestServerSetup {
worker := services.NewWorker(3)
collectionHandler := handlers.NewCollectionHandler(queries, connManager)
mediaHandler := handlers.NewMediaHandler(queries, libraryService, worker)
searchHandler := handlers.NewSearchHandler(queries)
matchingHandler := handlers.NewMatchingHandler(queries, connManager)
// Create conversion service for OPDS
@@ -387,7 +442,6 @@ func setupTestServer(t *testing.T) *TestServerSetup {
LibraryHandler: libraryHandler,
DeviceHandler: deviceHandler,
MediaHandler: mediaHandler,
SearchHandler: searchHandler,
MatchingHandler: matchingHandler,
KOReaderHandler: koreaderHandler,
WSHandler: wsHandler,