Files
Febe/tests/Febe.Tests/Engine/EngineMonitorTests.cs
T

128 lines
6.5 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 EngineMonitorTests
{
[TestMethod]
public async Task AtNightTheMonitorSaysWhenWorkResumes()
{
var night = EngineTestBed.Local(2026, 10, 14, 2, 0);
await using var bed = await EngineTestBed.CreateAsync(night);
await bed.AddAccountAsync("marta.demo", followed: true);
var host = bed.Start(new ScriptedRunner(bed.Governor, bed.Clock));
await bed.RunForAsync(TimeSpan.FromMinutes(15));
var snapshot = await host.Monitor.GetSnapshotAsync();
Assert.AreEqual(EngineMode.OutsideWindow, snapshot.Mode);
Assert.AreEqual("Fuori fascia", snapshot.Headline);
StringAssert.Contains(snapshot.SidebarLine, "Riprende verso le ");
var window = await bed.Services.Planner.GetWindowAsync(new DateOnly(2026, 10, 14));
Assert.AreEqual(window!.Start, snapshot.ResumesAround);
Assert.IsNull(snapshot.Banner);
Assert.IsTrue(snapshot.Today.Spans.Any(s => s.Kind == StripSpanKind.OutsideWindow && s.Start == snapshot.Today.Start));
Assert.IsTrue(snapshot.Today.Spans.Any(s => s.Kind == StripSpanKind.PlannedSession && s.Indicative));
Assert.IsTrue(snapshot.Queue.All(q => q.When.StartsWith("verso le", StringComparison.Ordinal)));
Assert.AreEqual(4, snapshot.MainBudgets.Count);
CollectionAssert.AreEqual(
new[] { ActionKind.ProfileOpen, ActionKind.StoryView, ActionKind.BackfillPage, ActionKind.DataDownload },
snapshot.MainBudgets.Select(b => b.Kind).ToArray());
Assert.AreEqual(Enum.GetValues<ActionKind>().Length, snapshot.AllBudgets.Count);
Assert.AreEqual("Profili aperti", snapshot.ProfilesOpenedToday.Label);
Assert.AreEqual("di 100", snapshot.ProfilesOpenedToday.LimitText);
}
[TestMethod]
public async Task DuringTheDayTheStripShowsSessionsRestsAndTheSummary()
{
await using var bed = await EngineTestBed.CreateAsync(EngineTestBed.Local(2026, 10, 14, 6, 0));
await bed.AddAccountAsync("marta.demo", followed: true);
await bed.AddAccountAsync("luca.demo", followed: false, Frequency.High);
var host = bed.Start(new ScriptedRunner(bed.Governor, bed.Clock));
await bed.RunUntilAsync(EngineTestBed.Local(2026, 10, 14, 15, 0));
var snapshot = await host.Monitor.GetSnapshotAsync();
Assert.IsTrue(snapshot.Mode is EngineMode.Resting or EngineMode.InSession);
Assert.IsTrue(snapshot.Today.Spans.Count(s => s.Kind is StripSpanKind.Session or StripSpanKind.CurrentSession) >= 5);
Assert.IsTrue(snapshot.Today.Spans.Any(s => s.Kind == StripSpanKind.Rest));
StringAssert.Contains(snapshot.Today.Summary, "sessioni");
StringAssert.Contains(snapshot.Today.Summary, "nessun rallentamento");
Assert.IsNotEmpty(snapshot.RecentActions);
Assert.IsTrue(snapshot.RecentActions.Any(a => a.Title == "Giro dei profili che segui" || a.Title.StartsWith('@')));
if (snapshot.Mode == EngineMode.Resting)
{
Assert.AreEqual("In ascolto", snapshot.Headline);
StringAssert.StartsWith(snapshot.Details[0], "Riprende verso le ");
}
var profiles = snapshot.ProfilesOpenedToday;
Assert.IsTrue(profiles.UsedToday > 0);
StringAssert.StartsWith(profiles.Note, "In questa ora ");
}
[TestMethod]
public async Task AThrottleShowsAnAmberStripAndSuspendsTheQueue()
{
await using var bed = await EngineTestBed.CreateAsync();
await bed.AddAccountAsync("marta.demo", followed: true);
var host = bed.Start(new ScriptedRunner(bed.Governor, bed.Clock));
await bed.RunForAsync(TimeSpan.FromMinutes(1));
await bed.Breaker.ReportAsync(new Signal(SignalLevel.Throttle, "http-429", null, bed.Clock.Now));
var snapshot = await host.Monitor.GetSnapshotAsync();
Assert.AreEqual(EngineMode.Throttled, snapshot.Mode);
Assert.AreEqual(BannerLevel.Warning, snapshot.Banner!.Level);
StringAssert.Contains(snapshot.Banner.Message, "tetti dimezzati");
Assert.IsTrue(snapshot.Queue.All(q => q.Suspended && q.When == "Sospeso"));
Assert.IsTrue(snapshot.Today.Spans.Any(s => s.Kind == StripSpanKind.Throttle));
StringAssert.StartsWith(snapshot.ProfilesOpenedToday.Note, "Fermo dalle ");
StringAssert.Contains(snapshot.ReductionNote!, "Tetti dimezzati");
}
[TestMethod]
public async Task TheEventStreamDeliversTimelineLines()
{
await using var bed = await EngineTestBed.CreateAsync();
var host = bed.Start(new ScriptedRunner(bed.Governor, bed.Clock));
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(20));
var received = new List<EngineNotification>();
var reading = Task.Run(async () =>
{
await foreach (var notification in host.Monitor.SubscribeAsync(cts.Token))
{
received.Add(notification);
if (notification.Kind == EngineNotificationKind.Event && notification.Event!.Code == EngineEventCodes.Paused)
{
break;
}
}
});
await Task.Delay(100);
await host.PauseAsync();
await reading;
Assert.IsTrue(received.Any(n => n.Event?.Code == EngineEventCodes.Paused));
StringAssert.Contains(received.Last().Event!.Message, "Riprendi");
}
[TestMethod]
public void ItalianTextUsesDotsCommasAndVersoLe()
{
Assert.AreEqual("7.682", ItalianText.Number(7682));
Assert.AreEqual("1.234.567", ItalianText.Number(1234567));
Assert.AreEqual("1,3 GB", ItalianText.Bytes((long)(1.3 * 1024 * 1024 * 1024)));
Assert.AreEqual("0,5", ItalianText.Decimal(0.5));
var zone = EngineTestBed.Zone;
var now = EngineTestBed.Local(2026, 10, 14, 12, 0);
Assert.AreEqual("verso le 15:30", ItalianText.Around(EngineTestBed.Local(2026, 10, 14, 15, 30), now, zone));
Assert.AreEqual("domani verso le 7:42", ItalianText.Around(EngineTestBed.Local(2026, 10, 15, 7, 42), now, zone));
Assert.AreEqual("verso l'1:05", ItalianText.Around(EngineTestBed.Local(2026, 10, 14, 13, 5).AddHours(12), now.AddHours(12), zone));
Assert.AreEqual("1 ora e 20 minuti", ItalianText.Duration(TimeSpan.FromMinutes(80)));
Assert.AreEqual("ogni 75 minuti", ItalianText.Every(TimeSpan.FromMinutes(75)));
Assert.AreEqual("ogni 3 ore", ItalianText.Every(TimeSpan.FromHours(3)));
}
}