package ingest

import "testing"

func TestChunkToFramesMonoDuplicate(t *testing.T) {
	frames, err := ChunkToFrames(PCMChunk{
		Channels: 1,
		Samples:  []int32{2147483647, -2147483648},
	})
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if len(frames) != 2 {
		t.Fatalf("expected 2 frames, got %d", len(frames))
	}
	if frames[0].L != frames[0].R {
		t.Fatalf("expected mono duplication, got L=%v R=%v", frames[0].L, frames[0].R)
	}
	if frames[1].L != frames[1].R {
		t.Fatalf("expected mono duplication, got L=%v R=%v", frames[1].L, frames[1].R)
	}
}

func TestChunkToFramesStereoPassThrough(t *testing.T) {
	frames, err := ChunkToFrames(PCMChunk{
		Channels: 2,
		Samples:  []int32{100, 200, -300, -400},
	})
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if len(frames) != 2 {
		t.Fatalf("expected 2 frames, got %d", len(frames))
	}
	if !(frames[0].L < frames[0].R) {
		t.Fatalf("expected left < right for first frame, got %v >= %v", frames[0].L, frames[0].R)
	}
	if !(frames[1].L > frames[1].R) {
		t.Fatalf("expected left > right for second frame, got %v <= %v", frames[1].L, frames[1].R)
	}
}

func TestChunkToFramesRejectsUnsupportedChannels(t *testing.T) {
	_, err := ChunkToFrames(PCMChunk{Channels: 3, Samples: []int32{1, 2, 3}})
	if err == nil {
		t.Fatal("expected error for unsupported channels")
	}
}

func TestChunkToFramesRejectsInvalidInterleaving(t *testing.T) {
	_, err := ChunkToFrames(PCMChunk{Channels: 2, Samples: []int32{1, 2, 3}})
	if err == nil {
		t.Fatal("expected error for invalid interleaving")
	}
}
