mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-06 08:06:50 +00:00
test(openai): cover websocket tier separation
This commit is contained in:
1 parent
2b8cb628b6
commit
d39fc491ec
4 files changed
+26
-25
No files matched your search
@@ -888,8 +888,8 @@ func TestApplyOpenAIFastPolicyToBody_PassNormalizesFastAlias(t *testing.T) {
|
||||
// TestPassthroughBilling_PostFilterServiceTier is the fix3 regression. The
|
||||
// passthrough adapter (openai_ws_v2_passthrough_adapter.go) now extracts
|
||||
// requestServiceTier from firstClientMessage AFTER applyOpenAIFastPolicy
|
||||
// has rewritten it, so a filter hit causes billing to report nil (default
|
||||
// tier) instead of the user-requested "priority". This test pins the
|
||||
// has rewritten it, so a filter hit preserves a nil outbound tier instead of
|
||||
// the user-requested "priority". This test pins the
|
||||
// contract those two helpers must uphold for the adapter's billing path.
|
||||
func TestPassthroughBilling_PostFilterServiceTier(t *testing.T) {
|
||||
svc := newOpenAIGatewayServiceWithSettings(t, openAIFastFilterPriorityPolicy())
|
||||
@@ -912,10 +912,10 @@ func TestPassthroughBilling_PostFilterServiceTier(t *testing.T) {
|
||||
require.NotContains(t, string(filtered), `"service_tier"`)
|
||||
|
||||
// Post-filter: extracting from the rewritten frame returns nil. This
|
||||
// is the value the adapter now passes to OpenAIForwardResult.ServiceTier,
|
||||
// so billing records "default" instead of "priority".
|
||||
// is the value the adapter now passes to OpenAIForwardResult.ServiceTier;
|
||||
// any observed response tier remains separate for usage-time resolution.
|
||||
post := extractOpenAIServiceTierFromBody(filtered)
|
||||
require.Nil(t, post, "fix3: post-filter extraction must return nil so passthrough billing reports default tier instead of the requested priority")
|
||||
require.Nil(t, post, "fix3: post-filter extraction must preserve a nil outbound tier")
|
||||
|
||||
// And the byte-level invariant the adapter relies on: filtering an
|
||||
// already-filtered frame is a no-op (idempotent), so re-running the
|
||||
@@ -1012,7 +1012,7 @@ func TestPassthroughBilling_MultiTurnServiceTierFollowsFilteredFrames(t *testing
|
||||
}
|
||||
|
||||
// First-frame initialization mirrors the adapter: extract from the
|
||||
// post-filter payload so a filter-on-first-frame zeroes billing too.
|
||||
// post-filter payload so a filter-on-first-frame clears the outbound tier.
|
||||
firstFrame := []byte(`{"type":"response.create","model":"gpt-5.5","service_tier":"priority"}`)
|
||||
firstOut, firstBlocked, firstErr := svc.applyOpenAIFastPolicyToWSResponseCreate(context.Background(), account, "gpt-5.5", firstFrame)
|
||||
require.NoError(t, firstErr)
|
||||
@@ -1020,7 +1020,7 @@ func TestPassthroughBilling_MultiTurnServiceTierFollowsFilteredFrames(t *testing
|
||||
requestServiceTierPtr.Store(extractOpenAIServiceTierFromBody(firstOut))
|
||||
capturedSessionModel = openAIWSPassthroughPolicyModelForFrame(account, firstFrame)
|
||||
require.Nil(t, requestServiceTierPtr.Load(),
|
||||
"turn 1: filter strips service_tier=priority, billing must reflect upstream-actual nil tier")
|
||||
"turn 1: filter strips service_tier=priority, leaving a nil outbound tier")
|
||||
|
||||
// Turn 2: client switches to flex, should pass and update billing.
|
||||
turn2 := []byte(`{"type":"response.create","model":"gpt-5.5","service_tier":"flex"}`)
|
||||
|
||||
@@ -12,10 +12,9 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// HTTP POST /v1/responses → forwardOpenAIWSV2 共用 stream/non-stream 的
|
||||
// OpenAIForwardResult:上游 response.completed.service_tier 必须覆盖请求
|
||||
// fast/priority,不能只读 reqBody。
|
||||
func TestForwardOpenAIWSV2_UpstreamDefaultServiceTierWinsOverRequest(t *testing.T) {
|
||||
// HTTP POST /v1/responses -> forwardOpenAIWSV2 keeps the canonical outbound
|
||||
// tier separate from response.completed.service_tier for usage-time billing.
|
||||
func TestForwardOpenAIWSV2_KeepsOutboundAndObservedServiceTiersSeparate(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
cases := []struct {
|
||||
@@ -88,7 +87,8 @@ func TestForwardOpenAIWSV2_UpstreamDefaultServiceTierWinsOverRequest(t *testing.
|
||||
require.Equal(t, tc.stream, result.Stream)
|
||||
require.Equal(t, "resp_tier_v2", result.RequestID)
|
||||
require.NotNil(t, result.ServiceTier)
|
||||
require.Equal(t, "default", *result.ServiceTier)
|
||||
require.Equal(t, "priority", *result.ServiceTier)
|
||||
require.Equal(t, "default", result.UpstreamResponseServiceTier)
|
||||
require.Equal(t, "priority", captureConn.lastWrite["service_tier"],
|
||||
"outbound WS payload still carries the requested Fast tier")
|
||||
})
|
||||
|
||||
@@ -60,13 +60,13 @@ func TestPrepareOpenAIWSHTTPBridgeBodyStripsNoneReasoningForCompatibleEndpoint(t
|
||||
require.Equal(t, "none", gjson.GetBytes(officialBody, "reasoning.effort").String())
|
||||
}
|
||||
|
||||
func TestProxyOpenAIWSHTTPBridgeTurn_UpstreamDefaultServiceTierWinsOverRequest(t *testing.T) {
|
||||
func TestProxyOpenAIWSHTTPBridgeTurn_KeepsOutboundAndObservedServiceTiersSeparate(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
// proxyOpenAIWSHTTPBridgeTurn 是 client WS→HTTP bridge,本身不 canonicalize
|
||||
// fast→priority;生产入口的归一化在 openai_ws_forwarder_ingress.go 的 fast
|
||||
// policy。本测试只覆盖局部 observer:canonical 请求 priority 被上游
|
||||
// response.completed service_tier=default 覆盖。
|
||||
// policy. This test covers the local observer while preserving the canonical
|
||||
// outbound priority tier independently.
|
||||
sse := strings.Join([]string{
|
||||
`data: {"type":"response.completed","response":{"id":"resp_tier","model":"gpt-5.5","status":"completed","service_tier":"default","usage":{"input_tokens":1,"output_tokens":1}}}`,
|
||||
``,
|
||||
@@ -96,14 +96,15 @@ func TestProxyOpenAIWSHTTPBridgeTurn_UpstreamDefaultServiceTierWinsOverRequest(t
|
||||
require.NotNil(t, result)
|
||||
require.Equal(t, "priority", gjson.GetBytes(upstream.lastBody, "service_tier").String())
|
||||
require.NotNil(t, result.ServiceTier)
|
||||
require.Equal(t, "default", *result.ServiceTier)
|
||||
require.Equal(t, "priority", *result.ServiceTier)
|
||||
require.Equal(t, "default", result.UpstreamResponseServiceTier)
|
||||
}
|
||||
|
||||
func TestProxyOpenAIWSHTTPBridgeTurn_UpstreamDefaultWinsOverFastAlias(t *testing.T) {
|
||||
func TestProxyOpenAIWSHTTPBridgeTurn_NormalizesFastWithoutLosingObservedDefault(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
// 客户端别名 fast 同样被上游回显的 default 覆盖:局部 observer 的
|
||||
// ServiceTier() 是唯一计费依据,绝不回退到请求侧 fast。
|
||||
// The client alias fast is canonicalized to priority while the local observer
|
||||
// independently captures the upstream default declaration.
|
||||
sse := strings.Join([]string{
|
||||
`data: {"type":"response.completed","response":{"id":"resp_tier2","model":"gpt-5.5","status":"completed","service_tier":"default","usage":{"input_tokens":1,"output_tokens":1}}}`,
|
||||
``,
|
||||
@@ -132,8 +133,8 @@ func TestProxyOpenAIWSHTTPBridgeTurn_UpstreamDefaultWinsOverFastAlias(t *testing
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
require.NotNil(t, result.ServiceTier)
|
||||
require.Equal(t, "default", *result.ServiceTier,
|
||||
"local observer's upstream-echoed default must win over the fast alias")
|
||||
require.Equal(t, "priority", *result.ServiceTier)
|
||||
require.Equal(t, "default", result.UpstreamResponseServiceTier)
|
||||
}
|
||||
|
||||
func TestProxyOpenAIWSHTTPBridgeTurnAPIKeyAdaptsClientTools(t *testing.T) {
|
||||
|
||||
@@ -793,10 +793,10 @@ func (s *OpenAIGatewayService) proxyResponsesWebSocketV2Passthrough(
|
||||
firstClientMessage = updatedFirst
|
||||
|
||||
// 在 policy filter 之后再提取 service_tier / reasoning_effort 用于
|
||||
// usage 上报:filter
|
||||
// 命中时 service_tier 已经从 firstClientMessage 中删除,billing 应当
|
||||
// 反映上游实际处理的 tier(nil = default),而不是用户最初请求的
|
||||
// "priority"。HTTP 入口(line ~2728 extractOpenAIServiceTier(reqBody))
|
||||
// usage 上报:filter 命中时 service_tier 已经从 firstClientMessage 中删除,
|
||||
// 最终出站 tier 应为 nil,而不是用户最初请求的 "priority"。观察到的回包
|
||||
// tier 单独保存在 UpstreamResponseServiceTier,由 usage 阶段统一决策。
|
||||
// HTTP 入口(line ~2728 extractOpenAIServiceTier(reqBody))
|
||||
// 与 WS ingress(openai_ws_forwarder.go:2991 取自 payload)的语义一致。
|
||||
//
|
||||
// 多轮 passthrough:OpenAI Realtime / Responses WS 协议允许客户端在
|
||||
|
||||
Reference in new issue
Block a user