using MeterVault.Infrastructure.Ingestion;
using Microsoft.Extensions.Logging.Abstractions;
namespace MeterVault.Integration.Tests.Ingestion;
///
/// The HA connection tester must fail closed on missing config before any network call —
/// so a misconfigured connector gives a clear message, never an exception. The stub factory throws
/// if the tester ever tries to create an HttpClient, proving these branches never reach the network.
///
public sealed class HaConnectionTesterTests
{
[Fact]
public async Task Missing_base_url_fails_without_network()
{
var result = await NewTester().TestAsync(baseUrl: "", token: "a-token", entityId: null);
Assert.False(result.Ok);
Assert.Contains("Base URL", result.Message, StringComparison.OrdinalIgnoreCase);
}
[Fact]
public async Task Missing_token_fails_without_network()
{
var result = await NewTester().TestAsync(baseUrl: "http://ha.local:8123", token: "", entityId: null);
Assert.False(result.Ok);
Assert.Contains("token", result.Message, StringComparison.OrdinalIgnoreCase);
}
private static HaConnectionTester NewTester() =>
new(new ThrowingHttpClientFactory(), NullLogger.Instance);
private sealed class ThrowingHttpClientFactory : IHttpClientFactory
{
public HttpClient CreateClient(string name) =>
throw new InvalidOperationException("Network must not be touched for a config-guard failure.");
}
}