package mellaris

import (
	"errors"
	"testing"
)

func TestConfigError_Error(t *testing.T) {
	e := ConfigError{Field: "port", Err: errors.New("must be > 0")}
	want := "invalid config: port: must be > 0"
	if got := e.Error(); got != want {
		t.Errorf("Error() = %q, want %q", got, want)
	}
}

func TestConfigError_Error_NilErr(t *testing.T) {
	e := ConfigError{Field: "host", Err: nil}
	want := "invalid config: host: %!s(<nil>)"
	if got := e.Error(); got != want {
		t.Errorf("Error() = %q, want %q", got, want)
	}
}

func TestConfigError_Unwrap(t *testing.T) {
	wrapped := errors.New("inner error")
	e := ConfigError{Field: "field", Err: wrapped}
	if got := e.Unwrap(); got != wrapped {
		t.Errorf("Unwrap() = %v, want %v", got, wrapped)
	}
}

func TestConfigError_Unwrap_Nil(t *testing.T) {
	e := ConfigError{Field: "field"}
	if got := e.Unwrap(); got != nil {
		t.Errorf("Unwrap() = %v, want nil", got)
	}
}

func TestConfigError_ErrorsIs(t *testing.T) {
	wrapped := errors.New("inner")
	e := ConfigError{Field: "x", Err: wrapped}
	if !errors.Is(e, wrapped) {
		t.Error("errors.Is should find wrapped error")
	}
}
