ci / build-test (push) Successful in 2m31s
The dashboards told several stories at once. Overview asked for full calendar years, meter detail for a fixed 12-month window that was really 13, Trends for 24 months with an Apply button, and the energy pages for 60. Each page derived "today" from UTC, so the first hours of a local day belonged to yesterday. A missing tariff, a month nobody measured and a genuine zero all rendered as 0. And a virtual meter -- the one thing the spreadsheet leans on hardest -- was excluded from analysis outright: MeterPeriodService returned null for it and the page offered a flow diagram instead. docs/DASHBOARD_ANALYSIS_CHANGE_BRIEF.md is the work order. Every choice it left open is settled in docs/ANALYSIS_IMPLEMENTATION_NOTE.md as D-01..D-58 plus amendments A-01..A-30; code, tests and release notes cite those ids. The analysis layer Core/Analysis holds the pure rules: period presets resolved once in the instance zone into a local date range and a half-open UTC range, bucket plans, calendar-unit comparisons, coverage runs with a resolution class, normalized quantities and units, the totals policy, the virtual formula parser/validator/evaluator, and the cost calculator. "Now" comes from TimeProvider; services never read the clock. Normalization now writes, in the same transaction as consumption and by diff, per-meter rollups by local day and month plus coverage runs and a rollup state (AnalysisDataWriter). AnalysisReader answers a request from those tables -- month rollups for month and year buckets, day rollups otherwise, at most two partial edge days from consumption -- and CostReader prices the result month by month. Pages, /api/v1 and the CSV export read nothing else. The unused continuous aggregates are dropped. The reader's statement count per request is constant whether it covers one meter or a thousand. On a synthetic 1,000-meter, ten-year instance the brief's target request (100 meters, ten years, monthly) takes 374 ms against a two-second target, and the Overview went from 48,244 SQL statements per load to 205. Missing is not zero Every bucket carries a status -- available, partial, missing, unresolved, invalid, pending -- derived from coverage, never from the amount, with provenance and a reason code beside it. A true zero is a number and a bar on the baseline; an unknown bucket is a gap that says why; a month whose data only exists monthly says so instead of inventing daily detail; a scope with no tariff says "not priced" instead of 0. Rows whose interval closes after now are reported separately rather than counted. Virtual meters are analysis subjects A virtual meter stores a canonical definition -- expression over m<id> references, result kind, unit and cost rule -- validated on save and on read for syntax, unknown or self references, loops and unit/kind rules. It is evaluated on read from its sources' rollups over their joint coverage: a missing source makes the bucket missing, an observed zero is a valid input, a non-finite result is invalid with its dependency path, and the page lists each source's contribution. Topology links are topology only and never rewrite a saved calculation; expression-less meters from older installs are converted once at startup. The editor has Sum, Difference and Advanced modes with a live preview. Totals and the bill Per energy type the totals policy separates use, grid import, export, generation and runtime, marks breakdown meters as breakdowns and virtual meters as views, and never adds across units. The bill follows it: grid import where there is one, separately priced subsections at their own price, feed-in only on export meters, standing charges once per scope per local day, manual costs once on their start day, categories as non-overlapping covers whose composition reconciles to the bill. The seeded demo's yearly totals now match the spreadsheet. Pages and navigation The period lives in the URL and every page reads the same contract, so a link, a reload and the browser's Back button keep it. Shared components carry it: page header with breadcrumbs, period toolbar, theme-aware chart with an accessible table beside it, metric cards, comparison and availability states, attention items that each link to the one action that fixes them. Meter detail leads with an Analysis tab and resolves its tabs by key; the energy page has Overview, History, Flow and Meters; the old cost-only Trends page is a general Analysis page over portfolio, type, category, meter or a meter comparison. Records tabs are paged server-side instead of showing the latest 200. Everything is English and German, light and dark, down to 360px. Some figures change on purpose; docs/RELEASE_NOTES.md lists each one and what the first start after the update does (it rebuilds all analysis data before the web server listens). docs/SDD.md and CLAUDE.md describe the system as it now is. Tests: 1,733 Core and 746 integration, all green, plus an opt-in performance suite with a synthetic 1,000-meter generator.
222 lines
11 KiB
C#
222 lines
11 KiB
C#
using System.Globalization;
|
||
using MeterVault.App;
|
||
using MeterVault.Core.Analysis;
|
||
|
||
namespace MeterVault.Integration.Tests.Localization;
|
||
|
||
/// <summary>
|
||
/// <see cref="Format"/> formats against the reader's culture rather than a fixed de-DE (SDD §12, M7).
|
||
/// </summary>
|
||
/// <remarks>
|
||
/// The importer's de-DE parsing is deliberately untouched by this: that dialect is a property of the
|
||
/// spreadsheet files, not of who is looking at the dashboard, and <c>GermanParsingTests</c> pins it.
|
||
/// </remarks>
|
||
public sealed class FormatCultureTests
|
||
{
|
||
private static readonly TimeZoneInfo Berlin = TimeZoneInfo.FindSystemTimeZoneById("Europe/Berlin");
|
||
|
||
[Fact]
|
||
public void Digit_grouping_follows_the_reader()
|
||
{
|
||
Assert.Equal("1.234,5", WithCulture("de", () => Format.Number(1234.5, 1)));
|
||
Assert.Equal("1,234.5", WithCulture("en", () => Format.Number(1234.5, 1)));
|
||
|
||
Assert.Equal("2.940", WithCulture("de", () => Format.Number(2940)));
|
||
Assert.Equal("2,940", WithCulture("en", () => Format.Number(2940)));
|
||
}
|
||
|
||
[Fact]
|
||
public void Money_is_in_the_configured_currency_written_the_readers_way()
|
||
{
|
||
// Deliberately rewritten (note §10): the symbol was a hard-coded euro; it is now the configured currency's
|
||
// (D-43). Only the grouping is localized — an English reader sees the same money written their way, not
|
||
// relabelled as dollars — and the placement stays "number, space, symbol".
|
||
Assert.Equal("1.234,50 €", WithCulture("de", () => Format.Money(1234.5, "EUR")));
|
||
Assert.Equal("1,234.50 €", WithCulture("en", () => Format.Money(1234.5, "EUR")));
|
||
Assert.Equal("1,234.50 $", WithCulture("en", () => Format.Money(1234.5, "USD")));
|
||
Assert.Equal("1.234,50 £", WithCulture("de", () => Format.Money(1234.5, "gbp")));
|
||
Assert.Equal("1.234,50 CHF", WithCulture("de", () => Format.Money(1234.5, "CHF")));
|
||
Assert.Equal("12.00 SEK", WithCulture("en", () => Format.Money(12, " sek ")));
|
||
Assert.Equal("12.00 €", WithCulture("en", () => Format.Money(12, null)));
|
||
Assert.Equal("—", Format.Money(null, "EUR"));
|
||
}
|
||
|
||
[Fact]
|
||
public void The_instance_currency_comes_from_the_settings()
|
||
{
|
||
var usd = new InstanceCurrency(Microsoft.Extensions.Options.Options.Create(new MeterVault.Infrastructure.Options.MeterVaultOptions { Currency = " USD " }));
|
||
Assert.Equal("USD", usd.Code);
|
||
Assert.Equal("$", usd.Symbol);
|
||
Assert.Equal("1,234.50 $", WithCulture("en", () => usd.Format(1234.5)));
|
||
Assert.Equal("—", usd.Format((double?)null));
|
||
|
||
var blank = new InstanceCurrency(Microsoft.Extensions.Options.Options.Create(new MeterVault.Infrastructure.Options.MeterVaultOptions { Currency = " " }));
|
||
Assert.Equal("EUR", blank.Code);
|
||
}
|
||
|
||
[Fact]
|
||
public void Signed_money_shows_its_sign_and_never_a_negative_zero()
|
||
{
|
||
Assert.Equal("+12,00 €", WithCulture("de", () => Format.MoneySigned(12, "EUR")));
|
||
Assert.Equal("-3,50 €", WithCulture("de", () => Format.MoneySigned(-3.5, "EUR")));
|
||
Assert.Equal("0,00 €", WithCulture("de", () => Format.MoneySigned(0.004, "EUR")));
|
||
Assert.Equal("0,00 €", WithCulture("de", () => Format.MoneySigned(-0.004, "EUR")));
|
||
Assert.Equal("0.00 €", WithCulture("en", () => Format.Money(-0.001, "EUR")));
|
||
}
|
||
|
||
[Fact]
|
||
public void Quantities_keep_their_unit_and_say_when_they_are_unknown()
|
||
{
|
||
Assert.Equal("—", Format.Quantity(null, "kWh"));
|
||
Assert.Equal("—", Format.Quantity(double.NaN, "kWh"));
|
||
Assert.Equal("1,235 kWh", WithCulture("en", () => Format.Quantity(1234.5, "kWh")));
|
||
Assert.Equal("5,3 m³", WithCulture("de", () => Format.Quantity(5.25, "m³")));
|
||
Assert.Equal("0.12 m³", WithCulture("en", () => Format.Quantity(0.123, "m³")));
|
||
Assert.Equal("0 kWh", WithCulture("en", () => Format.Quantity(0, "kWh")));
|
||
Assert.Equal("1,234.50 L", WithCulture("en", () => Format.Quantity(1234.5, "L", 2)));
|
||
Assert.Equal("14", WithCulture("en", () => Format.Quantity(14, null)));
|
||
}
|
||
|
||
[Fact]
|
||
public void Percentages_keep_their_explicit_sign()
|
||
{
|
||
Assert.Equal("+12,4 %", WithCulture("de", () => Format.Percent(12.4)));
|
||
Assert.Equal("+12.4 %", WithCulture("en", () => Format.Percent(12.4)));
|
||
|
||
Assert.Equal("-7,2 %", WithCulture("de", () => Format.Percent(-7.2)));
|
||
Assert.Equal("-7.2 %", WithCulture("en", () => Format.Percent(-7.2)));
|
||
}
|
||
|
||
[Fact]
|
||
public void A_change_always_states_the_difference_and_a_percentage_only_where_it_applies()
|
||
{
|
||
string Money(double v) => Format.Money(v, "EUR");
|
||
|
||
Assert.Equal("+20,00 € (+20,0 %)", WithBoth("de", () => Format.ChangeText(Change.Between(120, 100), Money)));
|
||
Assert.Equal("-25.00 € (-25.0 %)", WithBoth("en", () => Format.ChangeText(Change.Between(75, 100), Money)));
|
||
|
||
// No baseline, or a negative one: the difference stands, the percentage says it does not apply (D-08).
|
||
Assert.Equal("+50,00 € (keine Prozentangabe möglich)", WithBoth("de", () => Format.ChangeText(Change.Between(50, 0), Money)));
|
||
Assert.Equal("-2.00 € (percentage not applicable)", WithBoth("en", () => Format.ChangeText(Change.Between(-3, -1), Money)));
|
||
Assert.Equal("0.00 € (+0.0 %)", WithBoth("en", () => Format.ChangeText(Change.Between(100, 100), Money)));
|
||
|
||
// A missing value is no change at all, never "-100 %".
|
||
Assert.Equal("—", Format.ChangeText(Change.Between(null, 100), Money));
|
||
Assert.Equal("—", Format.ChangePercent(Change.Unavailable));
|
||
}
|
||
|
||
[Fact]
|
||
public void Month_labels_are_written_in_the_readers_language()
|
||
{
|
||
var march = new DateOnly(2025, 3, 1);
|
||
|
||
var german = WithCulture("de", () => Format.MonthLabel(march));
|
||
var english = WithCulture("en", () => Format.MonthLabel(march));
|
||
|
||
// Asserting the exact German abbreviation would pin us to one ICU version ("Mrz" vs "Mär"),
|
||
// so assert what actually matters: the label is culture-sensitive, not invariant.
|
||
Assert.Equal("Mar 25", english);
|
||
Assert.NotEqual(english, german);
|
||
Assert.EndsWith("25", german, StringComparison.Ordinal);
|
||
}
|
||
|
||
[Fact]
|
||
public void Dates_and_ranges_name_the_year_where_it_is_needed()
|
||
{
|
||
var aug = new DateOnly(2026, 8, 19);
|
||
var sep = new DateOnly(2026, 9, 19);
|
||
|
||
Assert.Equal("Sep 19, 2026", WithCulture("en", () => Format.Date(sep)));
|
||
Assert.Equal("Sep 19", WithCulture("en", () => Format.Date(sep, includeYear: false)));
|
||
Assert.Equal("Aug 19 – Sep 19, 2026", WithCulture("en", () => Format.DateRange(aug, sep)));
|
||
Assert.Equal("Dec 1, 2025 – Jan 31, 2026", WithCulture("en", () => Format.DateRange(new DateOnly(2025, 12, 1), new DateOnly(2026, 1, 31))));
|
||
Assert.Equal("Aug 19 – Sep 19", WithCulture("en", () => Format.DateRange(aug, sep, includeYear: false)));
|
||
Assert.Equal("Sep 19, 2026", WithCulture("en", () => Format.DateRange(sep, sep)));
|
||
|
||
// German day-first order, whatever the ICU month abbreviation.
|
||
var german = WithCulture("de", () => Format.Date(sep));
|
||
Assert.StartsWith("19.", german, StringComparison.Ordinal);
|
||
Assert.EndsWith("2026", german, StringComparison.Ordinal);
|
||
}
|
||
|
||
[Fact]
|
||
public void A_period_shows_the_dates_it_actually_covers()
|
||
{
|
||
var now = new DateTimeOffset(2026, 9, 19, 14, 37, 0, TimeSpan.FromHours(2));
|
||
|
||
var twelve = PeriodResolver.Resolve(PeriodPreset.Last12Months, null, null, now, Berlin);
|
||
Assert.Equal("Oct 1, 2025 – Sep 19, 2026", WithCulture("en", () => Format.PeriodRange(twelve)));
|
||
|
||
var none = PeriodResolver.Resolve(PeriodPreset.AllHistory, null, null, now, Berlin);
|
||
Assert.Equal("—", Format.PeriodRange(none));
|
||
}
|
||
|
||
[Fact]
|
||
public void Bucket_labels_carry_the_year_across_years_and_real_dates_for_partial_units()
|
||
{
|
||
static AnalysisBucket Bucket(BucketSize size, DateOnly first, DateOnly end, DateOnly? nominal = null) =>
|
||
new(first, end, new DateTimeOffset(first.ToDateTime(TimeOnly.MinValue), TimeSpan.Zero),
|
||
new DateTimeOffset(end.ToDateTime(TimeOnly.MinValue), TimeSpan.Zero), size, nominal);
|
||
|
||
var september = Bucket(BucketSize.Month, new DateOnly(2026, 9, 1), new DateOnly(2026, 10, 1));
|
||
Assert.Equal("Sep", WithCulture("en", () => Format.BucketLabel(september, includeYear: false)));
|
||
Assert.Equal("Sep 2026", WithCulture("en", () => Format.BucketLabel(september, includeYear: true)));
|
||
|
||
// The current month cut at now is still that month; a month clipped by a custom range shows its days.
|
||
var toDate = Bucket(BucketSize.Month, new DateOnly(2026, 9, 1), new DateOnly(2026, 9, 20), new DateOnly(2026, 10, 1));
|
||
Assert.Equal("Sep 2026", WithCulture("en", () => Format.BucketLabel(toDate, includeYear: true)));
|
||
var clipped = Bucket(BucketSize.Month, new DateOnly(2026, 1, 15), new DateOnly(2026, 2, 1));
|
||
Assert.Equal("Jan 15 – Jan 31, 2026", WithCulture("en", () => Format.BucketLabel(clipped, includeYear: true)));
|
||
|
||
// A week always shows its real first and last day — a partial week never looks whole.
|
||
var partialWeek = Bucket(BucketSize.Week, new DateOnly(2026, 9, 28), new DateOnly(2026, 10, 1));
|
||
Assert.Equal("Sep 28 – Sep 30", WithCulture("en", () => Format.BucketLabel(partialWeek, includeYear: false)));
|
||
var yearEndWeek = Bucket(BucketSize.Week, new DateOnly(2025, 12, 29), new DateOnly(2026, 1, 5));
|
||
Assert.Equal("Dec 29, 2025 – Jan 4, 2026", WithCulture("en", () => Format.BucketLabel(yearEndWeek, includeYear: true)));
|
||
|
||
Assert.Equal("Sep 19, 2026", WithCulture("en", () => Format.BucketLabel(Bucket(BucketSize.Day, new DateOnly(2026, 9, 19), new DateOnly(2026, 9, 20)), true)));
|
||
Assert.Equal("2025", WithCulture("en", () => Format.BucketLabel(Bucket(BucketSize.Year, new DateOnly(2025, 1, 1), new DateOnly(2026, 1, 1)), true)));
|
||
|
||
Assert.True(Format.SpansYears([yearEndWeek]));
|
||
Assert.False(Format.SpansYears([september, toDate]));
|
||
Assert.False(Format.SpansYears([]));
|
||
}
|
||
|
||
[Fact]
|
||
public void Direction_icons_are_language_neutral()
|
||
{
|
||
Assert.Equal("▲", Format.DirectionIcon(1));
|
||
Assert.Equal("▼", Format.DirectionIcon(-1));
|
||
Assert.Equal("—", Format.DirectionIcon(0));
|
||
}
|
||
|
||
private static T WithCulture<T>(string culture, Func<T> body)
|
||
{
|
||
var previous = CultureInfo.CurrentCulture;
|
||
try
|
||
{
|
||
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo(culture);
|
||
return body();
|
||
}
|
||
finally
|
||
{
|
||
CultureInfo.CurrentCulture = previous;
|
||
}
|
||
}
|
||
|
||
/// <summary>Formatting and wording in one language, as a request gets them from the localization middleware.</summary>
|
||
private static T WithBoth<T>(string culture, Func<T> body)
|
||
{
|
||
var previous = CultureInfo.CurrentUICulture;
|
||
try
|
||
{
|
||
CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo(culture);
|
||
return WithCulture(culture, body);
|
||
}
|
||
finally
|
||
{
|
||
CultureInfo.CurrentUICulture = previous;
|
||
}
|
||
}
|
||
}
|