using Khaemhat.Core.Logging; using Khaemhat.Core.Scheduling; namespace Khaemhat.Core.Tests.Common; /// The five-field cron of the scheduled tasks (SPEC §9.6) and the log sections (SPEC §9.5). [TestClass] public sealed class CronExpressionTests { private static readonly TimeZoneInfo Rome = TimeZoneInfo.FindSystemTimeZoneById("Europe/Rome"); [TestMethod] [DataRow("15 * * * *", "2026-11-11T10:20:00Z", "2026-11-11T11:15:00Z")] [DataRow("*/10 * * * *", "2026-11-11T10:20:00Z", "2026-11-11T10:30:00Z")] [DataRow("0 9-17/4 * * *", "2026-11-11T10:00:00Z", "2026-11-11T13:00:00Z")] [DataRow("30 3 1,15 * *", "2026-11-11T00:00:00Z", "2026-11-15T03:30:00Z")] [DataRow("0 0 * * 7", "2026-11-11T00:00:00Z", "2026-11-15T00:00:00Z")] [DataRow("0 0 29 2 *", "2026-03-01T00:00:00Z", "2028-02-29T00:00:00Z")] public void NextAfter_InUtc(string cron, string after, string expected) { var next = CronExpression.Parse(cron).NextAfter(DateTime.Parse(after, null, System.Globalization.DateTimeStyles.AdjustToUniversal), TimeZoneInfo.Utc); Assert.AreEqual(DateTime.Parse(expected, null, System.Globalization.DateTimeStyles.AdjustToUniversal), next); } [TestMethod] public void DayOfMonthAndDayOfWeek_MatchEither() { // The 13th or any Friday: after Wednesday 11 November 2026 comes Friday the 13th, then Friday the 20th. var cron = CronExpression.Parse("0 12 13 * 5"); var first = cron.NextAfter(new DateTime(2026, 11, 11, 0, 0, 0, DateTimeKind.Utc), TimeZoneInfo.Utc); var second = cron.NextAfter(first!.Value, TimeZoneInfo.Utc); Assert.AreEqual(new DateTime(2026, 11, 13, 12, 0, 0, DateTimeKind.Utc), first); Assert.AreEqual(new DateTime(2026, 11, 20, 12, 0, 0, DateTimeKind.Utc), second); } [TestMethod] public void LocalTimes_FollowTheTimeZone_AndSkipTheMissingHour() { var cron = CronExpression.Parse("30 2 * * *"); // 29 March 2026: in Rome clocks jump from 02:00 to 03:00, so 02:30 does not exist that night. var spring = cron.NextAfter(new DateTime(2026, 3, 28, 12, 0, 0, DateTimeKind.Utc), Rome); Assert.AreEqual(new DateTime(2026, 3, 30, 0, 30, 0, DateTimeKind.Utc), spring); // In winter Rome is UTC+1. var winter = cron.NextAfter(new DateTime(2026, 11, 11, 12, 0, 0, DateTimeKind.Utc), Rome); Assert.AreEqual(new DateTime(2026, 11, 12, 1, 30, 0, DateTimeKind.Utc), winter); } [TestMethod] [DataRow("")] [DataRow("* * * *")] [DataRow("60 * * * *")] [DataRow("* 24 * * *")] [DataRow("* * 0 * *")] [DataRow("* * * 13 *")] [DataRow("* * * * 8")] [DataRow("*/0 * * * *")] [DataRow("a * * * *")] public void InvalidExpressions_AreRefused(string text) { Assert.IsFalse(CronExpression.TryParse(text, out _, out var error)); Assert.IsFalse(string.IsNullOrEmpty(error)); } [TestMethod] public void LogSections_ComeFromTheCategory() { Assert.AreEqual(LogSection.Migrations, LogSections.FromCategory("Khaemhat.Data.Migrations.MigrationRunner")); Assert.AreEqual("Sistema", LogSections.Label(LogSection.System)); Assert.AreEqual(LogSection.Authentication, LogSections.FromCategory("Khaemhat.Application.Security.SignInService")); Assert.AreEqual(LogSection.ScheduledJobs, LogSections.FromCategory("Khaemhat.Application.Jobs.JobScheduler")); Assert.AreEqual(LogSection.Documents, LogSections.FromCategory("Khaemhat.Application.Documents.DocumentEngine")); Assert.AreEqual(LogSection.Email, LogSections.FromCategory("Khaemhat.Infrastructure.Mail.MailQueue")); Assert.AreEqual("Performance", LogSections.Scope(LogSection.Performance).Single().Value); } }