package dsp

import (
	"math"
	"testing"
)

func TestMPXLimiterPassesQuiet(t *testing.T) {
	lim := NewMPXLimiter(1.0, 0.1, 50, 228000)
	for i := 0; i < 100; i++ {
		in := 0.3 * math.Sin(float64(i))
		out := lim.Process(in)
		if math.Abs(out-in) > 1e-9 {
			t.Fatalf("limiter altered quiet signal at sample %d: in=%.6f out=%.6f", i, in, out)
		}
	}
}

func TestMPXLimiterClamps(t *testing.T) {
	lim := NewMPXLimiter(1.0, 0.01, 50, 228000)
	// Feed a signal well above ceiling
	var maxOut float64
	for i := 0; i < 10000; i++ {
		in := 3.0 * math.Sin(2*math.Pi*1000*float64(i)/228000)
		out := lim.Process(in)
		if math.Abs(out) > maxOut {
			maxOut = math.Abs(out)
		}
	}
	// After attack settles, output should approach ceiling
	if maxOut > 1.5 {
		t.Fatalf("limiter didn't reduce level enough: maxOut=%.4f", maxOut)
	}
}

func TestHardClip(t *testing.T) {
	if HardClip(1.5, 1.0) != 1.0 {
		t.Fatal("expected clip to 1.0")
	}
	if HardClip(-1.5, 1.0) != -1.0 {
		t.Fatal("expected clip to -1.0")
	}
	if HardClip(0.5, 1.0) != 0.5 {
		t.Fatal("expected passthrough")
	}
}
