gateway_usage_billing_fallback_test.go 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //go:build unit
  2. package service
  3. import (
  4. "context"
  5. "testing"
  6. "github.com/Wei-Shaw/sub2api/internal/config"
  7. "github.com/stretchr/testify/require"
  8. )
  9. // composite 分组的公开别名经 BillingModelSource 来源覆盖成为计费模型后有两类错计:
  10. // 任意别名(如 team/best)查无价静默落 $0;含家族词的别名(如 all/claude)被价格表
  11. // 家族模糊匹配错计(Opus 流量按 Sonnet 兜底价)。compositeBillableModel 要求别名必须
  12. // 有显式渠道定价才可参与计费,否则回退实际转发的具体模型。
  13. func TestCompositeBillableModel(t *testing.T) {
  14. svc := &GatewayService{billingService: NewBillingService(&config.Config{}, nil)}
  15. apiKey := &APIKey{}
  16. ctx := context.Background()
  17. // 别名无渠道定价(含家族词也一样)→ 回退具体模型
  18. require.Equal(t, "claude-opus-4-7",
  19. svc.compositeBillableModel(ctx, apiKey, "all/claude", "claude-opus-4-7"))
  20. require.Equal(t, "claude-sonnet-4",
  21. svc.compositeBillableModel(ctx, apiKey, "team/best", "claude-sonnet-4"))
  22. // 未发生来源覆盖(计费模型已是具体模型)→ 原样返回
  23. require.Equal(t, "claude-sonnet-4",
  24. svc.compositeBillableModel(ctx, apiKey, "claude-sonnet-4", "claude-sonnet-4"))
  25. // 具体模型缺失 → 保持原值(走后续通用兜底/既有路径)
  26. require.Equal(t, "all/claude",
  27. svc.compositeBillableModel(ctx, apiKey, "all/claude", ""))
  28. }
  29. // billableModelWithFallback 是通用安全网:选定计费模型查不到任何价格时回退到
  30. // 实际转发的具体模型;已定价流量(含家族兜底可解析的名字)不受影响。
  31. func TestBillableModelWithFallback(t *testing.T) {
  32. svc := &GatewayService{billingService: NewBillingService(&config.Config{}, nil)}
  33. apiKey := &APIKey{}
  34. ctx := context.Background()
  35. // 完全无价的别名 → 回退到具体转发模型(claude-sonnet-4 有内置回退价格)
  36. require.Equal(t, "claude-sonnet-4",
  37. svc.billableModelWithFallback(ctx, apiKey, "team/best", "", "claude-sonnet-4"))
  38. // 已定价模型不回退,候选被忽略
  39. require.Equal(t, "claude-sonnet-4",
  40. svc.billableModelWithFallback(ctx, apiKey, "claude-sonnet-4", "claude-opus-4"))
  41. // 所有候选都无价 → 保持原值,走既有 warn + 零成本路径
  42. require.Equal(t, "team/best",
  43. svc.billableModelWithFallback(ctx, apiKey, "team/best", "another/alias", ""))
  44. // 空计费模型 + 有价候选 → 取候选
  45. require.Equal(t, "claude-sonnet-4",
  46. svc.billableModelWithFallback(ctx, apiKey, "", "claude-sonnet-4"))
  47. }
  48. func TestHasResolvableTokenPricing(t *testing.T) {
  49. svc := &GatewayService{billingService: NewBillingService(&config.Config{}, nil)}
  50. apiKey := &APIKey{}
  51. ctx := context.Background()
  52. require.True(t, svc.hasResolvableTokenPricing(ctx, "claude-sonnet-4", apiKey))
  53. // 注意:含家族词的名字(all/claude)会被价格表家族兜底解析为"有价",
  54. // 这正是 compositeBillableModel 必须先于通用兜底拦截别名的原因。
  55. require.True(t, svc.hasResolvableTokenPricing(ctx, "all/claude", apiKey))
  56. require.False(t, svc.hasResolvableTokenPricing(ctx, "team/best", apiKey))
  57. require.False(t, svc.hasResolvableTokenPricing(ctx, "", apiKey))
  58. // billingService 缺失时 fail-closed(不误判有价)
  59. empty := &GatewayService{}
  60. require.False(t, empty.hasResolvableTokenPricing(ctx, "claude-sonnet-4", apiKey))
  61. }