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
|
package api
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"google.golang.org/api/option"
gtasks "google.golang.org/api/tasks/v1"
)
func newTestGoogleTasksClient(t *testing.T, server *httptest.Server, tasklistID string, tz *time.Location) *GoogleTasksClient {
t.Helper()
if tz == nil {
tz = time.UTC
}
httpClient := &http.Client{Transport: &redirectingTransport{server: server}}
srv, err := gtasks.NewService(context.Background(),
option.WithHTTPClient(httpClient),
option.WithoutAuthentication(),
)
if err != nil {
t.Fatalf("failed to create test tasks service: %v", err)
}
return &GoogleTasksClient{
srv: srv,
tasklistID: tasklistID,
displayTZ: tz,
}
}
func tasksAPIResponse(items []map[string]interface{}) string {
b, _ := json.Marshal(map[string]interface{}{
"kind": "tasks#tasks",
"items": items,
})
return string(b)
}
// newTasksServer returns an httptest.Server that serves a fixed tasks JSON body.
func newTasksServer(body string) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !strings.Contains(r.URL.Path, "/lists/") {
http.NotFound(w, r)
return
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(body))
}))
}
// --- GetTasksByDateRange boundary tests ---
func TestGetTasksByDateRange_StartBoundaryIncluded(t *testing.T) {
// A task due exactly on the start date should be included (inclusive lower bound).
start := time.Date(2026, 3, 17, 0, 0, 0, 0, time.UTC)
end := time.Date(2026, 3, 20, 0, 0, 0, 0, time.UTC)
body := tasksAPIResponse([]map[string]interface{}{
{
"id": "task-start",
"title": "Task on start date",
"status": "needsAction",
"due": "2026-03-17T00:00:00Z",
"updated": "2026-03-17T00:00:00Z",
},
})
server := newTasksServer(body)
defer server.Close()
client := newTestGoogleTasksClient(t, server, "@default", time.UTC)
tasks, err := client.GetTasksByDateRange(context.Background(), start, end)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(tasks) != 1 {
t.Fatalf("expected 1 task (start boundary included), got %d", len(tasks))
}
if tasks[0].ID != "task-start" {
t.Errorf("expected task-start, got %s", tasks[0].ID)
}
}
func TestGetTasksByDateRange_EndBoundaryExcluded(t *testing.T) {
// A task due exactly on the end date should be excluded (exclusive upper bound).
start := time.Date(2026, 3, 17, 0, 0, 0, 0, time.UTC)
end := time.Date(2026, 3, 20, 0, 0, 0, 0, time.UTC)
body := tasksAPIResponse([]map[string]interface{}{
{
"id": "task-end",
"title": "Task on end date",
"status": "needsAction",
"due": "2026-03-20T00:00:00Z",
"updated": "2026-03-17T00:00:00Z",
},
})
server := newTasksServer(body)
defer server.Close()
client := newTestGoogleTasksClient(t, server, "@default", time.UTC)
tasks, err := client.GetTasksByDateRange(context.Background(), start, end)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(tasks) != 0 {
t.Fatalf("expected 0 tasks (end boundary excluded), got %d", len(tasks))
}
}
func TestGetTasksByDateRange_NoDueDateAlwaysIncluded(t *testing.T) {
// Tasks without a due date are always included regardless of the range.
start := time.Date(2026, 3, 17, 0, 0, 0, 0, time.UTC)
end := time.Date(2026, 3, 20, 0, 0, 0, 0, time.UTC)
body := tasksAPIResponse([]map[string]interface{}{
{
"id": "task-no-due",
"title": "No due date",
"status": "needsAction",
"updated": "2026-03-17T00:00:00Z",
// no "due" field
},
})
server := newTasksServer(body)
defer server.Close()
client := newTestGoogleTasksClient(t, server, "@default", time.UTC)
tasks, err := client.GetTasksByDateRange(context.Background(), start, end)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(tasks) != 1 {
t.Fatalf("expected 1 task (no due date always included), got %d", len(tasks))
}
if tasks[0].DueDate != nil {
t.Errorf("expected nil DueDate, got %v", tasks[0].DueDate)
}
}
func TestGetTasksByDateRange_OutOfRangeExcluded(t *testing.T) {
// A task before start or after end should be excluded.
start := time.Date(2026, 3, 17, 0, 0, 0, 0, time.UTC)
end := time.Date(2026, 3, 20, 0, 0, 0, 0, time.UTC)
body := tasksAPIResponse([]map[string]interface{}{
{
"id": "task-before",
"title": "Task before range",
"status": "needsAction",
"due": "2026-03-16T00:00:00Z",
"updated": "2026-03-17T00:00:00Z",
},
{
"id": "task-after",
"title": "Task after range",
"status": "needsAction",
"due": "2026-03-21T00:00:00Z",
"updated": "2026-03-17T00:00:00Z",
},
{
"id": "task-in-range",
"title": "Task in range",
"status": "needsAction",
"due": "2026-03-18T00:00:00Z",
"updated": "2026-03-17T00:00:00Z",
},
})
server := newTasksServer(body)
defer server.Close()
client := newTestGoogleTasksClient(t, server, "@default", time.UTC)
tasks, err := client.GetTasksByDateRange(context.Background(), start, end)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(tasks) != 1 {
t.Fatalf("expected 1 task (only in-range), got %d", len(tasks))
}
if tasks[0].ID != "task-in-range" {
t.Errorf("expected task-in-range, got %s", tasks[0].ID)
}
}
|