using MeterVault.App; using MeterVault.Core.Domain; using MeterVault.Integration.Tests.Costing; using Microsoft.EntityFrameworkCore; namespace MeterVault.Integration.Tests.MeterPage; /// /// Brief §3.2 "Change the source/connector: Meter → Sources → Edit connection": the connector a source uses opens for /// editing from the meter's Sources tab, through the connector page's way back to the meter (the draft detour). /// [Collection("Timescale")] public sealed class MeterSourcesRenderTests(TimescaleFixture fx) { [Fact] public async Task The_sources_tab_links_the_connector_in_use_for_editing() { await using var box = new CostSandbox(fx); var type = await box.TypeAsync("m³"); var meter = await box.MeterAsync(type, MeterMode.CumulativeCounter, "m³", name: $"Wasser {Guid.NewGuid():N}"); int endpointId; int sourceId; await using (var db = fx.CreateContext()) { var endpoint = new IngestionEndpoint { Type = EndpointType.HomeAssistant, Name = $"HA {Guid.NewGuid():N}", Config = """{"baseUrl":"http://ha.local:8123","tokenEnv":"HA_TOKEN"}""", }; db.IngestionEndpoints.Add(endpoint); await db.SaveChangesAsync(); endpointId = endpoint.Id; var source = new MeterSource { MeterId = meter, SourceType = SourceType.HomeAssistant, EndpointId = endpointId, Config = """{"entityId":"sensor.water_total"}""", }; db.MeterSources.Add(source); await db.SaveChangesAsync(); sourceId = source.Id; } try { using var factory = new MeterVaultAppFactory(fx.ConnectionString); using var client = factory.CreateClient(); var html = System.Net.WebUtility.HtmlDecode( await client.GetStringAsync(new Uri($"/meters/{meter}?tab=sources", UriKind.Relative))); var edit = MeterLinks.EditConnector(meter, sourceId, SourceType.HomeAssistant, endpointId); Assert.Contains($"href=\"{edit}\"", html, StringComparison.Ordinal); } finally { await using var db = fx.CreateContext(); await db.MeterSources.Where(s => s.Id == sourceId).ExecuteDeleteAsync(); await db.IngestionEndpoints.Where(e => e.Id == endpointId).ExecuteDeleteAsync(); } } }