using Febe.Core.Abstractions; using Febe.Core.Domain; using Febe.Core.Engine; using Febe.Data; using Febe.Tests.Support; namespace Febe.Tests.Engine; [TestClass] public sealed class EngineSettingsTests { [TestMethod] public async Task SettingsAreStoredAndReadBackWithACustomProfile() { using var folder = new TempFolder("impostazioni"); await using var db = await FebeDatabase.OpenAsync(folder.Combine("dati")); Assert.AreEqual(CautionProfileKind.Prudent, (await EngineSettingsStore.LoadAsync(db.Settings)).ProfileKind, "«Prudente» is the default."); var custom = CautionProfile.Balanced with { PauseMin = TimeSpan.FromMinutes(15), WindowStart = new TimeOnly(8, 0), WindowEnd = new TimeOnly(0, 30), Budgets = new Dictionary(CautionProfile.Balanced.Budgets) { [ActionKind.ProfileOpen] = new Budget(18, 150) }, }; var settings = new EngineSettings { Pace = GesturePace.Slow, GradualStart = false, ActiveDays = new HashSet { DayOfWeek.Monday, DayOfWeek.Saturday } } .WithCustomValues(custom); await EngineSettingsStore.SaveAsync(db.Settings, settings); var loaded = await EngineSettingsStore.LoadAsync(db.Settings); Assert.AreEqual(CautionProfileKind.Custom, loaded.ProfileKind); Assert.AreEqual(GesturePace.Slow, loaded.Pace); Assert.IsFalse(loaded.GradualStart); CollectionAssert.AreEquivalent(new[] { DayOfWeek.Monday, DayOfWeek.Saturday }, loaded.ActiveDays.ToArray()); var effective = loaded.EffectiveProfile; Assert.AreEqual(CautionProfileKind.Custom, effective.Kind); Assert.AreEqual(TimeSpan.FromMinutes(15), effective.PauseMin); Assert.AreEqual(new TimeOnly(0, 30), effective.WindowEnd); Assert.AreEqual(new Budget(18, 150), effective.Budgets[ActionKind.ProfileOpen]); Assert.AreEqual(CautionProfile.Balanced.DataPerDayBytes, effective.DataPerDayBytes); Assert.AreEqual(GesturePace.Slow, effective.Pace); } [TestMethod] public void AStoryViewIsARequestOrAStoryShownAndItsBudgetsStayAsTheyWere() { // Issue #36, S3: by requests one StoryView brings every story of an account. The budgets do not change in S3, // so that Instagram (still by the pages) and a saved «Su misura» behave as before; halving them is a question // for Alberto when Instagram goes by requests (S4). Assert.AreEqual(new Budget(50, 300), CautionProfile.Prudent.Budgets[ActionKind.StoryView]); Assert.AreEqual(new Budget(80, 500), CautionProfile.Balanced.Budgets[ActionKind.StoryView]); Assert.AreEqual(new Budget(120, 800), CautionProfile.Fast.Budgets[ActionKind.StoryView]); Assert.AreEqual(new Budget(240, 1600), CautionProfile.Limits.MaxBudget(ActionKind.StoryView), "«Su misura» stays within twice «Rapido»."); Assert.IsTrue(CautionProfile.Prudent.Budgets[ActionKind.StoryView].PerHour >= new Febe.Core.Collection.Recipes.RecipeOptions().MinStoryBudget, "The viewer still has room for its minimum of stories."); } [TestMethod] public void TheLimitsOfTheInterfaceAreEnforced() { var bad = CautionProfile.Fast with { PauseMin = TimeSpan.FromMinutes(4), SessionMax = TimeSpan.FromMinutes(31), WindowStart = new TimeOnly(5, 0), WindowEnd = new TimeOnly(2, 0), Budgets = new Dictionary(CautionProfile.Fast.Budgets) { [ActionKind.StoryView] = new Budget(241, 800) }, DataPerDayBytes = CautionProfile.Limits.MaxDataPerDayBytes + 1, }; var errors = CautionProfileRules.Validate(bad); Assert.IsTrue(errors.Any(e => e.Contains("5 minuti", StringComparison.Ordinal))); Assert.IsTrue(errors.Any(e => e.Contains("30 minuti", StringComparison.Ordinal))); Assert.IsTrue(errors.Any(e => e.Contains("20 ore", StringComparison.Ordinal))); Assert.IsTrue(errors.Any(e => e.Contains("storie viste", StringComparison.Ordinal))); Assert.IsTrue(errors.Any(e => e.Contains("dati scaricati", StringComparison.Ordinal))); Assert.HasCount(0, CautionProfileRules.Validate(CautionProfile.Fast)); Assert.HasCount(0, CautionProfileRules.Validate(CautionProfile.Prudent)); Assert.HasCount(0, CautionProfileRules.Validate(CautionProfile.Balanced)); Assert.AreEqual(TimeSpan.FromHours(18), CautionProfileRules.WindowLength(new TimeOnly(6, 30), new TimeOnly(0, 30))); } [TestMethod] public async Task InvalidSettingsAreNotStored() { using var folder = new TempFolder("impostazioni"); await using var db = await FebeDatabase.OpenAsync(folder.Combine("dati")); var settings = new EngineSettings().WithCustomValues(CautionProfile.Prudent with { PauseMin = TimeSpan.FromMinutes(1) }); var error = await Assert.ThrowsAsync(() => EngineSettingsStore.SaveAsync(db.Settings, settings)); StringAssert.Contains(error.Message, "pausa minima"); Assert.IsNull(await db.Settings.GetAsync(EngineSettingKeys.Profile)); } [TestMethod] public void TheCustomProfileSurvivesTheJsonRoundTrip() { var custom = CautionProfile.Fast with { Kind = CautionProfileKind.Custom, WindowJitter = TimeSpan.FromMinutes(25), RampUpDays = 10 }; var decoded = CautionProfileCodec.TryDecode(CautionProfileCodec.Encode(custom)); Assert.IsNotNull(decoded); Assert.AreEqual(custom.WindowJitter, decoded!.WindowJitter); Assert.AreEqual(10, decoded.RampUpDays); foreach (var (kind, budget) in custom.Budgets) { Assert.AreEqual(budget, decoded.Budgets[kind]); } Assert.IsNull(CautionProfileCodec.TryDecode("{non è json")); Assert.IsNull(CautionProfileCodec.TryDecode("{}")); } }