| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908 |
- //go:build unit
- package service
- import (
- "context"
- "errors"
- "testing"
- "github.com/stretchr/testify/require"
- )
- func newTestBillingServiceForResolver() *BillingService {
- bs := &BillingService{
- fallbackPrices: make(map[string]*ModelPricing),
- }
- bs.fallbackPrices["claude-sonnet-4"] = &ModelPricing{
- InputPricePerToken: 3e-6,
- OutputPricePerToken: 15e-6,
- CacheCreationPricePerToken: 3.75e-6,
- CacheReadPricePerToken: 0.3e-6,
- SupportsCacheBreakdown: false,
- }
- return bs
- }
- func TestResolve_NoGroupID(t *testing.T) {
- bs := newTestBillingServiceForResolver()
- r := NewModelPricingResolver(&ChannelService{}, bs)
- resolved := r.Resolve(context.Background(), PricingInput{
- Model: "claude-sonnet-4",
- GroupID: nil,
- })
- require.NotNil(t, resolved)
- require.Equal(t, BillingModeToken, resolved.Mode)
- require.NotNil(t, resolved.BasePricing)
- require.InDelta(t, 3e-6, resolved.BasePricing.InputPricePerToken, 1e-12)
- // BillingService.GetModelPricing uses fallback internally, but resolveBasePricing
- // reports "litellm" when GetModelPricing succeeds (regardless of internal source)
- require.Equal(t, "litellm", resolved.Source)
- }
- func TestResolve_UnknownModel(t *testing.T) {
- bs := newTestBillingServiceForResolver()
- r := NewModelPricingResolver(&ChannelService{}, bs)
- resolved := r.Resolve(context.Background(), PricingInput{
- Model: "unknown-model-xyz",
- GroupID: nil,
- })
- require.NotNil(t, resolved)
- require.Nil(t, resolved.BasePricing)
- // Unknown model: GetModelPricing returns error, source is "fallback"
- require.Equal(t, "fallback", resolved.Source)
- }
- func TestGetIntervalPricing_NoIntervals(t *testing.T) {
- bs := newTestBillingServiceForResolver()
- r := NewModelPricingResolver(&ChannelService{}, bs)
- basePricing := &ModelPricing{InputPricePerToken: 5e-6}
- resolved := &ResolvedPricing{
- Mode: BillingModeToken,
- BasePricing: basePricing,
- Intervals: nil,
- }
- result := r.GetIntervalPricing(resolved, 50000)
- require.Equal(t, basePricing, result)
- }
- func TestGetIntervalPricing_MatchesInterval(t *testing.T) {
- bs := newTestBillingServiceForResolver()
- r := NewModelPricingResolver(&ChannelService{}, bs)
- resolved := &ResolvedPricing{
- Mode: BillingModeToken,
- BasePricing: &ModelPricing{InputPricePerToken: 5e-6},
- SupportsCacheBreakdown: true,
- Intervals: []PricingInterval{
- {MinTokens: 0, MaxTokens: testPtrInt(128000), InputPrice: testPtrFloat64(1e-6), OutputPrice: testPtrFloat64(2e-6)},
- {MinTokens: 128000, MaxTokens: nil, InputPrice: testPtrFloat64(3e-6), OutputPrice: testPtrFloat64(6e-6)},
- },
- }
- result := r.GetIntervalPricing(resolved, 50000)
- require.NotNil(t, result)
- require.InDelta(t, 1e-6, result.InputPricePerToken, 1e-12)
- require.InDelta(t, 2e-6, result.OutputPricePerToken, 1e-12)
- require.True(t, result.SupportsCacheBreakdown)
- result2 := r.GetIntervalPricing(resolved, 200000)
- require.NotNil(t, result2)
- require.InDelta(t, 3e-6, result2.InputPricePerToken, 1e-12)
- }
- func TestGetIntervalPricing_NoMatch_FallsBackToBase(t *testing.T) {
- bs := newTestBillingServiceForResolver()
- r := NewModelPricingResolver(&ChannelService{}, bs)
- basePricing := &ModelPricing{InputPricePerToken: 99e-6}
- resolved := &ResolvedPricing{
- Mode: BillingModeToken,
- BasePricing: basePricing,
- Intervals: []PricingInterval{
- {MinTokens: 10000, MaxTokens: testPtrInt(50000), InputPrice: testPtrFloat64(1e-6)},
- },
- }
- result := r.GetIntervalPricing(resolved, 5000)
- require.Equal(t, basePricing, result)
- }
- func TestGPT56ExplicitZeroCacheWritePriceIsPreserved(t *testing.T) {
- bs := &BillingService{}
- resolver := NewModelPricingResolver(nil, bs)
- zero := 0.0
- t.Run("flat channel price", func(t *testing.T) {
- resolved := &ResolvedPricing{
- Mode: BillingModeToken,
- BasePricing: &ModelPricing{
- InputPricePerToken: 5e-6,
- OutputPricePerToken: 30e-6,
- },
- }
- resolver.applyTokenOverrides(&ChannelModelPricing{CacheWritePrice: &zero}, resolved)
- require.True(t, resolved.BasePricing.CacheCreationPriceExplicit)
- cost, err := bs.CalculateCostUnified(CostInput{
- Model: "gpt-5.6-sol",
- Tokens: UsageTokens{CacheCreationTokens: 100},
- RateMultiplier: 1,
- Resolver: resolver,
- Resolved: resolved,
- })
- require.NoError(t, err)
- require.Zero(t, cost.CacheCreationCost)
- })
- t.Run("interval price", func(t *testing.T) {
- pricing := intervalToModelPricing(&PricingInterval{CacheWritePrice: &zero}, &ModelPricing{}, nil)
- require.True(t, pricing.CacheCreationPriceExplicit)
- cost, err := bs.CalculateCostUnified(CostInput{
- Model: "gpt-5.6-sol",
- Tokens: UsageTokens{CacheCreationTokens: 100},
- RateMultiplier: 1,
- Resolver: resolver,
- Resolved: &ResolvedPricing{
- Mode: BillingModeToken,
- BasePricing: pricing,
- },
- })
- require.NoError(t, err)
- require.Zero(t, cost.CacheCreationCost)
- })
- }
- func TestGetRequestTierPrice(t *testing.T) {
- bs := newTestBillingServiceForResolver()
- r := NewModelPricingResolver(&ChannelService{}, bs)
- resolved := &ResolvedPricing{
- Mode: BillingModePerRequest,
- RequestTiers: []PricingInterval{
- {TierLabel: "1K", PerRequestPrice: testPtrFloat64(0.04)},
- {TierLabel: "2K", PerRequestPrice: testPtrFloat64(0.08)},
- },
- }
- require.InDelta(t, 0.04, r.GetRequestTierPrice(resolved, "1K"), 1e-12)
- require.InDelta(t, 0.08, r.GetRequestTierPrice(resolved, "2K"), 1e-12)
- require.InDelta(t, 0.0, r.GetRequestTierPrice(resolved, "4K"), 1e-12)
- }
- func TestGetRequestTierPriceByContext(t *testing.T) {
- bs := newTestBillingServiceForResolver()
- r := NewModelPricingResolver(&ChannelService{}, bs)
- resolved := &ResolvedPricing{
- Mode: BillingModePerRequest,
- RequestTiers: []PricingInterval{
- {MinTokens: 0, MaxTokens: testPtrInt(128000), PerRequestPrice: testPtrFloat64(0.05)},
- {MinTokens: 128000, MaxTokens: nil, PerRequestPrice: testPtrFloat64(0.10)},
- },
- }
- require.InDelta(t, 0.05, r.GetRequestTierPriceByContext(resolved, 50000), 1e-12)
- require.InDelta(t, 0.10, r.GetRequestTierPriceByContext(resolved, 200000), 1e-12)
- }
- func TestGetRequestTierPrice_NilPerRequestPrice(t *testing.T) {
- bs := newTestBillingServiceForResolver()
- r := NewModelPricingResolver(&ChannelService{}, bs)
- resolved := &ResolvedPricing{
- Mode: BillingModePerRequest,
- RequestTiers: []PricingInterval{
- {TierLabel: "1K", PerRequestPrice: nil},
- },
- }
- require.InDelta(t, 0.0, r.GetRequestTierPrice(resolved, "1K"), 1e-12)
- }
- // ===========================================================================
- // Channel override tests — exercises applyChannelOverrides via Resolve
- // ===========================================================================
- // helper: creates a resolver wired to a ChannelService that returns the given
- // channel (active, groupID=100, platform=anthropic) with the specified pricing.
- func newResolverWithChannel(t *testing.T, pricing []ChannelModelPricing) *ModelPricingResolver {
- t.Helper()
- const groupID = 100
- repo := &mockChannelRepository{
- listAllFn: func(_ context.Context) ([]Channel, error) {
- return []Channel{{
- ID: 1,
- Name: "test-channel",
- Status: StatusActive,
- GroupIDs: []int64{groupID},
- ModelPricing: pricing,
- }}, nil
- },
- getGroupPlatformsFn: func(_ context.Context, _ []int64) (map[int64]string, error) {
- return map[int64]string{groupID: "anthropic"}, nil
- },
- }
- cs := NewChannelService(repo, nil, nil, nil, nil)
- bs := newTestBillingServiceForResolver()
- return NewModelPricingResolver(cs, bs)
- }
- // groupIDPtr returns a pointer to groupID 100 (the test constant).
- func groupIDPtr() *int64 { v := int64(100); return &v }
- // ---------------------------------------------------------------------------
- // 1. Token mode overrides
- // ---------------------------------------------------------------------------
- func TestResolve_WithChannelOverride_TokenFlat(t *testing.T) {
- r := newResolverWithChannel(t, []ChannelModelPricing{{
- Platform: "anthropic",
- Models: []string{"claude-sonnet-4"},
- BillingMode: BillingModeToken,
- InputPrice: testPtrFloat64(10e-6),
- OutputPrice: testPtrFloat64(50e-6),
- }})
- resolved := r.Resolve(context.Background(), PricingInput{
- Model: "claude-sonnet-4",
- GroupID: groupIDPtr(),
- })
- require.NotNil(t, resolved)
- require.Equal(t, BillingModeToken, resolved.Mode)
- require.Equal(t, "channel", resolved.Source)
- require.NotNil(t, resolved.BasePricing)
- require.InDelta(t, 10e-6, resolved.BasePricing.InputPricePerToken, 1e-12)
- require.Zero(t, resolved.BasePricing.InputPricePerTokenPriority)
- require.InDelta(t, 50e-6, resolved.BasePricing.OutputPricePerToken, 1e-12)
- require.Zero(t, resolved.BasePricing.OutputPricePerTokenPriority)
- }
- func TestResolve_WithChannelOverride_TokenPartialOverride(t *testing.T) {
- // Channel only sets InputPrice; OutputPrice should remain from the base (LiteLLM/fallback).
- r := newResolverWithChannel(t, []ChannelModelPricing{{
- Platform: "anthropic",
- Models: []string{"claude-sonnet-4"},
- BillingMode: BillingModeToken,
- InputPrice: testPtrFloat64(20e-6),
- // OutputPrice intentionally nil
- }})
- resolved := r.Resolve(context.Background(), PricingInput{
- Model: "claude-sonnet-4",
- GroupID: groupIDPtr(),
- })
- require.NotNil(t, resolved)
- require.Equal(t, "channel", resolved.Source)
- require.NotNil(t, resolved.BasePricing)
- // InputPrice overridden by channel
- require.InDelta(t, 20e-6, resolved.BasePricing.InputPricePerToken, 1e-12)
- // OutputPrice kept from base (fallback: 15e-6)
- require.InDelta(t, 15e-6, resolved.BasePricing.OutputPricePerToken, 1e-12)
- }
- func TestResolve_WithChannelOverride_TokenWithIntervals(t *testing.T) {
- r := newResolverWithChannel(t, []ChannelModelPricing{{
- Platform: "anthropic",
- Models: []string{"claude-sonnet-4"},
- BillingMode: BillingModeToken,
- Intervals: []PricingInterval{
- {MinTokens: 0, MaxTokens: testPtrInt(128000), InputPrice: testPtrFloat64(2e-6), OutputPrice: testPtrFloat64(8e-6)},
- {MinTokens: 128000, MaxTokens: nil, InputPrice: testPtrFloat64(4e-6), OutputPrice: testPtrFloat64(16e-6)},
- },
- }})
- resolved := r.Resolve(context.Background(), PricingInput{
- Model: "claude-sonnet-4",
- GroupID: groupIDPtr(),
- Group: &Group{LongContextPricingEnabled: false},
- })
- require.NotNil(t, resolved)
- require.Equal(t, "channel", resolved.Source)
- require.False(t, resolved.longContextPricingEnabled)
- require.Len(t, resolved.Intervals, 2)
- // GetIntervalPricing should use channel intervals
- iv := r.GetIntervalPricing(resolved, 50000)
- require.NotNil(t, iv)
- require.InDelta(t, 2e-6, iv.InputPricePerToken, 1e-12)
- require.InDelta(t, 8e-6, iv.OutputPricePerToken, 1e-12)
- iv2 := r.GetIntervalPricing(resolved, 200000)
- require.NotNil(t, iv2)
- require.InDelta(t, 4e-6, iv2.InputPricePerToken, 1e-12)
- require.InDelta(t, 16e-6, iv2.OutputPricePerToken, 1e-12)
- }
- func TestResolve_WithChannelOverride_TokenNilBasePricing(t *testing.T) {
- // Base pricing is nil (unknown model), channel has flat prices → creates new BasePricing.
- r := newResolverWithChannel(t, []ChannelModelPricing{{
- Platform: "anthropic",
- Models: []string{"unknown-model-xyz"},
- BillingMode: BillingModeToken,
- InputPrice: testPtrFloat64(7e-6),
- OutputPrice: testPtrFloat64(21e-6),
- }})
- resolved := r.Resolve(context.Background(), PricingInput{
- Model: "unknown-model-xyz",
- GroupID: groupIDPtr(),
- })
- require.NotNil(t, resolved)
- require.Equal(t, "channel", resolved.Source)
- // BasePricing was nil from resolveBasePricing but applyTokenOverrides creates a new one
- require.NotNil(t, resolved.BasePricing)
- require.InDelta(t, 7e-6, resolved.BasePricing.InputPricePerToken, 1e-12)
- require.InDelta(t, 21e-6, resolved.BasePricing.OutputPricePerToken, 1e-12)
- }
- // ---------------------------------------------------------------------------
- // 2. Per-request mode overrides
- // ---------------------------------------------------------------------------
- func TestResolve_WithChannelOverride_PerRequest(t *testing.T) {
- r := newResolverWithChannel(t, []ChannelModelPricing{{
- Platform: "anthropic",
- Models: []string{"claude-sonnet-4"},
- BillingMode: BillingModePerRequest,
- PerRequestPrice: testPtrFloat64(0.05),
- Intervals: []PricingInterval{
- {MinTokens: 0, MaxTokens: testPtrInt(128000), PerRequestPrice: testPtrFloat64(0.03)},
- {MinTokens: 128000, MaxTokens: nil, PerRequestPrice: testPtrFloat64(0.10)},
- },
- }})
- resolved := r.Resolve(context.Background(), PricingInput{
- Model: "claude-sonnet-4",
- GroupID: groupIDPtr(),
- })
- require.NotNil(t, resolved)
- require.Equal(t, BillingModePerRequest, resolved.Mode)
- require.Equal(t, "channel", resolved.Source)
- require.InDelta(t, 0.05, resolved.DefaultPerRequestPrice, 1e-12)
- require.Len(t, resolved.RequestTiers, 2)
- // Verify tier lookups
- require.InDelta(t, 0.03, r.GetRequestTierPriceByContext(resolved, 50000), 1e-12)
- require.InDelta(t, 0.10, r.GetRequestTierPriceByContext(resolved, 200000), 1e-12)
- }
- func TestResolve_WithChannelOverride_PerRequestNilPrice(t *testing.T) {
- // PerRequestPrice nil → DefaultPerRequestPrice stays 0.
- r := newResolverWithChannel(t, []ChannelModelPricing{{
- Platform: "anthropic",
- Models: []string{"claude-sonnet-4"},
- BillingMode: BillingModePerRequest,
- // PerRequestPrice intentionally nil
- Intervals: []PricingInterval{
- {MinTokens: 0, MaxTokens: testPtrInt(128000), PerRequestPrice: testPtrFloat64(0.02)},
- },
- }})
- resolved := r.Resolve(context.Background(), PricingInput{
- Model: "claude-sonnet-4",
- GroupID: groupIDPtr(),
- })
- require.NotNil(t, resolved)
- require.Equal(t, BillingModePerRequest, resolved.Mode)
- require.InDelta(t, 0.0, resolved.DefaultPerRequestPrice, 1e-12)
- require.Len(t, resolved.RequestTiers, 1)
- }
- // ---------------------------------------------------------------------------
- // 3. Image mode overrides
- // ---------------------------------------------------------------------------
- func TestResolve_WithChannelOverride_Image(t *testing.T) {
- r := newResolverWithChannel(t, []ChannelModelPricing{{
- Platform: "anthropic",
- Models: []string{"claude-sonnet-4"},
- BillingMode: BillingModeImage,
- PerRequestPrice: testPtrFloat64(0.08),
- Intervals: []PricingInterval{
- {TierLabel: "1K", PerRequestPrice: testPtrFloat64(0.04)},
- {TierLabel: "2K", PerRequestPrice: testPtrFloat64(0.08)},
- {TierLabel: "4K", PerRequestPrice: testPtrFloat64(0.16)},
- },
- }})
- resolved := r.Resolve(context.Background(), PricingInput{
- Model: "claude-sonnet-4",
- GroupID: groupIDPtr(),
- })
- require.NotNil(t, resolved)
- require.Equal(t, BillingModeImage, resolved.Mode)
- require.Equal(t, "channel", resolved.Source)
- require.InDelta(t, 0.08, resolved.DefaultPerRequestPrice, 1e-12)
- require.Len(t, resolved.RequestTiers, 3)
- }
- func TestResolve_WithChannelOverride_ImageTierLabels(t *testing.T) {
- r := newResolverWithChannel(t, []ChannelModelPricing{{
- Platform: "anthropic",
- Models: []string{"claude-sonnet-4"},
- BillingMode: BillingModeImage,
- Intervals: []PricingInterval{
- {TierLabel: "1K", PerRequestPrice: testPtrFloat64(0.04)},
- {TierLabel: "2K", PerRequestPrice: testPtrFloat64(0.08)},
- {TierLabel: "4K", PerRequestPrice: testPtrFloat64(0.16)},
- },
- }})
- resolved := r.Resolve(context.Background(), PricingInput{
- Model: "claude-sonnet-4",
- GroupID: groupIDPtr(),
- })
- require.InDelta(t, 0.04, r.GetRequestTierPrice(resolved, "1K"), 1e-12)
- require.InDelta(t, 0.08, r.GetRequestTierPrice(resolved, "2K"), 1e-12)
- require.InDelta(t, 0.16, r.GetRequestTierPrice(resolved, "4K"), 1e-12)
- require.InDelta(t, 0.0, r.GetRequestTierPrice(resolved, "8K"), 1e-12) // not found
- }
- // ---------------------------------------------------------------------------
- // 4. Source tracking & default mode
- // ---------------------------------------------------------------------------
- func TestResolve_WithChannelOverride_SourceIsChannel(t *testing.T) {
- r := newResolverWithChannel(t, []ChannelModelPricing{{
- Platform: "anthropic",
- Models: []string{"claude-sonnet-4"},
- BillingMode: BillingModeToken,
- InputPrice: testPtrFloat64(1e-6),
- }})
- resolved := r.Resolve(context.Background(), PricingInput{
- Model: "claude-sonnet-4",
- GroupID: groupIDPtr(),
- })
- require.Equal(t, "channel", resolved.Source)
- }
- func TestResolve_WithChannelOverride_DefaultMode(t *testing.T) {
- // Channel pricing with empty BillingMode → defaults to BillingModeToken.
- r := newResolverWithChannel(t, []ChannelModelPricing{{
- Platform: "anthropic",
- Models: []string{"claude-sonnet-4"},
- BillingMode: "", // intentionally empty
- InputPrice: testPtrFloat64(5e-6),
- }})
- resolved := r.Resolve(context.Background(), PricingInput{
- Model: "claude-sonnet-4",
- GroupID: groupIDPtr(),
- })
- require.Equal(t, "channel", resolved.Source)
- require.Equal(t, BillingModeToken, resolved.Mode)
- require.NotNil(t, resolved.BasePricing)
- require.InDelta(t, 5e-6, resolved.BasePricing.InputPricePerToken, 1e-12)
- }
- // ---------------------------------------------------------------------------
- // 5. GetIntervalPricing integration after channel override
- // ---------------------------------------------------------------------------
- func TestGetIntervalPricing_WithChannelIntervals(t *testing.T) {
- // Channel provides intervals that override the base pricing path.
- r := newResolverWithChannel(t, []ChannelModelPricing{{
- Platform: "anthropic",
- Models: []string{"claude-sonnet-4"},
- BillingMode: BillingModeToken,
- Intervals: []PricingInterval{
- {MinTokens: 0, MaxTokens: testPtrInt(100000), InputPrice: testPtrFloat64(1e-6), OutputPrice: testPtrFloat64(5e-6)},
- {MinTokens: 100000, MaxTokens: nil, InputPrice: testPtrFloat64(2e-6), OutputPrice: testPtrFloat64(10e-6)},
- },
- }})
- resolved := r.Resolve(context.Background(), PricingInput{
- Model: "claude-sonnet-4",
- GroupID: groupIDPtr(),
- })
- // Token count 50000 matches first interval
- pricing := r.GetIntervalPricing(resolved, 50000)
- require.NotNil(t, pricing)
- require.InDelta(t, 1e-6, pricing.InputPricePerToken, 1e-12)
- require.InDelta(t, 5e-6, pricing.OutputPricePerToken, 1e-12)
- // Token count 150000 matches second interval
- pricing2 := r.GetIntervalPricing(resolved, 150000)
- require.NotNil(t, pricing2)
- require.InDelta(t, 2e-6, pricing2.InputPricePerToken, 1e-12)
- require.InDelta(t, 10e-6, pricing2.OutputPricePerToken, 1e-12)
- }
- func TestGetIntervalPricing_ChannelIntervalsNoMatch(t *testing.T) {
- // Channel intervals don't match token count → falls back to BasePricing.
- r := newResolverWithChannel(t, []ChannelModelPricing{{
- Platform: "anthropic",
- Models: []string{"claude-sonnet-4"},
- BillingMode: BillingModeToken,
- InputPrice: testPtrFloat64(4e-6),
- Intervals: []PricingInterval{
- // Only covers tokens > 50000
- {MinTokens: 50000, MaxTokens: testPtrInt(200000), InputPrice: testPtrFloat64(9e-6)},
- },
- }})
- resolved := r.Resolve(context.Background(), PricingInput{
- Model: "claude-sonnet-4",
- GroupID: groupIDPtr(),
- })
- // Token count 1000 doesn't match any interval (1000 <= 50000 minTokens)
- pricing := r.GetIntervalPricing(resolved, 1000)
- // Should fall back to BasePricing after applying the channel default.
- require.NotNil(t, pricing)
- require.Equal(t, resolved.BasePricing, pricing)
- require.InDelta(t, 4e-6, pricing.InputPricePerToken, 1e-12)
- }
- // ===========================================================================
- // 6. Error path tests
- // ===========================================================================
- func TestResolve_WithChannelOverride_CacheError(t *testing.T) {
- // When ListAll returns an error, the ChannelService cache build fails.
- // Resolve should gracefully fall back to base pricing without panicking.
- repo := &mockChannelRepository{
- listAllFn: func(_ context.Context) ([]Channel, error) {
- return nil, errors.New("database unavailable")
- },
- }
- cs := NewChannelService(repo, nil, nil, nil, nil)
- bs := newTestBillingServiceForResolver()
- r := NewModelPricingResolver(cs, bs)
- gid := int64(100)
- resolved := r.Resolve(context.Background(), PricingInput{
- Model: "claude-sonnet-4",
- GroupID: &gid,
- })
- require.NotNil(t, resolved)
- // Should NOT panic, should NOT have source "channel"
- require.NotEqual(t, "channel", resolved.Source)
- // Base pricing should still be present (from BillingService fallback)
- require.NotNil(t, resolved.BasePricing)
- require.InDelta(t, 3e-6, resolved.BasePricing.InputPricePerToken, 1e-12)
- }
- // ===========================================================================
- // 7. GetRequestTierPriceByContext boundary tests
- // ===========================================================================
- func TestGetRequestTierPriceByContext_EmptyTiers(t *testing.T) {
- bs := newTestBillingServiceForResolver()
- r := NewModelPricingResolver(&ChannelService{}, bs)
- resolved := &ResolvedPricing{
- Mode: BillingModePerRequest,
- RequestTiers: nil, // empty
- }
- price := r.GetRequestTierPriceByContext(resolved, 50000)
- require.InDelta(t, 0.0, price, 1e-12)
- // Also test with explicit empty slice
- resolved2 := &ResolvedPricing{
- Mode: BillingModePerRequest,
- RequestTiers: []PricingInterval{},
- }
- price2 := r.GetRequestTierPriceByContext(resolved2, 50000)
- require.InDelta(t, 0.0, price2, 1e-12)
- }
- func TestGetRequestTierPriceByContext_ExactBoundary(t *testing.T) {
- bs := newTestBillingServiceForResolver()
- r := NewModelPricingResolver(&ChannelService{}, bs)
- resolved := &ResolvedPricing{
- Mode: BillingModePerRequest,
- RequestTiers: []PricingInterval{
- {MinTokens: 0, MaxTokens: testPtrInt(128000), PerRequestPrice: testPtrFloat64(0.05)},
- {MinTokens: 128000, MaxTokens: nil, PerRequestPrice: testPtrFloat64(0.10)},
- },
- }
- // totalContextTokens = 128000 exactly:
- // FindMatchingInterval checks: totalTokens > MinTokens && totalTokens <= MaxTokens
- // For first interval: 128000 > 0 (true) && 128000 <= 128000 (true) → matches first interval
- price := r.GetRequestTierPriceByContext(resolved, 128000)
- require.InDelta(t, 0.05, price, 1e-12)
- // totalContextTokens = 128001 should match second interval
- // For first interval: 128001 > 0 (true) && 128001 <= 128000 (false) → no match
- // For second interval: 128001 > 128000 (true) && MaxTokens == nil → matches
- 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: "interval with only multiplier kept",
- intervals: []PricingInterval{
- {MinTokens: 272000, InputMultiplier: testPtrFloat64(2)},
- },
- 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)
- })
- }
- }
- // ===========================================================================
- // 9. ImageOutputPriceExplicit tests
- // ===========================================================================
- func TestApplyTokenOverrides_FlatSetsImageOutputPriceExplicit(t *testing.T) {
- r := newResolverWithChannel(t, []ChannelModelPricing{{
- Platform: "anthropic",
- Models: []string{"claude-sonnet-4"},
- BillingMode: BillingModeToken,
- InputPrice: testPtrFloat64(3e-6),
- OutputPrice: testPtrFloat64(15e-6),
- // ImageOutputPrice intentionally nil
- }})
- resolved := r.Resolve(context.Background(), PricingInput{
- Model: "claude-sonnet-4",
- GroupID: groupIDPtr(),
- })
- require.Equal(t, PricingSourceChannel, resolved.Source)
- require.True(t, resolved.BasePricing.ImageOutputPriceExplicit)
- require.Equal(t, 0.0, resolved.BasePricing.ImageOutputPricePerToken)
- }
- func TestApplyTokenOverrides_FlatWithImageOutputPriceSetsExplicit(t *testing.T) {
- r := newResolverWithChannel(t, []ChannelModelPricing{{
- Platform: "anthropic",
- Models: []string{"claude-sonnet-4"},
- BillingMode: BillingModeToken,
- InputPrice: testPtrFloat64(3e-6),
- OutputPrice: testPtrFloat64(15e-6),
- ImageOutputPrice: testPtrFloat64(50e-6),
- }})
- resolved := r.Resolve(context.Background(), PricingInput{
- Model: "claude-sonnet-4",
- GroupID: groupIDPtr(),
- })
- require.True(t, resolved.BasePricing.ImageOutputPriceExplicit)
- require.InDelta(t, 50e-6, resolved.BasePricing.ImageOutputPricePerToken, 1e-12)
- }
- func TestApplyTokenOverrides_IntervalSetsImageOutputPriceExplicit(t *testing.T) {
- r := newResolverWithChannel(t, []ChannelModelPricing{{
- Platform: "anthropic",
- Models: []string{"claude-sonnet-4"},
- BillingMode: BillingModeToken,
- // No ImageOutputPrice
- Intervals: []PricingInterval{
- {MinTokens: 0, MaxTokens: testPtrInt(100000), InputPrice: testPtrFloat64(3e-6), OutputPrice: testPtrFloat64(15e-6)},
- },
- }})
- resolved := r.Resolve(context.Background(), PricingInput{
- Model: "claude-sonnet-4",
- GroupID: groupIDPtr(),
- })
- // BasePricing should have explicit mark (for interval fallback)
- require.True(t, resolved.BasePricing.ImageOutputPriceExplicit)
- require.Equal(t, 0.0, resolved.BasePricing.ImageOutputPricePerToken)
- // intervalToModelPricing should also have explicit mark
- pricing := r.GetIntervalPricing(resolved, 50000)
- require.True(t, pricing.ImageOutputPriceExplicit)
- require.Equal(t, 0.0, pricing.ImageOutputPricePerToken)
- }
- // ===========================================================================
- // 10. Regression: channel overrides must not pollute fallbackPrices
- // ===========================================================================
- // TestApplyTokenOverrides_FlatDoesNotPolluteFallbackPrices verifies that the
- // flat-override path in applyTokenOverrides clones the BasePricing struct
- // before mutation, so the shared fallbackPrices map entry is not written through.
- func TestApplyTokenOverrides_FlatDoesNotPolluteFallbackPrices(t *testing.T) {
- r := newResolverWithChannel(t, []ChannelModelPricing{{
- Platform: "anthropic",
- Models: []string{"claude-sonnet-4"},
- BillingMode: BillingModeToken,
- InputPrice: testPtrFloat64(10e-6), // base is 3e-6
- OutputPrice: testPtrFloat64(50e-6), // base is 15e-6
- }})
- resolved := r.Resolve(context.Background(), PricingInput{
- Model: "claude-sonnet-4",
- GroupID: groupIDPtr(),
- })
- // Resolved pricing should reflect the channel override
- require.NotNil(t, resolved)
- require.InDelta(t, 10e-6, resolved.BasePricing.InputPricePerToken, 1e-12)
- require.InDelta(t, 50e-6, resolved.BasePricing.OutputPricePerToken, 1e-12)
- // Global fallbackPrices must NOT be polluted
- fp := r.billingService.fallbackPrices["claude-sonnet-4"]
- require.InDelta(t, 3e-6, fp.InputPricePerToken, 1e-12, "fallback InputPricePerToken polluted")
- require.InDelta(t, 15e-6, fp.OutputPricePerToken, 1e-12, "fallback OutputPricePerToken polluted")
- require.False(t, fp.ImageOutputPriceExplicit, "fallback ImageOutputPriceExplicit polluted")
- }
- // TestApplyTokenOverrides_IntervalDoesNotPolluteFallbackPrices verifies that
- // the interval-override path also clones before mutation.
- func TestApplyTokenOverrides_IntervalDoesNotPolluteFallbackPrices(t *testing.T) {
- r := newResolverWithChannel(t, []ChannelModelPricing{{
- Platform: "anthropic",
- Models: []string{"claude-sonnet-4"},
- BillingMode: BillingModeToken,
- Intervals: []PricingInterval{
- {MinTokens: 0, MaxTokens: testPtrInt(100000), InputPrice: testPtrFloat64(2e-6), OutputPrice: testPtrFloat64(8e-6)},
- },
- }})
- resolved := r.Resolve(context.Background(), PricingInput{
- Model: "claude-sonnet-4",
- GroupID: groupIDPtr(),
- })
- require.NotNil(t, resolved)
- require.True(t, resolved.BasePricing.ImageOutputPriceExplicit)
- // Global fallbackPrices must NOT be polluted
- fp := r.billingService.fallbackPrices["claude-sonnet-4"]
- require.InDelta(t, 3e-6, fp.InputPricePerToken, 1e-12, "fallback InputPricePerToken polluted")
- require.InDelta(t, 15e-6, fp.OutputPricePerToken, 1e-12, "fallback OutputPricePerToken polluted")
- require.False(t, fp.ImageOutputPriceExplicit, "fallback ImageOutputPriceExplicit polluted")
- }
- func TestResolve_GroupPricingOverridesChannel(t *testing.T) {
- r := newResolverWithChannel(t, []ChannelModelPricing{{
- Platform: "anthropic", Models: []string{"claude-sonnet-4"}, BillingMode: BillingModeToken,
- InputPrice: testPtrFloat64(10e-6), OutputPrice: testPtrFloat64(20e-6),
- }})
- group := &Group{ID: 100, ModelPricing: []ChannelModelPricing{{
- Models: []string{"claude-sonnet-*"}, BillingMode: BillingModeToken,
- InputPrice: testPtrFloat64(1e-6), OutputPrice: testPtrFloat64(2e-6),
- }}}
- resolved := r.Resolve(context.Background(), PricingInput{Model: "claude-sonnet-4", GroupID: groupIDPtr(), Group: group})
- require.Equal(t, PricingSourceGroup, resolved.Source)
- require.InDelta(t, 1e-6, resolved.BasePricing.InputPricePerToken, 1e-12)
- require.InDelta(t, 2e-6, resolved.BasePricing.OutputPricePerToken, 1e-12)
- }
- func TestResolve_GroupLongContextUsesPresetNotCustomIntervals(t *testing.T) {
- bs := newTestBillingServiceForResolver()
- bs.fallbackPrices["claude-sonnet-4"].LongContextInputThreshold = 200000
- bs.fallbackPrices["claude-sonnet-4"].LongContextThresholdInclusive = true
- bs.fallbackPrices["claude-sonnet-4"].LongContextInputMultiplier = 2
- bs.fallbackPrices["claude-sonnet-4"].LongContextOutputMultiplier = 2
- r := NewModelPricingResolver(nil, bs)
- group := &Group{ID: 100, ModelPricing: []ChannelModelPricing{{
- Models: []string{"claude-sonnet-4"}, BillingMode: BillingModeToken,
- InputPrice: testPtrFloat64(1e-6), OutputPrice: testPtrFloat64(2e-6),
- Intervals: []PricingInterval{
- {MinTokens: 0, MaxTokens: testPtrInt(200000), InputPrice: testPtrFloat64(9e-6)},
- {MinTokens: 200000, InputPrice: testPtrFloat64(18e-6)},
- },
- }}}
- resolved := r.Resolve(context.Background(), PricingInput{Model: "claude-sonnet-4", Group: group})
- require.False(t, resolved.longContextPricingEnabled)
- require.Empty(t, resolved.Intervals, "group token intervals are not a user-facing long-context ladder")
- require.InDelta(t, 1e-6, r.GetIntervalPricing(resolved, 300000).InputPricePerToken, 1e-12)
- require.Equal(t, 200000, resolved.BasePricing.LongContextInputThreshold)
- group.LongContextPricingEnabled = true
- resolved = r.Resolve(context.Background(), PricingInput{Model: "claude-sonnet-4", Group: group})
- require.True(t, resolved.longContextPricingEnabled)
- require.Empty(t, resolved.Intervals)
- require.InDelta(t, 1e-6, r.GetIntervalPricing(resolved, 300000).InputPricePerToken, 1e-12)
- require.Equal(t, 200000, resolved.BasePricing.LongContextInputThreshold)
- require.InDelta(t, 2.0, resolved.BasePricing.LongContextInputMultiplier, 1e-12)
- }
- func TestCalculateCostUnified_UsesContinuousMediaUnits(t *testing.T) {
- bs := newTestBillingServiceForResolver()
- r := NewModelPricingResolver(nil, bs)
- price := 0.08
- group := &Group{ModelPricing: []ChannelModelPricing{{
- Models: []string{"grok-voice-think-fast-2.0"}, BillingMode: BillingModePerRequest,
- PerRequestPrice: &price,
- }}}
- cost, err := bs.CalculateCostUnified(CostInput{
- Ctx: context.Background(), Model: "grok-voice-think-fast-2.0", Group: group,
- UsageUnits: 1.5, RateMultiplier: 1, Resolver: r,
- })
- require.NoError(t, err)
- require.InDelta(t, 0.12, cost.TotalCost, 1e-12)
- }
|