Tau.Acuvim/portal/tests/Tau.Acuvim.Portal.Tests/TariffValidatorTests.cs
Diseri Pearson e17921a122 Add portal: customer-facing white-labeled monitoring stack
New top-level portal/ project, peer to console/ and firmware/. Delivers a
.NET 10 + React 18 + TimescaleDB + Grafana stack, one container set per
customer behind Traefik. Built in 12 phases per FrontEndPrompt spec; no
changes to existing console or firmware.

Backend (src/Tau.Acuvim.Portal/):
- .NET 10 minimal API, Serilog, ASP.NET Identity (cookie auth, lockout).
- Single AppDbContext with identity / app / monitoring schemas.
- MigrateAsync + TimescaleBootstrapper (idempotent hypertable creation)
  + IdentityBootstrapper (seeded admin + branding) on startup.
- Pure CostCalculator + DB-backed RateService for tariffs (effective-dated,
  TOU periods, VAT, fixed charges, per-municipality timezone).
- BrandingService with logo upload to mounted volume.
- Time-series ingest + bucketed query services (time_bucket aggregates,
  ON CONFLICT for idempotent re-delivery).
- ConfigOverviewService with redaction-by-construction (passwords never in
  payload).
- DataProtection keys persisted to /data/keys volume for cookie survival
  across container restarts.

Frontend (frontend/):
- React 18 + TypeScript + Vite + Ant Design 5 + TanStack Query.
- BrandingProvider + ThemedRoot for live re-themed white-labelling.
- RequireAuth / RequireRole guards.
- Pages: Login, Dashboard, Dashboards (embedded Grafana), Sites (admin),
  Settings tabs (Branding / Rates / Users / Grafana / App config).

Infra:
- Dev (docker-compose.yml) and prod (docker-compose.prod.yml) compose
  files. Three services per customer; Traefik subdomain + same-origin
  /grafana path-prefix routing wired with labels.
- Grafana 11 with provisioned timescaledb datasource (uid pinned) and
  starter power-overview.json dashboard with device template variable.
- Compose project name documented as lowercase (Compose v2 requirement).

Tests (tests/Tau.Acuvim.Portal.Tests/):
- xUnit, 40 tests. Covers CostCalculator (period match, TZ, overlap,
  VAT, fixed), ConnectionStringResolver (all 4 precedence branches incl.
  Production refusal), TariffValidator, DayOfWeekFlag.
- All passing locally against .NET 10.

Docs:
- README.md (onboarding + 11 spec sections), OPERATIONS.md (per-customer
  provisioning, secret rotation, backup, troubleshooting), TESTING.md
  (manual integration scenarios, frontend test scaffolding recipe).

Production safety guards:
- Refuses to start if Authentication:DefaultAdminPassword is unchanged
  default in Production.
- Refuses to start if Database:AutoProvisionLocalTimescaleDb=true in
  Production.
- Prod Grafana ships with anonymous off and auth mode unset (three
  options documented in README Security) so iframe refuses to load
  until a deliberate prod auth choice is made.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-18 09:30:30 +02:00

110 lines
3.2 KiB
C#

using Tau.Acuvim.Portal.DTOs;
using Tau.Acuvim.Portal.Services;
namespace Tau.Acuvim.Portal.Tests;
public class TariffValidatorTests
{
private static UpsertTariffRequest Valid(params TariffPeriodDto[] periods) => new(
Name: "Domestic 2026",
EffectiveFrom: new DateOnly(2026, 1, 1),
EffectiveTo: null,
DefaultRatePerKwh: 2.50m,
FixedMonthlyCharge: 100m,
VatPercentage: 15m,
IsActive: true,
Periods: periods);
[Fact]
public void Valid_NoPeriods_ReturnsNull()
{
Assert.Null(TariffValidator.Validate(Valid()));
}
[Fact]
public void Valid_WithGoodPeriod_ReturnsNull()
{
var period = new TariffPeriodDto("Peak", DaysOfWeek: 31, StartTime: "17:00", EndTime: "20:00", RatePerKwh: 3.50m);
Assert.Null(TariffValidator.Validate(Valid(period)));
}
[Fact]
public void EmptyName_Errors()
{
var req = Valid() with { Name = " " };
Assert.Contains("Tariff name is required", TariffValidator.Validate(req));
}
[Fact]
public void EffectiveTo_BeforeFrom_Errors()
{
var req = Valid() with { EffectiveTo = new DateOnly(2025, 12, 31) };
Assert.Contains("EffectiveTo must be on or after", TariffValidator.Validate(req));
}
[Fact]
public void NegativeRate_Errors()
{
var req = Valid() with { DefaultRatePerKwh = -0.01m };
Assert.Contains("DefaultRatePerKwh", TariffValidator.Validate(req));
}
[Fact]
public void NegativeFixedCharge_Errors()
{
var req = Valid() with { FixedMonthlyCharge = -1m };
Assert.Contains("FixedMonthlyCharge", TariffValidator.Validate(req));
}
[Theory]
[InlineData(-0.01)]
[InlineData(100.01)]
public void VatOutOfRange_Errors(double vat)
{
var req = Valid() with { VatPercentage = (decimal)vat };
Assert.Contains("VatPercentage", TariffValidator.Validate(req));
}
[Fact]
public void PeriodEmptyName_Errors()
{
var bad = new TariffPeriodDto("", 31, "00:00", "08:00", 1m);
Assert.Contains("Period name is required", TariffValidator.Validate(Valid(bad)));
}
[Fact]
public void PeriodZeroDays_Errors()
{
var bad = new TariffPeriodDto("Peak", 0, "17:00", "20:00", 3m);
Assert.Contains("at least one day", TariffValidator.Validate(Valid(bad)));
}
[Fact]
public void PeriodMidnightWrap_Errors()
{
var bad = new TariffPeriodDto("Overnight", 31, "22:00", "06:00", 1m);
Assert.Contains("no midnight wrap", TariffValidator.Validate(Valid(bad)));
}
[Fact]
public void PeriodEqualStartEnd_Errors()
{
var bad = new TariffPeriodDto("Zero", 31, "10:00", "10:00", 1m);
Assert.Contains("StartTime must be before EndTime", TariffValidator.Validate(Valid(bad)));
}
[Fact]
public void PeriodNegativeRate_Errors()
{
var bad = new TariffPeriodDto("Peak", 31, "17:00", "20:00", -0.1m);
Assert.Contains("RatePerKwh", TariffValidator.Validate(Valid(bad)));
}
[Fact]
public void PeriodBadTimeFormat_Errors()
{
var bad = new TariffPeriodDto("Peak", 31, "25:00", "20:00", 3m);
Assert.Contains("invalid times", TariffValidator.Validate(Valid(bad)));
}
}