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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
|
package handlers
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"task-dashboard/internal/models"
"task-dashboard/internal/store"
)
// mockGoogleTasksClient is a minimal api.GoogleTasksAPI stub for widget handler tests.
type mockGoogleTasksClient struct {
completedListID, completedTaskID string
notesListID, notesTaskID, notes string
}
func (m *mockGoogleTasksClient) GetTasks(ctx context.Context) ([]models.GoogleTask, error) {
return nil, nil
}
func (m *mockGoogleTasksClient) GetTasksByDateRange(ctx context.Context, start, end time.Time) ([]models.GoogleTask, error) {
return nil, nil
}
func (m *mockGoogleTasksClient) CompleteTask(ctx context.Context, listID, taskID string) error {
m.completedListID, m.completedTaskID = listID, taskID
return nil
}
func (m *mockGoogleTasksClient) UncompleteTask(ctx context.Context, listID, taskID string) error {
return nil
}
func (m *mockGoogleTasksClient) UpdateTaskNotes(ctx context.Context, listID, taskID, notes string) error {
m.notesListID, m.notesTaskID, m.notes = listID, taskID, notes
return nil
}
func (m *mockGoogleTasksClient) GetTaskLists(ctx context.Context) ([]models.TaskListInfo, error) {
return nil, nil
}
func (m *mockGoogleTasksClient) SetTaskListID(id string) {}
// mockTrelloWidgetClient is a minimal api.TrelloAPI stub for widget handler tests
// that records UpdateCard calls (the shared mockTrelloClient in handlers_test.go doesn't).
type mockTrelloWidgetClient struct {
updatedCardID string
updates map[string]interface{}
}
func (m *mockTrelloWidgetClient) GetBoards(ctx context.Context) ([]models.Board, error) {
return nil, nil
}
func (m *mockTrelloWidgetClient) GetCards(ctx context.Context, boardID string) ([]models.Card, error) {
return nil, nil
}
func (m *mockTrelloWidgetClient) GetLists(ctx context.Context, boardID string) ([]models.List, error) {
return nil, nil
}
func (m *mockTrelloWidgetClient) GetBoardsWithCards(ctx context.Context) ([]models.Board, error) {
return nil, nil
}
func (m *mockTrelloWidgetClient) CreateCard(ctx context.Context, listID, name, description string, dueDate *time.Time) (*models.Card, error) {
return nil, nil
}
func (m *mockTrelloWidgetClient) UpdateCard(ctx context.Context, cardID string, updates map[string]interface{}) error {
m.updatedCardID = cardID
m.updates = updates
return nil
}
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")
}
}
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)
}
}
func TestHandleWidgetDetail_NativeTask(t *testing.T) {
db, cleanup := setupTestDB(t)
defer cleanup()
task := models.Task{ID: "t1", Content: "Buy milk", Description: "2%, not skim"}
if err := db.CreateNativeTask(task); err != nil {
t.Fatalf("failed to seed native task: %v", err)
}
h := &Handler{store: db}
req := httptest.NewRequest("GET", "/api/widget/detail?id=t1&source=doot", nil)
w := httptest.NewRecorder()
http.HandlerFunc(h.HandleWidgetDetail).ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String())
}
var resp widgetDetailResponse
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
t.Fatalf("failed to decode response: %v", err)
}
if resp.Title != "Buy milk" || resp.Description != "2%, not skim" || !resp.Editable {
t.Errorf("unexpected detail: %+v", resp)
}
}
func TestHandleWidgetDetail_UnsupportedSource(t *testing.T) {
h := &Handler{}
req := httptest.NewRequest("GET", "/api/widget/detail?id=x&source=calendar", nil)
w := httptest.NewRecorder()
http.HandlerFunc(h.HandleWidgetDetail).ServeHTTP(w, req)
if w.Code != http.StatusBadRequest {
t.Fatalf("expected 400, got %d", w.Code)
}
}
func TestHandleWidgetUpdate_NativeTask_PreservesContent(t *testing.T) {
db, cleanup := setupTestDB(t)
defer cleanup()
task := models.Task{ID: "t2", Content: "Original title", Description: "old desc"}
if err := db.CreateNativeTask(task); err != nil {
t.Fatalf("failed to seed native task: %v", err)
}
h := &Handler{store: db}
body := `{"id":"t2","source":"doot","description":"new desc"}`
req := httptest.NewRequest("POST", "/api/widget/update", strings.NewReader(body))
w := httptest.NewRecorder()
http.HandlerFunc(h.HandleWidgetUpdate).ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String())
}
tasks, err := db.GetNativeTasks()
if err != nil {
t.Fatalf("failed to read back native tasks: %v", err)
}
if len(tasks) != 1 {
t.Fatalf("expected 1 native task, got %d", len(tasks))
}
if tasks[0].Content != "Original title" {
t.Errorf("title should be preserved, got %q", tasks[0].Content)
}
if tasks[0].Description != "new desc" {
t.Errorf("description should be updated, got %q", tasks[0].Description)
}
}
func TestHandleWidgetUpdate_UnsupportedSource(t *testing.T) {
h := &Handler{}
body := `{"id":"x","source":"calendar","description":"anything"}`
req := httptest.NewRequest("POST", "/api/widget/update", strings.NewReader(body))
w := httptest.NewRecorder()
http.HandlerFunc(h.HandleWidgetUpdate).ServeHTTP(w, req)
if w.Code != http.StatusBadRequest {
t.Fatalf("expected 400, got %d", w.Code)
}
}
func TestTimelineItemToWidgetItem_GTask_Completable(t *testing.T) {
item := models.TimelineItem{
ID: "g1",
Title: "Renew passport",
Source: "gtasks",
Type: models.TimelineItemTypeGTask,
}
wi := TimelineItemToWidgetItem(item)
if wi.Type != "task" {
t.Errorf("Type: got %q, want %q", wi.Type, "task")
}
if !wi.Completable {
t.Error("gtask should be completable")
}
}
func TestTimelineItemToWidgetItem_Card_Completable(t *testing.T) {
item := models.TimelineItem{
ID: "c1",
Title: "Trello card",
Source: "trello",
Type: models.TimelineItemTypeCard,
}
wi := TimelineItemToWidgetItem(item)
if !wi.Completable {
t.Error("trello card should be completable via widget")
}
}
func TestHandleWidgetComplete_GoogleTask(t *testing.T) {
db, cleanup := setupTestDB(t)
defer cleanup()
if err := db.SaveGoogleTasks([]models.GoogleTask{
{ID: "g1", Title: "Renew passport", ListID: "list-a", UpdatedAt: time.Now()},
}); err != nil {
t.Fatalf("failed to seed google task: %v", err)
}
mock := &mockGoogleTasksClient{}
h := &Handler{store: db, googleTasksClient: mock}
body := `{"id":"g1","source":"gtasks"}`
req := httptest.NewRequest("POST", "/api/widget/complete", strings.NewReader(body))
w := httptest.NewRecorder()
http.HandlerFunc(h.HandleWidgetComplete).ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String())
}
if mock.completedListID != "list-a" || mock.completedTaskID != "g1" {
t.Errorf("unexpected complete call: listID=%q taskID=%q", mock.completedListID, mock.completedTaskID)
}
}
func TestHandleWidgetComplete_GoogleTask_NotConfigured(t *testing.T) {
db, cleanup := setupTestDB(t)
defer cleanup()
h := &Handler{store: db}
body := `{"id":"g1","source":"gtasks"}`
req := httptest.NewRequest("POST", "/api/widget/complete", strings.NewReader(body))
w := httptest.NewRecorder()
http.HandlerFunc(h.HandleWidgetComplete).ServeHTTP(w, req)
if w.Code != http.StatusServiceUnavailable {
t.Fatalf("expected 503, got %d", w.Code)
}
}
func TestHandleWidgetDetail_GoogleTask(t *testing.T) {
db, cleanup := setupTestDB(t)
defer cleanup()
if err := db.SaveGoogleTasks([]models.GoogleTask{
{ID: "g1", Title: "Renew passport", Notes: "bring photo", ListID: "list-a", UpdatedAt: time.Now()},
}); err != nil {
t.Fatalf("failed to seed google task: %v", err)
}
h := &Handler{store: db, googleTasksClient: &mockGoogleTasksClient{}}
req := httptest.NewRequest("GET", "/api/widget/detail?id=g1&source=gtasks", nil)
w := httptest.NewRecorder()
http.HandlerFunc(h.HandleWidgetDetail).ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String())
}
var resp widgetDetailResponse
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
t.Fatalf("failed to decode response: %v", err)
}
if resp.Title != "Renew passport" || resp.Description != "bring photo" || !resp.Editable {
t.Errorf("unexpected detail: %+v", resp)
}
}
func TestHandleWidgetUpdate_GoogleTask(t *testing.T) {
db, cleanup := setupTestDB(t)
defer cleanup()
if err := db.SaveGoogleTasks([]models.GoogleTask{
{ID: "g1", Title: "Renew passport", ListID: "list-a", UpdatedAt: time.Now()},
}); err != nil {
t.Fatalf("failed to seed google task: %v", err)
}
mock := &mockGoogleTasksClient{}
h := &Handler{store: db, googleTasksClient: mock}
body := `{"id":"g1","source":"gtasks","description":"bring photo and $170"}`
req := httptest.NewRequest("POST", "/api/widget/update", strings.NewReader(body))
w := httptest.NewRecorder()
http.HandlerFunc(h.HandleWidgetUpdate).ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String())
}
if mock.notesListID != "list-a" || mock.notesTaskID != "g1" || mock.notes != "bring photo and $170" {
t.Errorf("unexpected update call: listID=%q taskID=%q notes=%q", mock.notesListID, mock.notesTaskID, mock.notes)
}
}
func seedTestCard(t *testing.T, db *store.Store) {
t.Helper()
err := db.SaveBoards([]models.Board{
{
ID: "board1",
Name: "Test Board",
Cards: []models.Card{
{ID: "c1", Name: "Ship the widget", Description: "don't forget the checkbox", ListName: "Doing"},
},
},
})
if err != nil {
t.Fatalf("failed to seed card: %v", err)
}
}
func TestHandleWidgetDetail_TrelloCard(t *testing.T) {
db, cleanup := setupTestDB(t)
defer cleanup()
seedTestCard(t, db)
h := &Handler{store: db, trelloClient: &mockTrelloWidgetClient{}}
req := httptest.NewRequest("GET", "/api/widget/detail?id=c1&source=trello", nil)
w := httptest.NewRecorder()
http.HandlerFunc(h.HandleWidgetDetail).ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String())
}
var resp widgetDetailResponse
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
t.Fatalf("failed to decode response: %v", err)
}
if resp.Title != "Ship the widget" || resp.Description != "don't forget the checkbox" || !resp.Editable {
t.Errorf("unexpected detail: %+v", resp)
}
}
func TestHandleWidgetUpdate_TrelloCard(t *testing.T) {
db, cleanup := setupTestDB(t)
defer cleanup()
seedTestCard(t, db)
mock := &mockTrelloWidgetClient{}
h := &Handler{store: db, trelloClient: mock}
body := `{"id":"c1","source":"trello","description":"updated desc"}`
req := httptest.NewRequest("POST", "/api/widget/update", strings.NewReader(body))
w := httptest.NewRecorder()
http.HandlerFunc(h.HandleWidgetUpdate).ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String())
}
if mock.updatedCardID != "c1" || mock.updates["desc"] != "updated desc" {
t.Errorf("unexpected update call: id=%q updates=%v", mock.updatedCardID, mock.updates)
}
}
func TestHandleWidgetComplete_TrelloCard_Archives(t *testing.T) {
db, cleanup := setupTestDB(t)
defer cleanup()
seedTestCard(t, db)
mock := &mockTrelloWidgetClient{}
h := &Handler{store: db, trelloClient: mock}
body := `{"id":"c1","source":"trello"}`
req := httptest.NewRequest("POST", "/api/widget/complete", strings.NewReader(body))
w := httptest.NewRecorder()
http.HandlerFunc(h.HandleWidgetComplete).ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String())
}
if mock.updatedCardID != "c1" || mock.updates["closed"] != true {
t.Errorf("expected card to be archived (closed=true), got id=%q updates=%v", mock.updatedCardID, mock.updates)
}
boards, err := db.GetBoards()
if err != nil {
t.Fatalf("failed to read back boards: %v", err)
}
for _, b := range boards {
for _, c := range b.Cards {
if c.ID == "c1" {
t.Error("completed card should have been removed from local cache")
}
}
}
}
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)
}
}
|