summaryrefslogtreecommitdiff
path: root/internal/handlers/heuristic_test.go
blob: b03b664d10a8db157d69b62f550d16243e378077 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package handlers

import (
	"net/http/httptest"
	"os"
	"strings"
	"testing"
	"time"

	"task-dashboard/internal/models"
	"task-dashboard/internal/store"
)

func TestHandleTasks_Heuristic(t *testing.T) {
	// Create temp database file
	tmpFile, err := os.CreateTemp("", "test_heuristic_*.db")
	if err != nil {
		t.Fatalf("Failed to create temp db: %v", err)
	}
	tmpFile.Close()
	defer os.Remove(tmpFile.Name())

	// Save current directory and change to project root
	originalDir, err := os.Getwd()
	if err != nil {
		t.Fatalf("Failed to get working directory: %v", err)
	}

	// Change to project root (2 levels up from internal/handlers)
	if err := os.Chdir("../../"); err != nil {
		t.Fatalf("Failed to change to project root: %v", err)
	}
	defer os.Chdir(originalDir)

	// Initialize store (this runs migrations)
	db, err := store.New(tmpFile.Name(), "migrations")
	if err != nil {
		t.Fatalf("Failed to initialize store: %v", err)
	}
	defer db.Close()

	// Seed Data
	// Board 1: Has actionable lists
	board1 := models.Board{ID: "b1", Name: "Work Board"}

	// Card 1: Has Due Date (Should appear)
	due := time.Now()
	card1 := models.Card{ID: "c1", Name: "Due Task", ListID: "l1", ListName: "Backlog", DueDate: &due, BoardName: "Work Board"}

	// Card 2: No Due Date, Actionable List (Should appear)
	card2 := models.Card{ID: "c2", Name: "Doing Task", ListID: "l2", ListName: "Doing", BoardName: "Work Board"}

	// Card 3: No Due Date, Non-Actionable List (Should NOT appear)
	card3 := models.Card{ID: "c3", Name: "Backlog Task", ListID: "l1", ListName: "Backlog", BoardName: "Work Board"}

	// Card 4: No Due Date, "To Do" List (Should appear)
	card4 := models.Card{ID: "c4", Name: "Todo Task", ListID: "l3", ListName: "To Do", BoardName: "Work Board"}

	board1.Cards = []models.Card{card1, card2, card3, card4}

	if err := db.SaveBoards([]models.Board{board1}); err != nil {
		t.Fatalf("Failed to save boards: %v", err)
	}

	// Create Handler
	h := NewTabsHandler(db, nil, "../../web/templates")

	// Skip if templates are not loaded
	if h.templates == nil {
		t.Skip("Templates not available in test environment")
	}

	req := httptest.NewRequest("GET", "/tabs/tasks", nil)
	w := httptest.NewRecorder()

	// Execute
	h.HandleTasks(w, req)

	// Verify
	resp := w.Body.String()

	// Check for presence of expected tasks
	if !strings.Contains(resp, "Due Task") {
		t.Errorf("Expected 'Due Task' to be present")
	}
	if !strings.Contains(resp, "Doing Task") {
		t.Errorf("Expected 'Doing Task' to be present")
	}
	if !strings.Contains(resp, "Todo Task") {
		t.Errorf("Expected 'Todo Task' to be present")
	}

	// Check for absence of non-expected tasks
	if strings.Contains(resp, "Backlog Task") {
		t.Errorf("Expected 'Backlog Task' to be ABSENT")
	}
}