📄 lib/tests/ExtendedHeaderTests.cs
using Shouldly;

namespace SharpDlt.Tests;

public class ExtendedHeaderTests
{
    [Fact]
    public void Parse_ReadsAllFields()
    {
        var data = TestDlt.ExtendedHeader(
            verbose: true,
            messageType: DltMessageType.Log,
            messageTypeInfo: (byte)DltLogInfo.Warn,
            numberOfArguments: 3,
            applicationId: "APP1",
            contextId: "CTX1"
        );

        var header = ExtendedHeader.Parse(data);

        header.Verbose.ShouldBeTrue();
        header.MessageType.ShouldBe(DltMessageType.Log);
        header.MessageTypeInfo.ShouldBe((byte)DltLogInfo.Warn);
        header.LogInfo.ShouldBe(DltLogInfo.Warn);
        header.NumberOfArguments.ShouldBe((byte)3);
        header.ApplicationId.ShouldBe("APP1");
        header.ContextId.ShouldBe("CTX1");
        header.MessageInfo.ShouldBe(data[0]);
    }

    [Theory]
    [InlineData(DltMessageType.Log, (byte)DltLogInfo.Fatal)]
    [InlineData(DltMessageType.AppTrace, (byte)DltTraceInfo.State)]
    [InlineData(DltMessageType.NetworkTrace, (byte)DltBusInfo.Can)]
    [InlineData(DltMessageType.Control, (byte)DltControlInfo.Response)]
    public void Parse_DecodesMessageTypeAndTypeInfo(DltMessageType messageType, byte typeInfo)
    {
        var data = TestDlt.ExtendedHeader(verbose: false, messageType: messageType, messageTypeInfo: typeInfo);

        var header = ExtendedHeader.Parse(data);

        header.Verbose.ShouldBeFalse();
        header.MessageType.ShouldBe(messageType);
        header.MessageTypeInfo.ShouldBe(typeInfo);
    }

    [Fact]
    public void TypedTypeInfoAccessors_ReinterpretTheSameValue()
    {
        var header = ExtendedHeader.Parse(TestDlt.ExtendedHeader(messageTypeInfo: (byte)DltTraceInfo.FunctionIn));

        header.TraceInfo.ShouldBe(DltTraceInfo.FunctionIn);
        header.BusInfo.ShouldBe(DltBusInfo.Can);
        header.ControlInfo.ShouldBe(DltControlInfo.Response);
        header.LogInfo.ShouldBe(DltLogInfo.Error);
    }

    [Fact]
    public void Parse_StripsNulPaddingOfIds()
    {
        var data = TestDlt.ExtendedHeader(applicationId: "AP", contextId: "C");

        var header = ExtendedHeader.Parse(data);

        header.ApplicationId.ShouldBe("AP");
        header.ContextId.ShouldBe("C");
    }

    [Fact]
    public void Parse_TooShortData_Throws()
    {
        Should.Throw<DltParseException>(() => ExtendedHeader.Parse(new byte[9]));
    }
}