summaryrefslogtreecommitdiff
path: root/internal/api/todoist_test.go
blob: 220446948a06b1cfcae18f6be0fab9f162575c8a (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
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
package api

import (
	"context"
	"encoding/json"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"
	"time"
)

// newTestTodoistClient creates a TodoistClient for testing with custom base URL
func newTestTodoistClient(baseURL, apiKey string) *TodoistClient {
	client := NewTodoistClient(apiKey)
	client.BaseURL = baseURL
	client.syncClient.BaseURL = baseURL
	return client
}

func TestTodoistClient_CreateTask(t *testing.T) {
	// Mock server
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		// Verify method and path
		if r.Method != "POST" {
			t.Errorf("Expected POST request, got %s", r.Method)
		}
		if r.URL.Path != "/tasks" {
			t.Errorf("Expected path /tasks, got %s", r.URL.Path)
		}

		// Verify Authorization header
		if !strings.HasPrefix(r.Header.Get("Authorization"), "Bearer ") {
			t.Errorf("Expected Bearer token in Authorization header")
		}

		// Parse JSON body
		var payload map[string]interface{}
		if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
			t.Errorf("Failed to decode request body: %v", err)
		}

		// Verify content
		if payload["content"] != "Test Task" {
			t.Errorf("Expected content=Test Task, got %v", payload["content"])
		}

		if payload["project_id"] != "project-123" {
			t.Errorf("Expected project_id=project-123, got %v", payload["project_id"])
		}

		// Return mock response
		response := todoistTaskResponse{
			ID:          "task-456",
			Content:     "Test Task",
			Description: "",
			ProjectID:   "project-123",
			Priority:    1,
			Labels:      []string{},
			URL:         "https://todoist.com/task/456",
			CreatedAt:   time.Now().Format(time.RFC3339),
		}

		w.Header().Set("Content-Type", "application/json")
		_ = json.NewEncoder(w).Encode(response)
	}))
	defer server.Close()

	// Create client with mock server URL
	client := newTestTodoistClient(server.URL, "test-key")

	// Test CreateTask
	ctx := context.Background()
	task, err := client.CreateTask(ctx, "Test Task", "project-123", nil, 0)

	if err != nil {
		t.Fatalf("CreateTask failed: %v", err)
	}

	// Verify response
	if task.ID != "task-456" {
		t.Errorf("Expected task ID task-456, got %s", task.ID)
	}
	if task.Content != "Test Task" {
		t.Errorf("Expected task content Test Task, got %s", task.Content)
	}
	if task.ProjectID != "project-123" {
		t.Errorf("Expected project ID project-123, got %s", task.ProjectID)
	}
}

func TestTodoistClient_CreateTask_WithDueDate(t *testing.T) {
	dueDate := time.Date(2026, 1, 15, 0, 0, 0, 0, time.UTC)

	// Mock server
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		// Parse JSON body
		var payload map[string]interface{}
		_ = json.NewDecoder(r.Body).Decode(&payload)

		// Verify due_date
		if payload["due_date"] != "2026-01-15" {
			t.Errorf("Expected due_date=2026-01-15, got %v", payload["due_date"])
		}

		// Return mock response with due date
		response := todoistTaskResponse{
			ID:        "task-789",
			Content:   "Task with Due Date",
			ProjectID: "project-456",
			URL:       "https://todoist.com/task/789",
			CreatedAt: time.Now().Format(time.RFC3339),
			Due: &dueInfo{
				Date:     "2026-01-15",
				Datetime: "",
			},
		}

		w.Header().Set("Content-Type", "application/json")
		_ = json.NewEncoder(w).Encode(response)
	}))
	defer server.Close()

	// Create client
	client := newTestTodoistClient(server.URL, "test-key")

	// Test CreateTask with due date
	ctx := context.Background()
	task, err := client.CreateTask(ctx, "Task with Due Date", "project-456", &dueDate, 0)

	if err != nil {
		t.Fatalf("CreateTask failed: %v", err)
	}

	// Verify due date is set
	if task.DueDate == nil {
		t.Error("Expected due date to be set")
	} else {
		expectedDate := time.Date(2026, 1, 15, 0, 0, 0, 0, time.UTC)
		if !task.DueDate.Equal(expectedDate) {
			t.Errorf("Expected due date %v, got %v", expectedDate, *task.DueDate)
		}
	}
}

func TestTodoistClient_UsesAPIv1BaseURL(t *testing.T) {
	client := NewTodoistClient("test-key")
	const want = "https://api.todoist.com/api/v1"
	if client.BaseURL != want {
		t.Errorf("expected base URL %q, got %q — Todoist REST v2 is deprecated (HTTP 410)", want, client.BaseURL)
	}
}

