package handlers import ( "net/http" "net/http/httptest" "strings" "testing" "task-dashboard/internal/api" "task-dashboard/internal/config" "task-dashboard/internal/store" ) func TestHandleDashboard_TabState(t *testing.T) { // Create a temporary database for testing db, err := store.New(":memory:", "../../migrations") if err != nil { t.Fatalf("Failed to create test database: %v", err) } defer db.Close() // Create mock API clients todoistClient := api.NewTodoistClient("test-key") trelloClient := api.NewTrelloClient("test-key", "test-token") // Create test config cfg := &config.Config{ Port: "8080", CacheTTLMinutes: 5, } // Create handler h := New(db, todoistClient, trelloClient, nil, nil, cfg) // Skip if templates are not loaded (test environment issue) if h.templates == nil { t.Skip("Templates not available in test environment") } tests := []struct { name string url string expectedTab string expectedActive string expectedHxGet string }{ { name: "default to tasks tab", url: "/", expectedTab: "tasks", expectedActive: `class="tab-button tab-button-active"`, expectedHxGet: `hx-get="/tabs/tasks"`, }, { name: "planning tab from query param", url: "/?tab=planning", expectedTab: "planning", expectedActive: `class="tab-button tab-button-active"`, expectedHxGet: `hx-get="/tabs/planning"`, }, { name: "meals tab from query param", url: "/?tab=meals", expectedTab: "meals", expectedActive: `class="tab-button tab-button-active"`, expectedHxGet: `hx-get="/tabs/meals"`, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { req := httptest.NewRequest(http.MethodGet, tt.url, nil) w := httptest.NewRecorder() h.HandleDashboard(w, req) resp := w.Result() defer resp.Body.Close() if resp.StatusCode != http.StatusOK { t.Errorf("Expected status 200, got %d", resp.StatusCode) return } body := w.Body.String() // Check that the expected tab has the active class // We need to find the button with the expected tab name and check if it has the active class if !strings.Contains(body, tt.expectedActive) { t.Errorf("Expected active class in response body") } // Check that the content div loads the correct tab if !strings.Contains(body, tt.expectedHxGet) { t.Errorf("Expected hx-get=\"/tabs/%s\" in response body, got: %s", tt.expectedTab, body) } }) } }