using Febe.Core.Abstractions; using Febe.Core.Alerts; using Febe.Core.Domain; using Febe.Core.Engine; using Febe.Core.Storage; using Febe.Tests.Alerts.Support; using Febe.Tests.Engine.Support; namespace Febe.Tests.Engine; /// /// «Storico completato» is one line of Attività and one alert per history, written by the engine with its numbers, /// also for the runs that end otherwise right after the last page. /// [TestClass] public sealed class BackfillCompletedTests { private static readonly DateTimeOffset Morning = EngineTestBed.Local(2026, 10, 14, 6, 0); /// /// The walk reached the beginning of the profile and stored it, then a signal or a cancellation changed how the /// run ended: the history is complete all the same and keeps its line (it would never be walked again). /// [TestMethod] [DataRow(RecipeStatus.StoppedBySignal)] [DataRow(RecipeStatus.Cancelled)] public async Task AHistoryCompletedRightBeforeARunEndsOtherwiseKeepsItsLine(RecipeStatus status) { await using var bed = await AlertBed.CreateAsync(start: Morning); var engine = bed.Engine; var account = await engine.AddAccountAsync("luca.prova", followed: true, state: AccountState.Backfilling, postCount: 30); await engine.Db.Accounts.UpdateAsync((await engine.Db.Accounts.GetAsync(account.Id))! with { KeepUpdated = false }); var runner = new ScriptedRunner(engine.Governor, engine.Clock); var backfills = 0; runner.Before = async r => { if (r.Kind == RecipeKind.Backfill && r.AccountId == account.Id && ++backfills == 1) { // What the recipe stores when the walk meets the end of the list. await engine.Db.Accounts.SetBackfillAsync(account.Id, null, engine.Clock.Now); } }; runner.Script = r => r.Kind == RecipeKind.Backfill && r.AccountId == account.Id && backfills == 1 ? new RecipeOutcome(status, 1, 6) { BackfillFinished = true } : null; engine.Start(runner); await engine.RunUntilAsync(Morning.AddDays(2)); await engine.StopAsync(); await bed.Alerts.FlushAsync(); Assert.AreEqual(1, backfills, "Never walked again."); var finished = await engine.Db.Accounts.GetAsync(account.Id); Assert.AreEqual(AccountState.Paused, finished!.State, "«Tieni aggiornato» off: one download, then paused."); var lines = await engine.Db.Events.ListAsync(new EventQuery { Codes = [EngineEventCodes.BackfillCompleted] }); Assert.HasCount(1, lines); Assert.AreEqual("Storico di @luca.prova completato: 0 post in libreria. «Tieni aggiornato» è spento: l'account resta in pausa.", lines[0].Message); Assert.HasCount(1, bed.Shell.Notifications.Where(n => n.Title == AlertCatalog.Title(AlertKind.BackfillDone))); } }