diff options
| -rw-r--r-- | internal/handlers/handlers_test.go | 105 | ||||
| -rw-r--r-- | internal/handlers/timeline_logic_test.go | 72 | ||||
| -rw-r--r-- | internal/store/sqlite.go | 15 | ||||
| -rw-r--r-- | internal/store/sqlite_test.go | 67 | ||||
| -rw-r--r-- | web/static/js/app.js | 29 | ||||
| -rw-r--r-- | web/templates/index.html | 16 | ||||
| -rw-r--r-- | web/templates/partials/conditions-tab.html | 6 | ||||
| -rw-r--r-- | web/templates/partials/meals-tab.html | 6 | ||||
| -rw-r--r-- | web/templates/partials/shopping-tab.html | 6 |
9 files changed, 282 insertions, 40 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 } diff --git a/internal/store/sqlite.go b/internal/store/sqlite.go index 465c3c1..24a24d7 100644 --- a/internal/store/sqlite.go +++ b/internal/store/sqlite.go @@ -735,14 +735,16 @@ func (s *Store) GetShoppingItemChecks(source string) (map[string]bool, error) { return checks, rows.Err() } -// GetTasksByDateRange retrieves tasks due within a specific date range +// GetTasksByDateRange retrieves tasks due within a specific date range, +// including overdue tasks (due before start) so they appear in the timeline. func (s *Store) GetTasksByDateRange(start, end time.Time) ([]models.Task, error) { rows, err := s.db.Query(` SELECT id, content, description, project_id, project_name, due_date, priority, completed, labels, url, created_at FROM tasks - WHERE due_date BETWEEN ? AND ? + WHERE due_date IS NOT NULL AND due_date <= ? + AND completed = FALSE ORDER BY due_date ASC, priority DESC - `, start, end) + `, end) if err != nil { return nil, err } @@ -755,15 +757,16 @@ func (s *Store) GetMealsByDateRange(start, end time.Time) ([]models.Meal, error) return s.GetMeals(start, end) } -// GetCardsByDateRange retrieves cards due within a specific date range +// GetCardsByDateRange retrieves cards due within a specific date range, +// including overdue cards (due before start) so they appear in the timeline. func (s *Store) GetCardsByDateRange(start, end time.Time) ([]models.Card, error) { rows, err := s.db.Query(` SELECT c.id, c.name, b.name, c.list_id, c.list_name, c.due_date, c.url FROM cards c JOIN boards b ON c.board_id = b.id - WHERE c.due_date BETWEEN ? AND ? + WHERE c.due_date IS NOT NULL AND c.due_date <= ? ORDER BY c.due_date ASC - `, start, end) + `, end) if err != nil { return nil, err } diff --git a/internal/store/sqlite_test.go b/internal/store/sqlite_test.go index 69d188a..9c56252 100644 --- a/internal/store/sqlite_test.go +++ b/internal/store/sqlite_test.go @@ -482,17 +482,78 @@ func TestGetTasksByDateRange(t *testing.T) { t.Fatalf("Failed to save tasks: %v", err) } - // Test range covering today and tomorrow + // Test range covering today and tomorrow — should include all tasks <= end start := now.Add(-1 * time.Hour) end := tomorrow.Add(1 * time.Hour) - + results, err := store.GetTasksByDateRange(start, end) if err != nil { t.Fatalf("GetTasksByDateRange failed: %v", err) } if len(results) != 2 { - t.Errorf("Expected 2 tasks, got %d", len(results)) + t.Errorf("Expected 2 tasks (today + tomorrow, not next week), got %d", len(results)) + } +} + +func TestGetTasksByDateRange_IncludesOverdue(t *testing.T) { + store := setupTestStoreWithTasks(t) + defer func() { _ = store.Close() }() + + now := time.Now() + yesterday := now.Add(-24 * time.Hour) + tomorrow := now.Add(24 * time.Hour) + + tasks := []models.Task{ + {ID: "overdue", Content: "Overdue", DueDate: &yesterday}, + {ID: "current", Content: "Current", DueDate: &now}, + } + + if err := store.SaveTasks(tasks); err != nil { + t.Fatalf("Failed to save tasks: %v", err) + } + + // Query from "today" onward — overdue tasks (before start) should also be included + results, err := store.GetTasksByDateRange(now, tomorrow) + if err != nil { + t.Fatalf("GetTasksByDateRange failed: %v", err) + } + + if len(results) != 2 { + t.Errorf("Expected 2 tasks (overdue + current), got %d", len(results)) + for _, r := range results { + t.Logf(" task: %s due=%v", r.Content, r.DueDate) + } + } +} + +func TestGetTasksByDateRange_ExcludesCompleted(t *testing.T) { + store := setupTestStoreWithTasks(t) + defer func() { _ = store.Close() }() + + now := time.Now() + yesterday := now.Add(-24 * time.Hour) + tomorrow := now.Add(24 * time.Hour) + + tasks := []models.Task{ + {ID: "done", Content: "Completed overdue", DueDate: &yesterday, Completed: true}, + {ID: "active", Content: "Active", DueDate: &now}, + } + + if err := store.SaveTasks(tasks); err != nil { + t.Fatalf("Failed to save tasks: %v", err) + } + + results, err := store.GetTasksByDateRange(now, tomorrow) + if err != nil { + t.Fatalf("GetTasksByDateRange failed: %v", err) + } + + if len(results) != 1 { + t.Errorf("Expected 1 task (only active), got %d", len(results)) + } + if len(results) > 0 && results[0].ID != "active" { + t.Errorf("Expected active task, got %s", results[0].ID) } } diff --git a/web/static/js/app.js b/web/static/js/app.js index 5dffacc..954dc8c 100644 --- a/web/static/js/app.js +++ b/web/static/js/app.js @@ -218,16 +218,9 @@ async function refreshData() { if (!refreshResponse.ok) throw new Error('Refresh failed'); - // Reload current tab from cache - const tabResponse = await fetch(`/tabs/${currentTab}`); - - if (!tabResponse.ok) throw new Error('Tab reload failed'); - - // Get HTML response and update tab content - const html = await tabResponse.text(); - const tabContent = document.getElementById('tab-content'); - tabContent.innerHTML = html; - htmx.process(tabContent); + // Trigger HTMX refresh on the current tab — each tab template + // has hx-trigger="refresh-tasks from:body" to handle its own reload + htmx.trigger(document.body, 'refresh-tasks'); // Update timestamp updateLastUpdatedTime(); @@ -275,17 +268,11 @@ async function autoRefresh() { if (!refreshResponse.ok) throw new Error('Refresh failed'); - // Reload current tab from cache - const tabResponse = await fetch(`/tabs/${currentTab}`); - - if (tabResponse.ok) { - const html = await tabResponse.text(); - const tabContent = document.getElementById('tab-content'); - tabContent.innerHTML = html; - htmx.process(tabContent); - updateLastUpdatedTime(); - console.log('Auto-refresh successful'); - } + // Trigger HTMX refresh on the current tab — each tab template + // has hx-trigger="refresh-tasks from:body" to handle its own reload + htmx.trigger(document.body, 'refresh-tasks'); + updateLastUpdatedTime(); + console.log('Auto-refresh successful'); } catch (error) { console.error('Auto-refresh failed:', error); } diff --git a/web/templates/index.html b/web/templates/index.html index 7e9a38f..9c90570 100644 --- a/web/templates/index.html +++ b/web/templates/index.html @@ -38,10 +38,18 @@ </svg> </button> <span class="text-sm text-white/70 tracking-wider font-light" id="last-updated">{{.LastUpdated.Format "3:04 PM"}}</span> - <form method="POST" action="/logout"> - <input type="hidden" name="csrf_token" value="{{.CSRFToken}}"> - <button type="submit" class="text-white/70 hover:text-white transition-colors text-xs tracking-wide">Logout</button> - </form> + <div class="flex items-center gap-3"> + <a href="/settings" class="text-white/70 hover:text-white transition-colors p-1" title="Settings"> + <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"> + <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.066 2.573c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.573 1.066c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.066-2.573c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z"></path> + <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"></path> + </svg> + </a> + <form method="POST" action="/logout"> + <input type="hidden" name="csrf_token" value="{{.CSRFToken}}"> + <button type="submit" class="text-white/70 hover:text-white transition-colors text-xs tracking-wide">Logout</button> + </form> + </div> </header> <!-- Tab Navigation --> diff --git a/web/templates/partials/conditions-tab.html b/web/templates/partials/conditions-tab.html index 94c2f99..3ded125 100644 --- a/web/templates/partials/conditions-tab.html +++ b/web/templates/partials/conditions-tab.html @@ -1,5 +1,9 @@ {{define "conditions-tab"}} -<div class="space-y-6 text-shadow-sm"> +<div class="space-y-6 text-shadow-sm" + hx-get="/tabs/conditions" + hx-trigger="refresh-tasks from:body" + hx-target="#tab-content" + hx-swap="innerHTML"> <!-- Kilauea Webcams --> <div> <h2 class="text-lg font-semibold mb-3 flex items-center gap-2 text-white/90"> diff --git a/web/templates/partials/meals-tab.html b/web/templates/partials/meals-tab.html index 5900368..97c3a4e 100644 --- a/web/templates/partials/meals-tab.html +++ b/web/templates/partials/meals-tab.html @@ -1,5 +1,9 @@ {{define "meals-tab"}} -<div class="space-y-6"> +<div class="space-y-6" + hx-get="/tabs/meals" + hx-trigger="refresh-tasks from:body" + hx-target="#tab-content" + hx-swap="innerHTML"> <!-- PlanToEat Meals Section --> {{template "plantoeat-meals" .}} </div> diff --git a/web/templates/partials/shopping-tab.html b/web/templates/partials/shopping-tab.html index 345549b..e3742d9 100644 --- a/web/templates/partials/shopping-tab.html +++ b/web/templates/partials/shopping-tab.html @@ -1,5 +1,9 @@ {{define "shopping-tab"}} -<div class="space-y-6 text-shadow-sm" id="shopping-tab-container"> +<div class="space-y-6 text-shadow-sm" id="shopping-tab-container" + hx-get="/tabs/shopping" + hx-trigger="refresh-tasks from:body" + hx-target="#tab-content" + hx-swap="innerHTML"> <!-- Header with View Toggle --> <div class="flex items-center justify-between"> |
