using Evergreen.Health.Application.Health.Integrations.Ib43; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using Xunit; namespace Admin.NET.Test.Health; [CollectionDefinition("Ib43Environment", DisableParallelization = true)] public sealed class Ib43EnvironmentCollection { } [Collection("Ib43Environment")] public sealed class Ib43ConfigurationTests { [Fact] public void Environment_overrides_defaults_after_binding_and_before_validation() { var names = new[] { "Ib43__Enabled", "Ib43__Password" }; var original = names.Select(Environment.GetEnvironmentVariable).ToArray(); try { Environment.SetEnvironmentVariable(names[0], "true"); Environment.SetEnvironmentVariable(names[1], "test-only-secret"); var services = new ServiceCollection(); services.AddOptions().Configure(options => { options.Enabled = false; options.Password = ""; options.Devices = [new() { ProductKey = "test", SerialNumber = "sim" }]; }).PostConfigure(Ib43Configuration.ApplyEnvironmentOverrides) .Validate(options => options.IsValid()); using var provider = services.BuildServiceProvider(); var options = provider.GetRequiredService>().Value; Assert.True(options.Enabled); Assert.Equal("test-only-secret", options.Password); Assert.Single(options.Devices); Environment.SetEnvironmentVariable(names[0], null); Environment.SetEnvironmentVariable(names[1], null); var defaults = new Ib43Options(); Ib43Configuration.ApplyEnvironmentOverrides(defaults); Assert.False(defaults.Enabled); Assert.Empty(defaults.Password); } finally { for (var i = 0; i < names.Length; i++) Environment.SetEnvironmentVariable(names[i], original[i]); } } }