planner/sync/service/recur_test.go

63 lines
1.4 KiB
Go
Raw Permalink Normal View History

2024-12-01 10:22:47 +01:00
package main
import (
"io"
"log/slog"
"testing"
"time"
"go-mod.ewintr.nl/planner/item"
)
func TestRecur(t *testing.T) {
t.Parallel()
2024-12-19 12:06:03 +01:00
now := time.Now()
today := item.NewDate(2024, 1, 1)
2025-01-04 08:54:13 +01:00
until := today.Add(3)
2024-12-01 10:22:47 +01:00
mem := NewMemory()
rec := NewRecur(mem, mem, slog.New(slog.NewTextHandler(io.Discard, nil)))
testItem := item.Item{
ID: "test-1",
2024-12-24 08:00:23 +01:00
Kind: item.KindTask,
2024-12-01 10:22:47 +01:00
Updated: now,
Deleted: false,
2024-12-19 12:06:03 +01:00
Recurrer: item.NewRecurrer("2024-01-01, daily"),
RecurNext: today,
2024-12-24 08:00:23 +01:00
Body: `{"title":"Test task","start":"2024-01-01T10:00:00Z","duration":"30m"}`,
2024-12-01 10:22:47 +01:00
}
// Store the item
if err := mem.Update(testItem, testItem.Updated); err != nil {
t.Fatalf("failed to store test item: %v", err)
}
// Run recurrence
2025-01-04 08:54:13 +01:00
if err := rec.Recur(until); err != nil {
2024-12-01 10:22:47 +01:00
t.Errorf("Recur failed: %v", err)
}
// Verify results
2024-12-24 08:00:23 +01:00
items, err := mem.Updated([]item.Kind{item.KindTask}, now)
2024-12-01 10:22:47 +01:00
if err != nil {
t.Errorf("failed to get updated items: %v", err)
}
2025-01-05 15:39:18 +01:00
if len(items) != 5 { // Original + 4 new instances
t.Errorf("expected 5 items, got %d", len(items))
2024-12-01 10:22:47 +01:00
}
// Check that RecurNext was updated
2025-01-04 08:54:13 +01:00
recurItems, err := mem.ShouldRecur(until.Add(1))
2024-12-01 10:22:47 +01:00
if err != nil {
2025-01-04 08:54:13 +01:00
t.Errorf("exp nil, got %v", err)
2024-12-01 10:22:47 +01:00
}
if len(recurItems) != 1 {
t.Errorf("expected 1 recur item, got %d", len(recurItems))
}
2024-12-19 12:06:03 +01:00
if !recurItems[0].RecurNext.After(today) {
2024-12-01 10:22:47 +01:00
t.Errorf("RecurNext was not updated, still %v", recurItems[0].RecurNext)
}
}