test: add unit tests for channel platform matching, interval validation, credits check

- TestIsPlatformPricingMatch: 12 cases covering all platform combinations
- TestMatchingPlatforms: 4 cases for platform expansion
- TestGetChannelModelPricing_AntigravityCrossPlatform: antigravity sees anthropic pricing
- TestGetChannelModelPricing_AnthropicCannotSeeAntigravityPricing: no reverse leakage
- TestResolveChannelMapping_AntigravityCrossPlatform: antigravity uses anthropic mapping
- TestFilterValidIntervals: 8 cases for empty interval filtering
- TestHasEnoughCredits: 10 cases for credits balance threshold logic
- Extract hasEnoughCredits() pure function for testability
This commit is contained in:
erio
2026-04-01 22:41:39 +08:00
parent 2355029dc1
commit 5534347328
4 changed files with 315 additions and 12 deletions

View File

@@ -585,3 +585,79 @@ func TestGetRequestTierPriceByContext_ExactBoundary(t *testing.T) {
price2 := r.GetRequestTierPriceByContext(resolved, 128001)
require.InDelta(t, 0.10, price2, 1e-12)
}
// ===========================================================================
// 8. filterValidIntervals
// ===========================================================================
func TestFilterValidIntervals(t *testing.T) {
tests := []struct {
name string
intervals []PricingInterval
wantLen int
}{
{
name: "empty list",
intervals: nil,
wantLen: 0,
},
{
name: "all-nil interval filtered out",
intervals: []PricingInterval{
{MinTokens: 0, MaxTokens: testPtrInt(128000)},
},
wantLen: 0,
},
{
name: "interval with only InputPrice kept",
intervals: []PricingInterval{
{MinTokens: 0, MaxTokens: testPtrInt(128000), InputPrice: testPtrFloat64(1e-6)},
},
wantLen: 1,
},
{
name: "interval with only OutputPrice kept",
intervals: []PricingInterval{
{MinTokens: 0, MaxTokens: testPtrInt(128000), OutputPrice: testPtrFloat64(2e-6)},
},
wantLen: 1,
},
{
name: "interval with only CacheWritePrice kept",
intervals: []PricingInterval{
{MinTokens: 0, CacheWritePrice: testPtrFloat64(3e-6)},
},
wantLen: 1,
},
{
name: "interval with only CacheReadPrice kept",
intervals: []PricingInterval{
{MinTokens: 0, CacheReadPrice: testPtrFloat64(0.5e-6)},
},
wantLen: 1,
},
{
name: "interval with only PerRequestPrice kept",
intervals: []PricingInterval{
{TierLabel: "1K", PerRequestPrice: testPtrFloat64(0.04)},
},
wantLen: 1,
},
{
name: "mixed valid and invalid",
intervals: []PricingInterval{
{MinTokens: 0, MaxTokens: testPtrInt(128000), InputPrice: testPtrFloat64(1e-6)},
{MinTokens: 128000, MaxTokens: nil}, // all-nil → filtered out
{MinTokens: 256000, OutputPrice: testPtrFloat64(5e-6)},
},
wantLen: 2,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := filterValidIntervals(tt.intervals)
require.Len(t, result, tt.wantLen)
})
}
}