Files
sub2api/backend/internal/service/post_billing_platform_test.go
T
李建琦 6d655c9903
Release / update-version (push) Has been cancelled
Release / build-frontend (push) Has been cancelled
Release / release (push) Has been cancelled
Release / sync-version-file (push) Has been cancelled
CI / shell (push) Canceled after 0s
CI / test (push) Canceled after 0s
CI / frontend (push) Canceled after 0s
CI / golangci-lint (push) Canceled after 0s
Security Scan / backend-security (push) Canceled after 0s
Security Scan / frontend-security (push) Canceled after 0s
Sub2API v1.0 - AI API 网关(二开初始版本,基于上游 Wei-Shaw/sub2api)
2026-08-21 18:30:13 +08:00

82 lines
2.5 KiB
Go

//go:build unit
package service
import (
"context"
"testing"
"github.com/Wei-Shaw/sub2api/internal/pkg/ctxkey"
)
func TestPlatformFromAPIKey_NilSafe(t *testing.T) {
if got := PlatformFromAPIKey(nil); got != "" {
t.Errorf("nil APIKey should yield empty string, got %q", got)
}
}
func TestPlatformFromAPIKey_NilGroup(t *testing.T) {
k := &APIKey{Group: nil}
if got := PlatformFromAPIKey(k); got != "" {
t.Errorf("APIKey with nil Group should yield empty string, got %q", got)
}
}
func TestPlatformFromAPIKey_DerivesFromGroup(t *testing.T) {
tests := []struct {
name string
platform string
}{
{"anthropic", "anthropic"},
{"openai", "openai"},
{"gemini", "gemini"},
{"antigravity", "antigravity"},
{"empty", ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
k := &APIKey{
Group: &Group{Platform: tt.platform},
}
got := PlatformFromAPIKey(k)
if got != tt.platform {
t.Errorf("PlatformFromAPIKey(%q) = %q, want %q", tt.platform, got, tt.platform)
}
})
}
}
// TestQuotaPlatform 锁定配额计量口径:ForcePlatform 路由(如 /antigravity)按 ForcePlatform 计,
// 否则回退到 Group 平台。preflight 与 post-billing 共用此口径,保证一致。
func TestQuotaPlatform(t *testing.T) {
apiKey := &APIKey{Group: &Group{Platform: PlatformAnthropic}}
t.Run("no force platform falls back to group platform", func(t *testing.T) {
if got := QuotaPlatform(context.Background(), apiKey); got != PlatformAnthropic {
t.Errorf("QuotaPlatform without force = %q, want %q", got, PlatformAnthropic)
}
})
t.Run("force platform overrides group platform", func(t *testing.T) {
ctx := context.WithValue(context.Background(), ctxkey.ForcePlatform, PlatformAntigravity)
if got := QuotaPlatform(ctx, apiKey); got != PlatformAntigravity {
t.Errorf("QuotaPlatform with force = %q, want %q", got, PlatformAntigravity)
}
})
t.Run("empty force platform falls back to group platform", func(t *testing.T) {
ctx := context.WithValue(context.Background(), ctxkey.ForcePlatform, "")
if got := QuotaPlatform(ctx, apiKey); got != PlatformAnthropic {
t.Errorf("QuotaPlatform with empty force = %q, want %q", got, PlatformAnthropic)
}
})
t.Run("nil api key with force platform returns force platform", func(t *testing.T) {
ctx := context.WithValue(context.Background(), ctxkey.ForcePlatform, PlatformAntigravity)
if got := QuotaPlatform(ctx, nil); got != PlatformAntigravity {
t.Errorf("QuotaPlatform(nil) with force = %q, want %q", got, PlatformAntigravity)
}
})
}