package ingest
import (
"testing"
"sourcecraft.dev/bigbes/lethe/internal/shared/wire"
)
func TestBuildBatches_SplitsByMaxLines(t *testing.T) {
events := []wire.TurnEvent{
turnEvent(0, "a"),
turnEvent(10, "b"),
turnEvent(20, "c"),
}
batches, err := BuildBatches(events, 2, 1024)
if err != nil {
t.Fatalf("BuildBatches: %v", err)
}
if len(batches) != 2 {
t.Fatalf("len(batches) = %d, want 2", len(batches))
}
assertBatch(t, batches[0], []int{0, 1})
assertBatch(t, batches[1], []int{2})
}
func TestBuildBatches_SplitsByMaxBytes(t *testing.T) {
events := []wire.TurnEvent{
turnEvent(0, "a"),
turnEvent(10, "b"),
turnEvent(20, "c"),
}
firstSize := mustEncodedLen(t, events[:1])
batches, err := BuildBatches(events, 100, firstSize+1)
if err != nil {
t.Fatalf("BuildBatches: %v", err)
}
if len(batches) != 3 {
t.Fatalf("len(batches) = %d, want 3", len(batches))
}
assertBatch(t, batches[0], []int{0})
assertBatch(t, batches[1], []int{1})
assertBatch(t, batches[2], []int{2})
}
func TestBuildBatches_RejectsNonPositiveCaps(t *testing.T) {
events := []wire.TurnEvent{turnEvent(0, "a")}
if _, err := BuildBatches(events, 0, 1024); err == nil {
t.Fatal("expected error for zero maxLines")
}
if _, err := BuildBatches(events, 1, 0); err == nil {
t.Fatal("expected error for zero maxBytes")
}
}
func assertBatch(t *testing.T, batch Batch, wantIndexes []int) {
t.Helper()
if len(batch.Events) != len(wantIndexes) {
t.Fatalf("len(batch.Events) = %d, want %d", len(batch.Events), len(wantIndexes))
}
if len(batch.EventIndexes) != len(wantIndexes) {
t.Fatalf("len(batch.EventIndexes) = %d, want %d", len(batch.EventIndexes), len(wantIndexes))
}
for i, want := range wantIndexes {
if batch.EventIndexes[i] != want {
t.Errorf("EventIndexes[%d] = %d, want %d", i, batch.EventIndexes[i], want)
}
}
}
func mustEncodedLen(t *testing.T, events []wire.TurnEvent) int {
t.Helper()
data, err := EncodeNDJSON(events)
if err != nil {
t.Fatalf("EncodeNDJSON: %v", err)
}
return len(data)
}