using RioJoy.Core.Protocol;
using Xunit;

namespace RioJoy.Core.Tests.Protocol;

public class RioChecksumTests
{
    [Fact]
    public void Empty_IsZero()
    {
        Assert.Equal(0, RioChecksum.Compute(new byte[0]));
    }

    [Fact]
    public void SumsLow7Bits()
    {
        // (0x84 & 0x7F) + (0x05 & 0x7F) + (0x3C & 0x7F) = 0x04 + 0x05 + 0x3C = 0x45
        byte sum = RioChecksum.Compute(new byte[] { 0x84, 0x05, 0x3C });
        Assert.Equal(0x45, sum);
    }

    [Fact]
    public void MasksResultTo7Bits()
    {
        // Sum of low-7-bits = 0x7F + 0x7F = 0xFE; masked to 7 bits → 0x7E.
        byte sum = RioChecksum.Compute(new byte[] { 0x7F, 0x7F });
        Assert.Equal(0x7E, sum);
    }

    [Fact]
    public void IgnoresHighBitOfInputs()
    {
        // 0x82 contributes only 0x02.
        Assert.Equal(0x02, RioChecksum.Compute(new byte[] { 0x82 }));
    }
}
