## Summary Step 3 of the analytics plan: three more views on the **Analyse** page, plus a **fix for a bug in `main`** where changing the year filter stopped the page from updating. ### Fix: year filter navigation loop Choosing a year in "Von" or "Bis" started an endless navigation loop: one click caused about 2,700 navigations in 3 seconds. The page never updated, and because the browser then throttled navigation, the Monat / Quartal / Jahr buttons stopped working too. **Cause:** `FluentSelect` reports its selected value again on every re-render. The handler compared it with the year on the page, which only changes once a navigation has finished, so every re-render started another navigation to the same URL and cancelled the previous one. **Fix:** `OnFromChanged` / `OnToChanged` take over the chosen year immediately, so a repeated report of the same value does nothing. The bug came in with #4. ### New views - **Zusammensetzung (treemap):** the children of the current level as areas, with their own children nested inside (e.g. subclass → accounts, account → booking texts). The colours are the same as in the bar chart and list. Clicking an area drills into it; clicking a nested tile goes straight to that deeper level. Refunds (negative amounts) are left out, since an area can't be negative. - **Jahresvergleich:** one line per year over Jan–Dez, switchable between *Monatlich* and *Kumuliert* (default). The latest year is a thick blue line, the year before orange, and older years fading grey. The running year stops at the last month with data. - **Jahre im Vergleich:** categories × years, with a total column and a total row. - Cells are shaded by **monthly average**, so the running year compares fairly. - Each cell shows the change versus the previous year over the **same months**: the running year is marked `2026*` with a footnote. - Rows drill down when clicked, and zero cells show "–". - This one view replaces both the heatmap and the table from the plan, which would have shown the same numbers twice. ### Other changes - **Chart.js has no sunburst**, so the composition view is a treemap. It uses `chartjs-chart-treemap` **4.2.2** (MIT, by a Chart.js maintainer), vendored in `wwwroot/lib/chartjs-chart-treemap` with its licence and checked against the npm registry checksum. It is loaded only when the treemap is first used. - **`ExpenseAnalysis`** also returns the year profiles, the category × year table and the two-level composition. - **`StackedBarChart` became a generic `ChartJsChart`**: `Kind` = stackedBar / line / treemap, and new `Data` updates the chart in place. `charts.js` has one `createChart` entry point. - **Click hints only appear when something can be drilled into.** At the account level they were shown before even though booking texts can't be opened (already the case in #4). The treemap only shows a hand cursor over clickable tiles. ## Test plan - [x] `dotnet build`: no warnings - [x] `dotnet test`: 81/81 pass, including 6 new `ExpenseAnalysisTestShould` tests (year profiles with empty future months, category × year values and same-months change, no previous year, nested composition, single-level composition for an account, negative amounts left out) - [x] On a copy of the real data in headless Chrome: all three new charts render without console errors; table year totals match the verified per-year totals (2022: 190'724 … 2026: 77'006); the treemap for 2022 adds up to that year's total - [x] Interactions: treemap area and nested-tile clicks, table row links, Monatlich/Kumuliert switch - [x] Year filter fix: Von 2025 → Jahr → Bis 2025 → Quartal → Alle Jahre → back make exactly one navigation each, and charts, tiles and table update every time - [ ] Narrow-screen layout checked by hand 🤖 Generated with [Claude Code](https://claude.com/claude-code) Reviewed-on: #5 Co-authored-by: Claudio Schaad <c.schaad@pog.ch> Co-committed-by: Claudio Schaad <c.schaad@pog.ch>
350 lines
13 KiB
C#
350 lines
13 KiB
C#
using Schaad.Accounting.Datasets.Analytics;
|
||
using Schaad.Accounting.Models;
|
||
using Schaad.Accounting.Services;
|
||
using Shouldly;
|
||
|
||
namespace Schaad.Accounting.Tests;
|
||
|
||
public class ExpenseAnalysisTestShould
|
||
{
|
||
private static readonly Account Bank = new() { Id = "bank", Number = 1020, Name = "Bank" };
|
||
private static readonly Account Salary = new() { Id = "salary", Number = 3000, Name = "Lohn" };
|
||
private static readonly Account Food = new() { Id = "food", Number = 4000, Name = "Lebensmittel" };
|
||
private static readonly Account Restaurant = new() { Id = "restaurant", Number = 4010, Name = "Restaurant" };
|
||
private static readonly Account Rent = new() { Id = "rent", Number = 4100, Name = "Miete" };
|
||
|
||
private static readonly SubClass[] SubClasses =
|
||
[
|
||
new() { Number = 40, Name = "Verpflegung" },
|
||
new() { Number = 41, Name = "Wohnen" }
|
||
];
|
||
|
||
private static Transaction Booking(string date, Account from, Account to, decimal value, string text = "Text") => new()
|
||
{
|
||
ValueDate = DateTime.Parse(date),
|
||
OriginAccountId = from.Id,
|
||
TargetAccountId = to.Id,
|
||
Value = value,
|
||
Text = text
|
||
};
|
||
|
||
private static YearData Year(int year, params Transaction[] transactions)
|
||
=> new(year, [Bank, Salary, Food, Restaurant, Rent], SubClasses, transactions);
|
||
|
||
private static AnalyticsQuery Query(int from, int to, AnalyticsNode? node = null, AnalyticsPeriod period = AnalyticsPeriod.Month)
|
||
=> new(from, to, period, node ?? AnalyticsNode.All);
|
||
|
||
[Fact]
|
||
public void SumExpensesPerSubClassWhenQueryingAll()
|
||
{
|
||
var sut = new ExpenseAnalysis([Year(2025,
|
||
Booking("2025-01-10", Bank, Food, 100),
|
||
Booking("2025-01-20", Bank, Restaurant, 50),
|
||
Booking("2025-02-01", Bank, Rent, 1000))]);
|
||
|
||
var result = sut.Query(Query(2025, 2025));
|
||
|
||
result.Total.ShouldBe(1150);
|
||
result.Categories.Select(c => (c.Name, c.Total)).ShouldBe([("Wohnen", 1000m), ("Verpflegung", 150m)]);
|
||
result.Categories[0].Node.ShouldBe(new AnalyticsNode(SubClass: 41));
|
||
result.Categories[0].Share.ShouldBe(1000m / 1150m);
|
||
}
|
||
|
||
[Fact]
|
||
public void SubtractRefundsWhenSummingExpenses()
|
||
{
|
||
var sut = new ExpenseAnalysis([Year(2025,
|
||
Booking("2025-01-10", Bank, Food, 100),
|
||
Booking("2025-01-15", Food, Bank, 30))]);
|
||
|
||
var result = sut.Query(Query(2025, 2025));
|
||
|
||
result.Total.ShouldBe(70);
|
||
}
|
||
|
||
[Fact]
|
||
public void IgnoreBookingsWithoutExpenseAccountWhenQuerying()
|
||
{
|
||
var sut = new ExpenseAnalysis([Year(2025,
|
||
Booking("2025-01-25", Salary, Bank, 5000),
|
||
Booking("2025-01-26", Bank, Food, 100))]);
|
||
|
||
var result = sut.Query(Query(2025, 2025));
|
||
|
||
result.Total.ShouldBe(100);
|
||
}
|
||
|
||
[Fact]
|
||
public void CancelTransfersWithinSubClassWhenQueryingAll()
|
||
{
|
||
// Reclassifying 20 from food to restaurant changes neither the total nor the subclass.
|
||
var sut = new ExpenseAnalysis([Year(2025,
|
||
Booking("2025-01-10", Bank, Food, 100),
|
||
Booking("2025-01-11", Food, Restaurant, 20))]);
|
||
|
||
var all = sut.Query(Query(2025, 2025));
|
||
var subClass = sut.Query(Query(2025, 2025, new AnalyticsNode(SubClass: 40)));
|
||
|
||
all.Total.ShouldBe(100);
|
||
all.Categories.Single().Total.ShouldBe(100);
|
||
subClass.Categories.Select(c => (c.Name, c.Total)).ShouldBe([("Lebensmittel", 80m), ("Restaurant", 20m)]);
|
||
}
|
||
|
||
[Fact]
|
||
public void GroupAccountsByNumberWithLatestNameWhenAccountChangedOverYears()
|
||
{
|
||
var oldRent = new Account { Id = "rent-old-id", Number = 4100, Name = "Miete alt" };
|
||
var sut = new ExpenseAnalysis(
|
||
[
|
||
new YearData(2024, [Bank, oldRent], SubClasses, [Booking("2024-03-01", Bank, oldRent, 900)]),
|
||
Year(2025, Booking("2025-03-01", Bank, Rent, 1000))
|
||
]);
|
||
|
||
var result = sut.Query(Query(2024, 2025, new AnalyticsNode(SubClass: 41)));
|
||
|
||
result.Categories.Single().Name.ShouldBe("Miete");
|
||
result.Categories.Single().Total.ShouldBe(1900);
|
||
}
|
||
|
||
[Fact]
|
||
public void ShowBookingTextGroupsWhenDrillingIntoAccount()
|
||
{
|
||
var sut = new ExpenseAnalysis([Year(2025,
|
||
Booking("2025-01-10", Bank, Food, 100, "Migros Zürich"),
|
||
Booking("2025-01-11", Bank, Food, 40, "Migros Bern"),
|
||
Booking("2025-01-12", Bank, Food, 30, "Coop"))]);
|
||
|
||
var result = sut.Query(Query(2025, 2025, new AnalyticsNode(SubClass: 40, Account: 4000)));
|
||
|
||
result.Categories.Select(c => (c.Name, c.Total)).ShouldBe([("Migros", 140m), ("Coop", 30m)]);
|
||
result.Categories[0].Node.ShouldBe(new AnalyticsNode(40, 4000, "Migros"));
|
||
result.Categories.ShouldAllBe(c => c.CanDrillDown == false);
|
||
}
|
||
|
||
[Fact]
|
||
public void ReturnItselfAsOnlyCategoryWhenQueryingBookingTextGroup()
|
||
{
|
||
var node = new AnalyticsNode(40, 4000, "Migros");
|
||
var sut = new ExpenseAnalysis([Year(2025,
|
||
Booking("2025-01-10", Bank, Food, 100, "Migros"),
|
||
Booking("2025-01-12", Bank, Food, 30, "Coop"))]);
|
||
|
||
var result = sut.Query(Query(2025, 2025, node));
|
||
|
||
result.Categories.Single().Node.ShouldBe(node);
|
||
result.Total.ShouldBe(100);
|
||
}
|
||
|
||
[Fact]
|
||
public void BuildBreadcrumbWithNamesWhenQueryingBookingTextGroup()
|
||
{
|
||
var sut = new ExpenseAnalysis([Year(2025, Booking("2025-01-10", Bank, Food, 100, "Migros"))]);
|
||
|
||
var result = sut.Query(Query(2025, 2025, new AnalyticsNode(40, 4000, "Migros")));
|
||
|
||
result.Breadcrumb.Select(b => b.Name).ShouldBe(["Ausgaben", "Verpflegung", "Lebensmittel", "Migros"]);
|
||
result.Breadcrumb[2].Node.ShouldBe(new AnalyticsNode(40, 4000));
|
||
}
|
||
|
||
[Fact]
|
||
public void CountBookingsInTheirBookYearWhenDatedOutsideIt()
|
||
{
|
||
var sut = new ExpenseAnalysis(
|
||
[
|
||
Year(2024, Booking("2023-12-29", Bank, Food, 10), Booking("2025-01-02", Bank, Food, 20)),
|
||
Year(2025, Booking("2025-03-10", Bank, Food, 30))
|
||
]);
|
||
|
||
var result = sut.Query(Query(2024, 2025, period: AnalyticsPeriod.Month));
|
||
|
||
result.Total.ShouldBe(60);
|
||
var values = result.Categories.Single().Values;
|
||
values[0].ShouldBe(10); // Jan 2024
|
||
values[11].ShouldBe(20); // Dec 2024
|
||
values[14].ShouldBe(30); // Mar 2025
|
||
}
|
||
|
||
[Fact]
|
||
public void StopAtLastMonthWithDataWhenRangeIncludesRunningYear()
|
||
{
|
||
var sut = new ExpenseAnalysis([Year(2026,
|
||
Booking("2026-01-10", Bank, Food, 100),
|
||
Booking("2026-03-10", Bank, Food, 200))]);
|
||
|
||
var result = sut.Query(Query(2026, 2026));
|
||
|
||
result.Periods.ShouldBe([new DateOnly(2026, 1, 1), new DateOnly(2026, 2, 1), new DateOnly(2026, 3, 1)]);
|
||
result.Categories.Single().Values.ShouldBe([100m, 0m, 200m]);
|
||
result.Months.ShouldBe(3);
|
||
result.AveragePerMonth.ShouldBe(100);
|
||
}
|
||
|
||
[Fact]
|
||
public void SumIntoQuartersAndYearsWhenPeriodIsCoarser()
|
||
{
|
||
var sut = new ExpenseAnalysis(
|
||
[
|
||
Year(2024, Booking("2024-02-10", Bank, Food, 10), Booking("2024-11-10", Bank, Food, 20)),
|
||
Year(2025, Booking("2025-05-10", Bank, Food, 30), Booking("2025-12-10", Bank, Food, 40))
|
||
]);
|
||
|
||
var quarters = sut.Query(Query(2024, 2025, period: AnalyticsPeriod.Quarter));
|
||
var years = sut.Query(Query(2024, 2025, period: AnalyticsPeriod.Year));
|
||
|
||
quarters.Periods.Count.ShouldBe(8);
|
||
quarters.Categories.Single().Values.ShouldBe([10m, 0m, 0m, 20m, 0m, 30m, 0m, 40m]);
|
||
years.Periods.ShouldBe([new DateOnly(2024, 1, 1), new DateOnly(2025, 1, 1)]);
|
||
years.Categories.Single().Values.ShouldBe([30m, 70m]);
|
||
}
|
||
|
||
[Fact]
|
||
public void LeaveOutYearsOutsideRangeWhenQuerying()
|
||
{
|
||
var sut = new ExpenseAnalysis(
|
||
[
|
||
Year(2024, Booking("2024-02-10", Bank, Food, 10)),
|
||
Year(2025, Booking("2025-02-10", Bank, Food, 30))
|
||
]);
|
||
|
||
var result = sut.Query(Query(2025, 2025));
|
||
|
||
result.Total.ShouldBe(30);
|
||
result.Months.ShouldBe(2);
|
||
}
|
||
|
||
[Fact]
|
||
public void CompareSameMonthsOfPreviousYearWhenYearIsRunning()
|
||
{
|
||
var sut = new ExpenseAnalysis(
|
||
[
|
||
Year(2025, Booking("2025-01-10", Bank, Food, 100), Booking("2025-09-10", Bank, Food, 500)),
|
||
Year(2026, Booking("2026-01-10", Bank, Food, 150), Booking("2026-02-10", Bank, Food, 50))
|
||
]);
|
||
|
||
// Range is 2026 only, but the comparison still looks at 2025.
|
||
var comparison = sut.Query(Query(2026, 2026)).Comparison;
|
||
|
||
comparison.ShouldNotBeNull();
|
||
comparison.Year.ShouldBe(2026);
|
||
comparison.Months.ShouldBe(2);
|
||
comparison.Current.ShouldBe(200);
|
||
comparison.Previous.ShouldBe(100);
|
||
comparison.ChangePercent.ShouldBe(100);
|
||
}
|
||
|
||
[Fact]
|
||
public void ReturnNoComparisonWhenPreviousYearHasNoData()
|
||
{
|
||
var sut = new ExpenseAnalysis([Year(2025, Booking("2025-01-10", Bank, Food, 100))]);
|
||
|
||
sut.Query(Query(2025, 2025)).Comparison.ShouldBeNull();
|
||
}
|
||
|
||
[Fact]
|
||
public void ReturnEmptyResultWhenThereIsNoData()
|
||
{
|
||
var sut = new ExpenseAnalysis([]);
|
||
|
||
var result = sut.Query(Query(2025, 2025));
|
||
|
||
sut.Years.ShouldBeEmpty();
|
||
result.Total.ShouldBe(0);
|
||
result.Categories.ShouldBeEmpty();
|
||
result.Periods.Count.ShouldBe(12);
|
||
}
|
||
|
||
[Fact]
|
||
public void LeaveMonthsWithoutDataEmptyWhenBuildingYearProfiles()
|
||
{
|
||
var sut = new ExpenseAnalysis(
|
||
[
|
||
Year(2025, Booking("2025-01-10", Bank, Food, 100), Booking("2025-12-10", Bank, Food, 50)),
|
||
Year(2026, Booking("2026-02-10", Bank, Food, 70))
|
||
]);
|
||
|
||
var profiles = sut.Query(Query(2025, 2026)).YearProfiles;
|
||
|
||
profiles.Select(p => p.Year).ShouldBe([2025, 2026]);
|
||
profiles[0].Months.Count.ShouldBe(12);
|
||
profiles[0].Months[0].ShouldBe(100);
|
||
profiles[0].Months[1].ShouldBe(0);
|
||
profiles[0].Total.ShouldBe(150);
|
||
profiles[1].Months[0].ShouldBe(0);
|
||
profiles[1].Months[1].ShouldBe(70);
|
||
profiles[1].Months[2].ShouldBeNull();
|
||
}
|
||
|
||
[Fact]
|
||
public void CompareSameMonthsWithPreviousYearWhenBuildingCategoryYears()
|
||
{
|
||
var sut = new ExpenseAnalysis(
|
||
[
|
||
Year(2024, Booking("2024-01-10", Bank, Food, 100), Booking("2024-06-10", Bank, Rent, 400)),
|
||
Year(2025, Booking("2025-01-10", Bank, Food, 150), Booking("2025-06-10", Bank, Rent, 500), Booking("2025-11-10", Bank, Rent, 999)),
|
||
Year(2026, Booking("2026-01-10", Bank, Food, 120), Booking("2026-02-10", Bank, Rent, 300))
|
||
]);
|
||
|
||
// Range starts in 2025, so 2025's change still compares with 2024.
|
||
var rows = sut.Query(Query(2025, 2026)).CategoryYears;
|
||
|
||
var food = rows.Single(r => r.Name == "Verpflegung");
|
||
food.Cells.Select(c => (c.Year, c.Value)).ShouldBe([(2025, 150m), (2026, 120m)]);
|
||
food.Cells[0].ChangePercent.ShouldBe(50);
|
||
food.Cells[1].ChangePercent.ShouldBe(-20); // Jan–Feb 2026 vs. Jan–Feb 2025
|
||
var rent = rows.Single(r => r.Name == "Wohnen");
|
||
rent.Cells[1].Value.ShouldBe(300);
|
||
rent.Cells[1].ChangePercent.ShouldBeNull(); // nothing in Jan–Feb 2025
|
||
rows.Select(r => r.Node).ShouldBe(sut.Query(Query(2025, 2026)).Categories.Select(c => c.Node));
|
||
}
|
||
|
||
[Fact]
|
||
public void ReturnNoChangeWhenYearBeforeHasNoData()
|
||
{
|
||
var sut = new ExpenseAnalysis([Year(2025, Booking("2025-01-10", Bank, Food, 100))]);
|
||
|
||
sut.Query(Query(2025, 2025)).CategoryYears.Single().Cells.Single().ChangePercent.ShouldBeNull();
|
||
}
|
||
|
||
[Fact]
|
||
public void NestAccountsInSubClassesWhenBuildingComposition()
|
||
{
|
||
var sut = new ExpenseAnalysis([Year(2025,
|
||
Booking("2025-01-10", Bank, Food, 100),
|
||
Booking("2025-01-11", Bank, Restaurant, 40),
|
||
Booking("2025-01-12", Bank, Rent, 1000))]);
|
||
|
||
var items = sut.Query(Query(2025, 2025)).Composition;
|
||
|
||
items.Select(i => (i.ParentName, i.ChildName, i.Value)).ShouldBe(
|
||
[
|
||
("Wohnen", "Miete", 1000m),
|
||
("Verpflegung", "Lebensmittel", 100m),
|
||
("Verpflegung", "Restaurant", 40m)
|
||
]);
|
||
items[1].Child.ShouldBe(new AnalyticsNode(40, 4000));
|
||
}
|
||
|
||
[Fact]
|
||
public void UseSingleLevelWhenBuildingCompositionForAccount()
|
||
{
|
||
var sut = new ExpenseAnalysis([Year(2025,
|
||
Booking("2025-01-10", Bank, Food, 100, "Migros"),
|
||
Booking("2025-01-11", Bank, Food, 30, "Coop"))]);
|
||
|
||
var items = sut.Query(Query(2025, 2025, new AnalyticsNode(40, 4000))).Composition;
|
||
|
||
items.Select(i => (i.ParentName, i.Child, i.Value)).ShouldBe([("Migros", (AnalyticsNode?)null, 100m), ("Coop", null, 30m)]);
|
||
}
|
||
|
||
[Fact]
|
||
public void LeaveOutNegativeAmountsWhenBuildingComposition()
|
||
{
|
||
var sut = new ExpenseAnalysis([Year(2025,
|
||
Booking("2025-01-10", Bank, Food, 100),
|
||
Booking("2025-01-11", Restaurant, Bank, 40))]);
|
||
|
||
var items = sut.Query(Query(2025, 2025, new AnalyticsNode(SubClass: 40))).Composition;
|
||
|
||
items.Single().ParentName.ShouldBe("Lebensmittel");
|
||
}
|
||
}
|