package com.lingniu.ingest.protocol.gb32960.inbound;

import com.lingniu.ingest.protocol.gb32960.model.CommandType;
import com.lingniu.ingest.protocol.gb32960.model.ProtocolVersion;
import com.lingniu.ingest.protocol.gb32960.model.ResponseFlag;
import io.netty.buffer.ByteBuf;
import io.netty.channel.embedded.EmbeddedChannel;
import org.junit.jupiter.api.Test;

import java.nio.charset.StandardCharsets;
import java.time.Instant;

import static org.assertj.core.api.Assertions.assertThat;

class Gb32960AckServiceTest {

    @Test
    void writeResponse_writesGb32960AckFrameToChannel() {
        Gb32960AckService service = new Gb32960AckService();
        EmbeddedChannel channel = new EmbeddedChannel();
        byte[] rawVin = "LTEST000000000001".getBytes(StandardCharsets.US_ASCII);

        service.writeResponse(
                channel,
                ProtocolVersion.V2016,
                CommandType.VEHICLE_LOGIN,
                ResponseFlag.SUCCESS,
                rawVin,
                Instant.parse("2026-04-20T10:00:00Z"),
                null,
                "vehicle-login-ack");

        ByteBuf out = channel.readOutbound();
        assertThat(out).isNotNull();
        byte[] bytes = new byte[out.readableBytes()];
        out.readBytes(bytes);
        assertThat(bytes[0]).isEqualTo((byte) 0x23);
        assertThat(bytes[1]).isEqualTo((byte) 0x23);
        assertThat(bytes[2]).isEqualTo((byte) CommandType.VEHICLE_LOGIN.code());
        assertThat(bytes[3]).isEqualTo((byte) ResponseFlag.SUCCESS.code());
    }
}
