using Microsoft.AspNetCore.Http;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.AdminUI.Api;
using ZB.MOM.WW.OtOpcUa.Commons.Messages.Admin;
using ZB.MOM.WW.OtOpcUa.Commons.Types;
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Api;
/// Unit tests for the pure helpers behind the headless deploy REST endpoint —
/// (fixed-time API-key check) and
/// (outcome → HTTP status mapping).
public sealed class DeployApiEndpointsTests
{
// ── IsAuthorized ────────────────────────────────────────────────────────────
/// Verifies the exact configured key authorizes.
[Fact]
public void IsAuthorized_true_for_matching_key()
=> DeployApiEndpoints.IsAuthorized("s3cret", "s3cret").ShouldBeTrue();
/// Verifies a wrong key is rejected.
[Fact]
public void IsAuthorized_false_for_wrong_key()
=> DeployApiEndpoints.IsAuthorized("nope", "s3cret").ShouldBeFalse();
/// Verifies a missing/empty provided key is rejected.
[Theory]
[InlineData(null)]
[InlineData("")]
public void IsAuthorized_false_for_missing_provided_key(string? provided)
=> DeployApiEndpoints.IsAuthorized(provided, "s3cret").ShouldBeFalse();
/// Verifies that when no key is configured nothing authorizes (endpoint stays closed).
[Theory]
[InlineData(null)]
[InlineData("")]
public void IsAuthorized_false_when_no_key_configured(string? configured)
=> DeployApiEndpoints.IsAuthorized("anything", configured).ShouldBeFalse();
// ── ToResult ────────────────────────────────────────────────────────────────
/// Verifies each deployment outcome maps to the right HTTP status.
[Theory]
[InlineData(StartDeploymentOutcome.Accepted, StatusCodes.Status202Accepted)]
[InlineData(StartDeploymentOutcome.NoChanges, StatusCodes.Status200OK)]
[InlineData(StartDeploymentOutcome.AnotherDeploymentInFlight, StatusCodes.Status409Conflict)]
[InlineData(StartDeploymentOutcome.Rejected, StatusCodes.Status422UnprocessableEntity)]
[InlineData(StartDeploymentOutcome.OutcomeUnknown, StatusCodes.Status202Accepted)]
public void ToResult_maps_outcome_to_status(StartDeploymentOutcome outcome, int expectedStatus)
{
var result = DeployApiEndpoints.ToResult(
new StartDeploymentResult(outcome, DeploymentId: null, RevisionHash: null,
Message: "x", CorrelationId: CorrelationId.NewId()));
result.ShouldBeAssignableTo();
((IStatusCodeHttpResult)result).StatusCode.ShouldBe(expectedStatus);
}
///
/// lmxopcua#949: an ask timeout is not a failure. The REST trigger answers 202 with the pre-allocated id in
/// the body and the Location, so the caller can poll for the deployment that may still go ahead.
///
[Fact]
public void ToResult_OutcomeUnknown_is_202_with_the_preallocated_id_and_a_pollable_Location()
{
var id = Guid.NewGuid();
var result = DeployApiEndpoints.ToResult(new StartDeploymentResult(
StartDeploymentOutcome.OutcomeUnknown, new DeploymentId(id), RevisionHash: null,
Message: "no reply", CorrelationId: CorrelationId.NewId()));
var accepted = result.ShouldBeOfType>();
accepted.Location.ShouldBe($"/api/deployments/{id}");
accepted.Value!.Outcome.ShouldBe("OutcomeUnknown");
accepted.Value.DeploymentId.ShouldBe(id);
accepted.Value.Message.ShouldBe("no reply");
}
/// A 409 for a compose still running names the deployment that is going ahead (#949).
[Fact]
public void ToResult_InFlight_carries_the_in_flight_id()
{
var id = Guid.NewGuid();
var result = DeployApiEndpoints.ToResult(new StartDeploymentResult(
StartDeploymentOutcome.AnotherDeploymentInFlight, new DeploymentId(id), null, "busy", CorrelationId.NewId()));
var conflict = result.ShouldBeOfType>();
conflict.Value!.DeploymentId.ShouldBe(id);
}
}