Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
474 lines
25 KiB
C#
474 lines
25 KiB
C#
using Febe.Core.Abstractions;
|
||
using Febe.Core.Domain;
|
||
using Febe.Core.Engine;
|
||
using Febe.Core.Storage;
|
||
using Febe.Tests.Engine.Support;
|
||
|
||
namespace Febe.Tests.Engine;
|
||
|
||
[TestClass]
|
||
public sealed class EngineHostTests
|
||
{
|
||
private static readonly DateTimeOffset Morning = EngineTestBed.Local(2026, 10, 14, 6, 0);
|
||
|
||
private static async Task<IReadOnlyList<SessionRecord>> SessionsAsync(EngineTestBed bed) =>
|
||
await bed.Db.Sessions.ListAsync(bed.Clock.Now.AddDays(-30), bed.Clock.Now.AddDays(1));
|
||
|
||
[TestMethod]
|
||
public async Task SessionsStayInsideTheWindowAndRespectThePauseFloor()
|
||
{
|
||
await using var bed = await EngineTestBed.CreateAsync(Morning);
|
||
await bed.AddAccountAsync("uno.demo", followed: false, Frequency.High);
|
||
await bed.AddAccountAsync("due.demo", followed: true);
|
||
var runner = new ScriptedRunner(bed.Governor, bed.Clock);
|
||
bed.Start(runner);
|
||
await bed.RunUntilAsync(Morning.AddDays(2));
|
||
|
||
var sessions = await SessionsAsync(bed);
|
||
Assert.IsTrue(sessions.Count > 20, $"{sessions.Count} sessions");
|
||
for (var i = 1; i < sessions.Count; i++)
|
||
{
|
||
var pause = sessions[i].StartedAt - sessions[i - 1].EndedAt!.Value;
|
||
Assert.IsTrue(pause >= TimeSpan.FromMinutes(20), $"pause of {pause}");
|
||
}
|
||
|
||
foreach (var session in sessions)
|
||
{
|
||
Assert.IsNotNull(await bed.Services.Planner.GetWindowAtAsync(session.StartedAt), $"session at {session.StartedAt:O}");
|
||
}
|
||
|
||
Assert.HasCount(0, await BudgetAudit.OutsideWindowAsync(bed.Db, bed.Services.Planner, Morning, bed.Clock.Now));
|
||
Assert.HasCount(0, await BudgetAudit.OverrunsAsync(bed.Db, bed.Runtime.Profile, EngineTestBed.Zone, Morning, bed.Clock.Now));
|
||
Assert.AreEqual(sessions.Count(s => s.EndedAt is not null), runner.Rests, "Every session ends on the empty page.");
|
||
|
||
// The first session comes 2–10 minutes after the start, but not before the window.
|
||
var window = await bed.Services.Planner.GetWindowAsync(new DateOnly(2026, 10, 14));
|
||
Assert.IsTrue(sessions[0].StartedAt >= window!.Start);
|
||
}
|
||
|
||
[TestMethod]
|
||
public async Task TheFirstSessionComesTwoToTenMinutesAfterTheStart()
|
||
{
|
||
await using var bed = await EngineTestBed.CreateAsync(EngineTestBed.Noon);
|
||
await bed.AddAccountAsync("uno.demo", followed: true);
|
||
bed.Start(new ScriptedRunner(bed.Governor, bed.Clock));
|
||
await bed.RunForAsync(TimeSpan.FromMinutes(30));
|
||
var first = (await SessionsAsync(bed))[0].StartedAt - EngineTestBed.Noon;
|
||
Assert.IsTrue(first >= TimeSpan.FromMinutes(2) && first <= TimeSpan.FromMinutes(10), $"first session after {first}");
|
||
}
|
||
|
||
[TestMethod]
|
||
public async Task ARestartResetsNoCountsAndDoesNotShortenPauses()
|
||
{
|
||
// 60 profiles a day, and no hourly limit in the way: demand is far above it.
|
||
var custom = CautionProfile.Prudent with
|
||
{
|
||
Budgets = new Dictionary<ActionKind, Budget>(CautionProfile.Prudent.Budgets) { [ActionKind.ProfileOpen] = new Budget(60, 60) },
|
||
};
|
||
await using var bed = await EngineTestBed.CreateAsync(Morning, settings: new EngineSettings { GradualStart = false }.WithCustomValues(custom));
|
||
for (var i = 0; i < 30; i++)
|
||
{
|
||
await bed.AddAccountAsync($"pubblico{i}.demo", followed: false, Frequency.High, types: ContentTypes.PostPhotos | ContentTypes.PostVideos);
|
||
}
|
||
|
||
await bed.AddAccountAsync("seguito.demo", followed: true);
|
||
bed.Start(new ScriptedRunner(bed.Governor, bed.Clock));
|
||
var window = await bed.Services.Planner.GetWindowAsync(new DateOnly(2026, 10, 14));
|
||
await bed.RunUntilAsync(window!.Start + TimeSpan.FromMinutes(70));
|
||
var openedBefore = await bed.Db.ActionLog.SumAsync(ActionKind.ProfileOpen, Morning, bed.Clock.Now);
|
||
Assert.IsTrue(openedBefore is > 0 and < 60, $"{openedBefore} profiles opened before the restart");
|
||
|
||
await bed.StopAsync();
|
||
var plannedNext = DateTimeOffset.FromUnixTimeMilliseconds((await bed.Db.Settings.GetInt64Async(EngineSettingKeys.NextSession))!.Value);
|
||
var lastEnd = (await SessionsAsync(bed)).Max(s => s.EndedAt!.Value);
|
||
var restartAt = bed.Clock.Now + TimeSpan.FromMinutes(1);
|
||
bed.Clock.AdvanceTo(restartAt);
|
||
await bed.ReopenAsync(seed: 4242);
|
||
bed.Start(new ScriptedRunner(bed.Governor, bed.Clock));
|
||
await bed.RunUntilAsync(EngineTestBed.Local(2026, 10, 15, 0, 0));
|
||
|
||
var after = (await SessionsAsync(bed)).Where(s => s.StartedAt >= restartAt).ToList();
|
||
Assert.IsNotEmpty(after);
|
||
Assert.IsTrue(after[0].StartedAt >= plannedNext, "Not earlier than the session planned before the restart.");
|
||
Assert.IsTrue(after[0].StartedAt - lastEnd >= TimeSpan.FromMinutes(20), "The pause floor holds across the restart.");
|
||
Assert.IsTrue(after[0].StartedAt - restartAt >= TimeSpan.FromMinutes(2));
|
||
|
||
// Demand is far above the budget: the day ends exactly at the daily budget, counting the morning before the restart.
|
||
var openedToday = await bed.Db.ActionLog.SumAsync(ActionKind.ProfileOpen, Morning, bed.Clock.Now);
|
||
Assert.AreEqual(60L, openedToday);
|
||
Assert.HasCount(0, await BudgetAudit.OverrunsAsync(bed.Db, bed.Runtime.Profile, EngineTestBed.Zone, Morning, bed.Clock.Now));
|
||
}
|
||
|
||
[TestMethod]
|
||
public async Task RepeatedRestartsDoNotProduceMoreSessions()
|
||
{
|
||
await using var bed = await EngineTestBed.CreateAsync(Morning);
|
||
await bed.AddAccountAsync("uno.demo", followed: true);
|
||
var seed = 500;
|
||
for (var hour = 0; hour < 18; hour++)
|
||
{
|
||
bed.Start(new ScriptedRunner(bed.Governor, bed.Clock));
|
||
await bed.RunForAsync(TimeSpan.FromMinutes(59));
|
||
await bed.StopAsync();
|
||
await bed.ReopenAsync(++seed);
|
||
}
|
||
|
||
var sessions = await SessionsAsync(bed);
|
||
for (var i = 1; i < sessions.Count; i++)
|
||
{
|
||
Assert.IsTrue(sessions[i].StartedAt - sessions[i - 1].EndedAt!.Value >= TimeSpan.FromMinutes(20));
|
||
}
|
||
|
||
// A program that stays on makes at most 16 h / 20 min sessions in a window; restarts every hour stay well below.
|
||
Assert.IsTrue(sessions.Count <= 48, $"{sessions.Count} sessions");
|
||
}
|
||
|
||
[TestMethod]
|
||
public async Task MostSessionsOpenWithTheSweepAndAccountsComeInRandomOrder()
|
||
{
|
||
await using var bed = await EngineTestBed.CreateAsync(Morning);
|
||
for (var i = 0; i < 8; i++)
|
||
{
|
||
await bed.AddAccountAsync($"pubblico{i}.demo", followed: false, Frequency.High, types: ContentTypes.PostPhotos | ContentTypes.PostVideos);
|
||
}
|
||
|
||
await bed.AddAccountAsync("seguito.demo", followed: true);
|
||
var runner = new ScriptedRunner(bed.Governor, bed.Clock);
|
||
bed.Start(runner);
|
||
await bed.RunUntilAsync(Morning.AddDays(3));
|
||
|
||
var bySession = runner.Runs.GroupBy(r => r.SessionId).ToList();
|
||
var openedWithSweep = bySession.Count(g => g.First().Request.Kind == RecipeKind.FollowingSweep) / (double)bySession.Count;
|
||
Assert.IsTrue(openedWithSweep is > 0.75 and < 0.99, $"sweep first in {openedWithSweep:P0} of sessions");
|
||
|
||
var orders = bySession
|
||
.Select(g => string.Join(",", g.Where(r => r.Request.Kind == RecipeKind.ProfileCheck).Select(r => r.Request.AccountId)))
|
||
.Where(o => o.Contains(',', StringComparison.Ordinal))
|
||
.ToList();
|
||
Assert.IsTrue(orders.Distinct().Count() > orders.Count / 2, "The order of the accounts changes from session to session.");
|
||
|
||
// About one session in three stops after sweep and stories even with profile checks due.
|
||
var withChecks = bySession.Count(g => g.Any(r => r.Request.Kind == RecipeKind.ProfileCheck)) / (double)bySession.Count;
|
||
Assert.IsTrue(withChecks is > 0.45 and < 0.85, $"{withChecks:P0} of sessions with profile checks");
|
||
}
|
||
|
||
[TestMethod]
|
||
public async Task BackfillWaitsWhileTheAccountHasMoreThanSixtyFilesToDownload()
|
||
{
|
||
await using var bed = await EngineTestBed.CreateAsync(Morning);
|
||
var account = await bed.AddAccountAsync("storico.demo", followed: true, state: AccountState.Backfilling, postCount: 3000);
|
||
for (var i = 0; i < 61; i++)
|
||
{
|
||
await bed.Db.Entries.AddAsync(
|
||
new Entry { AccountId = account.Id, Kind = EntryKind.Post, InstagramId = "attesa" + i, PublishedAt = Morning.AddDays(-400 + i), DiscoveredAt = Morning.AddDays(-1) },
|
||
[new Media { Position = 1, Kind = MediaKind.Photo, State = MediaState.ToDownload }]);
|
||
}
|
||
|
||
var runner = new ScriptedRunner(bed.Governor, bed.Clock);
|
||
bed.Start(runner);
|
||
await bed.RunUntilAsync(Morning.AddDays(1));
|
||
Assert.IsFalse(runner.Runs.Any(r => r.Request.Kind == RecipeKind.Backfill));
|
||
Assert.IsTrue((await bed.Db.Events.ListAsync(new EventQuery())).Any(e => e.Code == EngineEventCodes.BackfillWaiting && e.AccountId == account.Id));
|
||
|
||
// Down to 60 waiting files: the backfill goes on.
|
||
var pending = await bed.Db.Media.ListPendingDownloadsAsync(10);
|
||
await bed.Db.Media.MarkStoredAsync(pending[0].Media.Id, new StoredFile("post/a.jpg", 10, new string('a', 64)), MediaState.Downloaded, bed.Clock.Now);
|
||
await bed.RunUntilAsync(Morning.AddDays(2));
|
||
Assert.IsTrue(runner.Runs.Any(r => r.Request.Kind == RecipeKind.Backfill && r.Request.AccountId == account.Id));
|
||
}
|
||
|
||
[TestMethod]
|
||
public async Task BackfillWaitsWhenTheDataBudgetIsReached()
|
||
{
|
||
await using var bed = await EngineTestBed.CreateAsync(Morning);
|
||
await bed.AddAccountAsync("storico.demo", followed: true, state: AccountState.Backfilling, postCount: 3000);
|
||
await bed.Governor.RecordAsync(4L * 1024 * 1024 * 1024, null, CancellationToken.None);
|
||
var runner = new ScriptedRunner(bed.Governor, bed.Clock);
|
||
bed.Start(runner);
|
||
await bed.RunUntilAsync(EngineTestBed.Local(2026, 10, 15, 0, 0));
|
||
Assert.IsFalse(runner.Runs.Any(r => r.Request.Kind == RecipeKind.Backfill));
|
||
await bed.RunUntilAsync(EngineTestBed.Local(2026, 10, 15, 23, 0));
|
||
Assert.IsTrue(runner.Runs.Any(r => r.Request.Kind == RecipeKind.Backfill));
|
||
}
|
||
|
||
[TestMethod]
|
||
public async Task BackfillAlternatesAmongAccountsAndPausesAfterTheSingleDownload()
|
||
{
|
||
await using var bed = await EngineTestBed.CreateAsync(Morning);
|
||
var first = await bed.AddAccountAsync("primo.demo", followed: true, state: AccountState.Backfilling, postCount: 500);
|
||
var second = await bed.AddAccountAsync("secondo.demo", followed: true, state: AccountState.Backfilling, postCount: 500);
|
||
await bed.Db.Accounts.UpdateAsync((await bed.Db.Accounts.GetAsync(second.Id))! with { KeepUpdated = false });
|
||
var runner = new ScriptedRunner(bed.Governor, bed.Clock);
|
||
var backfills = 0;
|
||
DateTimeOffset? finishedAt = null;
|
||
runner.Script = request =>
|
||
{
|
||
if (request.Kind != RecipeKind.Backfill || request.AccountId != second.Id || ++backfills < 3)
|
||
{
|
||
return null;
|
||
}
|
||
|
||
finishedAt = bed.Clock.Now;
|
||
return new RecipeOutcome(RecipeStatus.Completed, 1, 12) { BackfillFinished = true };
|
||
};
|
||
bed.Start(runner);
|
||
await bed.RunUntilAsync(Morning.AddDays(1));
|
||
|
||
var sequence = runner.Runs.Where(r => r.Request.Kind == RecipeKind.Backfill).Select(r => r.Request.AccountId!.Value).Take(4).ToList();
|
||
CollectionAssert.AreEqual(new[] { first.Id, second.Id, first.Id, second.Id }, sequence, "One account per session, alternating.");
|
||
Assert.IsTrue(runner.Runs.GroupBy(r => r.SessionId).All(g => g.Count(r => r.Request.Kind == RecipeKind.Backfill) <= 1));
|
||
|
||
var finished = await bed.Db.Accounts.GetAsync(second.Id);
|
||
Assert.AreEqual(AccountState.Paused, finished!.State, "«Tieni aggiornato» off: one download, then paused.");
|
||
Assert.IsNotNull(finished.BackfillCompletedAt);
|
||
Assert.IsTrue((await bed.Db.Events.ListAsync(new EventQuery())).Any(e => e.Code == EngineEventCodes.BackfillCompleted));
|
||
Assert.IsFalse(runner.Runs.Any(r => r.Request.AccountId == second.Id && r.At > finishedAt), "A paused account is not contacted.");
|
||
}
|
||
|
||
[TestMethod]
|
||
public async Task ThreeUnrecognizedFormatsInARowSuspendTheRecipe()
|
||
{
|
||
await using var bed = await EngineTestBed.CreateAsync(Morning);
|
||
var account = await bed.AddAccountAsync("strano.demo", followed: false, Frequency.High);
|
||
var runner = new ScriptedRunner(bed.Governor, bed.Clock)
|
||
{
|
||
Script = r => r.Kind == RecipeKind.ProfileCheck ? new RecipeOutcome(RecipeStatus.UnrecognizedFormat, 1, 0) : null,
|
||
};
|
||
var host = bed.Start(runner);
|
||
await bed.RunUntilAsync(Morning.AddDays(2));
|
||
|
||
Assert.AreEqual(3, runner.Runs.Count(r => r.Request.Kind == RecipeKind.ProfileCheck));
|
||
Assert.IsTrue(host.SuspendedRecipes.ContainsKey(RecipeKind.ProfileCheck));
|
||
var events = await bed.Db.Events.ListAsync(new EventQuery());
|
||
Assert.AreEqual(3, events.Count(e => e.Code == EngineEventCodes.FormatUnrecognized));
|
||
Assert.IsTrue(events.Any(e => e.Code == EngineEventCodes.RecipeSuspended && e.Severity == EventSeverity.Error));
|
||
Assert.IsTrue((await bed.Db.Accounts.GetAsync(account.Id))!.Notices.HasFlag(AccountNotices.UnrecognizedFormat));
|
||
var snapshot = await host.Monitor.GetSnapshotAsync();
|
||
Assert.HasCount(1, snapshot.SuspendedRecipes);
|
||
|
||
runner.Script = null;
|
||
Assert.IsTrue(await host.ResumeRecipeAsync(RecipeKind.ProfileCheck));
|
||
await bed.RunUntilAsync(Morning.AddDays(3));
|
||
Assert.IsTrue(runner.Runs.Any(r => r.Request.Kind == RecipeKind.ProfileCheck && r.Status == RecipeStatus.Completed));
|
||
}
|
||
|
||
[TestMethod]
|
||
public async Task AnUnreachableAccountIsNotContactedAnyMore()
|
||
{
|
||
await using var bed = await EngineTestBed.CreateAsync(Morning);
|
||
var account = await bed.AddAccountAsync("sparito.demo", followed: false, Frequency.High);
|
||
var runner = new ScriptedRunner(bed.Governor, bed.Clock)
|
||
{
|
||
Script = r => r.AccountId == account.Id ? new RecipeOutcome(RecipeStatus.AccountUnreachable, 1, 0) : null,
|
||
};
|
||
bed.Start(runner);
|
||
await bed.RunUntilAsync(Morning.AddDays(1));
|
||
Assert.AreEqual(1, runner.Runs.Count(r => r.Request.AccountId == account.Id));
|
||
Assert.AreEqual(AccountState.Unreachable, (await bed.Db.Accounts.GetAsync(account.Id))!.State);
|
||
Assert.IsTrue((await bed.Db.Events.ListAsync(new EventQuery())).Any(e => e.Code == EngineEventCodes.AccountUnreachable));
|
||
}
|
||
|
||
[TestMethod]
|
||
public async Task AThrottleClosesTheSessionAtOnceAndStopsForAnHour()
|
||
{
|
||
await using var bed = await EngineTestBed.CreateAsync(Morning);
|
||
await bed.AddAccountAsync("uno.demo", followed: false, Frequency.High);
|
||
await bed.AddAccountAsync("due.demo", followed: false, Frequency.High);
|
||
var runner = new ScriptedRunner(bed.Governor, bed.Clock);
|
||
DateTimeOffset? throttledAt = null;
|
||
runner.AfterGrant = r =>
|
||
{
|
||
if (throttledAt is null && r.Kind == RecipeKind.ProfileCheck)
|
||
{
|
||
throttledAt = bed.Clock.Now;
|
||
bed.Breaker.Report(new Signal(SignalLevel.Throttle, "http-429", null, bed.Clock.Now));
|
||
}
|
||
};
|
||
bed.Start(runner);
|
||
await bed.RunUntilAsync(Morning.AddDays(1));
|
||
|
||
Assert.IsNotNull(throttledAt);
|
||
var sessions = await SessionsAsync(bed);
|
||
var stopped = sessions.Single(s => s.StartedAt <= throttledAt && s.EndedAt >= throttledAt);
|
||
Assert.AreEqual(SessionOutcome.Throttled, stopped.Outcome);
|
||
Assert.IsTrue(stopped.EndedAt - throttledAt < TimeSpan.FromSeconds(30), "The session closes at once.");
|
||
var during = await bed.Db.ActionLog.ListAsync(throttledAt.Value.AddMilliseconds(1), throttledAt.Value + TimeSpan.FromHours(1));
|
||
Assert.HasCount(0, during.Where(a => a.Category != ActionKind.DataDownload).ToList());
|
||
Assert.IsTrue(sessions.Any(s => s.StartedAt > throttledAt.Value + TimeSpan.FromHours(1)), "It starts again by itself.");
|
||
}
|
||
|
||
[TestMethod]
|
||
public async Task AfterAVerificationNothingStartsUntilResumeWhichRestartsTheGradualStart()
|
||
{
|
||
await using var bed = await EngineTestBed.CreateAsync(Morning, gradualStart: true);
|
||
await bed.AddAccountAsync("uno.demo", followed: false, Frequency.High);
|
||
var runner = new ScriptedRunner(bed.Governor, bed.Clock);
|
||
var challenged = false;
|
||
runner.AfterGrant = r =>
|
||
{
|
||
if (!challenged)
|
||
{
|
||
challenged = true;
|
||
bed.Breaker.Report(new Signal(SignalLevel.Challenge, "challenge-url", "https://sito.prova/challenge", bed.Clock.Now));
|
||
}
|
||
};
|
||
var host = bed.Start(runner);
|
||
await bed.RunUntilAsync(Morning.AddDays(3));
|
||
|
||
Assert.IsTrue(challenged);
|
||
Assert.AreEqual(1, runner.Runs.Count, "Nothing after the verification signal.");
|
||
Assert.AreEqual(0, runner.Rests, "The page is left as it is.");
|
||
var snapshot = await host.Monitor.GetSnapshotAsync();
|
||
Assert.AreEqual(EngineMode.StoppedForVerification, snapshot.Mode);
|
||
Assert.IsNotNull(snapshot.Banner);
|
||
Assert.AreEqual(BannerLevel.Error, snapshot.Banner!.Level);
|
||
|
||
var resume = await host.ResumeAsync();
|
||
Assert.IsTrue(resume.Resumed);
|
||
Assert.AreEqual(0.5, bed.Governor.GetEffectiveBudgets(bed.Clock.Now).Factor, 1e-9);
|
||
await bed.RunForAsync(TimeSpan.FromHours(6));
|
||
Assert.IsTrue(runner.Runs.Count > 1, "Work goes on after «Riprendi».");
|
||
}
|
||
|
||
[TestMethod]
|
||
public async Task PauseStopsEverythingUntilResume()
|
||
{
|
||
await using var bed = await EngineTestBed.CreateAsync(Morning);
|
||
await bed.AddAccountAsync("uno.demo", followed: true);
|
||
var runner = new ScriptedRunner(bed.Governor, bed.Clock);
|
||
var host = bed.Start(runner);
|
||
await bed.RunUntilAsync(EngineTestBed.Local(2026, 10, 14, 10, 0));
|
||
await host.PauseAsync();
|
||
var count = runner.Runs.Count;
|
||
await bed.RunUntilAsync(EngineTestBed.Local(2026, 10, 14, 20, 0));
|
||
Assert.AreEqual(count, runner.Runs.Count);
|
||
Assert.AreEqual(EngineMode.Paused, (await host.Monitor.GetSnapshotAsync()).Mode);
|
||
Assert.IsTrue(await bed.Db.Settings.GetBoolAsync(EngineSettingKeys.Paused, false));
|
||
|
||
await host.ResumeAsync();
|
||
await bed.RunUntilAsync(EngineTestBed.Local(2026, 10, 14, 22, 0));
|
||
Assert.IsTrue(runner.Runs.Count > count);
|
||
}
|
||
|
||
[TestMethod]
|
||
public async Task WhileAlbertoUsesTheBrowserTheEngineDoesNotAct()
|
||
{
|
||
var driving = false;
|
||
await using var bed = await EngineTestBed.CreateAsync(Morning, userIsDriving: () => driving);
|
||
await bed.AddAccountAsync("uno.demo", followed: true);
|
||
var runner = new ScriptedRunner(bed.Governor, bed.Clock);
|
||
var host = bed.Start(runner);
|
||
await bed.RunUntilAsync(EngineTestBed.Local(2026, 10, 14, 10, 0));
|
||
driving = true;
|
||
host.NotifyUserDrivingChanged();
|
||
var count = runner.Runs.Count;
|
||
await bed.RunUntilAsync(EngineTestBed.Local(2026, 10, 14, 14, 0));
|
||
Assert.AreEqual(count, runner.Runs.Count);
|
||
Assert.AreEqual(EngineMode.UserDriving, (await host.Monitor.GetSnapshotAsync()).Mode);
|
||
driving = false;
|
||
host.NotifyUserDrivingChanged();
|
||
await bed.RunUntilAsync(EngineTestBed.Local(2026, 10, 14, 16, 0));
|
||
Assert.IsTrue(runner.Runs.Count > count);
|
||
}
|
||
|
||
[TestMethod]
|
||
public async Task WithoutAnInstagramSessionNothingStarts()
|
||
{
|
||
var connected = false;
|
||
await using var bed = await EngineTestBed.CreateAsync(Morning, isConnected: () => connected);
|
||
await bed.AddAccountAsync("uno.demo", followed: true);
|
||
var runner = new ScriptedRunner(bed.Governor, bed.Clock);
|
||
var host = bed.Start(runner);
|
||
await bed.RunUntilAsync(EngineTestBed.Local(2026, 10, 14, 14, 0));
|
||
Assert.IsEmpty(runner.Runs);
|
||
Assert.AreEqual(EngineMode.NotConnected, (await host.Monitor.GetSnapshotAsync()).Mode);
|
||
|
||
connected = true;
|
||
await bed.RunUntilAsync(EngineTestBed.Local(2026, 10, 14, 16, 0));
|
||
Assert.IsNotEmpty(runner.Runs);
|
||
}
|
||
|
||
[TestMethod]
|
||
public async Task CheckNowOutsideTheWindowNeedsConfirmationAndRunsOnlyThatCheck()
|
||
{
|
||
var night = EngineTestBed.Local(2026, 10, 14, 1, 0);
|
||
await using var bed = await EngineTestBed.CreateAsync(night);
|
||
var asked = await bed.AddAccountAsync("chiesto.demo", followed: false);
|
||
await bed.AddAccountAsync("altro.demo", followed: false, Frequency.High);
|
||
await bed.AddAccountAsync("seguito.demo", followed: true);
|
||
var runner = new ScriptedRunner(bed.Governor, bed.Clock);
|
||
var host = bed.Start(runner);
|
||
await bed.RunForAsync(TimeSpan.FromMinutes(20));
|
||
Assert.IsEmpty(runner.Runs, "Nothing at night on the engine's initiative.");
|
||
|
||
var first = await host.CheckNowAsync(asked.Id);
|
||
Assert.AreEqual(CheckNowStatus.NeedsConfirmation, first.Status);
|
||
Assert.IsNotNull(first.ConfirmationToken);
|
||
StringAssert.Contains(first.Message, "fuori dalla fascia");
|
||
Assert.AreEqual(CheckNowStatus.NeedsConfirmation, (await host.CheckNowAsync(asked.Id, "gettone-sbagliato")).Status);
|
||
|
||
var confirmed = await host.CheckNowAsync(asked.Id, first.ConfirmationToken);
|
||
Assert.AreEqual(CheckNowStatus.Queued, confirmed.Status);
|
||
await bed.RunForAsync(TimeSpan.FromHours(2));
|
||
|
||
Assert.IsNotEmpty(runner.Runs);
|
||
Assert.IsTrue(runner.Runs.All(r => r.Request.AccountId == asked.Id), "Only the confirmed check.");
|
||
Assert.IsTrue(runner.Runs.Any(r => r.Request.Kind == RecipeKind.ProfileCheck && r.Request.Manual));
|
||
Assert.HasCount(1, runner.Runs.Select(r => r.SessionId).Distinct().ToList());
|
||
var actions = await bed.Db.ActionLog.ListAsync(night, bed.Clock.Now);
|
||
Assert.IsTrue(actions.All(a => a.AccountId == asked.Id));
|
||
|
||
// The token was used: a second check needs a new confirmation.
|
||
Assert.AreEqual(CheckNowStatus.NeedsConfirmation, (await host.CheckNowAsync(asked.Id, first.ConfirmationToken)).Status);
|
||
|
||
// Normal work waits for the window.
|
||
var window = await bed.Services.Planner.GetWindowAsync(new DateOnly(2026, 10, 14));
|
||
await bed.RunUntilAsync(window!.Start + TimeSpan.FromHours(2));
|
||
Assert.IsTrue(runner.Runs.Where(r => r.Request.AccountId != asked.Id).All(r => r.At >= window.Start));
|
||
Assert.IsTrue(runner.Runs.Any(r => r.Request.AccountId != asked.Id));
|
||
}
|
||
|
||
[TestMethod]
|
||
public async Task CheckNowInsideTheWindowGoesOnTopOfTheQueueButAfterThePauseFloor()
|
||
{
|
||
await using var bed = await EngineTestBed.CreateAsync(Morning);
|
||
var asked = await bed.AddAccountAsync("chiesto.demo", followed: true, Frequency.Low);
|
||
var runner = new ScriptedRunner(bed.Governor, bed.Clock);
|
||
var host = bed.Start(runner);
|
||
await bed.RunUntilAsync(EngineTestBed.Local(2026, 10, 14, 12, 0));
|
||
var before = runner.Runs.Count(r => r.Request.Manual);
|
||
|
||
var askedAt = bed.Clock.Now;
|
||
var result = await host.CheckNowAsync(asked.Id);
|
||
Assert.AreEqual(CheckNowStatus.Queued, result.Status);
|
||
StringAssert.Contains(result.Message, "cima alla coda");
|
||
await bed.RunForAsync(TimeSpan.FromMinutes(45));
|
||
var manual = runner.Runs.Where(r => r.Request.Manual).ToList();
|
||
Assert.AreEqual(before + 2, manual.Count, "Profile check and stories, on request.");
|
||
var session = (await SessionsAsync(bed)).Single(s => s.Id == manual[^1].SessionId);
|
||
var previous = (await SessionsAsync(bed)).Where(s => s.EndedAt <= session.StartedAt).Max(s => s.EndedAt!.Value);
|
||
Assert.IsTrue(session.StartedAt - previous >= TimeSpan.FromMinutes(20));
|
||
if (session.StartedAt >= askedAt)
|
||
{
|
||
Assert.IsTrue(runner.Runs.First(r => r.SessionId == session.Id).Request.Manual, "First thing of the session.");
|
||
}
|
||
}
|
||
|
||
[TestMethod]
|
||
public async Task CheckNowWithTheBudgetExhaustedIsRefused()
|
||
{
|
||
await using var bed = await EngineTestBed.CreateAsync(Morning);
|
||
var asked = await bed.AddAccountAsync("chiesto.demo", followed: false);
|
||
for (var i = 0; i < 100; i++)
|
||
{
|
||
await bed.Db.ActionLog.AppendAsync(new ActionLogRecord { At = Morning.AddSeconds(i), Category = ActionKind.ProfileOpen });
|
||
}
|
||
|
||
var host = bed.Start(new ScriptedRunner(bed.Governor, bed.Clock));
|
||
await bed.RunUntilAsync(EngineTestBed.Local(2026, 10, 14, 12, 0));
|
||
var result = await host.CheckNowAsync(asked.Id);
|
||
Assert.AreEqual(CheckNowStatus.Refused, result.Status);
|
||
StringAssert.Contains(result.Message, "tetto giornaliero");
|
||
}
|
||
}
|