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-23 10:06:59 +01:00
|
|
|
now := time.Now()
|
|
|
|
today := item.NewDate(2024, 1, 1)
|
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",
|
|
|
|
Kind: item.KindEvent,
|
|
|
|
Updated: now,
|
|
|
|
Deleted: false,
|
2024-12-23 10:06:59 +01:00
|
|
|
Recurrer: item.NewRecurrer("2024-01-01, daily"),
|
|
|
|
RecurNext: today,
|
2024-12-01 10:22:47 +01:00
|
|
|
Body: `{"title":"Test Event","start":"2024-01-01T10:00:00Z","duration":"30m"}`,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the item
|
|
|
|
if err := mem.Update(testItem, testItem.Updated); err != nil {
|
|
|
|
t.Fatalf("failed to store test item: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run recurrence
|
|
|
|
if err := rec.Recur(); err != nil {
|
|
|
|
t.Errorf("Recur failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify results
|
|
|
|
items, err := mem.Updated([]item.Kind{item.KindEvent}, now)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to get updated items: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(items) != 2 { // Original + new instance
|
|
|
|
t.Errorf("expected 2 items, got %d", len(items))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that RecurNext was updated
|
2024-12-23 10:06:59 +01:00
|
|
|
recurItems, err := mem.ShouldRecur(today.Add(1))
|
2024-12-01 10:22:47 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(recurItems) != 1 {
|
|
|
|
t.Errorf("expected 1 recur item, got %d", len(recurItems))
|
|
|
|
}
|
2024-12-23 10:06:59 +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)
|
|
|
|
}
|
|
|
|
}
|