using System;
using ZB.MOM.WW.MxGateway.Contracts;

namespace ZB.MOM.WW.MxGateway.Worker.Tests.TestSupport;

/// <summary>
///     Marks an xUnit test as requiring installed MXAccess COM and live
///     provider state. When the opt-in environment variable named by
///     <see cref="GatewayContractInfo.LiveMxAccessOptInVariableName"/> is
///     not set to <c>1</c>, the test is reported as <c>Skipped</c> by
///     xUnit rather than silently returning early (which xUnit would
///     otherwise report as <c>Passed</c>). Mirrors
///     <c>ZB.MOM.WW.MxGateway.IntegrationTests.LiveMxAccessFactAttribute</c>; both
///     copies bind to the same <c>GatewayContractInfo</c> constant so the
///     env-var name has a single literal source of truth.
/// </summary>
public sealed class LiveMxAccessFactAttribute : FactAttribute
{
    /// <summary>
    ///     The environment variable that opts the suite into running live
    ///     MXAccess COM tests. Must be set to <c>1</c> on a machine with the
    ///     installed MXAccess runtime and a reachable Galaxy provider.
    ///     Sourced from <see cref="GatewayContractInfo.LiveMxAccessOptInVariableName"/>
    ///     so a single constant gates both Worker.Tests and IntegrationTests.
    /// </summary>
    public const string LiveMxAccessVariableName = GatewayContractInfo.LiveMxAccessOptInVariableName;

    /// <summary>Initializes the attribute, skipping the test unless the env var is set.</summary>
    public LiveMxAccessFactAttribute()
    {
        if (!string.Equals(
                Environment.GetEnvironmentVariable(LiveMxAccessVariableName),
                "1",
                StringComparison.Ordinal))
        {
            Skip = $"Set {LiveMxAccessVariableName}=1 to run live MXAccess tests.";
        }
    }
}
