diff options
| author | Peter Stone <thepeterstone@gmail.com> | 2026-07-14 20:30:16 +0000 |
|---|---|---|
| committer | Peter Stone <thepeterstone@gmail.com> | 2026-07-16 02:44:32 +0000 |
| commit | ba5205213f6f1995ca4e64b08925bcc6c48b8211 (patch) | |
| tree | 7d8fbaa6c2007d8582252d7b1e417ce003566caa /internal/store/native_tasks_test.go | |
| parent | 550ffde0d8b71be96a4d21d7c07b718f367c2c48 (diff) | |
feat(tasks): add periodic due-date check for recurring tasks
A recurring task's successor now also gets created once its due date
passes, independent of completion -- an ignored/overdue recurring task
no longer blocks the next occurrence from appearing. Runs every 15
minutes via a new goroutine in main.go, cancelled on shutdown.
Diffstat (limited to 'internal/store/native_tasks_test.go')
| -rw-r--r-- | internal/store/native_tasks_test.go | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/internal/store/native_tasks_test.go b/internal/store/native_tasks_test.go index fae7e90..a299ac6 100644 --- a/internal/store/native_tasks_test.go +++ b/internal/store/native_tasks_test.go @@ -358,3 +358,104 @@ func TestSetNextOccurrenceOverride_UnknownID_ReturnsErrNotFound(t *testing.T) { t.Fatalf("expected ErrNativeTaskNotFound, got %v", err) } } + +func TestAdvanceDueRecurringTasks_DueUncompleted_CreatesSuccessor(t *testing.T) { + s := newNativeTasksTestStore(t) + if _, err := s.db.Exec(` + INSERT INTO native_tasks (id, content, due_date, recurrence_freq, recurrence_interval, recurrence_series_id) + VALUES ('rec-1', 'Water plants', '2026-07-13', 'weekly', 1, 'series-1') + `); err != nil { + t.Fatal(err) + } + now := time.Date(2026, 7, 14, 0, 0, 0, 0, time.UTC) // one day after due, still uncompleted + + n, err := s.AdvanceDueRecurringTasks(now) + if err != nil { + t.Fatalf("AdvanceDueRecurringTasks: %v", err) + } + if n != 1 { + t.Fatalf("expected 1 iteration created, got %d", n) + } + + var completed bool + if err := s.db.QueryRow(`SELECT completed FROM native_tasks WHERE id = 'rec-1'`).Scan(&completed); err != nil { + t.Fatal(err) + } + if completed { + t.Error("expected rec-1 to remain uncompleted -- due-date passing doesn't complete it, just spawns the successor") + } + + var count int + if err := s.db.QueryRow(`SELECT COUNT(*) FROM native_tasks WHERE recurrence_series_id = 'series-1'`).Scan(&count); err != nil { + t.Fatal(err) + } + if count != 2 { + t.Fatalf("expected 2 rows in the series (original + successor), got %d", count) + } +} + +func TestAdvanceDueRecurringTasks_NotYetDue_LeavesAlone(t *testing.T) { + s := newNativeTasksTestStore(t) + if _, err := s.db.Exec(` + INSERT INTO native_tasks (id, content, due_date, recurrence_freq, recurrence_interval, recurrence_series_id) + VALUES ('rec-1', 'Water plants', '2026-07-13', 'weekly', 1, 'series-1') + `); err != nil { + t.Fatal(err) + } + now := time.Date(2026, 7, 10, 0, 0, 0, 0, time.UTC) // before the due date + + n, err := s.AdvanceDueRecurringTasks(now) + if err != nil { + t.Fatalf("AdvanceDueRecurringTasks: %v", err) + } + if n != 0 { + t.Errorf("expected 0 iterations created for a not-yet-due task, got %d", n) + } +} + +func TestAdvanceDueRecurringTasks_AlreadyHasSuccessor_SkipsIt(t *testing.T) { + s := newNativeTasksTestStore(t) + if _, err := s.db.Exec(` + INSERT INTO native_tasks (id, content, due_date, recurrence_freq, recurrence_interval, recurrence_series_id) + VALUES ('rec-1', 'Water plants', '2026-07-13', 'weekly', 1, 'series-1') + `); err != nil { + t.Fatal(err) + } + if _, err := s.db.Exec(` + INSERT INTO native_tasks (id, content, due_date, recurrence_freq, recurrence_interval, recurrence_series_id) + VALUES ('rec-2', 'Water plants', '2026-07-20', 'weekly', 1, 'series-1') + `); err != nil { + t.Fatal(err) + } + now := time.Date(2026, 7, 21, 0, 0, 0, 0, time.UTC) + + n, err := s.AdvanceDueRecurringTasks(now) + if err != nil { + t.Fatalf("AdvanceDueRecurringTasks: %v", err) + } + if n != 1 { + t.Fatalf("expected only rec-2 (the latest, now also due) to spawn a successor, got n=%d", n) + } + + var count int + if err := s.db.QueryRow(`SELECT COUNT(*) FROM native_tasks WHERE recurrence_series_id = 'series-1'`).Scan(&count); err != nil { + t.Fatal(err) + } + if count != 3 { + t.Fatalf("expected 3 rows total (rec-1, rec-2, and rec-2's new successor), got %d", count) + } +} + +func TestAdvanceDueRecurringTasks_NonRecurringTask_NeverTouched(t *testing.T) { + s := newNativeTasksTestStore(t) + // "real-1" (from newNativeTasksTestStore) has no due_date and no recurrence. + now := time.Date(2026, 7, 14, 0, 0, 0, 0, time.UTC) + + n, err := s.AdvanceDueRecurringTasks(now) + if err != nil { + t.Fatalf("AdvanceDueRecurringTasks: %v", err) + } + if n != 0 { + t.Errorf("expected 0 iterations created (no recurring tasks in fixture), got %d", n) + } +} |
