package core

import (
	"testing"
)

func TestAssertJSONEqual(t *testing.T) {
	t.Run("json identico retorna true y diff vacio", func(t *testing.T) {
		ok, diff := AssertJSONEqual([]byte(`{"a":1}`), []byte(`{"a":1}`))
		if !ok || diff != "" {
			t.Errorf("got ok=%v diff=%q, want ok=true diff=\"\"", ok, diff)
		}
	})

	t.Run("objetos con claves en distinto orden son iguales", func(t *testing.T) {
		ok, diff := AssertJSONEqual(
			[]byte(`{"a":1,"b":2}`),
			[]byte(`{"b":2,"a":1}`),
		)
		if !ok || diff != "" {
			t.Errorf("got ok=%v diff=%q, want ok=true diff=\"\"", ok, diff)
		}
	})

	t.Run("valores distintos retorna false con diff", func(t *testing.T) {
		ok, diff := AssertJSONEqual([]byte(`{"a":1}`), []byte(`{"a":2}`))
		if ok {
			t.Error("expected ok=false for different values")
		}
		if diff == "" {
			t.Error("expected non-empty diff")
		}
	})

	t.Run("json invalido en expected retorna false con mensaje de error", func(t *testing.T) {
		ok, diff := AssertJSONEqual([]byte(`{not json}`), []byte(`{"a":1}`))
		if ok {
			t.Error("expected ok=false for invalid expected JSON")
		}
		if diff == "" {
			t.Error("expected error message in diff")
		}
	})

	t.Run("json invalido en actual retorna false con mensaje de error", func(t *testing.T) {
		ok, diff := AssertJSONEqual([]byte(`{"a":1}`), []byte(`{not json}`))
		if ok {
			t.Error("expected ok=false for invalid actual JSON")
		}
		if diff == "" {
			t.Error("expected error message in diff")
		}
	})

	t.Run("arrays con mismo contenido son iguales", func(t *testing.T) {
		ok, diff := AssertJSONEqual([]byte(`[1,2,3]`), []byte(`[1,2,3]`))
		if !ok || diff != "" {
			t.Errorf("got ok=%v diff=%q, want ok=true diff=\"\"", ok, diff)
		}
	})

	t.Run("arrays con distinto orden son distintos", func(t *testing.T) {
		ok, diff := AssertJSONEqual([]byte(`[1,2,3]`), []byte(`[3,2,1]`))
		if ok {
			t.Error("expected ok=false for different array order")
		}
		if diff == "" {
			t.Error("expected non-empty diff for different arrays")
		}
	})
}
