summaryrefslogtreecommitdiff
path: root/internal/handlers/widget_test.go
blob: 06e1749d485e18c7946f3fc6a2932b6f22290301 (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package handlers

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

	"task-dashboard/internal/models"
)

func TestWidgetAuthMiddleware_NoToken(t *testing.T) {
	called := false
	inner := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { called = true })
	h := WidgetAuthMiddleware("secret", inner)

	req := httptest.NewRequest("GET", "/api/widget", nil)
	w := httptest.NewRecorder()
	h.ServeHTTP(w, req)

	if w.Code != http.StatusUnauthorized {
		t.Fatalf("expected 401, got %d", w.Code)
	}
	if called {
		t.Fatal("inner handler should not have been called")
	}
}

func TestWidgetAuthMiddleware_WrongToken(t *testing.T) {
	inner := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
	h := WidgetAuthMiddleware("secret", inner)

	req := httptest.NewRequest("GET", "/api/widget", nil)
	req.Header.Set("Authorization", "Bearer wrong")
	w := httptest.NewRecorder()
	h.ServeHTTP(w, req)

	if w.Code != http.StatusUnauthorized {
		t.Fatalf("expected 401, got %d", w.Code)
	}
}

func TestWidgetAuthMiddleware_CorrectToken(t *testing.T) {
	called := false
	inner := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { called = true })
	h := WidgetAuthMiddleware("secret", inner)

	req := httptest.NewRequest("GET", "/api/widget", nil)
	req.Header.Set("Authorization", "Bearer secret")
	w := httptest.NewRecorder()
	h.ServeHTTP(w, req)

	if !called {
		t.Fatal("inner handler should have been called")
	}
}

func TestTimelineItemToWidgetItem_Task(t *testing.T) {
	now := time.Now()
	item := models.TimelineItem{
		ID:       "abc",
		Title:    "Write notes",
		Source:   "doot",
		Type:     models.TimelineItemTypeTask,
		Time:     now,
		IsAllDay: true,
		URL:      "https://example.com/task/abc",
	}

	wi := TimelineItemToWidgetItem(item)

	if wi.ID != "abc" {
		t.Errorf("ID: got %q, want %q", wi.ID, "abc")
	}
	if wi.Type != "task" {
		t.Errorf("Type: got %q, want %q", wi.Type, "task")
	}
	if !wi.Completable {
		t.Error("doot task should be completable")
	}
	if wi.Start != nil {
		t.Error("all-day item should have nil Start")
	}
}

func TestTimelineItemToWidgetItem_Event(t *testing.T) {
	start := time.Now()
	end := start.Add(time.Hour)
	item := models.TimelineItem{
		ID:      "cal1",
		Title:   "Team sync",
		Source:  "calendar",
		Type:    models.TimelineItemTypeEvent,
		Time:    start,
		EndTime: &end,
	}

	wi := TimelineItemToWidgetItem(item)

	if wi.Type != "event" {
		t.Errorf("Type: got %q, want %q", wi.Type, "event")
	}
	if wi.Completable {
		t.Error("calendar event should not be completable")
	}
	if wi.Start == nil {
		t.Error("timed event should have non-nil Start")
	}
}

// TestTimelineItemToWidgetItem_AllDayEvent proves the 2026-07-12 fix: an
// all-day CALENDAR EVENT (Type == event, IsAllDay == true) must get Start
// populated with its real date -- previously Start was nil for every
// IsAllDay item regardless of type, which meant the widget client's
// hourly-slot packer had no date information to pin all-day events to the
// top of the correct day and they could silently fall outside the visible
// grid range instead. End stays nil since there's no meaningful end time to
// show for an all-day item.
func TestTimelineItemToWidgetItem_AllDayEvent(t *testing.T) {
	day := time.Date(2026, 7, 12, 0, 0, 0, 0, time.Local)
	item := models.TimelineItem{
		ID:       "cal-holiday",
		Title:    "Company Holiday",
		Source:   "calendar",
		Type:     models.TimelineItemTypeEvent,
		Time:     day,
		IsAllDay: true,
	}

	wi := TimelineItemToWidgetItem(item)

	if wi.Type != "event" {
		t.Errorf("Type: got %q, want %q", wi.Type, "event")
	}
	if !wi.IsAllDay {
		t.Error("expected IsAllDay to be true")
	}
	if wi.Start == nil {
		t.Fatal("all-day event should have non-nil Start (needed to pin it to the correct day)")
	}
	if !wi.Start.Equal(day) {
		t.Errorf("Start = %v, want %v", *wi.Start, day)
	}
	if wi.End != nil {
		t.Error("all-day event should have nil End")
	}
}

