package utils

import (
	"testing"
)

func TestExtractSegmentSamples(t *testing.T) {
	samples := []float64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
	sampleRate := 2 // 2 samples per sec

	// sec 1.0 to 3.0 => indices 2 to 6 => [2, 3, 4, 5]
	seg := ExtractSegmentSamples(samples, sampleRate, 1.0, 3.0)
	if len(seg) != 4 || seg[0] != 2 || seg[len(seg)-1] != 5 {
		t.Errorf("unexpected segment extraction: %v", seg)
	}

	// out of bounds
	empty := ExtractSegmentSamples(samples, sampleRate, 5.0, 6.0)
	if len(empty) != 0 {
		t.Error("expected empty segment")
	}
}

func TestGenerateSpectrogram_Basic(t *testing.T) {
	samples := make([]float64, 1000) // Silent buffer
	cfg := DefaultSpectrogramConfig(16000)
	res := GenerateSpectrogram(samples, cfg)
	if len(res) == 0 {
		t.Error("expected spectrogram generation to succeed")
	}

	shortSamples := []float64{0.0, 0.1}
	resShort := GenerateSpectrogram(shortSamples, cfg)
	if resShort != nil {
		t.Error("expected nil for samples smaller than window size")
	}
}

func TestGetCachedHannWindow(t *testing.T) {
	w1 := getCachedHannWindow(256)
	w2 := getCachedHannWindow(256)

	if len(w1) != 256 {
		t.Errorf("expected length 256, got %d", len(w1))
	}

	// Ensure memory address is the same (cached)
	if &w1[0] != &w2[0] {
		t.Error("expected cached slice to have the same memory address")
	}
}