func TestTodoistClient_CompleteTask(t *testing.T) {
	// Mock server
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		// Verify method and path
		if r.Method != "POST" {
			t.Errorf("Expected POST request, got %s", r.Method)
		}
		expectedPath := "/tasks/task-123/close"
		if r.URL.Path != expectedPath {
			t.Errorf("Expected path %s, got %s", expectedPath, r.URL.Path)
		}

		// Verify Authorization header
		if !strings.HasPrefix(r.Header.Get("Authorization"), "Bearer ") {
			t.Errorf("Expected Bearer token in Authorization header")
		}

		// Return 204 No Content
		w.WriteHeader(http.StatusNoContent)
	}))
	defer server.Close()

	// Create client
	client := newTestTodoistClient(server.URL, "test-key")

	// Test CompleteTask
	ctx := context.Background()
	err := client.CompleteTask(ctx, "task-123")

	if err != nil {
		t.Fatalf("CompleteTask failed: %v", err)
	}
}

func TestTodoistClient_CompleteTask_Error(t *testing.T) {
	// Mock server that returns error
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusNotFound)
		_, _ = w.Write([]byte(`{"error":"Task not found"}`))
	}))
	defer server.Close()

	// Create client
	client := newTestTodoistClient(server.URL, "test-key")

	// Test CompleteTask with error
	ctx := context.Background()
	err := client.CompleteTask(ctx, "invalid-task")

	if err == nil {
		t.Error("Expected error, got nil")
	}
	if !strings.Contains(err.Error(), "404") {
		t.Errorf("Expected error to contain status 404, got: %v", err)
	}
}

func TestTodoistClient_GetProjects(t *testing.T) {
	// Mock server
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		// Verify method and path
		if r.Method != "GET" {
			t.Errorf("Expected GET request, got %s", r.Method)
		}
		if r.URL.Path != "/projects" {
			t.Errorf("Expected path /projects, got %s", r.URL.Path)
		}

		// Return mock response
		response := []todoistProjectResponse{
			{ID: "proj-1", Name: "Project 1"},
			{ID: "proj-2", Name: "Project 2"},
		}

		w.Header().Set("Content-Type", "application/json")
		_ = json.NewEncoder(w).Encode(response)
	}))
	defer server.Close()

	// Create client
	client := newTestTodoistClient(server.URL, "test-key")

	// Test GetProjects
	ctx := context.Background()
	projects, err := client.GetProjects(ctx)

	if err != nil {
		t.Fatalf("GetProjects failed: %v", err)
	}

	// Verify response
	if len(projects) != 2 {
		t.Errorf("Expected 2 projects, got %d", len(projects))
	}

	if projects[0].ID != "proj-1" || projects[0].Name != "Project 1" {
		t.Errorf("Project 1 mismatch: got ID=%s Name=%s", projects[0].ID, projects[0].Name)
	}

	if projects[1].ID != "proj-2" || projects[1].Name != "Project 2" {
		t.Errorf("Project 2 mismatch: got ID=%s Name=%s", projects[1].ID, projects[1].Name)
	}
}

func TestTodoistClient_GetTasks(t *testing.T) {
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Method != "GET" {
			t.Errorf("Expected GET, got %s", r.Method)
		}

		w.Header().Set("Content-Type", "application/json")

		// GetTasks also calls GetProjects internally
		if r.URL.Path == "/projects" {
			response := []todoistProjectResponse{
				{ID: "proj-1", Name: "Project 1"},
			}
			json.NewEncoder(w).Encode(response)
			return
		}

		if r.URL.Path == "/tasks" {
			response := []todoistTaskResponse{
				{ID: "task-1", Content: "Task 1", ProjectID: "proj-1", CreatedAt: time.Now().Format(time.RFC3339)},
				{ID: "task-2", Content: "Task 2", ProjectID: "proj-1", CreatedAt: time.Now().Format(time.RFC3339)},
			}
			json.NewEncoder(w).Encode(response)
			return
		}

		t.Errorf("Unexpected path: %s", r.URL.Path)
	}))
	defer server.Close()

	client := newTestTodoistClient(server.URL, "test-key")
	tasks, err := client.GetTasks(context.Background())
	if err != nil {
		t.Fatalf("GetTasks failed: %v", err)
	}

	if len(tasks) != 2 {
		t.Errorf("Expected 2 tasks, got %d", len(tasks))
	}
}

func TestTodoistClient_ReopenTask(t *testing.T) {
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Method != "POST" {
			t.Errorf("Expected POST, got %s", r.Method)
		}
		expectedPath := "/tasks/task-123/reopen"
		if r.URL.Path != expectedPath {
			t.Errorf("Expected path %s, got %s", expectedPath, r.URL.Path)
		}

		w.WriteHeader(http.StatusNoContent)
	}))
	defer server.Close()

	client := newTestTodoistClient(server.URL, "test-key")
	err := client.ReopenTask(context.Background(), "task-123")
	if err != nil {
		t.Fatalf("ReopenTask failed: %v", err)
	}
}

func TestTodoistClient_UpdateTask(t *testing.T) {
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Method != "POST" {
			t.Errorf("Expected POST, got %s", r.Method)
		}
		expectedPath := "/tasks/task-123"
		if r.URL.Path != expectedPath {
			t.Errorf("Expected path %s, got %s", expectedPath, r.URL.Path)
		}

		var payload map[string]interface{}
		json.NewDecoder(r.Body).Decode(&payload)
		if payload["content"] != "Updated Content" {
			t.Errorf("Expected content 'Updated Content', got %v", payload["content"])
		}

		w.Header().Set("Content-Type", "application/json")
		json.NewEncoder(w).Encode(map[string]string{"id": "task-123"})
	}))
	defer server.Close()

	client := newTestTodoistClient(server.URL, "test-key")
	err := client.UpdateTask(context.Background(), "task-123", map[string]interface{}{"content": "Updated Content"})
	if err != nil {
		t.Fatalf("UpdateTask failed: %v", err)
	}
}

