openai_compact_service_tier_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package service
  2. import (
  3. "bytes"
  4. "context"
  5. "net/http"
  6. "net/http/httptest"
  7. "testing"
  8. "github.com/gin-gonic/gin"
  9. "github.com/stretchr/testify/require"
  10. "github.com/tidwall/gjson"
  11. )
  12. func TestNormalizeOpenAICompactRequestBodyPreservesServiceTier(t *testing.T) {
  13. body := []byte(`{
  14. "model":"gpt-5.6-sol",
  15. "input":[{"type":"message","role":"user","content":"hello"}],
  16. "service_tier":"priority",
  17. "prompt_cache_key":"compact-cache-key",
  18. "store":false,
  19. "stream":true
  20. }`)
  21. normalized, changed, err := normalizeOpenAICompactRequestBody(body)
  22. require.NoError(t, err)
  23. require.True(t, changed)
  24. require.Equal(t, "gpt-5.6-sol", gjson.GetBytes(normalized, "model").String())
  25. require.Equal(t, "priority", gjson.GetBytes(normalized, "service_tier").String())
  26. require.False(t, gjson.GetBytes(normalized, "prompt_cache_key").Exists())
  27. require.False(t, gjson.GetBytes(normalized, "store").Exists())
  28. require.False(t, gjson.GetBytes(normalized, "stream").Exists())
  29. }
  30. func TestOpenAIOAuthCompactHTTPBuildersUsePreservedServiceTierInRoutingHint(t *testing.T) {
  31. gin.SetMode(gin.TestMode)
  32. body := []byte(`{
  33. "model":"gpt-5.6-sol",
  34. "input":[{"type":"message","role":"user","content":"hello"}],
  35. "service_tier":"priority",
  36. "stream":true
  37. }`)
  38. normalized, changed, err := normalizeOpenAICompactRequestBody(body)
  39. require.NoError(t, err)
  40. require.True(t, changed)
  41. require.Equal(t, "priority", gjson.GetBytes(normalized, "service_tier").String())
  42. account := &Account{
  43. Platform: PlatformOpenAI,
  44. Type: AccountTypeOAuth,
  45. Credentials: map[string]any{
  46. "chatgpt_account_id": "test-account",
  47. },
  48. }
  49. svc := &OpenAIGatewayService{}
  50. tests := []struct {
  51. name string
  52. build func(*gin.Context) (*http.Request, error)
  53. }{
  54. {
  55. name: "ordinary",
  56. build: func(c *gin.Context) (*http.Request, error) {
  57. return svc.buildUpstreamRequest(
  58. context.Background(), c, account, normalized, "test-token",
  59. false, "", true,
  60. )
  61. },
  62. },
  63. {
  64. name: "passthrough",
  65. build: func(c *gin.Context) (*http.Request, error) {
  66. return svc.buildUpstreamRequestOpenAIPassthrough(
  67. context.Background(), c, account, normalized, "test-token",
  68. )
  69. },
  70. },
  71. }
  72. for _, tt := range tests {
  73. t.Run(tt.name, func(t *testing.T) {
  74. recorder := httptest.NewRecorder()
  75. c, _ := gin.CreateTestContext(recorder)
  76. c.Request = httptest.NewRequest(
  77. http.MethodPost,
  78. "/v1/responses/compact",
  79. bytes.NewReader(normalized),
  80. )
  81. req, buildErr := tt.build(c)
  82. require.NoError(t, buildErr)
  83. require.Equal(
  84. t,
  85. "model=gpt-5.6-sol;tier=priority",
  86. req.Header.Get(openAICodexRoutingHintHeader),
  87. )
  88. require.Equal(t, "priority", gjson.GetBytes(normalized, "service_tier").String())
  89. })
  90. }
  91. }