using StandardScene.Fass2Simulator;
using StandardScene.Magnetic.Protocol;
using Xunit;

namespace StandardScene.Magnetic.Tests.Protocol
{
    /// <summary>
    /// 验证 Magnetic 编解码与模拟器 100B 状态帧字段布局一致。
    /// </summary>
    public sealed class Fass2SimMagneticProtocolCrossTests
    {
        [Fact]
        public void StateFrame_NodeBlockOffset_MatchesMagneticParser()
        {
            var vehicle = new Fass2SimVehicle(new Fass2SimConfig
            {
                VehicleCode = 1,
                InitialNode = 9,
                InitialState = 2
            });
            vehicle.StartStop = 2;
            vehicle.Distance = 777;
            vehicle.Speed = 120;
            vehicle.Lift = 1;
            vehicle.Task = 1001;

            var simFrame = Fass2SimProtocol.BuildState(vehicle.Snapshot());
            var report = Fass2Protocol.ParseState(simFrame);

            Assert.Equal((ushort)1, report.Car);
            Assert.Equal((byte)2, report.State);
            Assert.Equal((ulong)1001, report.Task);
            Assert.Equal((ushort)9, report.Node.Node);
            Assert.Equal((ushort)777, report.Node.Distance);
            Assert.Equal((byte)2, report.Node.StartStop);
            Assert.Equal((ushort)120, report.Node.Speed);
            Assert.Equal((byte)1, report.Node.Lift);
        }

        [Fact]
        public void NodeBytes_Are25BytesAligned()
        {
            var node = new Fass2NodeMessage
            {
                Node = 4,
                Distance = 100,
                StartStop = 1,
                Byroad = 2,
                Speed = 60
            };

            var bytes = node.ToBytes();
            Assert.Equal(25, bytes.Length);

            var simNode = Fass2SimNodeMessage.FromBytes(bytes, 0);
            Assert.Equal((ushort)4, simNode.Node);
            Assert.Equal((ushort)100, simNode.Distance);
            Assert.Equal((byte)1, simNode.StartStop);
            Assert.Equal((byte)2, simNode.Byroad);
            Assert.Equal((ushort)60, simNode.Speed);
        }
    }
}
