package handlers import ( "bytes" "encoding/json" "net/http" "net/http/httptest" "testing" "bookhoard/internal/database" "github.com/google/uuid" "github.com/jackc/pgx/v5/pgtype" "github.com/labstack/echo/v5" "github.com/stretchr/testify/assert" ) func TestPreviewCollection_NoAuth(t *testing.T) { e := echo.New() handler := &CollectionHandler{} reqBody := map[string]interface{}{ "library_id": uuid.New().String(), "rules": []map[string]interface{}{}, "manual_book_ids": []string{}, "limit": 20, } body, _ := json.Marshal(reqBody) req := httptest.NewRequest("POST", "/api/collections/preview", bytes.NewBuffer(body)) req.Header.Set("Content-Type", "application/json") rec := httptest.NewRecorder() c := e.NewContext(req, rec) err := handler.PreviewCollection(c) assert.NoError(t, err) assert.Equal(t, http.StatusUnauthorized, rec.Code) } func TestPreviewCollection_MissingLibraryID(t *testing.T) { e := echo.New() handler := &CollectionHandler{} reqBody := map[string]interface{}{ "library_id": "", "rules": []map[string]interface{}{}, "manual_book_ids": []string{}, "limit": 20, } body, _ := json.Marshal(reqBody) req := httptest.NewRequest("POST", "/api/collections/preview", bytes.NewBuffer(body)) req.Header.Set("Content-Type", "application/json") rec := httptest.NewRecorder() // Mock user (would normally come from middleware) user := database.Users{ ID: pgtype.UUID{Bytes: uuid.New(), Valid: true}, } c := e.NewContext(req, rec) c.Set("user", user) err := handler.PreviewCollection(c) assert.NoError(t, err) assert.Equal(t, http.StatusBadRequest, rec.Code) } func TestPreviewCollection_InvalidLibraryID(t *testing.T) { e := echo.New() handler := &CollectionHandler{} reqBody := map[string]interface{}{ "library_id": "invalid-uuid", "rules": []map[string]interface{}{}, "manual_book_ids": []string{}, "limit": 20, } body, _ := json.Marshal(reqBody) req := httptest.NewRequest("POST", "/api/collections/preview", bytes.NewBuffer(body)) req.Header.Set("Content-Type", "application/json") rec := httptest.NewRecorder() // Mock user user := database.Users{ ID: pgtype.UUID{Bytes: uuid.New(), Valid: true}, } c := e.NewContext(req, rec) c.Set("user", user) err := handler.PreviewCollection(c) assert.NoError(t, err) assert.Equal(t, http.StatusBadRequest, rec.Code) }