package aoiprxkit

import (
	"bytes"
	"testing"
)

func TestWriteAndReadPCM32Packet(t *testing.T) {
	var buf bytes.Buffer
	samples := []int32{1, -1, 10, -10}
	if err := WritePCM32Packet(&buf, 2, 48000, 2, 7, 1234, samples); err != nil {
		t.Fatalf("unexpected write error: %v", err)
	}
	hdr, err := ReadStreamHeader(&buf)
	if err != nil {
		t.Fatalf("unexpected read header error: %v", err)
	}
	if hdr.Codec != StreamCodecPCM_S32LE || hdr.Channels != 2 || hdr.SampleRateHz != 48000 || hdr.FrameSamples != 2 || hdr.Sequence != 7 || hdr.Timestamp != 1234 || hdr.PayloadBytes != 16 {
		t.Fatalf("unexpected header: %+v", hdr)
	}
	payload := make([]byte, hdr.PayloadBytes)
	if _, err := buf.Read(payload); err != nil {
		t.Fatalf("unexpected payload read error: %v", err)
	}
	got, err := DecodeS32LE(payload, 2)
	if err != nil {
		t.Fatalf("unexpected decode error: %v", err)
	}
	for i := range samples {
		if got[i] != samples[i] {
			t.Fatalf("sample %d mismatch: got=%d want=%d", i, got[i], samples[i])
		}
	}
}
