mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-05-12 00:58:11 +08:00
- Added `buildAdditionalModelRecord` to filter out zero-token usage details. - Introduced `hasNonZeroTokenUsage` helper function for token usage validation. - Updated tests to cover scenarios for zero and non-zero token usage.
83 lines
2.7 KiB
Go
83 lines
2.7 KiB
Go
package helps
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/usage"
|
|
)
|
|
|
|
func TestParseOpenAIUsageChatCompletions(t *testing.T) {
|
|
data := []byte(`{"usage":{"prompt_tokens":1,"completion_tokens":2,"total_tokens":3,"prompt_tokens_details":{"cached_tokens":4},"completion_tokens_details":{"reasoning_tokens":5}}}`)
|
|
detail := ParseOpenAIUsage(data)
|
|
if detail.InputTokens != 1 {
|
|
t.Fatalf("input tokens = %d, want %d", detail.InputTokens, 1)
|
|
}
|
|
if detail.OutputTokens != 2 {
|
|
t.Fatalf("output tokens = %d, want %d", detail.OutputTokens, 2)
|
|
}
|
|
if detail.TotalTokens != 3 {
|
|
t.Fatalf("total tokens = %d, want %d", detail.TotalTokens, 3)
|
|
}
|
|
if detail.CachedTokens != 4 {
|
|
t.Fatalf("cached tokens = %d, want %d", detail.CachedTokens, 4)
|
|
}
|
|
if detail.ReasoningTokens != 5 {
|
|
t.Fatalf("reasoning tokens = %d, want %d", detail.ReasoningTokens, 5)
|
|
}
|
|
}
|
|
|
|
func TestParseOpenAIUsageResponses(t *testing.T) {
|
|
data := []byte(`{"usage":{"input_tokens":10,"output_tokens":20,"total_tokens":30,"input_tokens_details":{"cached_tokens":7},"output_tokens_details":{"reasoning_tokens":9}}}`)
|
|
detail := ParseOpenAIUsage(data)
|
|
if detail.InputTokens != 10 {
|
|
t.Fatalf("input tokens = %d, want %d", detail.InputTokens, 10)
|
|
}
|
|
if detail.OutputTokens != 20 {
|
|
t.Fatalf("output tokens = %d, want %d", detail.OutputTokens, 20)
|
|
}
|
|
if detail.TotalTokens != 30 {
|
|
t.Fatalf("total tokens = %d, want %d", detail.TotalTokens, 30)
|
|
}
|
|
if detail.CachedTokens != 7 {
|
|
t.Fatalf("cached tokens = %d, want %d", detail.CachedTokens, 7)
|
|
}
|
|
if detail.ReasoningTokens != 9 {
|
|
t.Fatalf("reasoning tokens = %d, want %d", detail.ReasoningTokens, 9)
|
|
}
|
|
}
|
|
|
|
func TestUsageReporterBuildRecordIncludesLatency(t *testing.T) {
|
|
reporter := &UsageReporter{
|
|
provider: "openai",
|
|
model: "gpt-5.4",
|
|
requestedAt: time.Now().Add(-1500 * time.Millisecond),
|
|
}
|
|
|
|
record := reporter.buildRecord(usage.Detail{TotalTokens: 3}, false)
|
|
if record.Latency < time.Second {
|
|
t.Fatalf("latency = %v, want >= 1s", record.Latency)
|
|
}
|
|
if record.Latency > 3*time.Second {
|
|
t.Fatalf("latency = %v, want <= 3s", record.Latency)
|
|
}
|
|
}
|
|
|
|
func TestUsageReporterBuildAdditionalModelRecordSkipsZeroTokens(t *testing.T) {
|
|
reporter := &UsageReporter{
|
|
provider: "codex",
|
|
model: "gpt-5.4",
|
|
requestedAt: time.Now(),
|
|
}
|
|
|
|
if _, ok := reporter.buildAdditionalModelRecord("gpt-image-2", usage.Detail{}); ok {
|
|
t.Fatalf("expected all-zero token usage to be skipped")
|
|
}
|
|
if _, ok := reporter.buildAdditionalModelRecord("gpt-image-2", usage.Detail{InputTokens: 2}); !ok {
|
|
t.Fatalf("expected non-zero input token usage to be recorded")
|
|
}
|
|
if _, ok := reporter.buildAdditionalModelRecord("gpt-image-2", usage.Detail{CachedTokens: 2}); !ok {
|
|
t.Fatalf("expected non-zero cached token usage to be recorded")
|
|
}
|
|
}
|