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
+14 -54
View File
@@ -10,7 +10,7 @@ import (
"github.com/stretchr/testify/require"
)
// TestListMediaItemsSorting tests the sorting functionality for different contexts
// TestListMediaItemsSorting tests sorting functionality for different contexts
func TestListMediaItemsSorting(t *testing.T) {
t.Run("No user context - GET /api/media-items without authentication", func(t *testing.T) {
@@ -26,12 +26,6 @@ func TestListMediaItemsSorting(t *testing.T) {
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]interface{}{
"data": []map[string]interface{}{
{"id": "1", "title": "A Book", "library_id": "test-lib-id"},
{"id": "2", "title": "B Book", "library_id": "test-lib-id"},
},
})
})
handler.ServeHTTP(rr, req)
@@ -52,14 +46,8 @@ func TestListMediaItemsSorting(t *testing.T) {
return
}
// Return sorted data
results := []map[string]interface{}{
{"id": "1", "title": "A Book", "author": "Author A", "library_id": "test-lib-id", "library_name": "Test Library"},
{"id": "2", "title": "B Book", "author": "Author B", "library_id": "test-lib-id", "library_name": "Test Library"},
{"id": "3", "title": "C Book", "author": "Author C", "library_id": "test-lib-id", "library_name": "Test Library"},
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]interface{}{"data": results})
w.Write([]byte(`{"data": [{"id": "1", "title": "A Book", "author": "Author A", "library_id": "test-lib-id", "library_name": "Test Library"}, {"id": "2", "title": "B Book", "author": "Author B", "library_id": "test-lib-id", "library_name": "Test Library"}, {"id": "3", "title": "C Book", "author": "Author C", "library_id": "test-lib-id", "library_name": "Test Library"}]}`))
})
handler.ServeHTTP(rr, req)
@@ -82,17 +70,12 @@ func TestListMediaItemsSorting(t *testing.T) {
authHeader := r.Header.Get("Authorization")
if authHeader == "" || !containsPrefix(authHeader, "Bearer ") {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte(`{"message":"missing or malformed jwt"}`))
return
}
// Return data sorted by author descending
results := []map[string]interface{}{
{"id": "1", "title": "Book C", "author": "Smith", "library_id": "test-lib-id"},
{"id": "2", "title": "Book A", "author": "Jones", "library_id": "test-lib-id"},
{"id": "3", "title": "Book B", "author": "Anderson", "library_id": "test-lib-id"},
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]interface{}{"data": results})
w.Write([]byte(`{"data": [{"id": "1", "title": "Book C", "author": "Smith", "library_id": "test-lib-id"}, {"id": "2", "title": "Book A", "author": "Jones", "library_id": "test-lib-id"}, {"id": "3", "title": "Book B", "author": "Anderson", "library_id": "test-lib-id"}]}`))
})
handler.ServeHTTP(rr, req)
@@ -115,17 +98,12 @@ func TestListMediaItemsSorting(t *testing.T) {
authHeader := r.Header.Get("Authorization")
if authHeader == "" || !containsPrefix(authHeader, "Bearer ") {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte(`{"message":"missing or malformed jwt"}`))
return
}
// Return data sorted by page count descending (longest first)
results := []map[string]interface{}{
{"id": "1", "title": "Long Book", "page_count": 500, "library_id": "test-lib-id"},
{"id": "2", "title": "Medium Book", "page_count": 300, "library_id": "test-lib-id"},
{"id": "3", "title": "Short Book", "page_count": 100, "library_id": "test-lib-id"},
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]interface{}{"data": results})
w.Write([]byte(`{"data": [{"id": "1", "title": "Long Book", "page_count": 500, "library_id": "test-lib-id"}, {"id": "2", "title": "Medium Book", "page_count": 300, "library_id": "test-lib-id"}, {"id": "3", "title": "Short Book", "page_count": 100, "library_id": "test-lib-id"}]}`))
})
handler.ServeHTTP(rr, req)
@@ -148,16 +126,12 @@ func TestListMediaItemsSorting(t *testing.T) {
authHeader := r.Header.Get("Authorization")
if authHeader == "" || !containsPrefix(authHeader, "Bearer ") {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte(`{"message":"missing or malformed jwt"}`))
return
}
// Should default to created_at DESC
results := []map[string]interface{}{
{"id": "1", "title": "Newest Book", "created_at": "2024-01-15T10:00:00Z", "library_id": "test-lib-id"},
{"id": "2", "title": "Older Book", "created_at": "2023-06-15T10:00:00Z", "library_id": "test-lib-id"},
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]interface{}{"data": results})
w.Write([]byte(`{"data": [{"id": "1", "title": "Newest Book", "created_at": "2024-01-15T10:00:00Z", "library_id": "test-lib-id"}, {"id": "2", "title": "Older Book", "created_at": "2023-06-15T10:00:00Z", "library_id": "test-lib-id"}]}`))
})
handler.ServeHTTP(rr, req)
@@ -173,17 +147,12 @@ func TestListMediaItemsSorting(t *testing.T) {
authHeader := r.Header.Get("Authorization")
if authHeader == "" || !containsPrefix(authHeader, "Bearer ") {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte(`{"message":"missing or malformed jwt"}`))
return
}
// Return data sorted by genre
results := []map[string]interface{}{
{"id": "1", "title": "Book A", "genre": "Fiction", "library_id": "test-lib-id"},
{"id": "2", "title": "Book B", "genre": "Non-Fiction", "library_id": "test-lib-id"},
{"id": "3", "title": "Book C", "genre": "Science Fiction", "library_id": "test-lib-id"},
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]interface{}{"data": results})
w.Write([]byte(`{"data": [{"id": "1", "title": "Book A", "genre": "Fiction", "library_id": "test-lib-id"}, {"id": "2", "title": "Book B", "genre": "Non-Fiction", "library_id": "test-lib-id"}, {"id": "3", "title": "Book C", "genre": "Science Fiction", "library_id": "test-lib-id"}]}`))
})
handler.ServeHTTP(rr, req)
@@ -206,17 +175,12 @@ func TestListMediaItemsSorting(t *testing.T) {
authHeader := r.Header.Get("Authorization")
if authHeader == "" || !containsPrefix(authHeader, "Bearer ") {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte(`{"message":"missing or malformed jwt"}`))
return
}
// Return data sorted by copyright year (newest first)
results := []map[string]interface{}{
{"id": "1", "title": "Modern Book", "copyright_year": 2023, "library_id": "test-lib-id"},
{"id": "2", "title": "90s Book", "copyright_year": 1995, "library_id": "test-lib-id"},
{"id": "3", "title": "Classic Book", "copyright_year": 1980, "library_id": "test-lib-id"},
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]interface{}{"data": results})
w.Write([]byte(`{"data": [{"id": "1", "title": "Modern Book", "copyright_year": 2023, "library_id": "test-lib-id"}, {"id": "2", "title": "90s Book", "copyright_year": 1995, "library_id": "test-lib-id"}, {"id": "3", "title": "Classic Book", "copyright_year": 1980, "library_id": "test-lib-id"}]}`))
})
handler.ServeHTTP(rr, req)
@@ -239,16 +203,12 @@ func TestListMediaItemsSorting(t *testing.T) {
authHeader := r.Header.Get("Authorization")
if authHeader == "" || !containsPrefix(authHeader, "Bearer ") {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte(`{"message":"missing or malformed jwt"}`))
return
}
// Return paginated results (second page)
results := []map[string]interface{}{
{"id": "2", "title": "B Book", "library_id": "test-lib-id"},
{"id": "3", "title": "C Book", "library_id": "test-lib-id"},
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]interface{}{"data": results})
w.Write([]byte(`{"data": [{"id": "2", "title": "B Book", "library_id": "test-lib-id"}, {"id": "3", "title": "C Book", "library_id": "test-lib-id"}]}`))
})
handler.ServeHTTP(rr, req)