package scheduler

import (
	"testing"
	"time"
)

func TestCronNext(t *testing.T) {
	cases := []struct {
		expression, after, want string
	}{
		{"*/15 * * * *", "2026-08-12T10:07:00Z", "2026-08-12T10:15:00Z"},
		{"0 9 * * 1-5", "2026-08-14T09:01:00Z", "2026-08-17T09:00:00Z"},
		{"30 8 1 * *", "2026-08-12T00:00:00Z", "2026-09-01T08:30:00Z"},
	}
	for _, tc := range cases {
		schedule, err := ParseCron(tc.expression)
		if err != nil {
			t.Fatal(err)
		}
		after, _ := time.Parse(time.RFC3339, tc.after)
		got, err := schedule.Next(after, time.UTC)
		if err != nil || got.Format(time.RFC3339) != tc.want {
			t.Errorf("%s next=%s err=%v want=%s", tc.expression, got.Format(time.RFC3339), err, tc.want)
		}
	}
}

func TestCronRejectsInvalid(t *testing.T) {
	for _, expression := range []string{"* * *", "60 * * * *", "*/0 * * * *", "* 24 * * *"} {
		if _, err := ParseCron(expression); err == nil {
			t.Errorf("expected %q to be rejected", expression)
		}
	}
}
