Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
175 lines
7.8 KiB
C#
175 lines
7.8 KiB
C#
using System.Text;
|
|
using Febe.Core.Abstractions;
|
|
using Febe.Core.Domain;
|
|
using Febe.Core.Engine;
|
|
using Febe.Core.Simulation;
|
|
using Febe.Core.Storage;
|
|
using Febe.Data;
|
|
using Febe.Tests.Support;
|
|
|
|
namespace Febe.Tests.Engine;
|
|
|
|
[TestClass]
|
|
public sealed class EngineSimulationTests
|
|
{
|
|
private static async Task<SimulationReport> SimulateAsync(SimulationOptions options)
|
|
{
|
|
using var folder = new TempFolder("simula");
|
|
return await EngineSimulation.RunAsync(options, async ct => await FebeDatabase.OpenAsync(folder.Combine("dati"), ct));
|
|
}
|
|
|
|
[TestMethod]
|
|
[DataRow(CautionProfileKind.Prudent)]
|
|
[DataRow(CautionProfileKind.Balanced)]
|
|
[DataRow(CautionProfileKind.Fast)]
|
|
public async Task EveryProfileRunsAWeekWithoutExceedingAnyBudget(CautionProfileKind profile)
|
|
{
|
|
var report = await SimulateAsync(new SimulationOptions { Days = 7, Profile = profile, Seed = 3 });
|
|
|
|
Assert.AreEqual(0, report.Overruns, string.Join("\n", report.OverrunDetails));
|
|
Assert.AreEqual(0, report.OverrunsOfFullBudgets);
|
|
Assert.AreEqual(0, report.ActionsOutsideWindow);
|
|
Assert.AreEqual(0, report.PauseFloorViolations);
|
|
Assert.IsTrue(report.ShortestPause >= CautionProfile.For(profile).PauseMin);
|
|
Assert.IsTrue(report.SessionStartCv >= 0.35, $"CV {report.SessionStartCv}");
|
|
Assert.IsTrue(report.SessionsPerDayMean > 5);
|
|
Assert.IsTrue(report.BackfillStored > 0);
|
|
Assert.IsTrue(report.FollowedPosts.Count > 0 && report.Stories.Count > 0);
|
|
|
|
var text = report.ToText();
|
|
StringAssert.Contains(text, "Sessioni");
|
|
StringAssert.Contains(text, "superamenti dei tetti: 0");
|
|
StringAssert.Contains(text, "Storico di 2.000 post");
|
|
var csv = report.ToCsv();
|
|
Assert.HasCount(report.Days.Count + 1, csv.Split('\n', StringSplitOptions.RemoveEmptyEntries));
|
|
StringAssert.StartsWith(csv, "giorno;sessioni;");
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task HeavyDemandSmallDataBudgetAndFrequentThrottlesStillExceedNothing()
|
|
{
|
|
var custom = CautionProfile.Prudent with
|
|
{
|
|
Kind = CautionProfileKind.Custom,
|
|
DataPerDayBytes = 150L * 1024 * 1024,
|
|
Budgets = new Dictionary<ActionKind, Budget>(CautionProfile.Prudent.Budgets) { [ActionKind.StoryView] = new Budget(10, 40) },
|
|
};
|
|
var report = await SimulateAsync(new SimulationOptions
|
|
{
|
|
Days = 5,
|
|
Seed = 11,
|
|
Settings = new EngineSettings().WithCustomValues(custom),
|
|
World = new SimulatedWorldOptions
|
|
{
|
|
Seed = 11,
|
|
FollowedAccounts = 15,
|
|
NotFollowedAccounts = 25,
|
|
MeanPostsPerDay = 1.5,
|
|
StoryAccountShare = 1,
|
|
HistoricalPosts = 3000,
|
|
ThrottleProbabilityPerAction = 0.004,
|
|
FrequencyOf = _ => Frequency.High,
|
|
},
|
|
});
|
|
|
|
Assert.AreEqual(0, report.Overruns, string.Join("\n", report.OverrunDetails));
|
|
Assert.AreEqual(0, report.ActionsOutsideWindow);
|
|
Assert.AreEqual(0, report.PauseFloorViolations);
|
|
Assert.IsNotEmpty(report.Throttles);
|
|
var data = report.CategoryTotals.Single(c => c.Kind == ActionKind.DataDownload);
|
|
// Gradual start and throttles halve it: under pressure means near half of the full budget at least.
|
|
Assert.IsTrue(data.MaxPerDay > data.DailyLimit * 0.45, "The data budget was really under pressure.\n" + report.ToText());
|
|
Assert.IsTrue(data.MaxPerDay <= data.DailyLimit);
|
|
var stories = report.CategoryTotals.Single(c => c.Kind == ActionKind.StoryView);
|
|
Assert.IsTrue(stories.MaxPerDay <= 40);
|
|
var profiles = report.CategoryTotals.Single(c => c.Kind == ActionKind.ProfileOpen);
|
|
Assert.IsTrue(profiles.WorstHour >= 6, "The hourly profile budget (halved at first) was reached.");
|
|
Assert.IsTrue(profiles.WorstHour <= 12 && profiles.MaxPerDay <= 100);
|
|
}
|
|
|
|
[TestMethod]
|
|
[DataRow(true)]
|
|
[DataRow(false)]
|
|
public async Task WithStoriesOnEveryAccountGetsAtLeastThreeStoryChecksADay(bool sweepCoversFollowedStories)
|
|
{
|
|
var report = await SimulateAsync(new SimulationOptions
|
|
{
|
|
Days = 4,
|
|
Seed = 5,
|
|
Engine = EngineOptions.Default with { SweepCoversFollowedStories = sweepCoversFollowedStories },
|
|
World = new SimulatedWorldOptions
|
|
{
|
|
Seed = 5,
|
|
FollowedAccounts = 3,
|
|
NotFollowedAccounts = 3,
|
|
StoryAccountShare = 1,
|
|
HistoricalPosts = 0,
|
|
ThrottleProbabilityPerAction = 0,
|
|
FrequencyOf = i => i % 2 == 0 ? Frequency.Low : Frequency.Normal,
|
|
},
|
|
});
|
|
|
|
var stats = report.World;
|
|
foreach (var day in report.Days.Skip(1))
|
|
{
|
|
foreach (var account in report.WorldAccounts)
|
|
{
|
|
var explicitChecks = stats.StoryChecks.GetValueOrDefault((account.Id, day.Day));
|
|
var bySweep = account.Followed && sweepCoversFollowedStories ? stats.Sweeps.GetValueOrDefault(day.Day) : 0;
|
|
Assert.IsTrue(explicitChecks + bySweep >= 3, $"{account.Username} on {day.Day}: {explicitChecks} checks, {bySweep} sweeps");
|
|
}
|
|
}
|
|
|
|
Assert.AreEqual(0, report.MissedStories);
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task TheSameSeedGivesTheSameSimulation()
|
|
{
|
|
var first = await SimulateAsync(new SimulationOptions { Days = 2, Seed = 9 });
|
|
var second = await SimulateAsync(new SimulationOptions { Days = 2, Seed = 9 });
|
|
Assert.AreEqual(first.SessionCount, second.SessionCount);
|
|
Assert.AreEqual(first.VirtualEvents, second.VirtualEvents);
|
|
CollectionAssert.AreEqual(first.Days.Select(d => d.Actions[ActionKind.ProfileOpen]).ToList(), second.Days.Select(d => d.Actions[ActionKind.ProfileOpen]).ToList());
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task TheCsvIsUtf8WithBomAndSemicolons()
|
|
{
|
|
var report = await SimulateAsync(new SimulationOptions { Days = 1, Seed = 2 });
|
|
using var folder = new TempFolder("csv");
|
|
var path = folder.Combine("resoconto.csv");
|
|
await report.WriteCsvAsync(path);
|
|
var bytes = await File.ReadAllBytesAsync(path);
|
|
CollectionAssert.AreEqual(new byte[] { 0xEF, 0xBB, 0xBF }, bytes.Take(3).ToArray());
|
|
var text = Encoding.UTF8.GetString(bytes, 3, bytes.Length - 3);
|
|
StringAssert.Contains(text, "minuti di attività");
|
|
Assert.IsTrue(text.Split('\n')[1].Split(';').Length > 10);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void TheCommandLineIsParsed()
|
|
{
|
|
Assert.IsTrue(Febe.Server.SimulateCommand.TryParse(["--simula", "30g", "--profilo", "equilibrato", "--csv", "x.csv", "--seme", "4"], out var options, out var csv, out _));
|
|
Assert.AreEqual(30, options.Days);
|
|
Assert.AreEqual(CautionProfileKind.Balanced, options.Profile);
|
|
Assert.AreEqual("x.csv", csv);
|
|
Assert.AreEqual(4, options.Seed);
|
|
Assert.IsFalse(Febe.Server.SimulateCommand.TryParse(["--simula", "trenta"], out _, out _, out var problem));
|
|
StringAssert.Contains(problem, "Durata non valida");
|
|
Assert.IsFalse(Febe.Server.SimulateCommand.TryParse(["--simula", "7g", "--profilo", "spericolato"], out _, out _, out problem));
|
|
StringAssert.Contains(problem, "Profilo sconosciuto");
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task TheCommandPrintsTheItalianReport()
|
|
{
|
|
using var output = new StringWriter();
|
|
using var error = new StringWriter();
|
|
var code = await Febe.Server.SimulateCommand.RunAsync(["--simula", "1g", "--profilo", "rapido"], output, error);
|
|
Assert.AreEqual(0, code, error.ToString());
|
|
StringAssert.Contains(output.ToString(), "profilo «Rapido»");
|
|
Assert.AreEqual(2, await Febe.Server.SimulateCommand.RunAsync(["--simula", "0g"], output, error));
|
|
}
|
|
}
|