using Mbproxy.Proxy.Cache;
using Shouldly;
using Xunit;

namespace Mbproxy.Tests.Proxy.Cache;

/// <summary>
/// Equality semantics for <see cref="CacheKey"/>. The key must distinguish every dimension
/// the cache uses to route a hit — same dimensions as <c>CoalescingKey</c> but a separate
/// type so the two phases can evolve independently.
/// </summary>
[Trait("Category", "Unit")]
public sealed class CacheKeyTests
{
    [Fact]
    public void Equality_IdenticalKeys_AreEqual()
    {
        var a = new CacheKey(UnitId: 1, Fc: 0x03, StartAddress: 100, Qty: 4);
        var b = new CacheKey(UnitId: 1, Fc: 0x03, StartAddress: 100, Qty: 4);

        a.ShouldBe(b);
        a.GetHashCode().ShouldBe(b.GetHashCode());
    }

    [Fact]
    public void Equality_Fc03_vs_Fc04_AtSameAddress_DifferentKeys()
    {
        var fc03 = new CacheKey(UnitId: 1, Fc: 0x03, StartAddress: 100, Qty: 1);
        var fc04 = new CacheKey(UnitId: 1, Fc: 0x04, StartAddress: 100, Qty: 1);

        fc03.ShouldNotBe(fc04, "FC03 and FC04 read different Modbus tables");
    }

    [Fact]
    public void Equality_DifferentUnitId_DifferentKeys()
    {
        var u1 = new CacheKey(UnitId: 1, Fc: 0x03, StartAddress: 100, Qty: 1);
        var u2 = new CacheKey(UnitId: 2, Fc: 0x03, StartAddress: 100, Qty: 1);

        u1.ShouldNotBe(u2, "different unit IDs never share cache entries");
    }
}
