package conversion

import (
	"errors"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestConversionError_Builder(t *testing.T) {
	cause := errors.New("原始错误")
	err := NewConversionError(ErrorCodeInvalidInput, "输入无效").
		WithClientProtocol("openai").
		WithDetail("field", "model").
		WithCause(cause)

	assert.Equal(t, ErrorCodeInvalidInput, err.Code)
	assert.Equal(t, "openai", err.ClientProtocol)
	assert.Equal(t, "输入无效", err.Message)
	assert.Equal(t, "model", err.Details["field"])
	assert.Equal(t, cause, err.Cause)
}

func TestConversionError_Unwrap(t *testing.T) {
	cause := errors.New("根本原因")
	err := NewConversionError(ErrorCodeJSONParseError, "解析失败").WithCause(cause)

	unwrapped := err.Unwrap()
	assert.Equal(t, cause, unwrapped)
}

func TestConversionError_Error_WithCause(t *testing.T) {
	err := NewConversionError(ErrorCodeInvalidInput, "输入无效").WithCause(errors.New("原因"))
	msg := err.Error()
	assert.Contains(t, msg, "INVALID_INPUT")
	assert.Contains(t, msg, "输入无效")
	assert.Contains(t, msg, "原因")
}

func TestConversionError_Error_WithoutCause(t *testing.T) {
	err := NewConversionError(ErrorCodeInvalidInput, "输入无效")
	msg := err.Error()
	assert.Contains(t, msg, "INVALID_INPUT")
	assert.Contains(t, msg, "输入无效")
}
