45 lines
1.6 KiB
Go
45 lines
1.6 KiB
Go
package scheduler
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/vasyansk/imap-copier/internal/store"
|
|
)
|
|
|
|
func TestNextRun(t *testing.T) {
|
|
anchor := time.Date(2026, 7, 3, 10, 0, 0, 0, time.UTC)
|
|
fin := time.Date(2026, 7, 3, 11, 0, 0, 0, time.UTC)
|
|
|
|
// No finished run yet → anchor + interval.
|
|
if got := NextRun(time.Hour, anchor, nil); !got.Equal(anchor.Add(time.Hour)) {
|
|
t.Fatalf("first run: got %v", got)
|
|
}
|
|
// Finished run exists → last finished + interval (from completion, not start).
|
|
if got := NextRun(time.Hour, anchor, &fin); !got.Equal(fin.Add(time.Hour)) {
|
|
t.Fatalf("recurring: got %v", got)
|
|
}
|
|
// Re-enable: anchor is newer than a stale lastFinished from before re-enable →
|
|
// use anchor, not the stale lastFinished (which would fire immediately/in the past).
|
|
staleFin := anchor.Add(-2 * time.Hour)
|
|
if got := NextRun(time.Hour, anchor, &staleFin); !got.Equal(anchor.Add(time.Hour)) {
|
|
t.Fatalf("re-enable: got %v, want %v", got, anchor.Add(time.Hour))
|
|
}
|
|
}
|
|
|
|
func TestDueTaskIDs(t *testing.T) {
|
|
now := time.Date(2026, 7, 3, 12, 0, 0, 0, time.UTC)
|
|
anchorOld := now.Add(-2 * time.Hour) // enabled 2h ago
|
|
finRecent := now.Add(-30 * time.Minute)
|
|
|
|
tasks := []store.SchedulableTask{
|
|
{ID: 1, IntervalSeconds: 3600, Anchor: anchorOld, LastFinished: nil}, // due: anchor+1h < now
|
|
{ID: 2, IntervalSeconds: 3600, Anchor: anchorOld, LastFinished: &finRecent}, // not due: fin+1h > now
|
|
{ID: 3, IntervalSeconds: 21600, Anchor: anchorOld, LastFinished: nil}, // not due: anchor+6h > now
|
|
}
|
|
ids := dueTaskIDs(tasks, now)
|
|
if len(ids) != 1 || ids[0] != 1 {
|
|
t.Fatalf("due IDs = %v, want [1]", ids)
|
|
}
|
|
}
|