video_billing_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package service
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/require"
  5. )
  6. func TestCanonicalGrokImagineVideoPriceFamily(t *testing.T) {
  7. t.Parallel()
  8. require.Equal(t, VideoPriceFamilyGrokImagineVideo, CanonicalGrokImagineVideoPriceFamily("grok-imagine-video"))
  9. require.Equal(t, VideoPriceFamilyGrokImagineVideo15, CanonicalGrokImagineVideoPriceFamily("grok-imagine-video-1.5"))
  10. require.Equal(t, VideoPriceFamilyGrokImagineVideo15, CanonicalGrokImagineVideoPriceFamily("grok-imagine-video-1.5-preview"))
  11. require.Equal(t, VideoPriceFamilyGrokImagineVideo15, CanonicalGrokImagineVideoPriceFamily("xai/grok-video-1.5"))
  12. require.Equal(t, "grok-imagine-video-2", CanonicalGrokImagineVideoPriceFamily("grok-imagine-video-2"))
  13. require.Equal(t, "grok-imagine-video-2", CanonicalGrokImagineVideoPriceFamily("xai/grok-imagine-video-2"))
  14. }
  15. func TestNormalizeAndLookupVideoModelPrices(t *testing.T) {
  16. t.Parallel()
  17. raw := map[string]map[string]float64{
  18. "grok-imagine-video-1.5-preview": {"480p": 0.08, "720p": 0.14},
  19. "grok-imagine-video": {"480p": 0.05},
  20. "grok-imagine-video-2": {"1080p": 0.4},
  21. }
  22. norm := NormalizeVideoModelPrices(raw)
  23. require.NotNil(t, norm)
  24. require.Contains(t, norm, VideoPriceFamilyGrokImagineVideo15)
  25. require.Contains(t, norm, VideoPriceFamilyGrokImagineVideo)
  26. require.Contains(t, norm, "grok-imagine-video-2")
  27. p15 := LookupVideoModelPrice(norm, "grok-imagine-video-1.5", "480p")
  28. require.NotNil(t, p15)
  29. require.InDelta(t, 0.08, *p15, 1e-9)
  30. pBase := LookupVideoModelPrice(norm, "grok-imagine-video", "480p")
  31. require.NotNil(t, pBase)
  32. require.InDelta(t, 0.05, *pBase, 1e-9)
  33. // A missing model-specific tier must fall back to the flat tier price,
  34. // rather than borrowing another model-specific resolution.
  35. require.Nil(t, LookupVideoModelPrice(norm, "grok-imagine-video", "720p"))
  36. p2 := LookupVideoModelPrice(norm, "grok-imagine-video-2", "1080p")
  37. require.NotNil(t, p2)
  38. require.InDelta(t, 0.4, *p2, 1e-9)
  39. // Unmatched model → nil (caller falls back to flat columns / defaults).
  40. require.Nil(t, LookupVideoModelPrice(norm, "unknown-model", "480p"))
  41. }
  42. func TestNormalizeVideoModelPricesDropsUnknownResolutions(t *testing.T) {
  43. t.Parallel()
  44. // "4k" and "1080i" are not billable tiers. Collapsing them into 480p would
  45. // charge a 480p request at the operator's high-resolution price.
  46. norm := NormalizeVideoModelPrices(map[string]map[string]float64{
  47. "grok-imagine-video": {"480p": 0.05, "4k": 0.50, "1080i": 0.30},
  48. })
  49. require.NotNil(t, norm)
  50. require.Equal(t, map[string]float64{VideoBillingResolution480P: 0.05}, norm[VideoPriceFamilyGrokImagineVideo])
  51. // A model whose tiers are all unrecognized contributes no family at all.
  52. require.Nil(t, NormalizeVideoModelPrices(map[string]map[string]float64{
  53. "grok-imagine-video": {"4k": 0.50},
  54. }))
  55. }
  56. func TestNormalizeVideoModelPricesIsDeterministicAcrossAliasConflicts(t *testing.T) {
  57. t.Parallel()
  58. // Both keys canonicalize onto grok-imagine-video-1.5 and disagree on 480p.
  59. // Whichever price wins, it must be the same one on every run — a Go map walk
  60. // would let two processes bill the same request differently.
  61. raw := map[string]map[string]float64{
  62. "grok-imagine-video-1.5": {"480p": 0.08},
  63. "grok-imagine-video-1.5-preview": {"480p": 0.11},
  64. "grok-video-1.5": {"480p": 0.09},
  65. }
  66. first := NormalizeVideoModelPrices(raw)
  67. require.NotNil(t, first)
  68. for i := 0; i < 50; i++ {
  69. require.Equal(t, first, NormalizeVideoModelPrices(raw), "run %d diverged", i)
  70. }
  71. // Aliases within one model key normalize to the same tier deterministically too.
  72. aliased := map[string]map[string]float64{
  73. "grok-imagine-video": {"720": 0.12, "720p": 0.13, "hd": 0.14},
  74. }
  75. firstAliased := NormalizeVideoModelPrices(aliased)
  76. require.NotNil(t, firstAliased)
  77. for i := 0; i < 50; i++ {
  78. require.Equal(t, firstAliased, NormalizeVideoModelPrices(aliased), "run %d diverged", i)
  79. }
  80. }
  81. func TestLookupVideoBillingResolutionReportsUnknownTiers(t *testing.T) {
  82. t.Parallel()
  83. for _, in := range []string{"480", "480p", "SD", "720", "hd", "1080", "full-hd", " fhd "} {
  84. normalized, ok := LookupVideoBillingResolution(in)
  85. require.True(t, ok, "input=%q", in)
  86. require.NotEmpty(t, normalized)
  87. }
  88. for _, in := range []string{"", "4k", "1080i", "2160p", "potato"} {
  89. normalized, ok := LookupVideoBillingResolution(in)
  90. require.False(t, ok, "input=%q", in)
  91. require.Empty(t, normalized)
  92. }
  93. // Runtime billing still needs a tier for unrecognized upstream values.
  94. require.Equal(t, VideoBillingResolution480P, NormalizeVideoBillingResolutionOrDefault("4k"))
  95. require.Equal(t, VideoBillingResolution1080P, NormalizeVideoBillingResolutionOrDefault("full_hd"))
  96. }
  97. func TestVideoModelPriceMissingTierFallsBackToFlatTierPrice(t *testing.T) {
  98. t.Parallel()
  99. flat720P := 0.7
  100. service := &BillingService{}
  101. result := service.CalculateVideoCost("grok-imagine-video", "720p", 1, 1, &VideoPriceConfig{
  102. Price720P: &flat720P,
  103. ModelPrices: map[string]map[string]float64{
  104. VideoPriceFamilyGrokImagineVideo: {VideoBillingResolution480P: 0.05},
  105. },
  106. }, 1)
  107. require.InDelta(t, flat720P, result.TotalCost, 1e-9)
  108. }