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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
|
package store
import (
"database/sql"
"fmt"
"path/filepath"
"testing"
"time"
_ "github.com/mattn/go-sqlite3"
"task-dashboard/internal/models"
)
// setupTestStore creates a test store with schema but without migrations directory
func setupTestStore(t *testing.T) *Store {
t.Helper()
// Create temporary database file
tempDir := t.TempDir()
dbPath := filepath.Join(tempDir, "test.db")
db, err := sql.Open("sqlite3", dbPath)
if err != nil {
t.Fatalf("Failed to open test database: %v", err)
}
// Enable foreign keys
if _, err := db.Exec("PRAGMA foreign_keys = ON"); err != nil {
t.Fatalf("Failed to enable foreign keys: %v", err)
}
// Enable WAL mode for better concurrency
if _, err := db.Exec("PRAGMA journal_mode = WAL"); err != nil {
t.Fatalf("Failed to enable WAL mode: %v", err)
}
// Serialize writes to prevent "database is locked" errors
db.SetMaxOpenConns(1)
store := &Store{db: db}
// Create notes table directly (without migrations)
schema := `
CREATE TABLE IF NOT EXISTS notes (
filename TEXT PRIMARY KEY,
title TEXT NOT NULL,
content TEXT,
modified DATETIME NOT NULL,
path TEXT NOT NULL,
tags TEXT,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_notes_modified ON notes(modified DESC);
`
if _, err := db.Exec(schema); err != nil {
t.Fatalf("Failed to create schema: %v", err)
}
return store
}
// TestGetNotes_LimitClause verifies that the LIMIT clause works correctly
// and prevents SQL injection
func TestGetNotes_LimitClause(t *testing.T) {
store := setupTestStore(t)
defer store.Close()
// Create 3 distinct notes with different modification times
baseTime := time.Now()
notes := []models.Note{
{
Filename: "note1.md",
Title: "Note 1",
Content: "Content of note 1",
Modified: baseTime.Add(-2 * time.Hour),
Path: "/vault/note1.md",
Tags: []string{"tag1"},
},
{
Filename: "note2.md",
Title: "Note 2",
Content: "Content of note 2",
Modified: baseTime.Add(-1 * time.Hour),
Path: "/vault/note2.md",
Tags: []string{"tag2"},
},
{
Filename: "note3.md",
Title: "Note 3",
Content: "Content of note 3",
Modified: baseTime,
Path: "/vault/note3.md",
Tags: []string{"tag3"},
},
}
// Save all 3 notes
if err := store.SaveNotes(notes); err != nil {
t.Fatalf("Failed to save notes: %v", err)
}
// Test 1: Call GetNotes(2) - should return exactly 2 notes
t.Run("GetNotes with limit 2", func(t *testing.T) {
result, err := store.GetNotes(2)
if err != nil {
t.Fatalf("GetNotes(2) returned error: %v", err)
}
if len(result) != 2 {
t.Errorf("Expected exactly 2 notes, got %d", len(result))
}
// Verify they are the most recent notes (note3 and note2)
if len(result) >= 2 {
if result[0].Filename != "note3.md" {
t.Errorf("Expected first note to be 'note3.md', got '%s'", result[0].Filename)
}
if result[1].Filename != "note2.md" {
t.Errorf("Expected second note to be 'note2.md', got '%s'", result[1].Filename)
}
}
})
// Test 2: Call GetNotes(5) - should return all 3 notes
t.Run("GetNotes with limit 5", func(t *testing.T) {
result, err := store.GetNotes(5)
if err != nil {
t.Fatalf("GetNotes(5) returned error: %v", err)
}
if len(result) != 3 {
t.Errorf("Expected all 3 notes, got %d", len(result))
}
// Verify order (most recent first)
if len(result) == 3 {
if result[0].Filename != "note3.md" {
t.Errorf("Expected first note to be 'note3.md', got '%s'", result[0].Filename)
}
if result[1].Filename != "note2.md" {
t.Errorf("Expected second note to be 'note2.md', got '%s'", result[1].Filename)
}
if result[2].Filename != "note1.md" {
t.Errorf("Expected third note to be 'note1.md', got '%s'", result[2].Filename)
}
}
})
// Test 3: Call GetNotes(0) - should return all notes (no limit)
t.Run("GetNotes with no limit", func(t *testing.T) {
result, err := store.GetNotes(0)
if err != nil {
t.Fatalf("GetNotes(0) returned error: %v", err)
}
if len(result) != 3 {
t.Errorf("Expected all 3 notes with no limit, got %d", len(result))
}
})
// Test 4: Call GetNotes(1) - should return exactly 1 note
t.Run("GetNotes with limit 1", func(t *testing.T) {
result, err := store.GetNotes(1)
if err != nil {
t.Fatalf("GetNotes(1) returned error: %v", err)
}
if len(result) != 1 {
t.Errorf("Expected exactly 1 note, got %d", len(result))
}
// Should be the most recent note
if len(result) == 1 && result[0].Filename != "note3.md" {
t.Errorf("Expected note to be 'note3.md', got '%s'", result[0].Filename)
}
})
}
// TestGetNotes_EmptyDatabase verifies behavior with empty database
func TestGetNotes_EmptyDatabase(t *testing.T) {
store := setupTestStore(t)
defer store.Close()
result, err := store.GetNotes(10)
if err != nil {
t.Fatalf("GetNotes on empty database returned error: %v", err)
}
if len(result) != 0 {
t.Errorf("Expected 0 notes from empty database, got %d", len(result))
}
}
// TestSaveNotes_Upsert verifies that SaveNotes properly upserts notes
func TestSaveNotes_Upsert(t *testing.T) {
store := setupTestStore(t)
defer store.Close()
baseTime := time.Now()
// Save initial note
initialNote := []models.Note{
{
Filename: "test.md",
Title: "Initial Title",
Content: "Initial content",
Modified: baseTime,
Path: "/vault/test.md",
Tags: []string{"initial"},
},
}
if err := store.SaveNotes(initialNote); err != nil {
t.Fatalf("Failed to save initial note: %v", err)
}
// Verify initial save
notes, err := store.GetNotes(0)
if err != nil {
t.Fatalf("Failed to get notes: %v", err)
}
if len(notes) != 1 {
t.Fatalf("Expected 1 note after initial save, got %d", len(notes))
}
if notes[0].Title != "Initial Title" {
t.Errorf("Expected title 'Initial Title', got '%s'", notes[0].Title)
}
// Update the same note
updatedNote := []models.Note{
{
Filename: "test.md",
Title: "Updated Title",
Content: "Updated content",
Modified: baseTime.Add(1 * time.Hour),
Path: "/vault/test.md",
Tags: []string{"updated"},
},
}
if err := store.SaveNotes(updatedNote); err != nil {
t.Fatalf("Failed to save updated note: %v", err)
}
// Verify update (should still be 1 note, not 2)
notes, err = store.GetNotes(0)
if err != nil {
t.Fatalf("Failed to get notes after update: %v", err)
}
if len(notes) != 1 {
t.Errorf("Expected 1 note after update (upsert), got %d", len(notes))
}
if notes[0].Title != "Updated Title" {
t.Errorf("Expected title 'Updated Title', got '%s'", notes[0].Title)
}
}
// TestGetNotes_NegativeLimit verifies behavior with negative limit
func TestGetNotes_NegativeLimit(t *testing.T) {
store := setupTestStore(t)
defer store.Close()
// Save a note
notes := []models.Note{
{
Filename: "test.md",
Title: "Test",
Content: "Test content",
Modified: time.Now(),
Path: "/vault/test.md",
Tags: []string{},
},
}
if err := store.SaveNotes(notes); err != nil {
t.Fatalf("Failed to save note: %v", err)
}
// Call with negative limit (should be treated as no limit)
result, err := store.GetNotes(-1)
if err != nil {
t.Fatalf("GetNotes(-1) returned error: %v", err)
}
// Should return all notes since negative is treated as 0/no limit
if len(result) != 1 {
t.Errorf("Expected 1 note with negative limit, got %d", len(result))
}
}
// TestGetNotes_SQLInjectionAttempt verifies that LIMIT parameter is properly sanitized
func TestGetNotes_SQLInjectionAttempt(t *testing.T) {
store := setupTestStore(t)
defer store.Close()
// Save some notes
notes := []models.Note{
{
Filename: "note1.md",
Title: "Note 1",
Content: "Content 1",
Modified: time.Now(),
Path: "/vault/note1.md",
Tags: []string{},
},
{
Filename: "note2.md",
Title: "Note 2",
Content: "Content 2",
Modified: time.Now(),
Path: "/vault/note2.md",
Tags: []string{},
},
}
if err := store.SaveNotes(notes); err != nil {
t.Fatalf("Failed to save notes: %v", err)
}
// The LIMIT parameter is now properly parameterized using sql.Query with args
// This test verifies that the code uses parameterized queries
// If it didn't, passing a malicious value could cause SQL injection
// Normal call should work
result, err := store.GetNotes(1)
if err != nil {
t.Fatalf("GetNotes(1) returned error: %v", err)
}
if len(result) != 1 {
t.Errorf("Expected 1 note, got %d", len(result))
}
// The fact that this test passes with a simple integer confirms
// that the implementation properly uses parameterized queries
// and is not vulnerable to SQL injection via the LIMIT clause
fmt.Println("✓ LIMIT clause is properly parameterized")
}
// setupTestStoreWithTasks creates a test store with tasks table
func setupTestStoreWithTasks(t *testing.T) *Store {
t.Helper()
tempDir := t.TempDir()
dbPath := filepath.Join(tempDir, "test.db")
db, err := sql.Open("sqlite3", dbPath)
if err != nil {
t.Fatalf("Failed to open test database: %v", err)
}
db.SetMaxOpenConns(1)
store := &Store{db: db}
schema := `
CREATE TABLE IF NOT EXISTS tasks (
id TEXT PRIMARY KEY,
content TEXT NOT NULL,
description TEXT,
project_id TEXT,
project_name TEXT,
due_date DATETIME,
priority INTEGER DEFAULT 1,
completed BOOLEAN DEFAULT FALSE,
labels TEXT,
url TEXT,
created_at DATETIME,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
`
if _, err := db.Exec(schema); err != nil {
t.Fatalf("Failed to create schema: %v", err)
}
return store
}
// setupTestStoreWithCards creates a test store with boards and cards tables
func setupTestStoreWithCards(t *testing.T) *Store {
t.Helper()
tempDir := t.TempDir()
dbPath := filepath.Join(tempDir, "test.db")
db, err := sql.Open("sqlite3", dbPath)
if err != nil {
t.Fatalf("Failed to open test database: %v", err)
}
db.SetMaxOpenConns(1)
store := &Store{db: db}
schema := `
CREATE TABLE IF NOT EXISTS boards (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS cards (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
board_id TEXT NOT NULL,
list_id TEXT,
list_name TEXT,
due_date DATETIME,
url TEXT,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
`
if _, err := db.Exec(schema); err != nil {
t.Fatalf("Failed to create schema: %v", err)
}
return store
}
// TestDeleteTask verifies that DeleteTask removes a task from the cache
func TestDeleteTask(t *testing.T) {
store := setupTestStoreWithTasks(t)
defer store.Close()
// Save some tasks
tasks := []models.Task{
{
ID: "task1",
Content: "Task 1",
Description: "Description 1",
Priority: 1,
Completed: false,
Labels: []string{},
CreatedAt: time.Now(),
},
{
ID: "task2",
Content: "Task 2",
Description: "Description 2",
Priority: 2,
Completed: false,
Labels: []string{},
CreatedAt: time.Now(),
},
{
ID: "task3",
Content: "Task 3",
Description: "Description 3",
Priority: 3,
Completed: false,
Labels: []string{},
CreatedAt: time.Now(),
},
}
if err := store.SaveTasks(tasks); err != nil {
t.Fatalf("Failed to save tasks: %v", err)
}
// Verify all 3 tasks exist
result, err := store.GetTasks()
if err != nil {
t.Fatalf("Failed to get tasks: %v", err)
}
if len(result) != 3 {
t.Fatalf("Expected 3 tasks, got %d", len(result))
}
// Delete task2
if err := store.DeleteTask("task2"); err != nil {
t.Fatalf("DeleteTask failed: %v", err)
}
// Verify only 2 tasks remain
result, err = store.GetTasks()
if err != nil {
t.Fatalf("Failed to get tasks after delete: %v", err)
}
if len(result) != 2 {
t.Errorf("Expected 2 tasks after delete, got %d", len(result))
}
// Verify task2 is gone
for _, task := range result {
if task.ID == "task2" {
t.Errorf("task2 should have been deleted but was found")
}
}
// Verify task1 and task3 still exist
foundTask1, foundTask3 := false, false
for _, task := range result {
if task.ID == "task1" {
foundTask1 = true
}
if task.ID == "task3" {
foundTask3 = true
}
}
if !foundTask1 {
t.Error("task1 should still exist")
}
if !foundTask3 {
t.Error("task3 should still exist")
}
}
// TestDeleteTask_NonExistent verifies that deleting a non-existent task doesn't error
func TestDeleteTask_NonExistent(t *testing.T) {
store := setupTestStoreWithTasks(t)
defer store.Close()
// Delete a task that doesn't exist - should not error
err := store.DeleteTask("nonexistent")
if err != nil {
t.Errorf("DeleteTask on non-existent task should not error, got: %v", err)
}
}
// TestDeleteCard verifies that DeleteCard removes a card from the cache
func TestDeleteCard(t *testing.T) {
store := setupTestStoreWithCards(t)
defer store.Close()
// First create a board
_, err := store.db.Exec(`INSERT INTO boards (id, name) VALUES (?, ?)`, "board1", "Test Board")
if err != nil {
t.Fatalf("Failed to create board: %v", err)
}
// Insert cards directly
cards := []struct {
id string
name string
boardID string
}{
{"card1", "Card 1", "board1"},
{"card2", "Card 2", "board1"},
{"card3", "Card 3", "board1"},
}
for _, card := range cards {
_, err := store.db.Exec(
`INSERT INTO cards (id, name, board_id, list_id, list_name) VALUES (?, ?, ?, ?, ?)`,
card.id, card.name, card.boardID, "list1", "To Do",
)
if err != nil {
t.Fatalf("Failed to insert card: %v", err)
}
}
// Verify all 3 cards exist
var count int
err = store.db.QueryRow(`SELECT COUNT(*) FROM cards`).Scan(&count)
if err != nil {
t.Fatalf("Failed to count cards: %v", err)
}
if count != 3 {
t.Fatalf("Expected 3 cards, got %d", count)
}
// Delete card2
if err := store.DeleteCard("card2"); err != nil {
t.Fatalf("DeleteCard failed: %v", err)
}
// Verify only 2 cards remain
err = store.db.QueryRow(`SELECT COUNT(*) FROM cards`).Scan(&count)
if err != nil {
t.Fatalf("Failed to count cards after delete: %v", err)
}
if count != 2 {
t.Errorf("Expected 2 cards after delete, got %d", count)
}
// Verify card2 is gone
var exists int
err = store.db.QueryRow(`SELECT COUNT(*) FROM cards WHERE id = ?`, "card2").Scan(&exists)
if err != nil {
t.Fatalf("Failed to check card2: %v", err)
}
if exists != 0 {
t.Errorf("card2 should have been deleted")
}
}
// TestDeleteCard_NonExistent verifies that deleting a non-existent card doesn't error
func TestDeleteCard_NonExistent(t *testing.T) {
store := setupTestStoreWithCards(t)
defer store.Close()
// Delete a card that doesn't exist - should not error
err := store.DeleteCard("nonexistent")
if err != nil {
t.Errorf("DeleteCard on non-existent card should not error, got: %v", err)
}
}
|