Le prove di BrowserStartPageTests fallivano a volte (pagina «about:blank» invece della pagina iniziale, o la pagina iniziale invece di quella aperta da Alberto). Non era l'attesa del browser di #67: la stessa corsa si vede anche con il codice di main. Dopo «Ho finito» il giro del motore vede che Alberto ha lasciato il browser e chiede il turno della pagina per il riposo; se nel frattempo Alberto riprende il browser e la sua navigazione prende il turno per prima, il riposo arriva subito dopo e gli porta via la pagina (una finestra bianca, #52). Ora il riposo dopo la guida e quello di fine sessione guardano di nuovo, con il turno in mano, se Alberto guida (o se una verifica tiene la pagina), come fa già la ricerca dei profili; il riposo resta dovuto per quando lascia il browser. Nuova prova deterministica in EngineHostTests. Il motore conta come avviato già in StartAsync, non solo quando il pool di thread arriva al suo giro: «Scollega» che fallisce lo dice subito (l'altra prova che falliva a volte, DesktopCompositionTests). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
678 lines
35 KiB
C#
678 lines
35 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");
|
||
}
|
||
|
||
[TestMethod]
|
||
public async Task WhenAlbertoLetsGoOfTheBrowserThePageGoesBackToRestUnlessAVerificationKeepsIt()
|
||
{
|
||
var driving = false;
|
||
await using var bed = await EngineTestBed.CreateAsync(Morning, userIsDriving: () => driving);
|
||
await bed.AddAccountAsync("uno.demo", followed: true);
|
||
await bed.AddAccountAsync("due.demo", followed: false, Frequency.High);
|
||
var runner = new ScriptedRunner(bed.Governor, bed.Clock);
|
||
EngineHost? host = null;
|
||
int? restsWhenTaken = null;
|
||
|
||
// He takes the browser in the middle of a recipe (a story viewer may be open): the session ends and leaves the
|
||
// page to him.
|
||
runner.Before = r =>
|
||
{
|
||
if (restsWhenTaken is null && r.Kind != RecipeKind.FollowingSweep)
|
||
{
|
||
restsWhenTaken = runner.Rests;
|
||
driving = true;
|
||
host!.NotifyUserDrivingChanged();
|
||
}
|
||
|
||
return Task.CompletedTask;
|
||
};
|
||
host = bed.Start(runner);
|
||
await bed.RunUntilAsync(EngineTestBed.Local(2026, 10, 14, 12, 0));
|
||
Assert.IsNotNull(restsWhenTaken, "Alberto took the browser during a session.");
|
||
Assert.AreEqual(restsWhenTaken, runner.Rests, "While he drives the page is his.");
|
||
|
||
// He lets go during a pause: nothing starts, but nothing of what was open stays on the site either (§6.1, §6.3).
|
||
await host.PauseAsync();
|
||
driving = false;
|
||
host.NotifyUserDrivingChanged();
|
||
await bed.RunForAsync(TimeSpan.FromSeconds(1));
|
||
Assert.AreEqual(restsWhenTaken + 1, runner.Rests, "Back to rest as soon as he lets go.");
|
||
|
||
// After a verification the page stays as it is until «Riprendi» (§7.6), also when he lets go.
|
||
bed.Breaker.Report(new Signal(SignalLevel.Challenge, "challenge-url", "https://sito.prova/challenge", bed.Clock.Now));
|
||
await bed.Breaker.FlushAsync();
|
||
driving = true;
|
||
host.NotifyUserDrivingChanged();
|
||
await bed.RunForAsync(TimeSpan.FromMinutes(5));
|
||
driving = false;
|
||
host.NotifyUserDrivingChanged();
|
||
await bed.RunForAsync(TimeSpan.FromMinutes(5));
|
||
Assert.AreEqual(restsWhenTaken + 1, runner.Rests, "The verification page is left as it is.");
|
||
}
|
||
|
||
[TestMethod]
|
||
[Timeout(60_000, CooperativeCancellation = true)]
|
||
public async Task ARestThatGetsThePageOnlyAfterAlbertoTookTheBrowserAgainLeavesHisPage()
|
||
{
|
||
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 host.PauseAsync();
|
||
driving = true;
|
||
host.NotifyUserDrivingChanged();
|
||
await bed.RunForAsync(TimeSpan.FromSeconds(1));
|
||
var rests = runner.Rests;
|
||
|
||
// He lets go while his own navigation still holds the page (the turn of «Usa il browser»): the rest waits for it.
|
||
var turns = bed.Services.PageTurns;
|
||
var albertos = await turns.TakeAsync(PageUser.Engine, CancellationToken.None);
|
||
driving = false;
|
||
host.NotifyUserDrivingChanged();
|
||
// Real time: the loop, woken, waits for the turn and for no timer of the virtual clock.
|
||
var watch = System.Diagnostics.Stopwatch.StartNew();
|
||
while (!turns.IsWaiting(PageUser.Engine))
|
||
{
|
||
Assert.IsLessThan(TimeSpan.FromSeconds(10), watch.Elapsed, "the rest waits for the page turn");
|
||
await Task.Delay(TimeSpan.FromMilliseconds(10));
|
||
}
|
||
|
||
// He takes the browser again and his page opens before the rest gets the turn: the page is his, not rested.
|
||
driving = true;
|
||
host.NotifyUserDrivingChanged();
|
||
albertos.Dispose();
|
||
await bed.RunForAsync(TimeSpan.FromSeconds(1));
|
||
Assert.AreEqual(rests, runner.Rests, "the page he took is not put to rest under him");
|
||
Assert.IsFalse(turns.IsWaiting(PageUser.Engine));
|
||
|
||
// The rest is still owed: when he lets go again, the page goes back to rest.
|
||
driving = false;
|
||
host.NotifyUserDrivingChanged();
|
||
await bed.RunForAsync(TimeSpan.FromSeconds(1));
|
||
Assert.AreEqual(rests + 1, runner.Rests, "back to rest once he lets go");
|
||
}
|
||
|
||
[TestMethod]
|
||
public async Task ChangingTheAccountsDuringARecipeDoesNotBringItsNextCheckForward()
|
||
{
|
||
await using var bed = await EngineTestBed.CreateAsync(Morning);
|
||
var watched = await bed.AddAccountAsync("uno.demo", followed: false, Frequency.Low, types: ContentTypes.PostPhotos | ContentTypes.PostVideos);
|
||
await bed.AddAccountAsync("due.demo", followed: false, Frequency.Low, types: ContentTypes.PostPhotos | ContentTypes.PostVideos);
|
||
var runner = new ScriptedRunner(bed.Governor, bed.Clock);
|
||
EngineHost? host = null;
|
||
DateTimeOffset? checkedAt = null;
|
||
|
||
// While the profile check of uno.demo runs (its task is «Running»), Alberto saves the settings of an account.
|
||
runner.Before = async r =>
|
||
{
|
||
if (checkedAt is null && r.Kind == RecipeKind.ProfileCheck && r.AccountId == watched.Id)
|
||
{
|
||
checkedAt = bed.Clock.Now;
|
||
await host!.NotifyAccountsChangedAsync();
|
||
}
|
||
};
|
||
host = bed.Start(runner);
|
||
await bed.RunUntilAsync(Morning.AddDays(1));
|
||
|
||
Assert.IsNotNull(checkedAt);
|
||
var visits = runner.Runs
|
||
.Where(r => r.Request.Kind == RecipeKind.ProfileCheck && r.Request.AccountId == watched.Id && r.Status == RecipeStatus.Completed)
|
||
.Select(r => r.At)
|
||
.ToList();
|
||
|
||
// «Bassa», not followed: about every 12 hours, never sooner than 0.6 × 12 h.
|
||
for (var i = 1; i < visits.Count; i++)
|
||
{
|
||
Assert.IsTrue(visits[i] - visits[i - 1] >= TimeSpan.FromHours(7), $"Visit after {visits[i] - visits[i - 1]}: the interval of the frequency holds.");
|
||
}
|
||
|
||
var pending = (await bed.Db.Tasks.ListPendingAsync(1000))
|
||
.Where(t => t.Kind == EngineTaskKind.ProfileCheck && t.AccountId == watched.Id)
|
||
.ToList();
|
||
Assert.HasCount(1, pending, "One planned check, not a second one from the queue sync.");
|
||
Assert.IsTrue(pending[0].DueAt >= visits[^1] + TimeSpan.FromHours(7), $"Next check at {pending[0].DueAt:O}, after {visits[^1]:O}.");
|
||
}
|
||
|
||
[TestMethod]
|
||
public async Task HighlightCollectionsAreLookedAtOncePerIntervalAndNoLongerOnceGone()
|
||
{
|
||
await using var bed = await EngineTestBed.CreateAsync(Morning);
|
||
var account = await bed.AddAccountAsync("uno.demo", followed: true, Frequency.High);
|
||
await bed.Db.Highlights.UpsertAsync(new Highlight { AccountId = account.Id, InstagramId = "17900000001", Title = "Viaggi", DiscoveredAt = Morning });
|
||
var food = await bed.Db.Highlights.UpsertAsync(new Highlight { AccountId = account.Id, InstagramId = "17900000002", Title = "Cucina", Position = 1, DiscoveredAt = Morning });
|
||
var noStories = await bed.AddAccountAsync("due.demo", followed: true, Frequency.High, types: ContentTypes.PostPhotos);
|
||
await bed.Db.Highlights.UpsertAsync(new Highlight { AccountId = noStories.Id, InstagramId = "17900000003", Title = "Altro", DiscoveredAt = Morning });
|
||
var runner = new ScriptedRunner(bed.Governor, bed.Clock);
|
||
|
||
// Like the real recipe: the collection is marked as looked at.
|
||
runner.Before = async r =>
|
||
{
|
||
if (r.Kind == RecipeKind.Highlight && await bed.Db.Highlights.FindAsync(account.Id, r.HighlightId!) is { } highlight)
|
||
{
|
||
await bed.Db.Highlights.UpsertAsync(highlight with { LastCheckedAt = bed.Clock.Now });
|
||
}
|
||
};
|
||
bed.Start(runner);
|
||
await bed.RunUntilAsync(Morning.AddDays(3));
|
||
|
||
List<RunRecord> Looks(string id) => [.. runner.Runs.Where(r => r.Request.Kind == RecipeKind.Highlight && r.Request.HighlightId == id)];
|
||
foreach (var id in new[] { "17900000001", "17900000002" })
|
||
{
|
||
var looks = Looks(id);
|
||
Assert.IsNotEmpty(looks, $"Collection {id} of an account that wants «Storie in evidenza» is looked at.");
|
||
Assert.IsTrue(looks.All(l => l.Request.AccountId == account.Id));
|
||
for (var i = 1; i < looks.Count; i++)
|
||
{
|
||
// «Alta»: every 24 hours or so, never sooner than 0.6 × 24 h.
|
||
Assert.IsTrue(looks[i].At - looks[i - 1].At >= TimeSpan.FromHours(14), $"Look after {looks[i].At - looks[i - 1].At}.");
|
||
}
|
||
}
|
||
|
||
Assert.IsEmpty(Looks("17900000003"), "«Storie in evidenza» off: not looked at.");
|
||
var all = runner.Runs.Where(r => r.Request.Kind == RecipeKind.Highlight).ToList();
|
||
Assert.AreEqual(all.Count, all.Select(r => r.SessionId).Distinct().Count(), "One collection per account and session.");
|
||
|
||
// A collection that left the profile is no longer looked at; the other one still is.
|
||
await bed.Db.Highlights.MarkRemovedAsync(food.Id, bed.Clock.Now);
|
||
var foodLooks = Looks("17900000002").Count;
|
||
var travelLooks = Looks("17900000001").Count;
|
||
await bed.RunUntilAsync(Morning.AddDays(6));
|
||
Assert.HasCount(foodLooks, Looks("17900000002"));
|
||
Assert.IsTrue(Looks("17900000001").Count > travelLooks);
|
||
}
|
||
|
||
[TestMethod]
|
||
public async Task ASinglePostQueuedByTheCollectionRunsInsteadOfBeingCancelledByTheQueue()
|
||
{
|
||
await using var bed = await EngineTestBed.CreateAsync(Morning);
|
||
var account = await bed.AddAccountAsync("uno.demo", followed: true);
|
||
var paused = await bed.AddAccountAsync("due.demo", followed: true, state: AccountState.Paused);
|
||
|
||
// As CollectionWriter.QueueSinglePostAsync queues it for an address that expired (spec §6.5 point 5).
|
||
EngineTask SinglePost(Account a, string entryId) =>
|
||
new() { Kind = EngineTaskKind.SinglePost, AccountId = a.Id, Payload = entryId, DueAt = Morning, CreatedAt = Morning };
|
||
var refresh = await bed.Db.Tasks.EnqueueAsync(SinglePost(account, "41"));
|
||
var held = await bed.Db.Tasks.EnqueueAsync(SinglePost(paused, "42"));
|
||
var runner = new ScriptedRunner(bed.Governor, bed.Clock);
|
||
bed.Start(runner);
|
||
await bed.RunUntilAsync(Morning.AddDays(1));
|
||
|
||
var runs = runner.Runs.Where(r => r.Request.Kind == RecipeKind.SinglePost).ToList();
|
||
Assert.HasCount(1, runs, "The expired address is refreshed once, on the account that may be contacted.");
|
||
Assert.AreEqual(41L, runs[0].Request.EntryId);
|
||
Assert.AreEqual(account.Id, runs[0].Request.AccountId);
|
||
Assert.AreEqual(EngineTaskState.Done, (await bed.Db.Tasks.GetAsync(refresh.Id))!.State);
|
||
Assert.AreEqual(EngineTaskState.Cancelled, (await bed.Db.Tasks.GetAsync(held.Id))!.State, "«In pausa»: not contacted.");
|
||
}
|
||
}
|