summaryrefslogtreecommitdiff
path: root/internal/handlers/budget_logic_test.go
blob: dad8079c0ed24275818fd4374ab0a3eb1c796d7f (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package handlers

import (
	"testing"
	"time"

	"task-dashboard/internal/models"
)

func mustParseInLoc(t *testing.T, layout, value string, loc *time.Location) time.Time {
	t.Helper()
	parsed, err := time.ParseInLocation(layout, value, loc)
	if err != nil {
		t.Fatal(err)
	}
	return parsed
}

func TestComputeBudgetPeriod_SumsAvailabilityMinusOverlappingEvent(t *testing.T) {
	loc := time.UTC
	// A Wednesday: 2026-07-15 is a Wednesday.
	day := mustParseInLoc(t, "2006-01-02", "2026-07-15", loc)
	start := day
	end := day.AddDate(0, 0, 1)

	blocks := []models.AvailabilityBlock{
		{ID: "b1", Weekday: int(day.Weekday()), StartTime: "18:00", EndTime: "20:00"}, // 120 min
	}
	events := []models.CalendarEvent{
		{ID: "e1", Start: mustParseInLoc(t, "2006-01-02 15:04", "2026-07-15 18:30", loc), End: mustParseInLoc(t, "2006-01-02 15:04", "2026-07-15 19:00", loc)}, // 30 min overlap
	}

	status := ComputeBudgetPeriod(blocks, events, nil, nil, nil, start, end)
	if status.AvailableMinutes != 90 {
		t.Errorf("AvailableMinutes = %d, want 90 (120 - 30 overlap)", status.AvailableMinutes)
	}
}

func TestComputeBudgetPeriod_EventFullyOutsideBlockDoesNotReduceIt(t *testing.T) {
	loc := time.UTC
	day := mustParseInLoc(t, "2006-01-02", "2026-07-15", loc)
	start := day
	end := day.AddDate(0, 0, 1)

	blocks := []models.AvailabilityBlock{
		{ID: "b1", Weekday: int(day.Weekday()), StartTime: "18:00", EndTime: "20:00"},
	}
	events := []models.CalendarEvent{
		{ID: "e1", Start: mustParseInLoc(t, "2006-01-02 15:04", "2026-07-15 09:00", loc), End: mustParseInLoc(t, "2006-01-02 15:04", "2026-07-15 10:00", loc)},
	}

	status := ComputeBudgetPeriod(blocks, events, nil, nil, nil, start, end)
	if status.AvailableMinutes != 120 {
		t.Errorf("AvailableMinutes = %d, want 120 (event doesn't overlap the block)", status.AvailableMinutes)
	}
}

func TestComputeBudgetPeriod_AvailableNeverGoesNegative(t *testing.T) {
	loc := time.UTC
	day := mustParseInLoc(t, "2006-01-02", "2026-07-15", loc)
	start := day
	end := day.AddDate(0, 0, 1)

	blocks := []models.AvailabilityBlock{
		{ID: "b1", Weekday: int(day.Weekday()), StartTime: "18:00", EndTime: "20:00"},
	}
	events := []models.CalendarEvent{
		{ID: "e1", Start: mustParseInLoc(t, "2006-01-02 15:04", "2026-07-15 17:00", loc), End: mustParseInLoc(t, "2006-01-02 15:04", "2026-07-15 21:00", loc)},
	}

	status := ComputeBudgetPeriod(blocks, events, nil, nil, nil, start, end)
	if status.AvailableMinutes != 0 {
		t.Errorf("AvailableMinutes = %d, want 0 (event fully covers the block)", status.AvailableMinutes)
	}
}

func TestComputeBudgetPeriod_OverlappingEventsUnionNotSummed(t *testing.T) {
	loc := time.UTC
	// A Wednesday: 2026-07-15 is a Wednesday.
	day := mustParseInLoc(t, "2006-01-02", "2026-07-15", loc)
	start := day
	end := day.AddDate(0, 0, 1)

	blocks := []models.AvailabilityBlock{
		{ID: "b1", Weekday: int(day.Weekday()), StartTime: "18:00", EndTime: "20:00"}, // 120 min
	}
	events := []models.CalendarEvent{
		// Event A: 18:00-19:00 (60 min overlap with block)
		{ID: "e1", Start: mustParseInLoc(t, "2006-01-02 15:04", "2026-07-15 18:00", loc), End: mustParseInLoc(t, "2006-01-02 15:04", "2026-07-15 19:00", loc)},
		// Event B: 18:30-19:30 (60 min overlap with block), overlapping event A from 18:30-19:00.
		{ID: "e2", Start: mustParseInLoc(t, "2006-01-02 15:04", "2026-07-15 18:30", loc), End: mustParseInLoc(t, "2006-01-02 15:04", "2026-07-15 19:30", loc)},
	}

	// Union of busy time is 18:00-19:30 = 90 min, so available should be
	// 120 - 90 = 30. A sum-based (rather than union-based) implementation
	// would incorrectly compute 120 - 60 - 60 = 0.
	status := ComputeBudgetPeriod(blocks, events, nil, nil, nil, start, end)
	if status.AvailableMinutes != 30 {
		t.Errorf("AvailableMinutes = %d, want 30 (120 - 90 union of overlapping events, not 120 - 60 - 60)", status.AvailableMinutes)
	}
}

func TestComputeBudgetPeriod_MultiDayWindowSumsPerMatchingWeekdayAndExcludesEnd(t *testing.T) {
	loc := time.UTC
	// 2026-07-13 is a Monday, 2026-07-14 Tuesday, 2026-07-15 Wednesday,
	// 2026-07-16 Thursday, 2026-07-17 Friday.
	start := mustParseInLoc(t, "2006-01-02", "2026-07-13", loc) // Monday
	end := mustParseInLoc(t, "2006-01-02", "2026-07-17", loc)   // Friday (exclusive)

	monday := int(time.Monday)
	wednesday := int(time.Wednesday)
	thursday := int(time.Thursday)
	friday := int(time.Friday)

	blocks := []models.AvailabilityBlock{
		// Occurs once, on 07-13 (Monday) -- 60 min.
		{ID: "mon", Weekday: monday, StartTime: "09:00", EndTime: "10:00"},
		// Occurs once, on 07-15 (Wednesday) -- 90 min.
		{ID: "wed", Weekday: wednesday, StartTime: "08:00", EndTime: "09:30"},
		// Occurs once, on 07-16 (Thursday) -- 60 min.
		{ID: "thu", Weekday: thursday, StartTime: "12:00", EndTime: "13:00"},
		// Weekday matches `end` (07-17, Friday) itself, which is one day
		// PAST the last iterated day (07-16). If the loop incorrectly
		// iterated through (or including) `end`, this block would wrongly
		// contribute 60 more minutes.
		{ID: "fri-at-end", Weekday: friday, StartTime: "09:00", EndTime: "10:00"},
	}

	// No events, so no overlap to subtract.
	status := ComputeBudgetPeriod(blocks, nil, nil, nil, nil, start, end)

	want := 60 + 90 + 60 // Monday + Wednesday + Thursday; Friday-at-end excluded.
	if status.AvailableMinutes != want {
		t.Errorf("AvailableMinutes = %d, want %d (60 Mon + 90 Wed + 60 Thu; the Friday block at `end` must not be counted)", status.AvailableMinutes, want)
	}
}

func TestComputeBudgetPeriod_OnlySumsTrackedIncompleteTasksDueInWindow(t *testing.T) {
	loc := time.UTC
	start := mustParseInLoc(t, "2006-01-02", "2026-07-15", loc)
	end := start.AddDate(0, 0, 1)
	due := mustParseInLoc(t, "2006-01-02 15:04", "2026-07-15 12:00", loc)
	outsideWindow := end.AddDate(0, 0, 5)

	tasks := []models.Task{
		{ID: "t-tracked-project", ProjectID: "p1", EstimatedMinutes: 30, DueDate: &due},
		{ID: "t-tracked-label", Labels: []string{"errands"}, EstimatedMinutes: 20, DueDate: &due},
		{ID: "t-untracked", ProjectID: "p2", EstimatedMinutes: 999, DueDate: &due},
		{ID: "t-completed", ProjectID: "p1", EstimatedMinutes: 999, DueDate: &due, Completed: true},
		{ID: "t-outside-window", ProjectID: "p1", EstimatedMinutes: 999, DueDate: &outsideWindow},
	}
	trackedProjects := map[string]bool{"p1": true}
	trackedLabels := map[string]bool{"errands": true}

	status := ComputeBudgetPeriod(nil, nil, tasks, trackedProjects, trackedLabels, start, end)
	if status.ScheduledMinutes != 50 {
		t.Errorf("ScheduledMinutes = %d, want 50 (30 + 20, excluding untracked/completed/out-of-window)", status.ScheduledMinutes)
	}
}