using System;
using System.Collections.Generic;
using GB5Shared.Deployment;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
namespace GB5SharedTests;
///
/// Covers AddGB5Environment's fail-fast posture — an unconfigured GB5:Environment must throw
/// at registration time, not silently resolve to Dev. Mirrors AddGB5Vault's own fail-fast
/// tests for Vault:Address/Token (same pattern, same reasoning: an unconfigured tier is worse
/// than a startup crash in production).
///
public class GB5EnvironmentServiceExtensionsTests
{
private static IConfiguration ConfigFrom(Dictionary values) =>
new ConfigurationBuilder().AddInMemoryCollection(values).Build();
[Fact]
public void AddGB5Environment_MissingSection_Throws()
{
var services = new ServiceCollection();
var config = ConfigFrom(new Dictionary());
Assert.Throws(() => services.AddGB5Environment(config));
}
[Fact]
public void AddGB5Environment_EmptyEnvironmentValue_Throws()
{
var services = new ServiceCollection();
var config = ConfigFrom(new Dictionary { ["GB5:Environment"] = "" });
Assert.Throws(() => services.AddGB5Environment(config));
}
[Fact]
public void AddGB5Environment_InvalidTierValue_Throws()
{
var services = new ServiceCollection();
var config = ConfigFrom(new Dictionary { ["GB5:Environment"] = "Staging" });
Assert.Throws(() => services.AddGB5Environment(config));
}
[Theory]
[InlineData("Dev", GB5Tier.Dev)]
[InlineData("QC", GB5Tier.QC)]
[InlineData("Live", GB5Tier.Live)]
[InlineData("live", GB5Tier.Live)]
public void AddGB5Environment_ValidTier_ResolvesIGB5Environment(string configuredValue, GB5Tier expectedTier)
{
var services = new ServiceCollection();
var config = ConfigFrom(new Dictionary { ["GB5:Environment"] = configuredValue });
services.AddGB5Environment(config);
var provider = services.BuildServiceProvider();
var env = provider.GetRequiredService();
Assert.Equal(expectedTier, env.Tier);
}
}