返回 DeepSeek-Reasonix
money_test.go
根目录 / internal / billing / money_test.go
1 package billing
2
3 import (
4 "testing"
5 )
6
7 func TestAmountRoundTrip(t *testing.T) {
8 a, err := NewAmountFromString("1.23456789")
9 if err != nil {
10 t.Fatal(err)
11 }
12 if got := a.String(); got != "1.23456789" {
13 t.Fatalf("String = %q", got)
14 }
15 if a.Float64() < 1.23456788 || a.Float64() > 1.23456790 {
16 t.Fatalf("Float64 = %v", a.Float64())
17 }
18 }
19
20 func TestAddMoneyRejectsMixedCurrency(t *testing.T) {
21 _, err := AddMoney(MoneyOf(NewAmountFromFloat(1), "CNY"), MoneyOf(NewAmountFromFloat(1), "USD"))
22 if err == nil {
23 t.Fatal("expected mixed currency error")
24 }
25 }
26
27 func TestAddMoneySameCurrency(t *testing.T) {
28 sum, err := AddMoney(MoneyOf(NewAmountFromFloat(1.5), "USD"), MoneyOf(NewAmountFromFloat(2.25), "USD"))
29 if err != nil {
30 t.Fatal(err)
31 }
32 if sum.Currency != "USD" || sum.Amount != "3.75" {
33 t.Fatalf("sum = %+v", sum)
34 }
35 }
36
37 func TestNormalizeCurrency(t *testing.T) {
38 cases := map[string]string{
39 "¥": "CNY", "$": "USD", "rmb": "CNY", "usd": "USD", "eur": "EUR",
40 }
41 for in, want := range cases {
42 if got := NormalizeCurrency(in); got != want {
43 t.Errorf("NormalizeCurrency(%q) = %q, want %q", in, got, want)
44 }
45 }
46 }
47
48 func TestOriginalCostMatchesLegacySemantics(t *testing.T) {
49 // Same as provider.Pricing.Cost cache-write split test: miss 500k, write 100k billed 200k, input rate 2 → 1.2
50 amt := OriginalCostAmount(RateCard{Input: 2, Currency: "USD"}, UsageTokens{
51 CacheMissTokens: 500_000, CacheWriteTokens: 100_000, CacheWriteBilledTokens: 200_000,
52 })
53 if got := amt.Float64(); got < 1.199 || got > 1.201 {
54 t.Fatalf("cost = %v, want ~1.2", got)
55 }
56 }
57
57 lines GO