Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
96 lines
4.8 KiB
C#
96 lines
4.8 KiB
C#
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<ActionKind, Budget>(CautionProfile.Balanced.Budgets) { [ActionKind.ProfileOpen] = new Budget(18, 150) },
|
|
};
|
|
var settings = new EngineSettings { Pace = GesturePace.Slow, GradualStart = false, ActiveDays = new HashSet<DayOfWeek> { 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 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<ActionKind, Budget>(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<ArgumentException>(() => 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("{}"));
|
|
}
|
|
}
|