using System.Net; using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.Extensions.Configuration; namespace ZB.MOM.WW.ScadaBridge.Host.Tests; /// /// Observability adoption: verifies the shared ZB.MOM.WW.Telemetry Prometheus /// scrape endpoint (/metrics, mounted by app.MapZbMetrics()) is wired into the /// Central composition root. AddZbTelemetry (registered in /// ) always wires the Prometheus /// exporter, so the endpoint returns the Prometheus exposition format regardless of DB / /// cluster state. This is a pure route assertion — it requires no database, LDAP, or formed /// Akka cluster. The Central-role factory bootstrap mirrors . /// public class MetricsEndpointTests : IDisposable { private readonly List _disposables = new(); public MetricsEndpointTests() { // Host-003: connection strings are externalised; supply them via env vars. _disposables.Add(new CentralDbTestEnvironment()); } public void Dispose() { foreach (var d in _disposables) { try { d.Dispose(); } catch { /* best effort */ } } } private WebApplicationFactory CreateCentralFactory() { var factory = new WebApplicationFactory() .WithWebHostBuilder(builder => { builder.ConfigureAppConfiguration((context, config) => { config.AddInMemoryCollection(new Dictionary { ["ScadaBridge:Node:NodeHostname"] = "localhost", ["ScadaBridge:Node:RemotingPort"] = "0", ["ScadaBridge:Cluster:SeedNodes:0"] = "akka.tcp://scadabridge@localhost:2551", ["ScadaBridge:Cluster:SeedNodes:1"] = "akka.tcp://scadabridge@localhost:2552", ["ScadaBridge:Database:SkipMigrations"] = "true", }); }); builder.UseSetting("ScadaBridge:Node:Role", "Central"); builder.UseSetting("ScadaBridge:Database:SkipMigrations", "true"); }); _disposables.Add(factory); return factory; } [Fact] public async Task Metrics_Endpoint_IsMapped() { var previousEnv = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT"); try { Environment.SetEnvironmentVariable("DOTNET_ENVIRONMENT", "Central"); var factory = CreateCentralFactory(); var client = factory.CreateClient(); _disposables.Add(client); var response = await client.GetAsync("/metrics"); Assert.Equal(HttpStatusCode.OK, response.StatusCode); var body = await response.Content.ReadAsStringAsync(); Assert.Contains("# ", body); // Prometheus exposition (HELP/TYPE comments) } finally { Environment.SetEnvironmentVariable("DOTNET_ENVIRONMENT", previousEnv); } } }