// TestTimelineItemToWidgetItem_AllDayTask_KeepsFloatingBehavior proves the
// same fix does NOT change behavior for undated doot/gtask tasks, which are
// also flagged IsAllDay as a "no specific time" fallback (see
// TimelineItem.ComputeDaySection) but are a different concept from a real
// all-day calendar event -- they must keep the existing nil-Start
// "floating" treatment so the hourly-slot packer still places them.
func TestTimelineItemToWidgetItem_AllDayTask_KeepsFloatingBehavior(t *testing.T) {
	item := models.TimelineItem{
		ID:       "undated-task",
		Title:    "Someday task",
		Source:   "doot",
		Type:     models.TimelineItemTypeTask,
		Time:     time.Now(),
		IsAllDay: true,
	}

	wi := TimelineItemToWidgetItem(item)

	if wi.Start != nil {
		t.Error("an undated task (IsAllDay as a fallback, not a real all-day event) should still have nil Start")
	}
}

// TestTimelineItemToWidgetItem_ForwardsIsOverdue proves the 2026-07-12
// overdue-badge fix: TimelineItem.IsOverdue (already computed correctly by
// ComputeDaySection, confirmed by the earlier fix that made overdue tasks
// appear in the timeline at all) must be forwarded onto WidgetItem so the
// Android client can render it distinctly -- previously it was silently
// dropped, so an overdue task looked identical to a normal one on the
// widget.
func TestTimelineItemToWidgetItem_ForwardsIsOverdue(t *testing.T) {
	item := models.TimelineItem{
		ID:        "overdue-1",
		Title:     "Pay the water bill",
		Source:    "doot",
		Type:      models.TimelineItemTypeTask,
		Time:      time.Now(),
		IsOverdue: true,
	}

	wi := TimelineItemToWidgetItem(item)

	if !wi.IsOverdue {
		t.Error("expected IsOverdue to be forwarded as true")
	}
}

func TestTimelineItemToWidgetItem_NotOverdueByDefault(t *testing.T) {
	item := models.TimelineItem{
		ID:     "today-1",
		Title:  "Water the plants",
		Source: "doot",
		Type:   models.TimelineItemTypeTask,
		Time:   time.Now(),
	}

	wi := TimelineItemToWidgetItem(item)

	if wi.IsOverdue {
		t.Error("expected IsOverdue to be false when the source item isn't overdue")
	}
}

func TestHandleWidgetComplete_NonCompletable(t *testing.T) {
	h := &Handler{}
	body := `{"id":"x","source":"calendar"}`
	req := httptest.NewRequest("POST", "/api/widget/complete", strings.NewReader(body))
	w := httptest.NewRecorder()
	http.HandlerFunc(h.HandleWidgetComplete).ServeHTTP(w, req)

	if w.Code != http.StatusBadRequest {
		t.Fatalf("expected 400, got %d", w.Code)
	}
}

// TestHandleWidgetComplete_UnknownID_Returns404 proves the 2026-07-12 fix at
// the handler layer: a "doot" completion for an id that doesn't exist must
// surface as 404, not the previous silent 200 (see
// store.ErrNativeTaskNotFound's doc comment for the underlying bug this
// closes -- a real production incident where the widget's completeTask tap
// intermittently looked like it worked but changed nothing).
func TestHandleWidgetComplete_UnknownID_Returns404(t *testing.T) {
	s, cleanup := setupTestDB(t)
	defer cleanup()
	h := &Handler{store: s}
	body := `{"id":"does-not-exist","source":"doot"}`
	req := httptest.NewRequest("POST", "/api/widget/complete", strings.NewReader(body))
	w := httptest.NewRecorder()
	http.HandlerFunc(h.HandleWidgetComplete).ServeHTTP(w, req)

	if w.Code != http.StatusNotFound {
		t.Fatalf("expected 404, got %d", w.Code)
	}
}

func TestWidgetAuthMiddleware_EmptyToken(t *testing.T) {
	inner := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
	h := WidgetAuthMiddleware("", inner)

	req := httptest.NewRequest("GET", "/api/widget", nil)
	req.Header.Set("Authorization", "Bearer ")
	w := httptest.NewRecorder()
	h.ServeHTTP(w, req)

	if w.Code != http.StatusUnauthorized {
		t.Fatalf("expected 401 with empty token, got %d", w.Code)
	}
}