fix(CLI-37,CLI-38): make status/HRESULT reply validation conformant across all five clients

One cross-client conformance pass; also closes first-cycle CLI-08.

CLI-37: an MxStatusProxy entry is a failure iff `category !=
MX_STATUS_CATEGORY_OK`. The proto contract has always said so — `success` is
the raw 16-bit COM member carried verbatim for diagnostics, not a boolean — but
four clients branched on `success` alone and .NET required both, so the same
gateway reply produced opposite verdicts per language. An absent entry stays
success; a present entry with an UNSPECIFIED category is a failure, because the
worker always maps a category and an unmapped one is not proven OK.

CLI-38: a reply fails on HRESULT iff `hresult` is present and negative, so
positive COM success codes such as S_FALSE (1) pass. .NET/Go/Java used `!= 0`,
which errored on a parity-preserving S_FALSE that Python and Rust accepted.
This makes the existing ClientLibrariesDesign.md claim true rather than
rewriting the doc to describe the divergence.

Four shared fixtures pin both rules cross-client, and each language suite also
carries a table test for the two edges a fixture cannot express (absent entry,
UNSPECIFIED category). A Java test fake that built a status with a bare
`setSuccess(1)` and no category is fixed — under the category rule that reply
was never a success.
This commit is contained in:
Joseph Doherty
2026-08-07 06:00:58 -04:00
parent cf66ebbcfb
commit d6b2f24c3f
30 changed files with 631 additions and 40 deletions
+6
View File
@@ -163,6 +163,12 @@ can keep the full `MxCommandReply`, HRESULT, and status array when MXAccess
itself rejects a command. `MxAccessException.Reply` contains the raw generated
reply.
`EnsureMxAccessSuccess()` follows COM semantics: only a **negative** HRESULT is
a failure, so positive success codes such as `S_FALSE` (1) pass. A status entry
fails only when `Category` is not `MxStatusCategory.Ok``MxStatusProxy.Success`
mirrors the raw COM member for diagnostics and never decides the verdict, which
is why `IsSuccess()` branches on the category alone.
## Write Semantics And Common Pitfalls
These are MXAccess parity behaviors that surprise new callers. The gateway
@@ -32,6 +32,57 @@ public sealed class MxCommandReplyExtensionsTests
Assert.Contains("0x80040200", exception.Message);
}
/// <summary>Verifies that a non-OK status category fails even when the raw success member is set.</summary>
[Fact]
public void EnsureMxAccessSuccess_WithNonOkCategoryAndSuccessSet_Throws()
{
MxCommandReply reply = ReadReplyFixture(
"write.status-category-error-success-set.reply.json");
reply.EnsureProtocolSuccess();
MxAccessException exception = Assert.Throws<MxAccessException>(
reply.EnsureMxAccessSuccess);
Assert.Equal(1, Assert.Single(exception.Statuses).Success);
Assert.Contains("CommunicationError", exception.Message, StringComparison.Ordinal);
}
/// <summary>Verifies that an Ok status category succeeds even when the raw success member is zero.</summary>
[Fact]
public void EnsureMxAccessSuccess_WithOkCategoryAndZeroSuccess_ReturnsReply()
{
MxCommandReply reply = ReadReplyFixture(
"write.status-category-ok-success-zero.reply.json");
Assert.Equal(0, Assert.Single(reply.Statuses).Success);
Assert.Same(reply, reply.EnsureProtocolSuccess());
Assert.Same(reply, reply.EnsureMxAccessSuccess());
}
/// <summary>Verifies that a positive HResult (S_FALSE) is a COM success code, not a failure.</summary>
[Fact]
public void EnsureMxAccessSuccess_WithPositiveHResult_ReturnsReply()
{
MxCommandReply reply = ReadReplyFixture("write.hresult-s-false.reply.json");
Assert.Equal(1, reply.Hresult);
Assert.Same(reply, reply.EnsureProtocolSuccess());
Assert.Same(reply, reply.EnsureMxAccessSuccess());
}
/// <summary>Verifies that a negative HResult fails even when every status entry is Ok.</summary>
[Fact]
public void EnsureMxAccessSuccess_WithNegativeHResult_Throws()
{
MxCommandReply reply = ReadReplyFixture("write.hresult-e-fail.reply.json");
reply.EnsureProtocolSuccess();
MxAccessException exception = Assert.Throws<MxAccessException>(
reply.EnsureMxAccessSuccess);
Assert.Equal(-2147467259, exception.HResultCode);
}
/// <summary>Verifies that session-not-found protocol failures throw the correct gateway exception.</summary>
[Fact]
public void EnsureProtocolSuccess_WithSessionFailure_ThrowsSessionException()
@@ -19,9 +19,8 @@ public sealed class MxStatusProxyExtensionsTests
{
MxStatusProxy status = JsonParser.Default.Parse<MxStatusProxy>(
testCase.GetProperty("status").GetRawText());
int success = testCase.GetProperty("status").GetProperty("success").GetInt32();
Assert.Equal(success != 0 && status.Category is MxStatusCategory.Ok, status.IsSuccess());
Assert.Equal(status.Category is MxStatusCategory.Ok, status.IsSuccess());
Assert.Equal(
testCase.GetProperty("status").GetProperty("rawCategory").GetInt32(),
status.RawCategory);
@@ -31,6 +30,22 @@ public sealed class MxStatusProxyExtensionsTests
}
}
/// <summary>Verifies that the raw success member never overrides the authoritative category.</summary>
[Theory]
[InlineData(MxStatusCategory.Ok, 0, true)]
[InlineData(MxStatusCategory.Ok, 1, true)]
[InlineData(MxStatusCategory.CommunicationError, 1, false)]
[InlineData(MxStatusCategory.Unspecified, 1, false)]
public void IsSuccess_BranchesOnCategoryOnly(
MxStatusCategory category,
int success,
bool expected)
{
MxStatusProxy status = new() { Category = category, Success = success };
Assert.Equal(expected, status.IsSuccess());
}
private static string ReadFixture(string category, string fileName)
{
DirectoryInfo directory = new(AppContext.BaseDirectory);
@@ -23,7 +23,11 @@ public static class MxCommandReplyExtensions
throw CreateProtocolException(reply, code);
}
/// <summary>Validates that the reply indicates MXAccess success (no HResult or status failures), throwing MxAccessException if not.</summary>
/// <summary>
/// Validates that the reply indicates MXAccess success, throwing MxAccessException if not.
/// Following COM semantics, only a negative HResult is a failure — positive success codes
/// such as <c>S_FALSE</c> pass — and a status entry fails only when its category is not Ok.
/// </summary>
/// <param name="reply">The command reply to check.</param>
/// <returns>The same reply, for chaining.</returns>
public static MxCommandReply EnsureMxAccessSuccess(this MxCommandReply reply)
@@ -31,7 +35,7 @@ public static class MxCommandReplyExtensions
ArgumentNullException.ThrowIfNull(reply);
bool mxAccessFailure = reply.ProtocolStatus?.Code is ProtocolStatusCode.MxaccessFailure;
bool hResultFailure = reply.HasHresult && reply.Hresult != 0;
bool hResultFailure = reply.HasHresult && reply.Hresult < 0;
bool statusFailure = reply.Statuses.Any(status => !status.IsSuccess());
if (!mxAccessFailure && !hResultFailure && !statusFailure)
@@ -5,15 +5,18 @@ namespace ZB.MOM.WW.MxGateway.Client;
/// <summary>Extension methods for MxStatusProxy values.</summary>
public static class MxStatusProxyExtensions
{
/// <summary>Returns whether the status indicates success (success flag set and category is Ok).</summary>
/// <summary>
/// Returns whether the status indicates success, which the wire contract defines as
/// <see cref="MxStatusCategory.Ok"/>. The raw <c>Success</c> member is a verbatim COM
/// diagnostic, not a boolean, so it never participates in the verdict.
/// </summary>
/// <param name="status">The status to check.</param>
/// <returns><see langword="true"/> if the status indicates success; otherwise <see langword="false"/>.</returns>
public static bool IsSuccess(this MxStatusProxy status)
{
ArgumentNullException.ThrowIfNull(status);
return status.Success != 0
&& status.Category is MxStatusCategory.Ok;
return status.Category is MxStatusCategory.Ok;
}
/// <summary>Returns a formatted summary of the status for diagnostic output.</summary>