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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
|
package handlers
import (
"encoding/json"
"fmt"
"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")
}
}
// TestTimelineItemToWidgetItem_DootTaskGetsDueDate proves the 2026-07-12
// clickable-reschedule fix: a doot task's raw due date must reach the
// client via a NEW field (DueDate) that is independent of Start/IsAllDay --
// Start is deliberately left nil for doot tasks (see
// TestTimelineItemToWidgetItem_AllDayTask_KeepsFloatingBehavior) so the
// client's floating-task SlotPacker can position it, and that must keep
// working unchanged. Before this fix there was no way for the Android
// detail popup to know a doot task's current due date at all.
func TestTimelineItemToWidgetItem_DootTaskGetsDueDate(t *testing.T) {
due := time.Date(2026, 7, 15, 0, 0, 0, 0, time.Local)
item := models.TimelineItem{
ID: "doot-1",
Title: "Pay the water bill",
Source: "doot",
Type: models.TimelineItemTypeTask,
Time: due,
IsAllDay: true,
}
wi := TimelineItemToWidgetItem(item)
if wi.DueDate == nil {
t.Fatal("expected DueDate to be set for a doot task with a real due date")
}
if !wi.DueDate.Equal(due) {
t.Errorf("DueDate = %v, want %v", *wi.DueDate, due)
}
// Start must stay nil -- this is the pre-existing floating-task
// behavior and this feature must not change it.
if wi.Start != nil {
t.Error("Start must remain nil for a doot task -- DueDate is a separate field, not a replacement")
}
}
func TestTimelineItemToWidgetItem_UndatedDootTask_NilDueDate(t *testing.T) {
item := models.TimelineItem{
ID: "doot-undated",
Title: "Someday task",
Source: "doot",
Type: models.TimelineItemTypeTask,
Time: time.Now(),
IsAllDay: true,
}
// Zero Time simulates the "no real due date" case at the field level;
// TimelineItemToWidgetItem's DueDate logic must guard on !Time.IsZero().
item.Time = time.Time{}
wi := TimelineItemToWidgetItem(item)
if wi.DueDate != nil {
t.Error("expected DueDate to be nil when the source item has a zero Time")
}
}
func TestTimelineItemToWidgetItem_CalendarEvent_NilDueDate(t *testing.T) {
start := time.Now()
item := models.TimelineItem{
ID: "cal-1",
Title: "Team sync",
Source: "calendar",
Type: models.TimelineItemTypeEvent,
Time: start,
}
wi := TimelineItemToWidgetItem(item)
if wi.DueDate != nil {
t.Error("expected DueDate to stay nil for a non-doot item (calendar event)")
}
}
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)
}
}
// TestHandleWidgetAdd_CreatesTask proves the quick-add feature: POSTing a
// title to /api/widget/add creates an undated native task the same way the
// web UI's HandleUnifiedAdd does, but via the widget's bearer-token JSON
// API instead of a session-authenticated HTML form.
func TestHandleWidgetAdd_CreatesTask(t *testing.T) {
s, cleanup := setupTestDB(t)
defer cleanup()
h := &Handler{store: s}
body := `{"title":"Buy milk"}`
req := httptest.NewRequest("POST", "/api/widget/add", strings.NewReader(body))
w := httptest.NewRecorder()
http.HandlerFunc(h.HandleWidgetAdd).ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d", w.Code)
}
tasks, err := s.GetUndatedNativeTasks()
if err != nil {
t.Fatalf("failed to read back tasks: %v", err)
}
found := false
for _, task := range tasks {
if task.Content == "Buy milk" {
found = true
}
}
if !found {
t.Error("expected a task with content 'Buy milk' to have been created")
}
}
func TestHandleWidgetAdd_EmptyTitle_Returns400(t *testing.T) {
s, cleanup := setupTestDB(t)
defer cleanup()
h := &Handler{store: s}
body := `{"title":""}`
req := httptest.NewRequest("POST", "/api/widget/add", strings.NewReader(body))
w := httptest.NewRecorder()
http.HandlerFunc(h.HandleWidgetAdd).ServeHTTP(w, req)
if w.Code != http.StatusBadRequest {
t.Fatalf("expected 400, got %d", w.Code)
}
}
func TestHandleWidgetAdd_WhitespaceOnlyTitle_Returns400(t *testing.T) {
s, cleanup := setupTestDB(t)
defer cleanup()
h := &Handler{store: s}
body := `{"title":" "}`
req := httptest.NewRequest("POST", "/api/widget/add", strings.NewReader(body))
w := httptest.NewRecorder()
http.HandlerFunc(h.HandleWidgetAdd).ServeHTTP(w, req)
if w.Code != http.StatusBadRequest {
t.Fatalf("expected 400, 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)
}
}
// TestTimelineItemToWidgetItem_ForwardsRecurringEventID proves the
// 2026-07-12 recurrence-display fix's data plumbing: a calendar event's
// RecurringEventId (captured from Google's API, which only puts the RRULE
// itself on the master event, not on expanded instances) must reach the
// client so it can look up the human-readable schedule on demand.
func TestTimelineItemToWidgetItem_ForwardsRecurringEventID(t *testing.T) {
item := models.TimelineItem{
ID: "cal-1",
Title: "Team sync",
Source: "calendar",
Type: models.TimelineItemTypeEvent,
Time: time.Now(),
RecurringEventID: "master-123",
}
wi := TimelineItemToWidgetItem(item)
if wi.RecurringEventID != "master-123" {
t.Errorf("RecurringEventID = %q, want %q", wi.RecurringEventID, "master-123")
}
}
func TestTimelineItemToWidgetItem_NonRecurring_EmptyRecurringEventID(t *testing.T) {
item := models.TimelineItem{
ID: "cal-2",
Title: "One-off meeting",
Source: "calendar",
Type: models.TimelineItemTypeEvent,
Time: time.Now(),
}
wi := TimelineItemToWidgetItem(item)
if wi.RecurringEventID != "" {
t.Errorf("expected empty RecurringEventID for a non-recurring event, got %q", wi.RecurringEventID)
}
}
// TestHandleWidgetRecurrence_ReturnsFormattedSchedule proves the recurrence
// lookup endpoint: given a recurring_event_id query param, it calls the
// calendar client's GetRecurrenceRule and returns the formatted text.
func TestHandleWidgetRecurrence_ReturnsFormattedSchedule(t *testing.T) {
mock := &MockCalendarClient{RecurrenceRule: "Repeats weekly on Monday"}
h := &Handler{googleCalendarClient: mock}
req := httptest.NewRequest("GET", "/api/widget/recurrence?recurring_event_id=master-1", nil)
w := httptest.NewRecorder()
http.HandlerFunc(h.HandleWidgetRecurrence).ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d", w.Code)
}
var resp struct {
Recurrence string `json:"recurrence"`
}
if err := json.NewDecoder(w.Body).Decode(&resp); err != nil {
t.Fatalf("failed to decode response: %v", err)
}
if resp.Recurrence != "Repeats weekly on Monday" {
t.Errorf("recurrence = %q, want %q", resp.Recurrence, "Repeats weekly on Monday")
}
}
func TestHandleWidgetRecurrence_NotFound_Returns404(t *testing.T) {
mock := &MockCalendarClient{RecurrenceErr: fmt.Errorf("not found")}
h := &Handler{googleCalendarClient: mock}
req := httptest.NewRequest("GET", "/api/widget/recurrence?recurring_event_id=missing", nil)
w := httptest.NewRecorder()
http.HandlerFunc(h.HandleWidgetRecurrence).ServeHTTP(w, req)
if w.Code != http.StatusNotFound {
t.Fatalf("expected 404, got %d", w.Code)
}
}
func TestHandleWidgetRecurrence_MissingParam_Returns400(t *testing.T) {
h := &Handler{}
req := httptest.NewRequest("GET", "/api/widget/recurrence", nil)
w := httptest.NewRecorder()
http.HandlerFunc(h.HandleWidgetRecurrence).ServeHTTP(w, req)
if w.Code != http.StatusBadRequest {
t.Fatalf("expected 400, got %d", w.Code)
}
}
|