using ClawdDotNet.Core.Tasks; using Shouldly; namespace ClawdDotNet.Core.Tests.Tasks; /// /// The schedule calculation — answer to B6 (no delays, only "what is due now") /// and B7 (every occurrence is explicitly interpreted in its time zone). A pure function, /// therefore without DB and without a fake clock: "now" is passed in directly. /// public sealed class TaskScheduleTests { private static DateTime Utc(int y, int mo, int d, int h, int mi) => new(y, mo, d, h, mi, 0, DateTimeKind.Utc); // ─── #597: invalid cron expression ─── [Fact] public void Ungueltiger_Cron_Ausdruck_wird_benannt_statt_verschwiegen() { var task = new TaskItem { Id = "t-1", CreatedAt = Utc(2026, 9, 14, 6, 0), When = new TaskWhen { Kind = TaskWhenKind.Cron, Value = "0 7-22/x * * *", TimeZone = "Europe/Berlin" } }; TaskSchedule.DueOccurrence(task, Utc(2026, 9, 14, 11, 0)).ShouldBeNull(); TaskSchedule.InvalidCron(task).ShouldNotBeNull().ShouldContain("'x' ist keine Zahl"); } [Fact] public void Nachrichten_Digest_aus_dem_Testbetrieb_ist_um_13_Uhr_faellig() { var task = new TaskItem { Id = "t-7e0bbe", CreatedAt = Utc(2026, 9, 14, 10, 30), When = new TaskWhen { Kind = TaskWhenKind.Cron, Value = "0 7-22/3 * * *", TimeZone = "Europe/Berlin" } }; TaskSchedule.InvalidCron(task).ShouldBeNull(); // 13:00 Berlin (CEST) = 11:00 UTC TaskSchedule.DueOccurrence(task, Utc(2026, 9, 14, 11, 0)).ShouldBe("2026-09-14T11:00:00.0000000Z"); } [Fact] public void Andere_Terminarten_haben_keinen_Cron_Fehler() => TaskSchedule.InvalidCron(new TaskItem { Id = "t-1", When = new TaskWhen { Kind = TaskWhenKind.Every, Value = "kaputt" } }).ShouldBeNull(); // ─── One-time, without a schedule ─── [Fact] public void Ohne_Termin_ist_die_Aufgabe_faellig_bis_sie_lief() { var task = new TaskItem { Id = "t-1", CreatedAt = Utc(2026, 7, 31, 6, 0) }; TaskSchedule.DueOccurrence(task, Utc(2026, 7, 31, 7, 0)).ShouldNotBeNull(); TaskSchedule.DueOccurrence(task with { LastOccurrence = "x" }, Utc(2026, 7, 31, 7, 0)).ShouldBeNull(); } // ─── at: one-time point in time ─── [Fact] public void Ein_at_Termin_ist_erst_ab_dem_Zeitpunkt_faellig() { var task = new TaskItem { Id = "t-1", When = new TaskWhen { Kind = TaskWhenKind.At, Value = "2026-07-31T09:00:00", TimeZone = "Europe/Berlin" } }; // 09:00 Berlin (summer time) = 07:00 UTC TaskSchedule.DueOccurrence(task, Utc(2026, 7, 31, 6, 59)).ShouldBeNull(); TaskSchedule.DueOccurrence(task, Utc(2026, 7, 31, 7, 0)).ShouldNotBeNull(); } [Fact] public void Ein_at_Termin_feuert_genau_einmal() { var task = new TaskItem { Id = "t-1", LastOccurrence = "2026-07-31T07:00:00.0000000Z", When = new TaskWhen { Kind = TaskWhenKind.At, Value = "2026-07-31T09:00:00", TimeZone = "Europe/Berlin" } }; TaskSchedule.DueOccurrence(task, Utc(2026, 7, 31, 12, 0)).ShouldBeNull(); } [Fact] public void Ein_at_Termin_mit_Z_wird_als_UTC_gedeutet() { var task = new TaskItem { Id = "t-1", When = new TaskWhen { Kind = TaskWhenKind.At, Value = "2026-07-31T09:00:00Z", TimeZone = "Europe/Berlin" } }; TaskSchedule.DueOccurrence(task, Utc(2026, 7, 31, 8, 59)).ShouldBeNull(); TaskSchedule.DueOccurrence(task, Utc(2026, 7, 31, 9, 0)).ShouldNotBeNull(); } // ─── every: interval ─── [Fact] public void Ein_Intervall_wird_erst_nach_Ablauf_faellig() { var task = new TaskItem { Id = "t-1", CreatedAt = Utc(2026, 7, 31, 7, 0), When = new TaskWhen { Kind = TaskWhenKind.Every, Value = "30m", TimeZone = "UTC" } }; TaskSchedule.DueOccurrence(task, Utc(2026, 7, 31, 7, 20)).ShouldBeNull(); TaskSchedule.DueOccurrence(task, Utc(2026, 7, 31, 7, 30)).ShouldNotBeNull(); } [Fact] public void Nach_dem_Marker_zaehlt_das_Intervall_neu() { var task = new TaskItem { Id = "t-1", LastOccurrence = "2026-07-31T07:30:00.0000000Z", When = new TaskWhen { Kind = TaskWhenKind.Every, Value = "30m", TimeZone = "UTC" } }; TaskSchedule.DueOccurrence(task, Utc(2026, 7, 31, 7, 55)).ShouldBeNull(); TaskSchedule.DueOccurrence(task, Utc(2026, 7, 31, 8, 0)).ShouldNotBeNull(); } // ─── cron: with time zone (the core of B7) ─── [Fact] public void Ein_Cron_Termin_wird_in_seiner_Zeitzone_gedeutet_Sommer() { var task = new TaskItem { Id = "t-1", CreatedAt = Utc(2026, 7, 31, 0, 0), When = new TaskWhen { Kind = TaskWhenKind.Cron, Value = "0 9 * * *", TimeZone = "Europe/Berlin" } }; // 09:00 Berlin in summer (CEST, UTC+2) = 07:00 UTC var occ = TaskSchedule.DueOccurrence(task, Utc(2026, 7, 31, 7, 5)); occ.ShouldBe("2026-07-31T07:00:00.0000000Z"); } [Fact] public void Derselbe_Cron_liegt_im_Winter_auf_einer_anderen_UTC_Stunde() { var task = new TaskItem { Id = "t-1", CreatedAt = Utc(2026, 1, 15, 0, 0), When = new TaskWhen { Kind = TaskWhenKind.Cron, Value = "0 9 * * *", TimeZone = "Europe/Berlin" } }; // 09:00 Berlin in winter (CET, UTC+1) = 08:00 UTC — exactly what B7 demands. var occ = TaskSchedule.DueOccurrence(task, Utc(2026, 1, 15, 8, 5)); occ.ShouldBe("2026-01-15T08:00:00.0000000Z"); } [Fact] public void Vor_der_geplanten_Zeit_ist_der_Cron_nicht_faellig() { var task = new TaskItem { Id = "t-1", CreatedAt = Utc(2026, 7, 31, 5, 0), When = new TaskWhen { Kind = TaskWhenKind.Cron, Value = "0 9 * * *", TimeZone = "Europe/Berlin" } }; // 06:00 UTC = 08:00 Berlin, before the 09:00 mark TaskSchedule.DueOccurrence(task, Utc(2026, 7, 31, 6, 0)).ShouldBeNull(); } [Fact] public void Eine_frische_Cron_Aufgabe_holt_keinen_Termin_von_vor_ihrer_Anlage_nach() { var task = new TaskItem { Id = "t-1", CreatedAt = Utc(2026, 7, 31, 8, 0), // after today's 09:00 Berlin mark (07:00 UTC) When = new TaskWhen { Kind = TaskWhenKind.Cron, Value = "0 9 * * *", TimeZone = "Europe/Berlin" } }; // Now 10:00 UTC — today's 09:00 mark was before creation, so do not catch up. TaskSchedule.DueOccurrence(task, Utc(2026, 7, 31, 10, 0)).ShouldBeNull(); } [Fact] public void Derselbe_Cron_Termin_feuert_nicht_zweimal() { var task = new TaskItem { Id = "t-1", LastOccurrence = "2026-07-31T07:00:00.0000000Z", When = new TaskWhen { Kind = TaskWhenKind.Cron, Value = "0 9 * * *", TimeZone = "Europe/Berlin" } }; // Later on the same day: the 09:00 occurrence is already ticked off. TaskSchedule.DueOccurrence(task, Utc(2026, 7, 31, 10, 0)).ShouldBeNull(); // The next day is due again. TaskSchedule.DueOccurrence(task, Utc(2026, 8, 1, 7, 5)).ShouldBe("2026-08-01T07:00:00.0000000Z"); } [Fact] public void Ein_kaputter_Cron_Ausdruck_kippt_den_Scanner_nicht() { var task = new TaskItem { Id = "t-1", When = new TaskWhen { Kind = TaskWhenKind.Cron, Value = "kein cron", TimeZone = "UTC" } }; Should.NotThrow(() => TaskSchedule.DueOccurrence(task, Utc(2026, 7, 31, 9, 0))) .ShouldBeNull(); } /// /// Formerly an unknown zone silently fell back to UTC. That is the worse /// error: a task for 09:00 local time ran at 07:00 in summer without anything /// being noticed anywhere. Now it does not fire at all — and the scanner reports once why. /// [Fact] public void Eine_unbekannte_Zeitzone_laesst_den_Termin_nicht_faellig_werden() { var task = new TaskItem { Id = "t-1", CreatedAt = Utc(2026, 7, 31, 0, 0), When = new TaskWhen { Kind = TaskWhenKind.Cron, Value = "0 9 * * *", TimeZone = "Phantasie/Ort" } }; TaskSchedule.DueOccurrence(task, Utc(2026, 7, 31, 9, 5)).ShouldBeNull(); TaskSchedule.UnresolvableTimeZone(task).ShouldBe("Phantasie/Ort"); } /// /// The core of the port: the same time zone, two spellings. A task file /// created under Windows must yield the same fire time under Linux — /// and vice versa. /// [Theory] [InlineData("Europe/Berlin")] [InlineData("W. Europe Standard Time")] public void Beide_Schreibweisen_derselben_Zone_ergeben_dieselbe_Feuerzeit(string tz) { var task = new TaskItem { Id = "t-1", CreatedAt = Utc(2026, 7, 31, 0, 0), When = new TaskWhen { Kind = TaskWhenKind.Cron, Value = "0 9 * * *", TimeZone = tz } }; TaskSchedule.UnresolvableTimeZone(task).ShouldBeNull(); // 09:00 local time in summer (CEST, UTC+2) = 07:00 UTC. TaskSchedule.DueOccurrence(task, Utc(2026, 7, 31, 9, 5)) .ShouldBe("2026-07-31T07:00:00.0000000Z"); } [Fact] public void Keine_Zeitzonenangabe_bedeutet_weiterhin_UTC() { var task = new TaskItem { Id = "t-1", CreatedAt = Utc(2026, 7, 31, 0, 0), When = new TaskWhen { Kind = TaskWhenKind.Cron, Value = "0 9 * * *", TimeZone = "" } }; TaskSchedule.UnresolvableTimeZone(task).ShouldBeNull(); TaskSchedule.DueOccurrence(task, Utc(2026, 7, 31, 9, 5)) .ShouldBe("2026-07-31T09:00:00.0000000Z"); } // ═══════════════════════════════════════════════════════════ // Daylight saving changes — R4 and R5 of the test strategy // // Europe/Berlin 2026: on 29 March the clock jumps at 02:00 to 03:00, on // 25 October at 03:00 back to 02:00. A daily occurrence at 02:00 thus hits // once a year a time that does not exist, and once one that exists // twice. // ═══════════════════════════════════════════════════════════ private static TaskItem TaeglichUmZwei(DateTime angelegt) => new() { Id = "t-dst", CreatedAt = angelegt, When = new TaskWhen { Kind = TaskWhenKind.Cron, Value = "0 2 * * *", TimeZone = "Europe/Berlin" } }; /// R5 — the case #235 is about. [Fact] public void Zeitumstellung_Maerz_wird_nicht_uebersprungen() { var task = TaeglichUmZwei(Utc(2026, 3, 28, 0, 0)); // "02:00" does not exist on 29 March. The occurrence still happens, namely // at the first point in time after it: 03:00 CEST = 01:00 UTC. // Previously null came here — the task was dropped on that day without replacement. TaskSchedule.DueOccurrence(task, Utc(2026, 3, 29, 2, 0)) .ShouldBe("2026-03-29T01:00:00.0000000Z"); } [Fact] public void Der_verschobene_Termin_feuert_trotzdem_nur_einmal() { var task = TaeglichUmZwei(Utc(2026, 3, 28, 0, 0)); var ersteFaelligkeit = TaskSchedule.DueOccurrence(task, Utc(2026, 3, 29, 2, 0)); ersteFaelligkeit.ShouldNotBeNull(); TaskSchedule.DueOccurrence(task with { LastOccurrence = ersteFaelligkeit }, Utc(2026, 3, 29, 2, 30)) .ShouldBeNull(); } /// R4 — the other direction. [Fact] public void Zeitumstellung_Oktober_feuert_genau_einmal() { var task = TaeglichUmZwei(Utc(2026, 10, 24, 0, 0)); // 02:00 occurs twice on 25 October: in summer time (00:00 UTC) and in // standard time (01:00 UTC). Both passes result in the same occurrence key — // the marker catches the second. var ersterDurchlauf = TaskSchedule.DueOccurrence(task, Utc(2026, 10, 25, 0, 30)); ersterDurchlauf.ShouldNotBeNull(); var zweiterDurchlauf = TaskSchedule.DueOccurrence( task with { LastOccurrence = ersterDurchlauf }, Utc(2026, 10, 25, 1, 30)); zweiterDurchlauf.ShouldBeNull(); } [Fact] public void An_gewoehnlichen_Tagen_bleibt_es_bei_zwei_Uhr_Ortszeit() { // The counter-check: the shift may only apply in the gap. var task = TaeglichUmZwei(Utc(2026, 3, 21, 0, 0)); // 02:00 CET = 01:00 UTC. TaskSchedule.DueOccurrence(task, Utc(2026, 3, 22, 2, 0)) .ShouldBe("2026-03-22T01:00:00.0000000Z"); } [Fact] public void Auch_ein_einmaliger_Termin_in_der_Luecke_faellt_nicht_aus() { // The same handling on the at path — the same rule and the same gap applied there. var task = new TaskItem { Id = "t-1", When = new TaskWhen { Kind = TaskWhenKind.At, Value = "2026-03-29T02:30:00", TimeZone = "Europe/Berlin" } }; // 02:30 does not exist; it becomes 03:00 CEST = 01:00 UTC. TaskSchedule.DueOccurrence(task, Utc(2026, 3, 29, 0, 59)).ShouldBeNull(); TaskSchedule.DueOccurrence(task, Utc(2026, 3, 29, 1, 0)) .ShouldBe("2026-03-29T01:00:00.0000000Z"); } }