package core

import (
	"math"
	"testing"
)

func TestGoldenDiff(t *testing.T) {
	t.Run("identicos retorna similarity 1 y matched true", func(t *testing.T) {
		matched, sim := GoldenDiff([]byte("hello"), []byte("hello"), 0.0)
		if !matched {
			t.Errorf("expected matched=true")
		}
		if math.Abs(sim-1.0) > 1e-9 {
			t.Errorf("expected similarity=1.0, got %v", sim)
		}
	})

	t.Run("totalmente distintos retorna similarity 0 y matched false", func(t *testing.T) {
		matched, sim := GoldenDiff([]byte("aaaaa"), []byte("bbbbb"), 0.0)
		if matched {
			t.Errorf("expected matched=false")
		}
		if math.Abs(sim-0.0) > 1e-9 {
			t.Errorf("expected similarity=0.0, got %v", sim)
		}
	})

	t.Run("1 byte distinto threshold 0 falla pero threshold 0.5 pasa", func(t *testing.T) {
		// "hellx" vs "hello": 4 de 5 bytes coinciden -> similarity = 0.8
		actual := []byte("hellx")
		golden := []byte("hello")

		matchedStrict, simStrict := GoldenDiff(actual, golden, 0.0)
		if matchedStrict {
			t.Errorf("threshold=0.0: expected matched=false with 1 byte difference")
		}
		if math.Abs(simStrict-0.8) > 1e-9 {
			t.Errorf("threshold=0.0: expected similarity=0.8, got %v", simStrict)
		}

		matchedLoose, _ := GoldenDiff(actual, golden, 0.5)
		if !matchedLoose {
			t.Errorf("threshold=0.5: expected matched=true with similarity=0.8")
		}
	})

	t.Run("longitudes distintas usa max como denominador", func(t *testing.T) {
		// "ab" vs "abcde": 2 de 5 bytes coinciden -> similarity = 0.4
		matched, sim := GoldenDiff([]byte("ab"), []byte("abcde"), 0.0)
		if matched {
			t.Errorf("expected matched=false")
		}
		if math.Abs(sim-0.4) > 1e-9 {
			t.Errorf("expected similarity=0.4, got %v", sim)
		}
	})
}
