Files
Khaemhat/tests/Khaemhat.Application.Tests/DashboardTests.cs
T
Alby96andClaude Opus 5.5 acb7e598d1 feat: widget dashboard and company settings (#30, #31)
The home page becomes the widget dashboard of mockups 004-006. Each user
switches widgets on and off, orders them (drag and drop or buttons that
also work from the keyboard), sizes them on the 12-column grid and picks
the start page; every change is saved at once. Whoever may change the
company settings sets a default layout per role, used by people who
never customized; Reset goes back to it.

Todo, Done and Deadlines collect lines from sources each module
registers, so later milestones fill them without touching the widgets;
a failing source is logged and skipped. M3 provides licence, unread
warnings and scheduled task runs. Notes, Shortcuts and System status
work now; widgets of modules still to come say which milestone brings
their data instead of showing fake numbers.

Role defaults live in the new company_settings table, written through
CompanySettingsService with company.settings.write (ADR 0022).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 19:15:06 +01:00

135 lines
6.0 KiB
C#

using Khaemhat.Application.Dashboard;
using Khaemhat.Application.Security;
using Khaemhat.Core.Security;
using Microsoft.Extensions.Logging.Abstractions;
namespace Khaemhat.Application.Tests;
/// <summary>The widget dashboard (SPEC §10.3, issue #30): the saved layout and the lists the widgets read.</summary>
[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<TodoAggregator>.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<DeadlineAggregator>.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<DoneAggregator>.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<IReadOnlyList<FeedItem>> GetAsync(FeedContext context, CancellationToken cancellationToken) =>
Task.FromResult<IReadOnlyList<FeedItem>>(items);
public Task<IReadOnlyList<FeedItem>> GetAsync(FeedContext context, DateOnly until, CancellationToken cancellationToken) =>
Task.FromResult<IReadOnlyList<FeedItem>>(items);
}
private sealed class FailingSource : ITodoSource
{
public Task<IReadOnlyList<FeedItem>> 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;
}
}