diff options
Diffstat (limited to 'internal/handlers/handlers_test.go')
| -rw-r--r-- | internal/handlers/handlers_test.go | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/internal/handlers/handlers_test.go b/internal/handlers/handlers_test.go index 96cb911..cd56e32 100644 --- a/internal/handlers/handlers_test.go +++ b/internal/handlers/handlers_test.go @@ -3,6 +3,7 @@ package handlers import ( "context" "encoding/json" + "fmt" "html/template" "net/http" "net/http/httptest" @@ -11,6 +12,8 @@ import ( "testing" "time" + "github.com/go-chi/chi/v5" + "task-dashboard/internal/api" "task-dashboard/internal/config" "task-dashboard/internal/models" @@ -698,3 +701,86 @@ func TestHandleCompleteAtom_APIError(t *testing.T) { t.Errorf("Task should NOT be deleted from cache on API error, found %d tasks", len(cachedTasks)) } } + +// TestHandleShoppingModeComplete tests completing (removing) shopping items +func TestHandleShoppingModeComplete(t *testing.T) { + db, cleanup := setupTestDB(t) + defer cleanup() + + tmpl := loadTestTemplates(t) + cfg := &config.Config{TemplateDir: "web/templates"} + + h := &Handler{ + store: db, + templates: tmpl, + config: cfg, + } + + // Add a user shopping item + err := db.SaveUserShoppingItem("Test Item", "TestStore") + if err != nil { + t.Fatalf("Failed to save user shopping item: %v", err) + } + + // Get the item to find its ID + items, err := db.GetUserShoppingItems() + if err != nil { + t.Fatalf("Failed to get user shopping items: %v", err) + } + if len(items) != 1 { + t.Fatalf("Expected 1 item, got %d", len(items)) + } + + itemID := items[0].ID + + t.Run("complete user item deletes it", func(t *testing.T) { + req := httptest.NewRequest("POST", "/shopping/mode/TestStore/complete", nil) + req.Form = map[string][]string{ + "id": {fmt.Sprintf("user-%d", itemID)}, + "source": {"user"}, + } + + // Add chi URL params + rctx := chi.NewRouteContext() + rctx.URLParams.Add("store", "TestStore") + req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx)) + + w := httptest.NewRecorder() + h.HandleShoppingModeComplete(w, req) + + if w.Code != http.StatusOK { + t.Errorf("Expected status 200, got %d", w.Code) + } + + // Verify item was deleted + remainingItems, _ := db.GetUserShoppingItems() + if len(remainingItems) != 0 { + t.Errorf("Expected 0 items after completion, got %d", len(remainingItems)) + } + }) + + t.Run("complete external item marks it checked", func(t *testing.T) { + req := httptest.NewRequest("POST", "/shopping/mode/TestStore/complete", nil) + req.Form = map[string][]string{ + "id": {"trello-123"}, + "source": {"trello"}, + } + + rctx := chi.NewRouteContext() + rctx.URLParams.Add("store", "TestStore") + req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx)) + + w := httptest.NewRecorder() + h.HandleShoppingModeComplete(w, req) + + if w.Code != http.StatusOK { + t.Errorf("Expected status 200, got %d", w.Code) + } + + // Verify item is marked as checked + checks, _ := db.GetShoppingItemChecks("trello") + if !checks["trello-123"] { + t.Error("Expected trello item to be marked as checked") + } + }) +} |
