using Shouldly;
using Xunit;
using ZB.MOM.WW.MxGateway.Contracts.Proto;
using ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Runtime;

namespace ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Tests;

/// <summary>
///     Driver.Galaxy-019 — pins <see cref="GatewayGalaxySubscriber.ClassifyIntervalReply"/>:
///     a <c>SetBufferedUpdateInterval</c> reply is only "applied" (and therefore cacheable
///     as the last-applied interval) when the gateway returns <see cref="ProtocolStatusCode.Ok"/>.
///     <see cref="ProtocolStatusCode.MxaccessFailure"/> means the COM-side set did NOT take
///     effect, so caching it would suppress the retry on the next subscribe — it must classify
///     as not-applied, as must any other unexpected code or a missing status.
/// </summary>
public sealed class GatewayGalaxySubscriberClassifyTests
{
    /// <summary>Ok is the only code that records the interval as applied.</summary>
    [Fact]
    public void Ok_classifies_as_applied()
    {
        GatewayGalaxySubscriber.ClassifyIntervalReply(ProtocolStatusCode.Ok).ShouldBeTrue();
    }

    /// <summary>MxaccessFailure must NOT cache — the COM-side set did not apply.</summary>
    [Fact]
    public void MxaccessFailure_classifies_as_not_applied()
    {
        GatewayGalaxySubscriber.ClassifyIntervalReply(ProtocolStatusCode.MxaccessFailure).ShouldBeFalse();
    }

    /// <summary>Any other unexpected code is treated as not-applied.</summary>
    [Fact]
    public void Unexpected_code_classifies_as_not_applied()
    {
        GatewayGalaxySubscriber.ClassifyIntervalReply(ProtocolStatusCode.Unspecified).ShouldBeFalse();
    }

    /// <summary>A missing protocol status (null) is treated as not-applied.</summary>
    [Fact]
    public void Null_classifies_as_not_applied()
    {
        GatewayGalaxySubscriber.ClassifyIntervalReply(null).ShouldBeFalse();
    }
}
