23 lines
1.2 KiB
Go
23 lines
1.2 KiB
Go
package billingexpr
|
|
|
|
import (
|
|
"github.com/stretchr/testify/require"
|
|
"testing"
|
|
)
|
|
|
|
func TestImageGenerationPriceSizesAndQuantity(t *testing.T) {
|
|
expression := `(param("size") != nil && (param("size") == "4k" || (param("size") matches "^[0-9]+x[0-9]+$" && min(float(split(param("size"), "x")[0]), float(split(param("size"), "x")[1])) > 2048)) ? tier("4k", fixed(0.12)) : param("size") != nil && (param("size") == "2k" || (param("size") matches "^[0-9]+x[0-9]+$" && min(float(split(param("size"), "x")[0]), float(split(param("size"), "x")[1])) > 1024)) ? tier("2k", fixed(0.08)) : tier("1k", fixed(0.06))) * image_count`
|
|
for _, tc := range []struct {
|
|
body, tier string
|
|
price float64
|
|
}{{`{}`, "1k", 0.06}, {`{"size":"auto"}`, "1k", 0.06}, {`{"size":"1024x1536"}`, "1k", 0.06}, {`{"size":"2048x1152"}`, "2k", 0.08}, {`{"size":"3840x2160"}`, "4k", 0.12}, {`{"size":"2k"}`, "2k", 0.08}} {
|
|
for _, n := range []int{1, 2, 3} {
|
|
cost, trace, err := RunExprWithRequest(expression, TokenParams{P: 10000, C: 5000}, RequestInput{Body: []byte(tc.body), ImageCount: &n})
|
|
require.NoError(t, err)
|
|
require.InDelta(t, tc.price*float64(n)*1000000, cost, 0.01)
|
|
require.Equal(t, tc.tier, trace.MatchedTier)
|
|
require.Equal(t, BillingUnitRequest, trace.BillingUnit)
|
|
}
|
|
}
|
|
}
|