package eventbus

import (
	"testing"

	"github.com/segmentio/kafka-go"
)

func TestMessagesAfterCommittedPrefixesKeepsPartitionGaps(t *testing.T) {
	messages := []kafka.Message{
		{Topic: "raw", Partition: 0, Offset: 10},
		{Topic: "raw", Partition: 1, Offset: 20},
		{Topic: "raw", Partition: 0, Offset: 11},
		{Topic: "raw", Partition: 1, Offset: 21},
		{Topic: "raw", Partition: 0, Offset: 12},
	}
	committed := []kafka.Message{
		{Topic: "raw", Partition: 0, Offset: 10},
		{Topic: "raw", Partition: 1, Offset: 21},
	}

	remaining := MessagesAfterCommittedPrefixes(messages, committed)
	if len(remaining) != 2 || remaining[0].Partition != 0 || remaining[0].Offset != 11 || remaining[1].Offset != 12 {
		t.Fatalf("remaining = %#v", remaining)
	}
}

func TestMessagesAfterCommittedPrefixesWithoutCommitKeepsWholeBatch(t *testing.T) {
	messages := []kafka.Message{{Topic: "raw", Partition: 0, Offset: 10}}
	remaining := MessagesAfterCommittedPrefixes(messages, nil)
	if len(remaining) != 1 || remaining[0].Offset != 10 {
		t.Fatalf("remaining = %#v", remaining)
	}
}
