Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
94 lines
4.6 KiB
C#
94 lines
4.6 KiB
C#
using Febe.Core.Abstractions;
|
||
using Febe.Core.Domain;
|
||
using Febe.Core.Engine;
|
||
using Febe.Tests.Engine.Support;
|
||
|
||
namespace Febe.Tests.Engine;
|
||
|
||
[TestClass]
|
||
public sealed class DayPlannerTests
|
||
{
|
||
[TestMethod]
|
||
public async Task TheWindowMovesEveryDayWithinTheJitterAndNotUniformly()
|
||
{
|
||
await using var bed = await EngineTestBed.CreateAsync();
|
||
var planner = bed.Services.Planner;
|
||
var profile = CautionProfile.Prudent;
|
||
var day = new DateOnly(2026, 10, 14);
|
||
var starts = new List<double>();
|
||
for (var i = 0; i < 300; i++)
|
||
{
|
||
var d = day.AddDays(i);
|
||
var window = await planner.GetWindowAsync(d);
|
||
Assert.IsNotNull(window);
|
||
var baseStart = LocalCalendar.FromLocal(d.ToDateTime(profile.WindowStart), EngineTestBed.Zone);
|
||
var baseEnd = LocalCalendar.FromLocal(d.ToDateTime(profile.WindowEnd), EngineTestBed.Zone);
|
||
var shiftStart = (window!.Start - baseStart).TotalMinutes;
|
||
var shiftEnd = (window.End - baseEnd).TotalMinutes;
|
||
Assert.IsTrue(Math.Abs(shiftStart) <= 40, $"start shift {shiftStart}");
|
||
Assert.IsTrue(Math.Abs(shiftEnd) <= 40, $"end shift {shiftEnd}");
|
||
starts.Add(shiftStart);
|
||
}
|
||
|
||
Assert.IsTrue(starts.Distinct().Count() > 250, "Every day has its own shift.");
|
||
|
||
// A normal shape: far more draws in the central half than a uniform draw would give (50%).
|
||
var central = starts.Count(s => Math.Abs(s) <= 20) / (double)starts.Count;
|
||
Assert.IsTrue(central > 0.62, $"central share {central}");
|
||
}
|
||
|
||
[TestMethod]
|
||
public async Task TheDailyShiftIsNotRedrawnAfterARestart()
|
||
{
|
||
await using var bed = await EngineTestBed.CreateAsync();
|
||
var day = new DateOnly(2026, 10, 20);
|
||
var before = await bed.Services.Planner.GetWindowAsync(day);
|
||
var restarted = await bed.ReopenAsync(seed: 12345);
|
||
var after = await restarted.Planner.GetWindowAsync(day);
|
||
Assert.AreEqual(before, after);
|
||
}
|
||
|
||
[TestMethod]
|
||
public async Task AWindowMayEndAfterMidnight()
|
||
{
|
||
await using var bed = await EngineTestBed.CreateAsync(profile: CautionProfileKind.Fast);
|
||
var day = new DateOnly(2026, 10, 14);
|
||
var window = await bed.Services.Planner.GetWindowAsync(day);
|
||
Assert.IsNotNull(window);
|
||
Assert.AreEqual(day.AddDays(1), LocalCalendar.Today(window!.End, EngineTestBed.Zone), "06:30–00:30 ends on the next day.");
|
||
|
||
// Ten minutes before the end, after midnight, the instant belongs to the window of the previous day.
|
||
var lateNight = window.End - TimeSpan.FromMinutes(10);
|
||
var found = await bed.Services.Planner.GetWindowAtAsync(lateNight);
|
||
Assert.AreEqual(window, found);
|
||
Assert.IsNull(await bed.Services.Planner.GetWindowAtAsync(window.End + TimeSpan.FromMinutes(10)));
|
||
}
|
||
|
||
[TestMethod]
|
||
public async Task InactiveDaysHaveNoWindow()
|
||
{
|
||
var weekdays = new HashSet<DayOfWeek> { DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday };
|
||
await using var bed = await EngineTestBed.CreateAsync(settings: new EngineSettings { GradualStart = false, ActiveDays = weekdays });
|
||
var saturday = new DateOnly(2026, 10, 17);
|
||
Assert.IsNull(await bed.Services.Planner.GetWindowAsync(saturday));
|
||
var next = await bed.Services.Planner.GetNextWindowAsync(LocalCalendar.StartOfDay(saturday, EngineTestBed.Zone).AddHours(12));
|
||
Assert.AreEqual(new DateOnly(2026, 10, 19), next!.Day);
|
||
}
|
||
|
||
[TestMethod]
|
||
public void WindowsAreComputedAcrossDaylightSavingTime()
|
||
{
|
||
// 25 October 2026: clocks go back in Rome at 3:00; the window is still 07:30–23:30 local time.
|
||
var profile = CautionProfile.Prudent with { WindowJitter = TimeSpan.Zero };
|
||
var window = DayPlanner.Compute(new DateOnly(2026, 10, 25), profile, 42, EngineTestBed.Zone);
|
||
Assert.AreEqual(new TimeOnly(7, 30), TimeOnly.FromDateTime(LocalCalendar.ToLocal(window.Start, EngineTestBed.Zone)));
|
||
Assert.AreEqual(new TimeOnly(23, 30), TimeOnly.FromDateTime(LocalCalendar.ToLocal(window.End, EngineTestBed.Zone)));
|
||
Assert.AreEqual(TimeSpan.FromHours(16), window.Length);
|
||
|
||
// 28 March 2027: clocks go forward at 2:00; a window from 22:00 to 6:00 loses that hour.
|
||
var night = profile with { WindowStart = new TimeOnly(22, 0), WindowEnd = new TimeOnly(6, 0) };
|
||
var spring = DayPlanner.Compute(new DateOnly(2027, 3, 27), night, 42, EngineTestBed.Zone);
|
||
Assert.AreEqual(TimeSpan.FromHours(7), spring.Length);
|
||
}
|
||
}
|