package audio

import (
	"math"
	"testing"
)

func TestResampledSource(t *testing.T) {
	src := &WAVSource{frames: []Frame{NewFrame(0.1, 0.1), NewFrame(0.2, 0.2)}, SampleRate: 48000}
	rs := NewResampledSource(src, 96000)
	a := rs.NextFrame()
	b := rs.NextFrame()
	if a == (Frame{}) || b == (Frame{}) {
		t.Fatal("expected non-zero frames")
	}
}

func TestResampledSourceInterpolation(t *testing.T) {
	// 2 samples at 48k, target at 96k -> ratio=0.5, so we should
	// get interpolated values between the two source frames.
	src := &WAVSource{
		frames:     []Frame{NewFrame(0, 0), NewFrame(1, 1)},
		SampleRate: 48000,
	}
	rs := NewResampledSource(src, 96000)

	// First sample: position=0.0, exact frame[0] -> (0,0)
	f0 := rs.NextFrame()
	if math.Abs(float64(f0.L)) > 1e-9 {
		t.Fatalf("expected L=0 at pos 0, got %.6f", f0.L)
	}

	// Second sample: position=0.5, interpolated -> (0.5, 0.5)
	f1 := rs.NextFrame()
	if math.Abs(float64(f1.L)-0.5) > 1e-9 {
		t.Fatalf("expected L=0.5 at pos 0.5, got %.6f", f1.L)
	}
}

func TestResampledSourceNilSrc(t *testing.T) {
	rs := NewResampledSource(nil, 48000)
	f := rs.NextFrame()
	if f.L != 0 || f.R != 0 {
		t.Fatal("expected zero frame for nil source")
	}
}
