using NTP.Application.Jobs; namespace NTP.Tests; public class JobScheduleRulesTests { private static readonly DateTime T0 = new(2026, 11, 17, 10, 0, 0); private static JobSettingsRow Interval(int minutes, DateTime? lastStart = null, DateTime? updated = null, bool enabled = true, string? status = null) => new() { JobKey = "j", Enabled = enabled ? 1 : 0, ScheduleType = JobScheduleTypes.Interval, IntervalMinutes = minutes, LastStart = lastStart, UpdatedDate = updated ?? T0, LastStatus = status }; private static JobSettingsRow Daily(string times, DateTime? lastStart = null, DateTime? updated = null, bool enabled = true) => new() { JobKey = "j", Enabled = enabled ? 1 : 0, ScheduleType = JobScheduleTypes.Daily, DailyTimes = times, LastStart = lastStart, UpdatedDate = updated ?? T0.AddDays(-5) }; // ---------------------------------------------------------------- interval [Fact] public void Interval_runs_one_interval_after_it_was_started_not_at_once() { var s = Interval(30, updated: T0); Assert.False(JobScheduleRules.IsDue(s, T0.AddMinutes(29))); Assert.True(JobScheduleRules.IsDue(s, T0.AddMinutes(30))); Assert.Equal(T0.AddMinutes(30), JobScheduleRules.NextRun(s, T0.AddMinutes(5))); } [Fact] public void Interval_is_counted_from_the_last_start() { var s = Interval(60, lastStart: T0.AddMinutes(100), updated: T0); Assert.False(JobScheduleRules.IsDue(s, T0.AddMinutes(159))); Assert.True(JobScheduleRules.IsDue(s, T0.AddMinutes(160))); } [Fact] public void A_stopped_or_running_job_is_never_due() { Assert.False(JobScheduleRules.IsDue(Interval(1, enabled: false), T0.AddDays(1))); Assert.Null(JobScheduleRules.NextRun(Interval(1, enabled: false), T0)); Assert.False(JobScheduleRules.IsDue(Interval(1, status: JobStatuses.Running), T0.AddDays(1))); } // ---------------------------------------------------------------- daily [Fact] public void Daily_runs_at_the_time_and_only_once() { var s = Daily("02:00", updated: T0.AddDays(-5)); var at = new DateTime(2026, 11, 18, 2, 0, 20); Assert.True(JobScheduleRules.IsDue(s, at)); s.LastStart = at; // the run started Assert.False(JobScheduleRules.IsDue(s, at.AddMinutes(1))); Assert.Equal(new DateTime(2026, 11, 19, 2, 0, 0), JobScheduleRules.NextRun(s, at.AddMinutes(1))); } [Fact] public void Daily_with_several_times_picks_each_in_turn() { var s = Daily("06:00,18:30", updated: T0.AddDays(-5)); Assert.True(JobScheduleRules.IsDue(s, new DateTime(2026, 11, 17, 18, 30, 5))); Assert.Equal(new DateTime(2026, 11, 17, 18, 30, 0), JobScheduleRules.NextRun(s, new DateTime(2026, 11, 17, 10, 0, 0))); Assert.Equal(new DateTime(2026, 11, 18, 6, 0, 0), JobScheduleRules.NextRun(s, new DateTime(2026, 11, 17, 20, 0, 0))); } [Fact] public void Saving_after_the_time_has_passed_does_not_run_it_retroactively() { var s = Daily("10:00", updated: new DateTime(2026, 11, 17, 10, 5, 0)); // saved at 10:05 Assert.False(JobScheduleRules.IsDue(s, new DateTime(2026, 11, 17, 10, 6, 0))); Assert.True(JobScheduleRules.IsDue(Daily("10:00", updated: new DateTime(2026, 11, 17, 9, 58, 0)), new DateTime(2026, 11, 17, 10, 0, 10))); } [Fact] public void A_missed_daily_time_is_caught_up_only_within_half_an_hour() { var s = Daily("02:00", updated: T0.AddDays(-5)); Assert.True(JobScheduleRules.IsDue(s, new DateTime(2026, 11, 17, 2, 29, 0))); // restart shortly after the time Assert.False(JobScheduleRules.IsDue(s, new DateTime(2026, 11, 17, 9, 0, 0))); // hours later: wait for tomorrow } // ---------------------------------------------------------------- validation [Theory] [InlineData("INTERVAL", 1, null, true)] [InlineData("INTERVAL", 0, null, false)] [InlineData("INTERVAL", 10081, null, false)] [InlineData("DAILY", null, "02:00", true)] [InlineData("DAILY", null, "2:00", false)] [InlineData("DAILY", null, "24:00", false)] [InlineData("DAILY", null, "", false)] [InlineData("WEEKLY", 5, "02:00", false)] public void Schedules_are_validated(string type, int? minutes, string? time, bool valid) { IReadOnlyList? times = time is null ? null : time == "" ? [] : [time]; Assert.Equal(valid, JobScheduleRules.Validate(type, minutes, times) is null); } [Fact] public void Times_are_normalised_sorted_and_deduplicated() { Assert.Equal("02:00,13:30", JobScheduleRules.JoinTimes(["13:30", "02:00", "13:30", "bad"])); Assert.Equal(["02:00", "13:30"], JobScheduleRules.SplitTimes("13:30, 02:00,x")); } [Fact] public void The_catalog_has_the_three_legacy_jobs_with_unique_keys() { Assert.Equal(3, JobCatalog.All.Count); Assert.Equal(JobCatalog.All.Count, JobCatalog.All.Select(j => j.Key).Distinct().Count()); Assert.NotNull(JobCatalog.Find("CLOSE-OLD-PERMITS")); } }