Files
Febe/tests/Febe.Tests/Desktop/SharedCompositionTests.cs
T

87 lines
4.6 KiB
C#

using Febe.App;
using Febe.Core.Alerts;
using Febe.Core.Domain;
using Febe.Core.Engine;
using Febe.Core.Library;
using Febe.Tests.Support;
namespace Febe.Tests.Desktop;
/// <summary>
/// Pieces of the shared composition (<see cref="FebeApp"/>) that both shells use: the alerts hook (spec §13.3) and the
/// library folder that changes 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), unchanged.
/// </summary>
[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<string, AlertKind>
{
[EngineEventCodes.ChallengeStop] = AlertKind.Verification,
[EngineEventCodes.DisconnectStop] = AlertKind.Disconnection,
[EngineEventCodes.ThrottleStop] = AlertKind.Throttle,
[EngineEventCodes.ThrottleEnd] = AlertKind.Throttle,
[EngineEventCodes.Resumed] = AlertKind.Throttle,
[EngineEventCodes.BackfillCompleted] = AlertKind.BackfillDone,
[EngineEventCodes.FormatUnrecognized] = AlertKind.UnknownFormat,
[AlertRules.DiskLowCode] = AlertKind.DiskLow,
};
foreach (var (code, kind) in expected)
{
var alert = AlertRules.FromEvent(Event(code));
Assert.IsNotNull(alert, code);
Assert.AreEqual(kind, alert.Kind, code);
Assert.AreEqual(at, alert.At);
Assert.IsFalse(string.IsNullOrWhiteSpace(alert.Title));
}
foreach (var code in new[] { EngineEventCodes.SessionEnded, EngineEventCodes.BudgetReached, EngineEventCodes.Paused, "ricetta.ProfileCheck", "collegamento.primo" })
{
Assert.IsNull(AlertRules.FromEvent(Event(code)), code);
}
// A long line (the key structure of an unknown format) is cut for a notification.
var long_ = AlertRules.FromEvent(Event(EngineEventCodes.FormatUnrecognized, new string('k', 1000)))!;
Assert.IsLessThanOrEqualTo(240, long_.Message.Length);
var none = new AlertEvents(false, false, false, false, false, false);
Assert.IsFalse(Enum.GetValues<AlertKind>().Any(k => AlertRules.Wanted(k, none)));
Assert.IsTrue(Enum.GetValues<AlertKind>().All(k => AlertRules.Wanted(k, AlertEvents.All)));
Assert.IsFalse(AlertRules.Wanted(AlertKind.Throttle, AlertEvents.All with { Throttle = false }));
}
[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");
}
}