package infra

import (
	"testing"
)

func TestConfigMerge(t *testing.T) {
	t.Run("override gana sobre base en claves comunes", func(t *testing.T) {
		base := map[string]string{"host": "localhost", "port": "5432"}
		over := map[string]string{"port": "9999"}
		got := ConfigMerge(base, over)
		if got["port"] != "9999" {
			t.Errorf("expected port=9999, got %q", got["port"])
		}
		if got["host"] != "localhost" {
			t.Errorf("expected host=localhost, got %q", got["host"])
		}
	})

	t.Run("claves solo en base se mantienen", func(t *testing.T) {
		base := map[string]string{"a": "1", "b": "2"}
		over := map[string]string{}
		got := ConfigMerge(base, over)
		if got["a"] != "1" || got["b"] != "2" {
			t.Errorf("unexpected result: %v", got)
		}
	})

	t.Run("claves solo en override se agregan", func(t *testing.T) {
		base := map[string]string{}
		over := map[string]string{"x": "new"}
		got := ConfigMerge(base, over)
		if got["x"] != "new" {
			t.Errorf("expected x=new, got %q", got["x"])
		}
	})

	t.Run("no muta el map base", func(t *testing.T) {
		base := map[string]string{"key": "original"}
		over := map[string]string{"key": "changed"}
		_ = ConfigMerge(base, over)
		if base["key"] != "original" {
			t.Errorf("base was mutated: got %q", base["key"])
		}
	})

	t.Run("merge con maps vacios retorna mapa vacio", func(t *testing.T) {
		got := ConfigMerge(map[string]string{}, map[string]string{})
		if len(got) != 0 {
			t.Errorf("expected empty map, got %v", got)
		}
	})
}
