docs(dashboard): use existing test helpers and document automatic cleanup

Fixed integration tests to use existing helpers from test_helpers.go:

Changes:
- Replace getUserUUIDFromToken() with getTestUserID(t, db) helper 
- Replace parseUUID() with uuid.MustParse() 
- Add explicit comments about automatic cleanup via t.Cleanup() 

Test Helpers Used (all from test_helpers.go):
- setupTestServer(t) - creates test server with automatic cleanup
- loginTestUser(t, ts, db) - logs in admin user
- loginRegularUser(t, ts, db) - logs in regular user
- setupDeviceTest(t) - creates server + user + device + library
- getTestUserID(t, db) - gets/creates admin test user UUID
- uuid.MustParse() - parses UUID strings

Cleanup Pattern:
- Automatic via t.Cleanup() inside setupTestServer()
- Registered automatically when setupTestServer() is called
- No manual defer setup.Close() needed
- Runs even if test fails or panics
- Cleanup order: queue → connections → server → database

Dashboard-Specific Helper:
- updateDashboardPreferences() - only for dashboard testing
- Saves dashboard preferences for test scenarios

Benefits:
- Uses proven, existing helpers (no reinventing the wheel)
- Automatic cleanup prevents resource leaks
- Follows project testing patterns exactly
- Less custom code = fewer bugs
This commit is contained in:
2026-02-17 22:31:38 -05:00
parent ac88031855
commit 0e386ca87f
+35 -9
View File
@@ -2192,6 +2192,9 @@ import (
func TestDashboardAPI_GetSections(t *testing.T) {
setup := setupTestServer(t)
// Note: t.Cleanup() is automatically registered inside setupTestServer()
// No manual cleanup needed - Close() called automatically when test completes
adminToken := loginTestUser(t, setup.Server, setup.DB)
// Create test library with media items
@@ -2309,6 +2312,8 @@ func TestDashboardAPI_GetSections(t *testing.T) {
func TestDashboardAPI_UserPreferences(t *testing.T) {
setup := setupTestServer(t)
// Automatic cleanup via t.Cleanup() - no manual cleanup needed
adminToken := loginTestUser(t, setup.Server, setup.DB)
// Create test library
@@ -2316,9 +2321,9 @@ func TestDashboardAPI_UserPreferences(t *testing.T) {
libraryID := deviceSetup.CreateLibrary(t, "Test Library", "ebooks")
t.Run("GetSections_WithHiddenSections", func(t *testing.T) {
// First, save preferences to hide a section
userUUID := getUserUUIDFromToken(t, setup.DB, adminToken)
libUUID := parseUUID(t, libraryID)
// Get admin user UUID using existing helper
userUUID := getTestUserID(t, setup.DB)
libUUID := uuid.MustParse(libraryID)
// Save dashboard preferences with hidden sections
updateDashboardPreferences(t, setup.DB, userUUID, libUUID, map[string]interface{}{
@@ -2349,8 +2354,8 @@ func TestDashboardAPI_UserPreferences(t *testing.T) {
})
t.Run("GetSections_WithCustomOrder", func(t *testing.T) {
userUUID := getUserUUIDFromToken(t, setup.DB, adminToken)
libUUID := parseUUID(t, libraryID)
userUUID := getTestUserID(t, setup.DB)
libUUID := uuid.MustParse(libraryID)
// Save dashboard preferences with custom order
customOrder := []string{"recently-read", "continue-reading", "in-progress"}
@@ -2390,6 +2395,8 @@ func TestDashboardAPI_UserPreferences(t *testing.T) {
func TestDashboardSSR_Page(t *testing.T) {
setup := setupTestServer(t)
// Automatic cleanup via t.Cleanup() - no manual cleanup needed
adminToken := loginTestUser(t, setup.Server, setup.DB)
// Create test library
@@ -2431,6 +2438,7 @@ func TestDashboardSSR_Page(t *testing.T) {
// Helper functions for dashboard tests
// updateDashboardPreferences saves dashboard preferences for testing
// NOTE: This is specific to dashboard testing - not in test_helpers.go
func updateDashboardPreferences(t *testing.T, db *database.Queries, userID, libraryID uuid.UUID, prefs map[string]interface{}) {
hiddenSections := prefs["hidden_sections"].([]string)
sectionOrder := prefs["section_order"].([]string)
@@ -2446,9 +2454,7 @@ func updateDashboardPreferences(t *testing.T, db *database.Queries, userID, libr
}
```
**Key Helper Functions Available:**
From `test_helpers.go`:
**Key Helper Functions Available (from test_helpers.go):**
```go
// setupTestServer creates complete test environment with auto cleanup
@@ -2471,6 +2477,20 @@ adminUUID := getTestUserID(t, setup.DB)
// getRegularUserID - gets/creates regular test user UUID
userUUID := getRegularUserID(t, setup.DB)
// uuid.MustParse - parse UUID string (from uuid package)
libUUID := uuid.MustParse(libraryID)
```
**Dashboard-Specific Helper (created for these tests):**
```go
// updateDashboardPreferences - saves dashboard preferences for testing
// NOTE: Only needed for dashboard testing, not a general helper
updateDashboardPreferences(t, setup.DB, userUUID, libUUID, map[string]interface{}{
"hidden_sections": []string{"recently-added"},
"section_order": []string{"recently-read", "continue-reading"},
})
```
**Cleanup Pattern:**
@@ -2619,7 +2639,13 @@ on('click', '[data-action="open-dashboard-settings"]', () => {
### Integration Tests (Phase 15):
- [ ] Integration tests created (`cmd/server/tests/dashboard_test.go`)
- [ ] Uses setupTestServer() helper
- [ ] Uses `setupTestServer(t)` helper with automatic `t.Cleanup()`
- [ ] Uses `loginTestUser(t, ts, db)` helper ✅
- [ ] Uses `loginRegularUser(t, ts, db)` helper ✅
- [ ] Uses `setupDeviceTest(t)` helper ✅
- [ ] Uses `getTestUserID(t, db)` helper ✅
- [ ] Uses `uuid.MustParse()` for UUID parsing ✅
- [ ] Automatic cleanup (no manual defer/Close needed) ✅
- [ ] Three-context testing (no auth, user, admin)
- [ ] GET /api/dashboard/sections tested
- [ ] User preferences tested (hidden sections, custom order)