test: improve test infrastructure and fix integration tests

- Add folder to library before scanning in fsnotify integration test
- Update API endpoint paths from /items to /media-items
- Refactor test server setup to support WebSocket hijacking
- Add JobsHandler to test server configuration
- Implement proper job status polling instead of fixed delays
- Consolidate addFolderToLibrary helper into test_helpers.go
- Remove duplicate helper function from media_item_isbn_test.go
- Add error logging for search test failures
- Improve test robustness with better nil handling and type assertions
- Update worker test to use EnqueueJob and poll for completion
- Add global worker instance reset in test cleanup
- Fix media_scanner_test to initialize folders before testing
This commit is contained in:
2026-03-06 01:52:19 -05:00
parent ba2f29983c
commit 4d8e3e5358
8 changed files with 140 additions and 51 deletions
+38 -2
View File
@@ -13,6 +13,7 @@ import (
"context"
"encoding/json"
"fmt"
"net"
"net/http"
"net/http/httptest"
"os"
@@ -105,6 +106,9 @@ func (s *TestServerSetup) Close() error {
s.QueueCancel = nil
}
// Reset global worker instance
services.WorkerInstance = nil
// Stop connection manager cleanup task
if s.CleanupCancel != nil {
s.CleanupCancel()
@@ -459,6 +463,8 @@ func setupTestServer(t *testing.T) *TestServerSetup {
// Create refactored handlers (matching main.go)
libraryService := services.NewLibraryService(queries)
worker := services.NewWorker(3, connManager)
services.WorkerInstance = worker
jobsHandler := handlers.NewJobsHandler(queries, worker)
collectionHandler := handlers.NewCollectionHandler(queries, libraryService, connManager)
dashboardService := services.NewDashboardService(queries)
dashboardHandler := handlers.NewDashboardHandler(queries)
@@ -505,6 +511,7 @@ func setupTestServer(t *testing.T) *TestServerSetup {
DashboardHandler: dashboardHandler,
DashboardService: dashboardService,
OPDSHandler: opdsHandler,
JobsHandler: jobsHandler,
ConnManager: connManager,
QueueProcessor: queueProcessor,
DeviceAuthMiddleware: deviceAuthMiddleware,
@@ -513,8 +520,17 @@ func setupTestServer(t *testing.T) *TestServerSetup {
router.RegisterRoutes(routerConfig)
// Create test server
ts := httptest.NewServer(e)
ln, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err, "Failed to create listener")
// Configure Echo's HTTP server with the listener
e.Server.Handler = e
e.Server.Addr = ln.Addr().String()
// Create test server using Echo's server config (supports WebSocket hijacking)
ts := &httptest.Server{
Listener: ln,
Config: e.Server,
}
ts.Start()
ctx := context.Background()
@@ -718,3 +734,23 @@ func createTestMediaItemID(t *testing.T, setup *TestServerSetup) string {
return mediaItemID
}
// addFolderToLibrary adds a folder to a test library via HTTP API
func addFolderToLibrary(t *testing.T, setup *TestServerSetup, libraryID string, folderPath string) {
t.Helper()
payload := map[string]interface{}{
"folder_path": folderPath,
}
body, _ := json.Marshal(payload)
req, _ := http.NewRequest("POST", setup.Server.URL+"/api/libraries/"+libraryID+"/folders", bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+setup.Token)
client := &http.Client{}
resp, err := client.Do(req)
require.NoError(t, err)
defer resp.Body.Close()
require.Equal(t, http.StatusCreated, resp.StatusCode)
}