using Khaemhat.Application.Dashboard; using Khaemhat.Application.Security; using Khaemhat.Core.Security; using Microsoft.Extensions.Logging.Abstractions; namespace Khaemhat.Application.Tests; /// The widget dashboard (SPEC ยง10.3, issue #30): the saved layout and the lists the widgets read. [TestClass] public sealed class DashboardTests { private static readonly Actor User = new("mario", UserLevel.Standard, null, 1, "demo"); private static readonly DateTimeOffset Now = new(2026, 11, 11, 10, 0, 0, TimeSpan.Zero); [TestMethod] public void Layout_RoundTrips_AndDropsWhatItCannotUse() { var layout = new DashboardLayout([new("todo", 4), new("notes", 6), new("kpi", 12)]); var read = DashboardLayout.Parse(layout.ToJson()); Assert.IsNotNull(read); Assert.IsTrue(layout.SameAs(read)); // Unknown and repeated widgets go, odd widths snap to the nearest allowed one (the wider on a tie), a missing width takes the default. var messy = DashboardLayout.Parse("""{"v":1,"widgets":[{"id":"todo","w":5},{"id":"nope","w":4},{"id":"todo","w":4},{"id":"notes"}]}"""); Assert.IsNotNull(messy); CollectionAssert.AreEqual(new[] { new WidgetPlacement("todo", 6), new WidgetPlacement("notes", 4) }, messy.Widgets.ToList()); Assert.IsNull(DashboardLayout.Parse(null)); Assert.IsNull(DashboardLayout.Parse("not json")); Assert.IsNull(DashboardLayout.Parse("""{"widgets":"x"}""")); } [TestMethod] public void Layout_Toggles_Moves_AndResizes() { var layout = new DashboardLayout([new("todo", 4), new("done", 4), new("notes", 4)]); var shown = layout.Toggle("shortcuts", true); Assert.AreEqual("shortcuts", shown.Widgets[^1].Id); Assert.AreEqual(WidgetCatalog.Find("shortcuts")!.DefaultWidth, shown.Widgets[^1].Width); Assert.AreSame(layout, layout.Toggle("todo", true), "Already shown."); Assert.AreSame(layout, layout.Toggle("nope", true), "Not a widget."); Assert.IsFalse(layout.Toggle("done", false).Contains("done")); CollectionAssert.AreEqual(new[] { "done", "todo", "notes" }, layout.Move("done", -1).Widgets.Select(w => w.Id).ToList()); CollectionAssert.AreEqual(new[] { "todo", "done", "notes" }, layout.Move("todo", -1).Widgets.Select(w => w.Id).ToList(), "Already first."); CollectionAssert.AreEqual(new[] { "notes", "todo", "done" }, layout.MoveTo("notes", 0).Widgets.Select(w => w.Id).ToList(), "Dropped on the first."); Assert.AreEqual(8, layout.Resize("todo", 7).WidthOf("todo")); Assert.AreEqual(6, WidgetDefinition.Step(4, 1)); Assert.AreEqual(3, WidgetDefinition.Step(3, -1)); Assert.AreEqual(12, WidgetDefinition.Step(12, 1)); } [TestMethod] public void Catalog_BuiltInLayout_UsesOnlyWidgetsWithData() { Assert.IsTrue(WidgetCatalog.BuiltIn.Widgets.All(w => WidgetCatalog.Find(w.Id) is { Milestone: null })); Assert.AreEqual(WidgetCatalog.All.Count, WidgetCatalog.All.Select(w => w.Id).Distinct().Count()); Assert.IsTrue(WidgetCatalog.All.All(w => WidgetDefinition.Widths.Contains(w.DefaultWidth))); } [TestMethod] public async Task Todo_LeavesOutAFailingSource_AndPutsTheUrgentFirst() { var aggregator = new TodoAggregator( [ new Source(new FeedItem("a", "Normale", "x") { When = Now.AddHours(-2) }), new FailingSource(), new Source(new FeedItem("b", "Urgente", "x", FeedTone.Error) { Priority = 10 }), ], new FixedClock(Now), NullLogger.Instance); var items = await aggregator.GetAsync(User); CollectionAssert.AreEqual(new[] { "b", "a" }, items.Select(i => i.Id).ToList()); } [TestMethod] public async Task Deadlines_KeepTheNext30Days_NearestFirst() { var aggregator = new DeadlineAggregator( [ new Source( new FeedItem("later", "Fra 20 giorni", "x") { When = Now.AddDays(20) }, new FeedItem("past", "Ieri", "x") { When = Now.AddDays(-1) }, new FeedItem("far", "Fra 40 giorni", "x") { When = Now.AddDays(40) }, new FeedItem("today", "Oggi", "x") { When = Now }, new FeedItem("undated", "Senza data", "x")), ], new FixedClock(Now), NullLogger.Instance); var items = await aggregator.GetAsync(User); CollectionAssert.AreEqual(new[] { "today", "later" }, items.Select(i => i.Id).ToList()); } [TestMethod] public async Task Done_LatestFirst() { var aggregator = new DoneAggregator( [new Source(new FeedItem("early", "Alle 8", "x") { When = Now.AddHours(-2) }, new FeedItem("late", "Alle 9", "x") { When = Now.AddHours(-1) })], new FixedClock(Now), NullLogger.Instance); CollectionAssert.AreEqual(new[] { "late", "early" }, (await aggregator.GetAsync(User)).Select(i => i.Id).ToList()); } private sealed class Source(params FeedItem[] items) : ITodoSource, IDoneSource, IDeadlineSource { public Task> GetAsync(FeedContext context, CancellationToken cancellationToken) => Task.FromResult>(items); public Task> GetAsync(FeedContext context, DateOnly until, CancellationToken cancellationToken) => Task.FromResult>(items); } private sealed class FailingSource : ITodoSource { public Task> GetAsync(FeedContext context, CancellationToken cancellationToken) => throw new InvalidOperationException("The module is down."); } private sealed class FixedClock(DateTimeOffset now) : TimeProvider { public override DateTimeOffset GetUtcNow() => now; public override TimeZoneInfo LocalTimeZone => TimeZoneInfo.Utc; } }