refactor: 实现 ConversionEngine 协议转换引擎,替代旧 protocol 包
引入 Canonical Model 和 ProtocolAdapter 架构,支持 OpenAI/Anthropic 协议间 无缝转换,统一 ProxyHandler 替代分散的 OpenAI/Anthropic Handler,简化 ProviderClient 为协议无关的 HTTP 发送器,Provider 新增 protocol 字段。
This commit is contained in:
45
backend/internal/conversion/errors_test.go
Normal file
45
backend/internal/conversion/errors_test.go
Normal file
@@ -0,0 +1,45 @@
|
||||
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, "输入无效")
|
||||
}
|
||||
Reference in New Issue
Block a user