image_billing_size_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package service
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/require"
  5. )
  6. func TestClassifyImageBillingTier(t *testing.T) {
  7. tests := []struct {
  8. name string
  9. size string
  10. wantTier string
  11. wantOK bool
  12. }{
  13. {name: "explicit 2k square", size: "2048x2048", wantTier: "2K", wantOK: true},
  14. {name: "explicit 2k landscape", size: "2048x1152", wantTier: "2K", wantOK: true},
  15. {name: "explicit 4k landscape", size: "3840x2160", wantTier: "4K", wantOK: true},
  16. {name: "explicit 4k portrait", size: "2160x3840", wantTier: "4K", wantOK: true},
  17. {name: "long edge 1k", size: "1024X768", wantTier: "1K", wantOK: true},
  18. {name: "long edge 2k", size: "1280x768", wantTier: "2K", wantOK: true},
  19. {name: "long edge 4k", size: "2560x1600", wantTier: "4K", wantOK: true},
  20. {name: "tier string 1k", size: "1k", wantTier: "1K", wantOK: true},
  21. {name: "empty", size: "", wantOK: false},
  22. {name: "auto", size: "auto", wantOK: false},
  23. {name: "invalid", size: "not-a-size", wantOK: false},
  24. }
  25. for _, tt := range tests {
  26. t.Run(tt.name, func(t *testing.T) {
  27. gotTier, gotOK := ClassifyImageBillingTier(tt.size)
  28. require.Equal(t, tt.wantOK, gotOK)
  29. require.Equal(t, tt.wantTier, gotTier)
  30. })
  31. }
  32. }
  33. func TestResolveImageBillingSize(t *testing.T) {
  34. tests := []struct {
  35. name string
  36. inputSize string
  37. outputSizes []string
  38. wantBilling string
  39. wantOutput string
  40. wantSource string
  41. wantBreakdown map[string]int
  42. }{
  43. {
  44. name: "output wins over input",
  45. inputSize: "1024x1024",
  46. outputSizes: []string{"3840x2160"},
  47. wantBilling: "4K",
  48. wantOutput: "3840x2160",
  49. wantSource: ImageSizeSourceOutput,
  50. wantBreakdown: map[string]int{"4K": 1},
  51. },
  52. {
  53. name: "input fallback",
  54. inputSize: "1024x1024",
  55. wantBilling: "1K",
  56. wantSource: ImageSizeSourceInput,
  57. },
  58. {
  59. name: "auto defaults",
  60. inputSize: "auto",
  61. wantBilling: "2K",
  62. wantSource: ImageSizeSourceDefault,
  63. },
  64. {
  65. name: "empty defaults",
  66. inputSize: "",
  67. wantBilling: "2K",
  68. wantSource: ImageSizeSourceDefault,
  69. },
  70. {
  71. name: "invalid defaults",
  72. inputSize: "largest",
  73. wantBilling: "2K",
  74. wantSource: ImageSizeSourceDefault,
  75. },
  76. {
  77. name: "mixed output chooses highest tier",
  78. inputSize: "1024x1024",
  79. outputSizes: []string{"1024x1024", "3840x2160", "1280x720"},
  80. wantBilling: "4K",
  81. wantOutput: "1024x1024",
  82. wantSource: ImageSizeSourceOutput,
  83. wantBreakdown: map[string]int{"1K": 1, "2K": 1, "4K": 1},
  84. },
  85. {
  86. name: "unparseable output falls back to parseable input",
  87. inputSize: "2048x1152",
  88. outputSizes: []string{"auto"},
  89. wantBilling: "2K",
  90. wantOutput: "auto",
  91. wantSource: ImageSizeSourceInput,
  92. },
  93. }
  94. for _, tt := range tests {
  95. t.Run(tt.name, func(t *testing.T) {
  96. got := ResolveImageBillingSize(tt.inputSize, tt.outputSizes)
  97. require.Equal(t, tt.wantBilling, got.BillingSize)
  98. require.Equal(t, tt.inputSize, got.InputSize)
  99. require.Equal(t, tt.wantOutput, got.OutputSize)
  100. require.Equal(t, tt.wantSource, got.Source)
  101. require.Equal(t, tt.wantBreakdown, got.Breakdown)
  102. })
  103. }
  104. }