Files
CLIProxyAPI/sdk/api/handlers/handlers_metadata_test.go
Luis Pater 71c185f614 feat(usage): add service tier tracking and defaults in usage reporting
- Introduced `service_tier` metadata key to capture client-requested service tiers.
- Updated usage records, context propagation, and plugins to include service tier data.
- Added default handling logic for cases where `service_tier` is absent.
- Implemented tests for `service_tier` extraction, defaults, and updates across components.
2026-05-28 22:15:54 +08:00

63 lines
2.0 KiB
Go

package handlers
import (
"testing"
coreexecutor "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/executor"
"golang.org/x/net/context"
)
func TestRequestExecutionMetadataIncludesExecutionSessionWithoutIdempotencyKey(t *testing.T) {
ctx := WithExecutionSessionID(context.Background(), "session-1")
meta := requestExecutionMetadata(ctx)
if got := meta[coreexecutor.ExecutionSessionMetadataKey]; got != "session-1" {
t.Fatalf("ExecutionSessionMetadataKey = %v, want %q", got, "session-1")
}
if _, ok := meta[idempotencyKeyMetadataKey]; ok {
t.Fatalf("unexpected idempotency key in metadata: %v", meta[idempotencyKeyMetadataKey])
}
}
func TestSetReasoningEffortMetadataUsesSuffixOverBody(t *testing.T) {
meta := make(map[string]any)
setReasoningEffortMetadata(meta, "openai", "gpt-5.4(high)", []byte(`{"reasoning_effort":"low"}`))
if got := meta[coreexecutor.ReasoningEffortMetadataKey]; got != "high" {
t.Fatalf("ReasoningEffortMetadataKey = %v, want %q", got, "high")
}
}
func TestSetReasoningEffortMetadataSupportsOpenAIResponses(t *testing.T) {
meta := make(map[string]any)
setReasoningEffortMetadata(meta, "openai-response", "gpt-5.4", []byte(`{"reasoning":{"effort":"medium"}}`))
if got := meta[coreexecutor.ReasoningEffortMetadataKey]; got != "medium" {
t.Fatalf("ReasoningEffortMetadataKey = %v, want %q", got, "medium")
}
}
func TestSetServiceTierMetadataExtractsValue(t *testing.T) {
meta := make(map[string]any)
setServiceTierMetadata(meta, []byte(`{"service_tier":"priority"}`))
gotServiceTier := meta[coreexecutor.ServiceTierMetadataKey]
if gotServiceTier != "priority" {
t.Fatalf("ServiceTierMetadataKey = %v, want %q", gotServiceTier, "priority")
}
}
func TestSetServiceTierMetadataDefaultsWhenMissing(t *testing.T) {
meta := make(map[string]any)
setServiceTierMetadata(meta, []byte(`{"model":"gpt-5.4"}`))
gotServiceTier := meta[coreexecutor.ServiceTierMetadataKey]
if gotServiceTier != "default" {
t.Fatalf("ServiceTierMetadataKey = %v, want %q", gotServiceTier, "default")
}
}