using Febe.App; using Febe.Core.Alerts; using Febe.Core.Domain; using Febe.Core.Engine; using Febe.Core.Library; using Febe.Tests.Alerts.Support; using Febe.Tests.Engine.Support; using Febe.Tests.Support; namespace Febe.Tests.Desktop; /// /// Pieces of the shared composition () that both shells use: which events become alerts (spec /// §13.3, ), the disk watch and the library folder that change while Febe runs (desktop, /// spec §4.7). The Docker composition on top of it is proved by the Server tests (end-to-end with a real Chromium). /// [TestClass] public sealed class SharedCompositionTests { public TestContext TestContext { get; set; } = null!; [TestMethod] public void TheEventsOfSpec13_3BecomeAlertsAndNothingElse() { var at = new DateTimeOffset(2026, 10, 13, 9, 0, 0, TimeSpan.Zero); EventRecord Event(string code, string message = "testo") => new() { At = at, Severity = EventSeverity.Info, Code = code, Message = message }; var expected = new Dictionary { [EngineEventCodes.ChallengeStop] = AlertKind.Verification, [EngineEventCodes.DisconnectStop] = AlertKind.Disconnection, [EngineEventCodes.ThrottleStop] = AlertKind.ThrottleStop, [EngineEventCodes.ThrottleEnd] = AlertKind.ThrottleEnd, [EngineEventCodes.BackfillCompleted] = AlertKind.BackfillDone, [EngineEventCodes.FormatUnrecognized] = AlertKind.UnknownFormat, [EngineEventCodes.RecipeSuspended] = AlertKind.RecipeSuspended, ["disco-quasi-pieno"] = AlertKind.DiskLow, }; foreach (var (code, kind) in expected) { Assert.AreEqual(kind, AlertCatalog.KindOf(code), code); var alert = AlertCatalog.FromEvent(kind, Event(code), TimeZoneInfo.Utc); Assert.AreEqual(at, alert.At); Assert.IsFalse(string.IsNullOrWhiteSpace(alert.Title)); Assert.IsFalse(string.IsNullOrWhiteSpace(alert.Message)); } foreach (var code in new[] { EngineEventCodes.SessionEnded, EngineEventCodes.BudgetReached, EngineEventCodes.Paused, EngineEventCodes.Resumed, AlertEventCodes.DeliveryFailed, "ricetta.ProfileCheck", "collegamento.primo" }) { Assert.IsNull(AlertCatalog.KindOf(code), code); } var none = new AlertEvents(false, false, false, false, false, false); Assert.IsFalse(Enum.GetValues().Any(k => AlertCatalog.IsWanted(k, none))); Assert.IsTrue(Enum.GetValues().All(k => AlertCatalog.IsWanted(k, AlertEvents.All))); var noThrottle = AlertEvents.All with { Throttle = false }; Assert.IsFalse(AlertCatalog.IsWanted(AlertKind.ThrottleStop, noThrottle)); Assert.IsFalse(AlertCatalog.IsWanted(AlertKind.ThrottleEnd, noThrottle)); } [TestMethod] public async Task TheDiskWatchFollowsALibraryFolderChangedWhileFebeRuns() { await using var engine = await EngineTestBed.CreateAsync(); var probe = new FakeFreeSpaceProbe(); var library = "/prova/libreria"; using var alerts = new AlertService( engine.Db.Settings, engine.Clock, engine.Services.Events, [], new AlertServiceOptions { FolderSource = () => [WatchedFolder.Library(library), WatchedFolder.Data("/prova/dati")], FreeSpace = probe }); // Impostazioni ▸ Generali ▸ «Cambia…» moves the library to an almost full disk: the next check sees it. probe.Set("/disco-piccolo/libreria", FakeFreeSpaceProbe.GB, 100 * FakeFreeSpaceProbe.GB); Assert.IsEmpty((await alerts.CheckDiskSpaceAsync(TestContext.CancellationToken)).Low); library = "/disco-piccolo/libreria"; var check = await alerts.CheckDiskSpaceAsync(TestContext.CancellationToken); Assert.AreEqual("libreria", check.Key); Assert.AreEqual(library, check.Low.Single().Folder.Path); } [TestMethod] public async Task AChangedLibraryFolderTakesTheNextFilesWithoutMovingAnything() { using var temp = new TempFolder("cartella-libreria"); await using var library = await TestLibrary.CreateAsync(temp); var account = await library.Db.Accounts.AddAsync(Make.Account("marta.demo", "100")); var before = await library.SaveAsync(account, Make.Entry(account.Id, "1", Make.T0), [TestFile.Photo(1, 31)]); var oldFile = library.Layout.FullPath(account, before.Media[0].RelativePath!); Assert.IsTrue(File.Exists(oldFile)); // Impostazioni ▸ Generali ▸ «Cambia…» (desktop): the account with files keeps its folder, as the API does. var newRoot = temp.Combine("nuova libreria"); await library.Db.Accounts.UpdateAsync(account with { Folder = library.Layout.AccountFolder(account) }, TestContext.CancellationToken); var other = await library.Db.Accounts.AddAsync(Make.Account("luca.demo", "200")); library.Ingest.UseLayout(new LibraryLayout(newRoot)); Assert.AreEqual(newRoot, library.Ingest.Layout.Root); var fixedAccount = (await library.Db.Accounts.GetAsync(account.Id, TestContext.CancellationToken))!; var again = await library.SaveAsync(fixedAccount, Make.Entry(account.Id, "2", Make.T0.AddDays(1)), [TestFile.Photo(1, 32)]); var fresh = await library.SaveAsync(other, Make.Entry(other.Id, "3", Make.T0.AddDays(2)), [TestFile.Photo(1, 33)]); Assert.IsTrue(File.Exists(oldFile), "nothing moves (rule 12)"); StringAssert.StartsWith(new LibraryLayout(newRoot).FullPath(fixedAccount, again.Media[0].RelativePath!), library.Layout.AccountFolder(account), "an account with files stays where it is"); Assert.IsTrue(File.Exists(Path.Combine(newRoot, "luca.demo", fresh.Media[0].RelativePath!.Replace('/', Path.DirectorySeparatorChar))), "a new account goes to the new folder"); } }