Publish Docker image (Multi-arch) / Build & push (amd64) (push) Failing after 1m40s
Release (Linux, macOS, Windows) / Linux Release (push) Failing after 42m16s
Publish Docker image (Multi-arch) / Build & push (arm64) (push) Canceled after 0s
Release (Linux, macOS, Windows) / macOS Release (push) Canceled after 0s
Release (Linux, macOS, Windows) / Windows Release (push) Canceled after 0s
Publish Docker image (Multi-arch) / Create multi-arch manifests (push) Canceled after 0s
Sync production source including routing, billing, Astra detection and public uptime. Update custom build instructions and license packaging.
112 lines
3.8 KiB
Go
112 lines
3.8 KiB
Go
package billing_setting
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/QuantumNous/new-api/common"
|
|
"github.com/QuantumNous/new-api/pkg/jsplugin"
|
|
relaycommon "github.com/QuantumNous/new-api/relay/common"
|
|
"github.com/QuantumNous/new-api/relaykit/dto"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestSmokeTestTaskExprValidatesDeclaredUsageVectors(t *testing.T) {
|
|
videoSchema := map[string]jsplugin.UsageFieldSchema{
|
|
"seconds": {Type: "number", Unit: "second"},
|
|
"mode": {Enum: []string{"std", "pro"}},
|
|
"quality": {Enum: []string{"sd", "hd"}},
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
schema map[string]jsplugin.UsageFieldSchema
|
|
expression string
|
|
expectedError string
|
|
}{
|
|
{
|
|
name: "fixed prices are not task usage prices",
|
|
schema: videoSchema,
|
|
expression: `true ? tier("normal", u("seconds") * 0.4) : tier("fixed", fixed(0.01))`,
|
|
expectedError: "fixed pricing is not supported for task usage expressions",
|
|
},
|
|
{
|
|
name: "declared numeric and enum facts",
|
|
schema: videoSchema,
|
|
expression: `u("mode") == "pro" ? tier("pro", u("seconds") * 0.8) : tier("std", u("seconds") * 0.4)`,
|
|
},
|
|
{
|
|
name: "undeclared literal key",
|
|
schema: videoSchema,
|
|
expression: `tier("base", u("clips") * 0.1)`,
|
|
expectedError: `usage key "clips" is not declared`,
|
|
},
|
|
{
|
|
name: "negative duration boundary",
|
|
schema: videoSchema,
|
|
expression: fmt.Sprintf(`u("seconds") == %d ? -1 : 0`, relaycommon.MaxTaskDurationSeconds),
|
|
expectedError: "result must be finite and non-negative",
|
|
},
|
|
{
|
|
name: "negative count boundary",
|
|
schema: map[string]jsplugin.UsageFieldSchema{"clips": {Type: "number", Unit: "count"}},
|
|
expression: fmt.Sprintf(`u("clips") == %d ? -1 : 0`, dto.MaxImageN),
|
|
expectedError: "result must be finite and non-negative",
|
|
},
|
|
{
|
|
name: "negative token boundary",
|
|
schema: map[string]jsplugin.UsageFieldSchema{"tokens": {Type: "number", Unit: "token"}},
|
|
expression: fmt.Sprintf(`u("tokens") == %d ? -1 : 0`, common.MaxQuota),
|
|
expectedError: "result must be finite and non-negative",
|
|
},
|
|
{
|
|
name: "negative credit boundary",
|
|
schema: map[string]jsplugin.UsageFieldSchema{"units": {Type: "number", Unit: "credit"}},
|
|
expression: fmt.Sprintf(`u("units") == %d ? -1 : 0`, common.MaxQuota),
|
|
expectedError: "result must be finite and non-negative",
|
|
},
|
|
{
|
|
name: "negative enum combination",
|
|
schema: videoSchema,
|
|
expression: `u("mode") == "pro" && u("quality") == "hd" ? -1 : 0`,
|
|
expectedError: "result must be finite and non-negative",
|
|
},
|
|
}
|
|
|
|
for _, testCase := range tests {
|
|
t.Run(testCase.name, func(t *testing.T) {
|
|
err := SmokeTestTaskExpr(testCase.expression, testCase.schema)
|
|
if testCase.expectedError == "" {
|
|
require.NoError(t, err)
|
|
return
|
|
}
|
|
require.ErrorContains(t, err, testCase.expectedError)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSmokeTestTaskExprCapsOversizedEnumProductsAtLastCombination(t *testing.T) {
|
|
schema := make(map[string]jsplugin.UsageFieldSchema, 7)
|
|
condition := ""
|
|
for index := range 7 {
|
|
schema[fmt.Sprintf("enum_%d", index)] = jsplugin.UsageFieldSchema{Enum: []string{"first", "middle", "last"}}
|
|
if condition != "" {
|
|
condition += " && "
|
|
}
|
|
condition += fmt.Sprintf(`u("enum_%d") == "last"`, index)
|
|
}
|
|
|
|
err := SmokeTestTaskExpr(condition+" ? -1 : 0", schema)
|
|
require.ErrorContains(t, err, "result must be finite and non-negative")
|
|
}
|
|
|
|
func TestSmokeTestExprRejectsTaskUsageWithoutSchema(t *testing.T) {
|
|
err := SmokeTestExpr(`u("mode") == "std" ? 1 : 2`)
|
|
require.Error(t, err)
|
|
assert.ErrorContains(t, err, "mode")
|
|
assert.ErrorContains(t, err, "no task plugin usage schema")
|
|
|
|
require.NoError(t, SmokeTestExpr(`tier("base", p * 2 + c * 8)`))
|
|
}
|