using ScadaLink.CLI;

namespace ScadaLink.CLI.Tests;

[Collection("Environment")]
public class CliConfigTests
{
    [Fact]
    public void Load_DefaultFormat_IsJson()
    {
        var origUrl = Environment.GetEnvironmentVariable("SCADALINK_MANAGEMENT_URL");
        var origFormat = Environment.GetEnvironmentVariable("SCADALINK_FORMAT");

        try
        {
            Environment.SetEnvironmentVariable("SCADALINK_MANAGEMENT_URL", null);
            Environment.SetEnvironmentVariable("SCADALINK_FORMAT", null);

            var config = CliConfig.Load();

            // DefaultFormat is always "json" unless overridden by config file or env var
            Assert.Equal("json", config.DefaultFormat);
        }
        finally
        {
            Environment.SetEnvironmentVariable("SCADALINK_MANAGEMENT_URL", origUrl);
            Environment.SetEnvironmentVariable("SCADALINK_FORMAT", origFormat);
        }
    }

    [Fact]
    public void Load_ManagementUrl_FromEnvironment()
    {
        var orig = Environment.GetEnvironmentVariable("SCADALINK_MANAGEMENT_URL");
        try
        {
            Environment.SetEnvironmentVariable("SCADALINK_MANAGEMENT_URL", "http://central:5000");

            var config = CliConfig.Load();

            Assert.Equal("http://central:5000", config.ManagementUrl);
        }
        finally
        {
            Environment.SetEnvironmentVariable("SCADALINK_MANAGEMENT_URL", orig);
        }
    }

    [Fact]
    public void Load_Format_FromEnvironment()
    {
        var orig = Environment.GetEnvironmentVariable("SCADALINK_FORMAT");
        try
        {
            Environment.SetEnvironmentVariable("SCADALINK_FORMAT", "table");

            var config = CliConfig.Load();

            Assert.Equal("table", config.DefaultFormat);
        }
        finally
        {
            Environment.SetEnvironmentVariable("SCADALINK_FORMAT", orig);
        }
    }
}
