| 1 | package eventwire |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "testing" |
| 6 | "time" |
| 7 | |
| 8 | "reasonix/internal/billing" |
| 9 | "reasonix/internal/event" |
| 10 | "reasonix/internal/provider" |
| 11 | ) |
| 12 | |
| 13 | // Ensures older clients still see cost/currency aliases while new clients get costQuote. |
| 14 | func TestToWireUsageDualWritesCostQuoteAndLegacyAliases(t *testing.T) { |
| 15 | e := event.Event{ |
| 16 | Kind: event.Usage, |
| 17 | ModelRef: "deepseek-flash/deepseek-v4-flash", |
| 18 | Usage: &provider.Usage{PromptTokens: 1_000_000, CompletionTokens: 1_000_000, TotalTokens: 2_000_000}, |
| 19 | Pricing: &provider.Pricing{CacheHit: 0.10, Input: 3, Output: 9, Currency: "¥"}, |
| 20 | CostQuote: func() *billing.CostQuote { |
| 21 | q := billing.BuildQuote(billing.QuoteInput{ |
| 22 | Usage: billing.UsageTokens{PromptTokens: 1_000_000, CompletionTokens: 1_000_000}, |
| 23 | Rates: billing.RateCard{CacheHit: 0.10, Input: 3, Output: 9, Currency: "CNY"}, |
| 24 | OccurredAt: time.Date(2026, 8, 17, 0, 30, 0, 0, time.UTC), |
| 25 | DisplayCurrency: "USD", |
| 26 | ProviderKind: "deepseek", |
| 27 | ModelID: "deepseek-v4-flash", |
| 28 | ScheduleID: billing.ScheduleDeepSeekV4August2026, |
| 29 | }) |
| 30 | return &q |
| 31 | }(), |
| 32 | } |
| 33 | w := ToWire(e) |
| 34 | if w.Usage == nil || w.Usage.CostQuote == nil { |
| 35 | t.Fatal("missing costQuote") |
| 36 | } |
| 37 | if w.Usage.CostQuote.Valuations["USD"].Basis != billing.BasisOfficialTable { |
| 38 | t.Fatalf("USD basis = %q, want official_table", w.Usage.CostQuote.Valuations["USD"].Basis) |
| 39 | } |
| 40 | if w.Usage.Cost <= 0 || w.Usage.CostUSD != w.Usage.Cost { |
| 41 | t.Fatalf("legacy cost aliases = cost:%v costUsd:%v", w.Usage.Cost, w.Usage.CostUSD) |
| 42 | } |
| 43 | raw, err := json.Marshal(w.Usage) |
| 44 | if err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | if !json.Valid(raw) { |
| 48 | t.Fatal("usage json invalid") |
| 49 | } |
| 50 | // Old clients ignore unknown fields; new field present. |
| 51 | var m map[string]any |
| 52 | if err := json.Unmarshal(raw, &m); err != nil { |
| 53 | t.Fatal(err) |
| 54 | } |
| 55 | if _, ok := m["costQuote"]; !ok { |
| 56 | t.Fatalf("costQuote missing from JSON: %s", raw) |
| 57 | } |
| 58 | if _, ok := m["cost"]; !ok { |
| 59 | t.Fatalf("legacy cost missing: %s", raw) |
| 60 | } |
| 61 | quoteJSON, _ := m["costQuote"].(map[string]any) |
| 62 | if quoteJSON["rateBand"] != billing.RateBandOffPeak || quoteJSON["ratedAt"] != "2026-08-17T00:30:00Z" { |
| 63 | t.Fatalf("scheduled quote metadata missing: %s", raw) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | func TestLegacyCostQuoteWithoutScheduleFieldsStillDecodes(t *testing.T) { |
| 68 | var quote billing.CostQuote |
| 69 | if err := json.Unmarshal([]byte(`{"original":{"amount":"1.25","currency":"CNY"},"estimated":true,"complete":true}`), "e); err != nil { |
| 70 | t.Fatal(err) |
| 71 | } |
| 72 | if quote.Original.Amount != "1.25" || quote.RateBand != "" || quote.RatedAt != "" { |
| 73 | t.Fatalf("legacy quote changed during decode: %+v", quote) |
| 74 | } |
| 75 | } |
| 76 |