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

import com.lingniu.ingest.protocol.gb32960.model.InfoBlock;
import com.lingniu.ingest.protocol.gb32960.model.InfoBlockType;
import io.netty.channel.embedded.EmbeddedChannel;
import org.junit.jupiter.api.Test;

import java.util.List;

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

class Gb32960FrameDiagnosticsTest {

    @Test
    void shouldLogFirstSeenRawSignatureOnlyOncePerChannel() {
        Gb32960FrameDiagnostics diagnostics = new Gb32960FrameDiagnostics(8);
        EmbeddedChannel channel = new EmbeddedChannel();

        List<String> rawTypes = List.of("0x30", "0x83");

        assertThat(diagnostics.markFirstSeen(channel, "VIN001", rawTypes)).isTrue();
        assertThat(diagnostics.markFirstSeen(channel, "VIN001", rawTypes)).isFalse();
    }

    @Test
    void markFirstSeen_keepsBoundedKeysPerChannel() {
        Gb32960FrameDiagnostics diagnostics = new Gb32960FrameDiagnostics(2);
        EmbeddedChannel channel = new EmbeddedChannel();

        assertThat(diagnostics.markFirstSeen(channel, "VIN001", List.of("0x30"))).isTrue();
        assertThat(diagnostics.markFirstSeen(channel, "VIN001", List.of("0x31"))).isTrue();
        assertThat(diagnostics.markFirstSeen(channel, "VIN001", List.of("0x32"))).isTrue();

        assertThat(diagnostics.seenKeyCount(channel)).isLessThanOrEqualTo(2);
    }

    @Test
    void collectRawTypeHex_returnsOnlyRawBlocks() {
        List<String> rawTypes = Gb32960FrameDiagnostics.collectRawTypeHex(List.of(
                new InfoBlock.Raw(0x30, InfoBlockType.RAW, new byte[] {1}),
                new InfoBlock.Raw(0x83, InfoBlockType.RAW, new byte[] {2})));

        assertThat(rawTypes).containsExactly("0x30", "0x83");
    }
}
