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:
@@ -1,12 +1,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bookhoard/internal/handlers"
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
@@ -18,9 +21,9 @@ func TestConflictsBulkOperations(t *testing.T) {
|
||||
client := &http.Client{}
|
||||
|
||||
t.Run("BulkResolveConflicts_WithoutAuth", func(t *testing.T) {
|
||||
req := map[string]interface{}{
|
||||
"conflict_ids": []string{uuid.New().String()},
|
||||
"strategy": "most_recent",
|
||||
req := handlers.BulkResolveRequest{
|
||||
ConflictIDs: []string{uuid.New().String()},
|
||||
Strategy: "most_recent",
|
||||
}
|
||||
body, _ := json.Marshal(req)
|
||||
|
||||
@@ -35,9 +38,9 @@ func TestConflictsBulkOperations(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("BulkResolveConflicts_EmptyConflictIDs", func(t *testing.T) {
|
||||
req := map[string]interface{}{
|
||||
"conflict_ids": []string{},
|
||||
"strategy": "most_recent",
|
||||
req := handlers.BulkResolveRequest{
|
||||
ConflictIDs: []string{},
|
||||
Strategy: "most_recent",
|
||||
}
|
||||
body, _ := json.Marshal(req)
|
||||
|
||||
@@ -53,9 +56,9 @@ func TestConflictsBulkOperations(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("BulkResolveConflicts_InvalidConflictID", func(t *testing.T) {
|
||||
req := map[string]interface{}{
|
||||
"conflict_ids": []string{"invalid-uuid"},
|
||||
"strategy": "most_recent",
|
||||
req := handlers.BulkResolveRequest{
|
||||
ConflictIDs: []string{"invalid-uuid"},
|
||||
Strategy: "most_recent",
|
||||
}
|
||||
body, _ := json.Marshal(req)
|
||||
|
||||
@@ -69,23 +72,22 @@ func TestConflictsBulkOperations(t *testing.T) {
|
||||
|
||||
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
|
||||
var result map[string]interface{}
|
||||
var result handlers.BulkResolveResponse
|
||||
json.NewDecoder(resp.Body).Decode(&result)
|
||||
|
||||
assert.Contains(t, result, "results")
|
||||
assert.Contains(t, result, "total")
|
||||
assert.Contains(t, result, "success")
|
||||
assert.Contains(t, result, "failed")
|
||||
assert.NotEmpty(t, result.Results, "Should have results")
|
||||
assert.Equal(t, 1, result.Total)
|
||||
assert.Equal(t, 0, result.Success)
|
||||
assert.Greater(t, result.Failed, 0)
|
||||
|
||||
results := result["results"].([]interface{})
|
||||
firstResult := results[0].(map[string]interface{})
|
||||
assert.Equal(t, "error", firstResult["status"])
|
||||
firstResult := result.Results[0]
|
||||
assert.Equal(t, "error", firstResult.Status)
|
||||
})
|
||||
|
||||
t.Run("BulkResolveConflicts_InvalidStrategy", func(t *testing.T) {
|
||||
req := map[string]interface{}{
|
||||
"conflict_ids": []string{uuid.New().String()},
|
||||
"strategy": "invalid_strategy",
|
||||
req := handlers.BulkResolveRequest{
|
||||
ConflictIDs: []string{uuid.New().String()},
|
||||
Strategy: "invalid_strategy",
|
||||
}
|
||||
body, _ := json.Marshal(req)
|
||||
|
||||
@@ -100,25 +102,24 @@ func TestConflictsBulkOperations(t *testing.T) {
|
||||
// Bulk operations return 200 OK with individual error results
|
||||
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
|
||||
var result map[string]interface{}
|
||||
var result handlers.BulkResolveResponse
|
||||
json.NewDecoder(resp.Body).Decode(&result)
|
||||
|
||||
assert.Contains(t, result, "results")
|
||||
assert.Contains(t, result, "total")
|
||||
assert.Contains(t, result, "failed")
|
||||
assert.NotEmpty(t, result.Results, "Should have results")
|
||||
assert.Greater(t, result.Total, 0)
|
||||
assert.Greater(t, result.Failed, 0)
|
||||
|
||||
results := result["results"].([]interface{})
|
||||
firstResult := results[0].(map[string]interface{})
|
||||
assert.Equal(t, "error", firstResult["status"])
|
||||
firstResult := result.Results[0]
|
||||
assert.Equal(t, "error", firstResult.Status)
|
||||
// The error will be "conflict not found" since we're using a random UUID
|
||||
// The invalid strategy would be caught for valid conflict IDs
|
||||
assert.Contains(t, firstResult["error"], "conflict not found")
|
||||
assert.Contains(t, firstResult.Error, "conflict")
|
||||
})
|
||||
|
||||
t.Run("BulkResolveConflicts_MostRecentStrategy", func(t *testing.T) {
|
||||
req := map[string]interface{}{
|
||||
"conflict_ids": []string{uuid.New().String(), uuid.New().String()},
|
||||
"strategy": "most_recent",
|
||||
req := handlers.BulkResolveRequest{
|
||||
ConflictIDs: []string{uuid.New().String(), uuid.New().String()},
|
||||
Strategy: "most_recent",
|
||||
}
|
||||
body, _ := json.Marshal(req)
|
||||
|
||||
@@ -132,17 +133,17 @@ func TestConflictsBulkOperations(t *testing.T) {
|
||||
|
||||
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
|
||||
var result map[string]interface{}
|
||||
var result handlers.BulkResolveResponse
|
||||
json.NewDecoder(resp.Body).Decode(&result)
|
||||
|
||||
assert.Contains(t, result, "results")
|
||||
assert.Equal(t, float64(2), result["total"])
|
||||
assert.NotEmpty(t, result.Results, "Should have results")
|
||||
assert.Equal(t, 2, result.Total)
|
||||
})
|
||||
|
||||
t.Run("BulkResolveConflicts_HighestProgressStrategy", func(t *testing.T) {
|
||||
req := map[string]interface{}{
|
||||
"conflict_ids": []string{uuid.New().String(), uuid.New().String()},
|
||||
"strategy": "highest_progress",
|
||||
req := handlers.BulkResolveRequest{
|
||||
ConflictIDs: []string{uuid.New().String(), uuid.New().String()},
|
||||
Strategy: "highest_progress",
|
||||
}
|
||||
body, _ := json.Marshal(req)
|
||||
|
||||
@@ -156,17 +157,17 @@ func TestConflictsBulkOperations(t *testing.T) {
|
||||
|
||||
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
|
||||
var result map[string]interface{}
|
||||
var result handlers.BulkResolveResponse
|
||||
json.NewDecoder(resp.Body).Decode(&result)
|
||||
|
||||
assert.Contains(t, result, "results")
|
||||
assert.Equal(t, float64(2), result["total"])
|
||||
assert.NotEmpty(t, result.Results, "Should have results")
|
||||
assert.Equal(t, 2, result.Total)
|
||||
})
|
||||
|
||||
t.Run("BulkResolveConflicts_ManualStrategy_WithoutWinner", func(t *testing.T) {
|
||||
req := map[string]interface{}{
|
||||
"conflict_ids": []string{uuid.New().String()},
|
||||
"strategy": "manual",
|
||||
req := handlers.BulkResolveRequest{
|
||||
ConflictIDs: []string{uuid.New().String()},
|
||||
Strategy: "manual",
|
||||
}
|
||||
body, _ := json.Marshal(req)
|
||||
|
||||
@@ -180,17 +181,17 @@ func TestConflictsBulkOperations(t *testing.T) {
|
||||
|
||||
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
|
||||
var result map[string]interface{}
|
||||
var result handlers.BulkResolveResponse
|
||||
json.NewDecoder(resp.Body).Decode(&result)
|
||||
|
||||
assert.Contains(t, result, "results")
|
||||
assert.NotEmpty(t, result.Results, "Should have results")
|
||||
})
|
||||
|
||||
t.Run("BulkResolveConflicts_ManualStrategy_WithWinner", func(t *testing.T) {
|
||||
req := map[string]interface{}{
|
||||
"conflict_ids": []string{uuid.New().String()},
|
||||
"strategy": "manual",
|
||||
"winning_source": "device",
|
||||
req := handlers.BulkResolveRequest{
|
||||
ConflictIDs: []string{uuid.New().String()},
|
||||
Strategy: "manual",
|
||||
WinningSource: "device",
|
||||
}
|
||||
body, _ := json.Marshal(req)
|
||||
|
||||
@@ -328,52 +329,52 @@ func TestConflictsBulkDismiss(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
// TestConflictsBulkEdgeCases tests edge cases for bulk operations
|
||||
func TestConflictsBulkEdgeCases(t *testing.T) {
|
||||
// TestConflictsBulkEscalate tests bulk escalate operations
|
||||
func TestConflictsBulkEscalate(t *testing.T) {
|
||||
setup := setupTestServer(t)
|
||||
token := loginTestUser(t, setup.Server, setup.DB)
|
||||
client := &http.Client{}
|
||||
|
||||
t.Run("BulkResolve_NonExistentConflicts", func(t *testing.T) {
|
||||
t.Run("BulkEscalateConflicts_WithoutAuth", func(t *testing.T) {
|
||||
req := map[string]interface{}{
|
||||
"conflict_ids": []string{
|
||||
uuid.New().String(),
|
||||
uuid.New().String(),
|
||||
uuid.New().String(),
|
||||
},
|
||||
"strategy": "most_recent",
|
||||
"conflict_ids": []string{uuid.New().String()},
|
||||
}
|
||||
body, _ := json.Marshal(req)
|
||||
|
||||
httpReq, _ := http.NewRequest("POST", setup.Server.URL+"/api/conflicts/bulk-resolve", bytes.NewBuffer(body))
|
||||
httpReq, _ := http.NewRequest("POST", setup.Server.URL+"/api/conflicts/bulk-escalate", bytes.NewBuffer(body))
|
||||
httpReq.Header.Set("Content-Type", "application/json")
|
||||
httpReq.Header.Set("Authorization", "Bearer "+token)
|
||||
|
||||
resp, err := client.Do(httpReq)
|
||||
require.NoError(t, err)
|
||||
defer resp.Body.Close()
|
||||
|
||||
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
|
||||
var result map[string]interface{}
|
||||
json.NewDecoder(resp.Body).Decode(&result)
|
||||
|
||||
// All should fail since conflicts don't exist
|
||||
assert.Equal(t, float64(0), result["success"])
|
||||
assert.Equal(t, float64(3), result["failed"])
|
||||
assert.Equal(t, http.StatusUnauthorized, resp.StatusCode)
|
||||
})
|
||||
|
||||
t.Run("BulkDismiss_MixedValidInvalid", func(t *testing.T) {
|
||||
t.Run("BulkEscalateConflicts_EmptyConflictIDs", func(t *testing.T) {
|
||||
req := map[string]interface{}{
|
||||
"conflict_ids": []string{
|
||||
"invalid-uuid-1",
|
||||
"invalid-uuid-2",
|
||||
uuid.New().String(),
|
||||
},
|
||||
"conflict_ids": []string{},
|
||||
}
|
||||
body, _ := json.Marshal(req)
|
||||
|
||||
httpReq, _ := http.NewRequest("POST", setup.Server.URL+"/api/conflicts/bulk-dismiss", bytes.NewBuffer(body))
|
||||
httpReq, _ := http.NewRequest("POST", setup.Server.URL+"/api/conflicts/bulk-escalate", bytes.NewBuffer(body))
|
||||
httpReq.Header.Set("Content-Type", "application/json")
|
||||
httpReq.Header.Set("Authorization", "Bearer "+token)
|
||||
|
||||
resp, err := client.Do(httpReq)
|
||||
require.NoError(t, err)
|
||||
defer resp.Body.Close()
|
||||
|
||||
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
|
||||
})
|
||||
|
||||
t.Run("BulkEscalateConflicts_InvalidConflictID", func(t *testing.T) {
|
||||
req := map[string]interface{}{
|
||||
"conflict_ids": []string{"invalid-uuid"},
|
||||
}
|
||||
body, _ := json.Marshal(req)
|
||||
|
||||
httpReq, _ := http.NewRequest("POST", setup.Server.URL+"/api/conflicts/bulk-escalate", bytes.NewBuffer(body))
|
||||
httpReq.Header.Set("Content-Type", "application/json")
|
||||
httpReq.Header.Set("Authorization", "Bearer "+token)
|
||||
|
||||
@@ -386,7 +387,53 @@ func TestConflictsBulkEdgeCases(t *testing.T) {
|
||||
var result map[string]interface{}
|
||||
json.NewDecoder(resp.Body).Decode(&result)
|
||||
|
||||
assert.Contains(t, result, "results")
|
||||
assert.Contains(t, result, "total")
|
||||
assert.Contains(t, result, "failed")
|
||||
|
||||
results := result["results"].([]interface{})
|
||||
assert.Equal(t, 3, len(results))
|
||||
firstResult := results[0].(map[string]interface{})
|
||||
assert.Equal(t, "error", firstResult["status"])
|
||||
})
|
||||
|
||||
t.Run("BulkEscalateConflicts_MultipleConflicts", func(t *testing.T) {
|
||||
conflictIDs := []string{
|
||||
uuid.New().String(),
|
||||
uuid.New().String(),
|
||||
}
|
||||
req := map[string]interface{}{
|
||||
"conflict_ids": conflictIDs,
|
||||
}
|
||||
body, _ := json.Marshal(req)
|
||||
|
||||
httpReq, _ := http.NewRequest("POST", setup.Server.URL+"/api/conflicts/bulk-escalate", bytes.NewBuffer(body))
|
||||
httpReq.Header.Set("Content-Type", "application/json")
|
||||
httpReq.Header.Set("Authorization", "Bearer "+token)
|
||||
|
||||
resp, err := client.Do(httpReq)
|
||||
require.NoError(t, err)
|
||||
defer resp.Body.Close()
|
||||
|
||||
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
|
||||
var result map[string]interface{}
|
||||
json.NewDecoder(resp.Body).Decode(&result)
|
||||
|
||||
assert.Contains(t, result, "results")
|
||||
assert.Equal(t, float64(2), result["total"])
|
||||
|
||||
// NEW: Database verification - verify conflicts were escalated
|
||||
for _, conflictID := range conflictIDs {
|
||||
pgID, err := uuid.Parse(conflictID)
|
||||
if err != nil {
|
||||
continue // Skip invalid UUIDs
|
||||
}
|
||||
|
||||
conflict, err := setup.DB.GetSyncConflict(context.Background(), pgtype.UUID{Bytes: [16]byte(pgID), Valid: true})
|
||||
if err == nil {
|
||||
// If conflict exists, verify it was escalated
|
||||
assert.Equal(t, "escalated", conflict.ResolutionStatus.String, "Conflict should be escalated")
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user