using ZB.MOM.WW.ScadaBridge.Commons.Types.Debugging;
namespace ZB.MOM.WW.ScadaBridge.Commons.Tests.Debugging;
///
/// The single parser behind the graph node's exception subtitle and the step
/// detail panel's exception block. Before it, the panel showed the OUTERMOST exception of a
/// wrapped chain while the node showed the innermost — the two now agree by construction.
///
public class ExceptionHeaderTests
{
/// Verifies that a plain detail splits the type from the message and shortens the type only when asked.
[Fact]
public void Parse_PlainDetail_SplitsTypeFromMessage_AndShortensOnlyWhenAsked()
{
const string detail = "System.InvalidOperationException: Recipe 42 rejected\n at X.Y()";
Assert.Equal(("InvalidOperationException", "Recipe 42 rejected"), ExceptionHeader.Parse(detail, shortenType: true));
Assert.Equal(("System.InvalidOperationException", "Recipe 42 rejected"), ExceptionHeader.Parse(detail, shortenType: false));
}
/// Verifies that a wrapped exception chain resolves to the innermost exception.
[Fact]
public void Parse_WrappedChain_TakesTheINNERMOSTException()
{
const string detail = "A.Outer: outer ---> B.Middle: middle ---> C.Innermost: the real one\n at X";
Assert.Equal(("Innermost", "the real one"), ExceptionHeader.Parse(detail, shortenType: true));
// The panel keeps the namespace but still names the innermost exception, not the wrapper.
Assert.Equal(("C.Innermost", "the real one"), ExceptionHeader.Parse(detail, shortenType: false));
}
/// Verifies that a generic type keeps its arity and arguments verbatim and only the head is shortened.
[Fact]
public void Parse_GenericType_KeepsTheArityAndArgumentsVerbatim_ShorteningOnlyTheHead()
{
Assert.Equal(("RecipeException`1[System.String]", "msg"),
ExceptionHeader.Parse("MyApp.RecipeException`1[System.String]: msg", shortenType: true));
Assert.Equal(("RecipeException`1[System.String]", "msg"),
ExceptionHeader.Parse("RecipeException`1[System.String]: msg", shortenType: true));
}
/// Verifies that the chain marker is matched after trimming, so a trailing marker is not treated as a chain.
[Fact]
public void Parse_ChainMarkerIsMatchedAfterTrimming_SoATrailingMarkerIsNotAChain()
{
// The first line is trimmed BEFORE the " ---> " chain is walked (behaviour carried over
// verbatim from the FlowGraphView original), so a detail ending in the marker keeps it as
// part of the message rather than resolving to an empty innermost segment.
Assert.Equal(("Outer", "outer --->"), ExceptionHeader.Parse("A.Outer: outer ---> ", shortenType: true));
}
/// Verifies that a line without a colon is treated as all type and no message.
[Fact]
public void Parse_LineWithoutAColon_IsAllTypeAndNoMessage()
{
Assert.Equal(("SomethingWentWrong", string.Empty), ExceptionHeader.Parse("SomethingWentWrong", shortenType: true));
}
/// Verifies that a CRLF detail has the carriage return trimmed off the first line.
[Fact]
public void Parse_CrLfDetail_TrimsTheCarriageReturnOffTheFirstLine()
{
Assert.Equal(("TimeoutException", "took too long"),
ExceptionHeader.Parse("System.TimeoutException: took too long\r\n at X\r\n", shortenType: true));
}
/// Verifies that a missing or blank detail returns null.
/// The exception detail text to parse.
[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData(" ")]
public void Parse_NothingUsable_ReturnsNull(string? detail)
{
Assert.Null(ExceptionHeader.Parse(detail, shortenType: true));
Assert.Null(ExceptionHeader.Parse(detail, shortenType: false));
}
}