@@ -12,10 +12,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/require"
)
)
func TestApplyPassthroughModelMapping(t *testing.T) {
body := []byte(`{"model":"gpt-4o","messages":[{"role":"user","content":"hi"}]}`)
info := &relaycommon.RelayInfo{ChannelMeta: &relaycommon.ChannelMeta{IsModelMapped: true, UpstreamModelName: "gpt-4.1"}}
func TestPassthroughModelMapping_AppliedWhenEnabled(t *testing.T) {
origRequestEnabled := model_setting.GetGlobalSettings().PassThroughRequestEnabled
origRequestEnabled := model_setting.GetGlobalSettings().PassThroughRequestEnabled
origApplyMapping := model_setting.GetGlobalSettings().PassthroughApplyModelMapping
origApplyMapping := model_setting.GetGlobalSettings().PassthroughApplyModelMapping
defer func() {
defer func() {
@@ -26,14 +23,15 @@ func TestApplyPassthroughModelMapping(t *testing.T) {
model_setting.GetGlobalSettings().PassThroughRequestEnabled = true
model_setting.GetGlobalSettings().PassThroughRequestEnabled = true
model_setting.GetGlobalSettings().PassthroughApplyModelMapping = true
model_setting.GetGlobalSettings().PassthroughApplyModelMapping = true
info := &relaycommon.RelayInfo{ChannelMeta: &relaycommon.ChannelMeta{IsModelMapped: true, UpstreamModelName: "gpt-4.1"}}
body := []byte(`{"model":"gpt-4o","messages":[{"role":"user","content":"hi"}]}`)
assert.True(t, needsPassthroughModelMapping(info))
got := applyPassthroughModelMapping(body, info)
got := applyPassthroughModelMapping(body, info)
assert.JSONEq(t, `{"model":"gpt-4.1","messages":[{"role":"user","content":"hi"}]}`, string(got))
assert.JSONEq(t, `{"model":"gpt-4.1","messages":[{"role":"user","content":"hi"}]}`, string(got))
}
}
func TestApplyPassthroughModelMapping_NonJSONKeepsOriginal(t *testing.T) {
body := []byte(`model=gpt-4o`)
info := &relaycommon.RelayInfo{ChannelMeta: &relaycommon.ChannelMeta{IsModelMapped: true, UpstreamModelName: "gpt-4.1"}}
func TestPassthroughModelMapping_SkippedWhenNotMapped(t *testing.T) {
origRequestEnabled := model_setting.GetGlobalSettings().PassThroughRequestEnabled
origRequestEnabled := model_setting.GetGlobalSettings().PassThroughRequestEnabled
origApplyMapping := model_setting.GetGlobalSettings().PassthroughApplyModelMapping
origApplyMapping := model_setting.GetGlobalSettings().PassthroughApplyModelMapping
defer func() {
defer func() {
@@ -44,14 +42,11 @@ func TestApplyPassthroughModelMapping_NonJSONKeepsOriginal(t *testing.T) {
model_setting.GetGlobalSettings().PassThroughRequestEnabled = true
model_setting.GetGlobalSettings().PassThroughRequestEnabled = true
model_setting.GetGlobalSettings().PassthroughApplyModelMapping = true
model_setting.GetGlobalSettings().PassthroughApplyModelMapping = true
got := applyPassthroughModelMapping(body, info)
assert.Equal(t, body, got)
}
func TestApplyPassthroughModelMapping_DisabledKeepsOriginal(t *testing.T) {
body := []byte(`{"model":"gpt-4o"}`)
info := &relaycommon.RelayInfo{ChannelMeta: &relaycommon.ChannelMeta{IsModelMapped: false, UpstreamModelName: "gpt-4.1"}}
info := &relaycommon.RelayInfo{ChannelMeta: &relaycommon.ChannelMeta{IsModelMapped: false, UpstreamModelName: "gpt-4.1"}}
assert.False(t, needsPassthroughModelMapping(info))
}
func TestPassthroughModelMapping_SkippedWhenPassthroughOff(t *testing.T) {
origRequestEnabled := model_setting.GetGlobalSettings().PassThroughRequestEnabled
origRequestEnabled := model_setting.GetGlobalSettings().PassThroughRequestEnabled
origApplyMapping := model_setting.GetGlobalSettings().PassthroughApplyModelMapping
origApplyMapping := model_setting.GetGlobalSettings().PassthroughApplyModelMapping
defer func() {
defer func() {
@@ -59,26 +54,18 @@ func TestApplyPassthroughModelMapping_DisabledKeepsOriginal(t *testing.T) {
model_setting.GetGlobalSettings().PassthroughApplyModelMapping = origApplyMapping
model_setting.GetGlobalSettings().PassthroughApplyModelMapping = origApplyMapping
}()
}()
model_setting.GetGlobalSettings().PassThroughRequestEnabled = tru e
model_setting.GetGlobalSettings().PassThroughRequestEnabled = fals e
model_setting.GetGlobalSettings().PassthroughApplyModelMapping = true
model_setting.GetGlobalSettings().PassthroughApplyModelMapping = true
got := applyPassthroughModelMapping(body, info)
assert.Equal(t, body, got )
info := &relaycommon.RelayInfo{ChannelMeta: &relaycommon.ChannelMeta{IsModelMapped: true, UpstreamModelName: "gpt-4.1"}}
assert.False(t, needsPassthroughModelMapping(info) )
}
}
func TestDoApiRequest_DoesNotChangeNonPassthroughBody(t *testing.T) {
origRequestEnabled := model_setting.GetGlobalSettings().PassThroughRequestEnabled
origApplyMapping := model_setting.GetGlobalSettings().PassthroughApplyModelMapping
defer func() {
model_setting.GetGlobalSettings().PassThroughRequestEnabled = origRequestEnabled
model_setting.GetGlobalSettings().PassthroughApplyModelMapping = origApplyMapping
}()
model_setting.GetGlobalSettings().PassThroughRequestEnabled = false
model_setting.GetGlobalSettings().PassthroughApplyModelMapping = true
func TestPassthroughModelMapping_NonJSONKeepsOriginal(t *testing.T) {
body := []byte(`model=gpt-4o`)
info := &relaycommon.RelayInfo{ChannelMeta: &relaycommon.ChannelMeta{IsModelMapped: true, UpstreamModelName: "gpt-4.1"}}
body := []byte(`{"model":"gpt-4o"}`)
got := applyPassthroughModelMapping(body, &relaycommon.RelayInfo{ChannelMeta: &relaycommon.ChannelMeta{IsModelMapped: true, UpstreamModelName: "gpt-4.1"}})
got := applyPassthroughModelMapping(body, info)
assert.Equal(t, body, got)
assert.Equal(t, body, got)
}
}