func TestTodoistClient_Sync(t *testing.T) {
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Method != "POST" {
			t.Errorf("Expected POST, got %s", r.Method)
		}
		if r.URL.Path != "/sync" {
			t.Errorf("Expected path /sync, got %s", r.URL.Path)
		}

		response := TodoistSyncResponse{
			SyncToken: "new-sync-token",
			FullSync:  true,
			Items: []SyncItemResponse{
				{ID: "item-1", Content: "Item 1", ProjectID: "proj-1"},
			},
			Projects: []SyncProjectResponse{
				{ID: "proj-1", Name: "Project 1"},
			},
		}

		w.Header().Set("Content-Type", "application/json")
		json.NewEncoder(w).Encode(response)
	}))
	defer server.Close()

	client := newTestTodoistClient(server.URL, "test-key")
	resp, err := client.Sync(context.Background(), "*")
	if err != nil {
		t.Fatalf("Sync failed: %v", err)
	}

	if resp.SyncToken != "new-sync-token" {
		t.Errorf("Expected sync token 'new-sync-token', got '%s'", resp.SyncToken)
	}
	if len(resp.Items) != 1 {
		t.Errorf("Expected 1 item, got %d", len(resp.Items))
	}
}

func TestConvertSyncItemsToTasks(t *testing.T) {
	projects := map[string]string{
		"proj-1": "Project 1",
	}

	items := []SyncItemResponse{
		{
			ID:          "item-1",
			Content:     "Task 1",
			Description: "Description 1",
			ProjectID:   "proj-1",
			Priority:    3,
			Labels:      []string{"label1"},
		},
		{
			ID:          "item-2",
			Content:     "Completed Task",
			ProjectID:   "proj-1",
			IsCompleted: true,
		},
	}

	tasks := ConvertSyncItemsToTasks(items, projects)

	// Should skip completed task
	if len(tasks) != 1 {
		t.Errorf("Expected 1 task (excluding completed), got %d", len(tasks))
	}

	if tasks[0].ID != "item-1" {
		t.Errorf("Expected task ID 'item-1', got '%s'", tasks[0].ID)
	}
	if tasks[0].ProjectName != "Project 1" {
		t.Errorf("Expected project name 'Project 1', got '%s'", tasks[0].ProjectName)
	}
}

func TestConvertSyncItemToTask(t *testing.T) {
	projects := map[string]string{"proj-1": "Project 1"}

	t.Run("active item returns task and true", func(t *testing.T) {
		item := SyncItemResponse{
			ID:          "item-1",
			Content:     "Active Task",
			Description: "desc",
			ProjectID:   "proj-1",
			Priority:    2,
			Labels:      []string{"work"},
			AddedAt:     "2026-01-01T00:00:00Z",
		}
		task, ok := ConvertSyncItemToTask(item, projects)
		if !ok {
			t.Fatal("expected ok=true for active item")
		}
		if task.ID != "item-1" {
			t.Errorf("expected ID 'item-1', got '%s'", task.ID)
		}
		if task.Content != "Active Task" {
			t.Errorf("expected Content 'Active Task', got '%s'", task.Content)
		}
		if task.ProjectName != "Project 1" {
			t.Errorf("expected ProjectName 'Project 1', got '%s'", task.ProjectName)
		}
		if task.Completed {
			t.Error("expected Completed=false")
		}
		if task.URL != "https://todoist.com/app/task/item-1" {
			t.Errorf("unexpected URL: %s", task.URL)
		}
	})

	t.Run("completed item returns false", func(t *testing.T) {
		item := SyncItemResponse{ID: "item-2", Content: "Done", ProjectID: "proj-1", IsCompleted: true}
		_, ok := ConvertSyncItemToTask(item, projects)
		if ok {
			t.Error("expected ok=false for completed item")
		}
	})

	t.Run("deleted item returns false", func(t *testing.T) {
		item := SyncItemResponse{ID: "item-3", Content: "Gone", ProjectID: "proj-1", IsDeleted: true}
		_, ok := ConvertSyncItemToTask(item, projects)
		if ok {
			t.Error("expected ok=false for deleted item")
		}
	})
}

func TestBuildProjectMapFromSync(t *testing.T) {
	projects := []SyncProjectResponse{
		{ID: "proj-1", Name: "Project 1"},
		{ID: "proj-2", Name: "Project 2"},
	}

	projectMap := BuildProjectMapFromSync(projects)

	if len(projectMap) != 2 {
		t.Errorf("Expected 2 projects in map, got %d", len(projectMap))
	}

	if projectMap["proj-1"] != "Project 1" {
		t.Errorf("Expected 'Project 1', got '%s'", projectMap["proj-1"])
	}
}