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: "", tokenEnv: "SOME_TOKEN", entityId: null);
Assert.False(result.Ok);
Assert.Contains("Base URL", result.Message, StringComparison.OrdinalIgnoreCase);
}
[Fact]
public async Task Missing_token_env_fails_without_network()
{
var result = await NewTester().TestAsync(baseUrl: "http://ha.local:8123", tokenEnv: "", entityId: null);
Assert.False(result.Ok);
Assert.Contains("env-var", result.Message, StringComparison.OrdinalIgnoreCase);
}
[Fact]
public async Task Unset_token_env_var_fails_without_network()
{
var result = await NewTester().TestAsync(
baseUrl: "http://ha.local:8123", tokenEnv: "METERVAULT_DEFINITELY_UNSET_TOKEN_VAR", entityId: null);
Assert.False(result.Ok);
Assert.Contains("is not set", 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.");
}
}