diff options
Diffstat (limited to 'internal/handlers')
| -rw-r--r-- | internal/handlers/handlers_test.go | 105 | ||||
| -rw-r--r-- | internal/handlers/timeline_logic_test.go | 72 |
2 files changed, 174 insertions, 3 deletions
diff --git a/internal/handlers/handlers_test.go b/internal/handlers/handlers_test.go index f91eb32..d338cd3 100644 --- a/internal/handlers/handlers_test.go +++ b/internal/handlers/handlers_test.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "io" "net/http" "net/http/httptest" "os" @@ -1853,9 +1854,107 @@ func TestHandleTimeline(t *testing.T) { h.HandleTimeline(w, req) - // May return 500 if template not found - if w.Code != http.StatusOK && w.Code != http.StatusInternalServerError { - t.Errorf("Expected status 200 or 500, got %d", w.Code) + if w.Code != http.StatusOK { + t.Errorf("Expected status 200, got %d", w.Code) + } +} + +func TestHandleTimeline_RendersDataToTemplate(t *testing.T) { + h, cleanup := setupTestHandler(t) + defer cleanup() + + // Seed tasks with due dates in range + now := config.Now() + todayNoon := time.Date(now.Year(), now.Month(), now.Day(), 12, 0, 0, 0, now.Location()) + tasks := []models.Task{ + {ID: "t1", Content: "Today task", DueDate: &todayNoon, Labels: []string{}, CreatedAt: now}, + } + _ = h.store.SaveTasks(tasks) + + req := httptest.NewRequest("GET", "/tabs/timeline", nil) + w := httptest.NewRecorder() + + h.HandleTimeline(w, req) + + if w.Code != http.StatusOK { + t.Errorf("Expected status 200, got %d", w.Code) + } + + // Verify the renderer received timeline data with items + mock := h.renderer.(*MockRenderer) + if len(mock.Calls) == 0 { + t.Fatal("Expected renderer to be called") + } + + lastCall := mock.Calls[len(mock.Calls)-1] + if lastCall.Name != "timeline-tab" { + t.Errorf("Expected template 'timeline-tab', got '%s'", lastCall.Name) + } + + data, ok := lastCall.Data.(TimelineData) + if !ok { + t.Fatalf("Expected TimelineData, got %T", lastCall.Data) + } + + if len(data.TodayItems) == 0 { + t.Error("Expected TodayItems to contain at least one item, got 0") + } +} + +// ============================================================================= +// Dashboard Content Verification Tests +// ============================================================================= + +func TestHandleDashboard_ContainsSettingsLink(t *testing.T) { + db, cleanup := setupTestDB(t) + defer cleanup() + + mockTodoist := &mockTodoistClient{} + mockTrello := &mockTrelloClient{} + + // Use a renderer that outputs actual content so we can check for settings link + renderer := NewMockRenderer() + renderer.RenderFunc = func(w io.Writer, name string, data interface{}) error { + if name == "index.html" { + // Check that data includes what the template needs + // We verify the template at the source: index.html must contain /settings link + fmt.Fprintf(w, "rendered:%s", name) + } + return nil + } + + h := &Handler{ + store: db, + todoistClient: mockTodoist, + trelloClient: mockTrello, + config: &config.Config{CacheTTLMinutes: 5}, + renderer: renderer, + } + + req := httptest.NewRequest("GET", "/", nil) + w := httptest.NewRecorder() + + h.HandleDashboard(w, req) + + if w.Code != http.StatusOK { + t.Errorf("Expected status 200, got %d", w.Code) + } +} + +// TestDashboardTemplate_HasSettingsLink verifies the index.html template +// contains a link to /settings. This catches regressions where the settings +// button is accidentally removed. +func TestDashboardTemplate_HasSettingsLink(t *testing.T) { + // Read the actual template file and verify it contains a settings link + content, err := os.ReadFile("../../web/templates/index.html") + if err != nil { + t.Skipf("Cannot read template file (running from unexpected directory): %v", err) + } + + templateStr := string(content) + + if !strings.Contains(templateStr, `href="/settings"`) { + t.Error("index.html must contain a link to /settings (settings button missing from UI)") } } diff --git a/internal/handlers/timeline_logic_test.go b/internal/handlers/timeline_logic_test.go index 9a71741..11406b9 100644 --- a/internal/handlers/timeline_logic_test.go +++ b/internal/handlers/timeline_logic_test.go @@ -251,6 +251,78 @@ func TestCalcCalendarBounds(t *testing.T) { } } +func TestBuildTimeline_IncludesOverdueItems(t *testing.T) { + s := setupTestStore(t) + + // Base: "today" is Jan 2, 2023 + today := time.Date(2023, 1, 2, 0, 0, 0, 0, time.UTC) + yesterday := time.Date(2023, 1, 1, 10, 0, 0, 0, time.UTC) // overdue + todayNoon := time.Date(2023, 1, 2, 12, 0, 0, 0, time.UTC) // current + + // Save a task that is overdue (yesterday) and one that is current (today) + _ = s.SaveTasks([]models.Task{ + {ID: "overdue1", Content: "Overdue task", DueDate: &yesterday}, + {ID: "current1", Content: "Current task", DueDate: &todayNoon}, + }) + + // Query range: today through tomorrow + end := today.AddDate(0, 0, 1) + + items, err := BuildTimeline(context.Background(), s, nil, nil, today, end) + if err != nil { + t.Fatalf("BuildTimeline failed: %v", err) + } + + // Should include both the overdue task and the current task + if len(items) < 2 { + t.Errorf("Expected at least 2 items (overdue + current), got %d", len(items)) + for _, item := range items { + t.Logf(" item: %s (type=%s, time=%s)", item.Title, item.Type, item.Time) + } + } + + // Verify overdue task is marked as overdue + foundOverdue := false + for _, item := range items { + if item.ID == "overdue1" { + foundOverdue = true + if !item.IsOverdue { + t.Error("Expected overdue task to be marked IsOverdue=true") + } + if item.DaySection != models.DaySectionToday { + t.Errorf("Expected overdue task in Today section, got %s", item.DaySection) + } + } + } + if !foundOverdue { + t.Error("Overdue task was not included in timeline results") + } +} + +func TestBuildTimeline_ExcludesCompletedOverdue(t *testing.T) { + s := setupTestStore(t) + + yesterday := time.Date(2023, 1, 1, 10, 0, 0, 0, time.UTC) + today := time.Date(2023, 1, 2, 0, 0, 0, 0, time.UTC) + end := today.AddDate(0, 0, 1) + + // Save a completed overdue task — should NOT appear + _ = s.SaveTasks([]models.Task{ + {ID: "done1", Content: "Done overdue", DueDate: &yesterday, Completed: true}, + }) + + items, err := BuildTimeline(context.Background(), s, nil, nil, today, end) + if err != nil { + t.Fatalf("BuildTimeline failed: %v", err) + } + + for _, item := range items { + if item.ID == "done1" { + t.Error("Completed overdue task should not appear in timeline") + } + } +} + func timePtr(t time.Time) *time.Time { return &t } |
