| 1 | package event |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | "time" |
| 6 | |
| 7 | "reasonix/internal/billing" |
| 8 | "reasonix/internal/provider" |
| 9 | ) |
| 10 | |
| 11 | func TestEnsureCostQuoteDoesNotUseRuntimeFX(t *testing.T) { |
| 12 | e := Event{Kind: Usage, ModelRef: "deepseek/deepseek-v4-flash", UsageSource: UsageSourceExecutor, |
| 13 | Pricing: &provider.Pricing{Input: 1, Output: 2, Currency: "CNY"}, |
| 14 | Usage: &provider.Usage{PromptTokens: 100, CompletionTokens: 100}} |
| 15 | ctx := &QuoteContext{DisplayCurrency: "USD"} |
| 16 | q := EnsureCostQuote(e, ctx) |
| 17 | if q == nil || q.Selected == nil || q.Selected.Currency != "CNY" || q.Complete { |
| 18 | t.Fatalf("quote = %+v", q) |
| 19 | } |
| 20 | for code, valuation := range q.Valuations { |
| 21 | if valuation.Basis == "fx" { |
| 22 | t.Fatalf("runtime FX valuation %s = %+v", code, valuation) |
| 23 | } |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | func TestCostQuoteSinkRatesOnceAndPreservesExistingQuote(t *testing.T) { |
| 28 | nowCalls := 0 |
| 29 | ctx := &QuoteContext{ |
| 30 | Now: func() time.Time { |
| 31 | nowCalls++ |
| 32 | return time.Date(2026, 8, 17, 0, 0, 0, 0, time.UTC) |
| 33 | }, |
| 34 | PricingContextForModel: func(string) billing.PricingContext { |
| 35 | return billing.PricingContext{ |
| 36 | ProviderKind: "deepseek", ModelID: "deepseek-v4-pro", BillingMode: billing.BillingModePAYG, |
| 37 | ScheduleID: billing.ScheduleDeepSeekV4August2026, CatalogSource: billing.DocDeepSeekPricing, |
| 38 | } |
| 39 | }, |
| 40 | } |
| 41 | e := Event{Kind: Usage, ModelRef: "deepseek/deepseek-v4-pro", |
| 42 | Pricing: &provider.Pricing{CacheHit: 0.30, Input: 9, Output: 27, Currency: "CNY"}, |
| 43 | Usage: &provider.Usage{CompletionTokens: 1_000_000, TotalTokens: 1_000_000}} |
| 44 | var first *billing.CostQuote |
| 45 | sink := NewCostQuoteSink(FuncSink(func(got Event) { first = got.CostQuote }), ctx) |
| 46 | sink.Emit(e) |
| 47 | if nowCalls != 1 || first == nil || first.RateBand != billing.RateBandOffPeak || first.Original.Amount != "13.5" { |
| 48 | t.Fatalf("quote=%+v nowCalls=%d", first, nowCalls) |
| 49 | } |
| 50 | |
| 51 | prebuilt := &billing.CostQuote{Original: billing.Money{Amount: "7", Currency: "USD"}, RateBand: billing.RateBandPeak} |
| 52 | e.CostQuote = prebuilt |
| 53 | sink.Emit(e) |
| 54 | if first != prebuilt || nowCalls != 1 { |
| 55 | t.Fatalf("existing quote was recomputed: got=%p want=%p nowCalls=%d", first, prebuilt, nowCalls) |
| 56 | } |
| 57 | } |
| 58 |