package aoiprxkit

import "testing"

func TestJitterBufferReordersAndReleases(t *testing.T) {
	jb := newJitterBuffer(8)
	p100 := RTPPacket{SequenceNumber: 100}
	p102 := RTPPacket{SequenceNumber: 102}
	p101 := RTPPacket{SequenceNumber: 101}

	ready, late, gap, reorder := jb.push(p100)
	if late || gap != 0 || reorder {
		t.Fatalf("unexpected state on first push")
	}
	if len(ready) != 1 || ready[0].SequenceNumber != 100 {
		t.Fatalf("unexpected ready on first push: %+v", ready)
	}

	ready, late, gap, reorder = jb.push(p102)
	if late || gap != 0 || !reorder {
		t.Fatalf("expected reorder on out-of-order push")
	}
	if len(ready) != 0 {
		t.Fatalf("unexpected ready before missing packet arrives: %+v", ready)
	}

	ready, late, gap, reorder = jb.push(p101)
	if late || gap != 0 {
		t.Fatalf("unexpected late/gap after completing sequence")
	}
	if len(ready) != 2 || ready[0].SequenceNumber != 101 || ready[1].SequenceNumber != 102 {
		t.Fatalf("unexpected ready after sequence repair: %+v", ready)
	}
}
