using ZB.MOM.WW.CBDD.Bson;
using Xunit;

namespace ZB.MOM.WW.CBDD.Tests;

public class ObjectIdTests
{
    [Fact]
    public void NewObjectId_ShouldCreate12ByteId()
    {
        var oid = ObjectId.NewObjectId();

        Span<byte> bytes = stackalloc byte[12];
        oid.WriteTo(bytes);

        bytes.Length.ShouldBe(12);
    }

    [Fact]
    public void ObjectId_ShouldRoundTrip()
    {
        var original = ObjectId.NewObjectId();

        Span<byte> bytes = stackalloc byte[12];
        original.WriteTo(bytes);

        var restored = new ObjectId(bytes);

        restored.ShouldBe(original);
    }

    [Fact]
    public void ObjectId_Equals_ShouldWork()
    {
        var oid1 = ObjectId.NewObjectId();
        var oid2 = oid1;
        var oid3 = ObjectId.NewObjectId();

        oid2.ShouldBe(oid1);
        oid3.ShouldNotBe(oid1);
    }

    [Fact]
    public void ObjectId_Timestamp_ShouldBeRecentUtc()
    {
        var oid = ObjectId.NewObjectId();
        var timestamp = oid.Timestamp;

        (timestamp <= DateTime.UtcNow).ShouldBeTrue();
        (timestamp >= DateTime.UtcNow.AddSeconds(-5)).ShouldBeTrue();
    }
}
