billingexpr_test.go 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  1. package billingexpr_test
  2. import (
  3. "math"
  4. "math/rand"
  5. "testing"
  6. "github.com/QuantumNous/new-api/pkg/billingexpr"
  7. )
  8. // ---------------------------------------------------------------------------
  9. // Claude-style: fixed tiers, input > 200K changes both input & output price
  10. // ---------------------------------------------------------------------------
  11. const claudeExpr = `p <= 200000 ? tier("standard", p * 1.5 + c * 7.5) : tier("long_context", p * 3.0 + c * 11.25)`
  12. func TestClaude_StandardTier(t *testing.T) {
  13. cost, trace, err := billingexpr.RunExpr(claudeExpr, billingexpr.TokenParams{P: 100000, C: 5000})
  14. if err != nil {
  15. t.Fatal(err)
  16. }
  17. want := 100000*1.5 + 5000*7.5
  18. if math.Abs(cost-want) > 1e-6 {
  19. t.Errorf("cost = %f, want %f", cost, want)
  20. }
  21. if trace.MatchedTier != "standard" {
  22. t.Errorf("tier = %q, want %q", trace.MatchedTier, "standard")
  23. }
  24. }
  25. func TestClaude_LongContextTier(t *testing.T) {
  26. cost, trace, err := billingexpr.RunExpr(claudeExpr, billingexpr.TokenParams{P: 300000, C: 10000})
  27. if err != nil {
  28. t.Fatal(err)
  29. }
  30. want := 300000*3.0 + 10000*11.25
  31. if math.Abs(cost-want) > 1e-6 {
  32. t.Errorf("cost = %f, want %f", cost, want)
  33. }
  34. if trace.MatchedTier != "long_context" {
  35. t.Errorf("tier = %q, want %q", trace.MatchedTier, "long_context")
  36. }
  37. }
  38. func TestClaude_BoundaryExact(t *testing.T) {
  39. cost, trace, err := billingexpr.RunExpr(claudeExpr, billingexpr.TokenParams{P: 200000, C: 1000})
  40. if err != nil {
  41. t.Fatal(err)
  42. }
  43. want := 200000*1.5 + 1000*7.5
  44. if math.Abs(cost-want) > 1e-6 {
  45. t.Errorf("cost = %f, want %f", cost, want)
  46. }
  47. if trace.MatchedTier != "standard" {
  48. t.Errorf("tier = %q, want %q", trace.MatchedTier, "standard")
  49. }
  50. }
  51. // ---------------------------------------------------------------------------
  52. // GLM-style: multi-condition tiers with both input and output dimensions
  53. // ---------------------------------------------------------------------------
  54. const glmExpr = `
  55. (
  56. p < 32000 && c < 200 ? tier("tier1_short", (p)*2 + c*8) :
  57. p < 32000 && c >= 200 ? tier("tier2_long_output", (p)*3 + c*14) :
  58. tier("tier3_long_input", (p)*4 + c*16)
  59. ) / 1000000
  60. `
  61. func TestGLM_Tier1(t *testing.T) {
  62. cost, trace, err := billingexpr.RunExpr(glmExpr, billingexpr.TokenParams{P: 15000, C: 100})
  63. if err != nil {
  64. t.Fatal(err)
  65. }
  66. want := (15000.0*2 + 100.0*8) / 1000000
  67. if math.Abs(cost-want) > 1e-10 {
  68. t.Errorf("cost = %f, want %f", cost, want)
  69. }
  70. if trace.MatchedTier != "tier1_short" {
  71. t.Errorf("tier = %q, want %q", trace.MatchedTier, "tier1_short")
  72. }
  73. }
  74. func TestGLM_Tier2(t *testing.T) {
  75. cost, trace, err := billingexpr.RunExpr(glmExpr, billingexpr.TokenParams{P: 15000, C: 500})
  76. if err != nil {
  77. t.Fatal(err)
  78. }
  79. want := (15000.0*3 + 500.0*14) / 1000000
  80. if math.Abs(cost-want) > 1e-10 {
  81. t.Errorf("cost = %f, want %f", cost, want)
  82. }
  83. if trace.MatchedTier != "tier2_long_output" {
  84. t.Errorf("tier = %q, want %q", trace.MatchedTier, "tier2_long_output")
  85. }
  86. }
  87. func TestGLM_Tier3(t *testing.T) {
  88. cost, trace, err := billingexpr.RunExpr(glmExpr, billingexpr.TokenParams{P: 50000, C: 100})
  89. if err != nil {
  90. t.Fatal(err)
  91. }
  92. want := (50000.0*4 + 100.0*16) / 1000000
  93. if math.Abs(cost-want) > 1e-10 {
  94. t.Errorf("cost = %f, want %f", cost, want)
  95. }
  96. if trace.MatchedTier != "tier3_long_input" {
  97. t.Errorf("tier = %q, want %q", trace.MatchedTier, "tier3_long_input")
  98. }
  99. }
  100. // ---------------------------------------------------------------------------
  101. // Simple flat-rate (no tier() call)
  102. // ---------------------------------------------------------------------------
  103. func TestSimpleExpr_NoTier(t *testing.T) {
  104. cost, trace, err := billingexpr.RunExpr("p * 0.5 + c * 1.0", billingexpr.TokenParams{P: 1000, C: 500})
  105. if err != nil {
  106. t.Fatal(err)
  107. }
  108. want := 1000*0.5 + 500*1.0
  109. if math.Abs(cost-want) > 1e-6 {
  110. t.Errorf("cost = %f, want %f", cost, want)
  111. }
  112. if trace.MatchedTier != "" {
  113. t.Errorf("tier should be empty, got %q", trace.MatchedTier)
  114. }
  115. }
  116. // ---------------------------------------------------------------------------
  117. // Math helper functions
  118. // ---------------------------------------------------------------------------
  119. func TestMathHelpers(t *testing.T) {
  120. cost, _, err := billingexpr.RunExpr("max(p, c) * 0.5 + min(p, c) * 0.1", billingexpr.TokenParams{P: 300, C: 500})
  121. if err != nil {
  122. t.Fatal(err)
  123. }
  124. want := 500*0.5 + 300*0.1
  125. if math.Abs(cost-want) > 1e-6 {
  126. t.Errorf("cost = %f, want %f", cost, want)
  127. }
  128. }
  129. func TestRequestProbeHelpers(t *testing.T) {
  130. cost, _, err := billingexpr.RunExprWithRequest(
  131. `p * 0.5 + c * 1.0 * (param("service_tier") == "fast" ? 2 : 1)`,
  132. billingexpr.TokenParams{P: 1000, C: 500},
  133. billingexpr.RequestInput{
  134. Body: []byte(`{"service_tier":"fast"}`),
  135. },
  136. )
  137. if err != nil {
  138. t.Fatal(err)
  139. }
  140. want := 1000*0.5 + 500*1.0*2
  141. if math.Abs(cost-want) > 1e-6 {
  142. t.Errorf("cost = %f, want %f", cost, want)
  143. }
  144. }
  145. func TestHeaderProbeHelper(t *testing.T) {
  146. cost, _, err := billingexpr.RunExprWithRequest(
  147. `p * 0.5 + c * 1.0 * (has(header("anthropic-beta"), "fast-mode") ? 2 : 1)`,
  148. billingexpr.TokenParams{P: 1000, C: 500},
  149. billingexpr.RequestInput{
  150. Headers: map[string]string{
  151. "Anthropic-Beta": "fast-mode-2026-02-01",
  152. },
  153. },
  154. )
  155. if err != nil {
  156. t.Fatal(err)
  157. }
  158. want := 1000*0.5 + 500*1.0*2
  159. if math.Abs(cost-want) > 1e-6 {
  160. t.Errorf("cost = %f, want %f", cost, want)
  161. }
  162. }
  163. func TestParamProbeNestedBool(t *testing.T) {
  164. cost, _, err := billingexpr.RunExprWithRequest(
  165. `p * (param("stream_options.fast_mode") == true ? 1.5 : 1.0)`,
  166. billingexpr.TokenParams{P: 100},
  167. billingexpr.RequestInput{
  168. Body: []byte(`{"stream_options":{"fast_mode":true}}`),
  169. },
  170. )
  171. if err != nil {
  172. t.Fatal(err)
  173. }
  174. want := 150.0
  175. if math.Abs(cost-want) > 1e-6 {
  176. t.Errorf("cost = %f, want %f", cost, want)
  177. }
  178. }
  179. func TestParamProbeArrayLength(t *testing.T) {
  180. cost, _, err := billingexpr.RunExprWithRequest(
  181. `p * (param("messages.#") > 20 ? 1.2 : 1.0)`,
  182. billingexpr.TokenParams{P: 100},
  183. billingexpr.RequestInput{
  184. Body: []byte(`{"messages":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]}`),
  185. },
  186. )
  187. if err != nil {
  188. t.Fatal(err)
  189. }
  190. want := 120.0
  191. if math.Abs(cost-want) > 1e-6 {
  192. t.Errorf("cost = %f, want %f", cost, want)
  193. }
  194. }
  195. func TestRequestProbeMissingFieldReturnsNil(t *testing.T) {
  196. cost, _, err := billingexpr.RunExprWithRequest(
  197. `param("missing.value") == nil ? 2 : 1`,
  198. billingexpr.TokenParams{},
  199. billingexpr.RequestInput{
  200. Body: []byte(`{"service_tier":"standard"}`),
  201. },
  202. )
  203. if err != nil {
  204. t.Fatal(err)
  205. }
  206. if cost != 2 {
  207. t.Errorf("cost = %f, want 2", cost)
  208. }
  209. }
  210. func TestRequestProbeMultipleRulesMultiply(t *testing.T) {
  211. cost, _, err := billingexpr.RunExprWithRequest(
  212. `(param("service_tier") == "fast" ? 2 : 1) * (has(header("anthropic-beta"), "fast-mode-2026-02-01") ? 2.5 : 1)`,
  213. billingexpr.TokenParams{},
  214. billingexpr.RequestInput{
  215. Headers: map[string]string{
  216. "Anthropic-Beta": "fast-mode-2026-02-01",
  217. },
  218. Body: []byte(`{"service_tier":"fast"}`),
  219. },
  220. )
  221. if err != nil {
  222. t.Fatal(err)
  223. }
  224. if math.Abs(cost-5) > 1e-6 {
  225. t.Errorf("cost = %f, want 5", cost)
  226. }
  227. }
  228. func TestCeilFloor(t *testing.T) {
  229. cost, _, err := billingexpr.RunExpr("ceil(p / 1000) * 0.5", billingexpr.TokenParams{P: 1500})
  230. if err != nil {
  231. t.Fatal(err)
  232. }
  233. want := math.Ceil(1500.0/1000) * 0.5
  234. if math.Abs(cost-want) > 1e-6 {
  235. t.Errorf("cost = %f, want %f", cost, want)
  236. }
  237. }
  238. // ---------------------------------------------------------------------------
  239. // Zero tokens
  240. // ---------------------------------------------------------------------------
  241. func TestZeroTokens(t *testing.T) {
  242. cost, _, err := billingexpr.RunExpr(claudeExpr, billingexpr.TokenParams{})
  243. if err != nil {
  244. t.Fatal(err)
  245. }
  246. if cost != 0 {
  247. t.Errorf("cost should be 0 for zero tokens, got %f", cost)
  248. }
  249. }
  250. // ---------------------------------------------------------------------------
  251. // Rounding
  252. // ---------------------------------------------------------------------------
  253. func TestQuotaRound(t *testing.T) {
  254. tests := []struct {
  255. in float64
  256. want int
  257. }{
  258. {0, 0},
  259. {0.4, 0},
  260. {0.5, 1},
  261. {0.6, 1},
  262. {1.5, 2},
  263. {-0.5, -1},
  264. {-0.6, -1},
  265. {999.4999, 999},
  266. {999.5, 1000},
  267. {1e9 + 0.5, 1e9 + 1},
  268. }
  269. for _, tt := range tests {
  270. got := billingexpr.QuotaRound(tt.in)
  271. if got != tt.want {
  272. t.Errorf("QuotaRound(%f) = %d, want %d", tt.in, got, tt.want)
  273. }
  274. }
  275. }
  276. // ---------------------------------------------------------------------------
  277. // Settlement
  278. // ---------------------------------------------------------------------------
  279. func TestComputeTieredQuota_Basic(t *testing.T) {
  280. snap := &billingexpr.BillingSnapshot{
  281. BillingMode: "tiered_expr",
  282. ExprString: claudeExpr,
  283. ExprHash: billingexpr.ExprHashString(claudeExpr),
  284. GroupRatio: 1.0,
  285. EstimatedPromptTokens: 100000,
  286. EstimatedCompletionTokens: 5000,
  287. EstimatedQuotaBeforeGroup: (100000*1.5 + 5000*7.5) / 1_000_000 * 500_000,
  288. EstimatedQuotaAfterGroup: billingexpr.QuotaRound((100000*1.5 + 5000*7.5) / 1_000_000 * 500_000),
  289. EstimatedTier: "standard",
  290. QuotaPerUnit: 500_000,
  291. }
  292. result, err := billingexpr.ComputeTieredQuota(snap, billingexpr.TokenParams{P: 300000, C: 10000})
  293. if err != nil {
  294. t.Fatal(err)
  295. }
  296. wantBefore := (300000*3.0 + 10000*11.25) / 1_000_000 * 500_000
  297. if math.Abs(result.ActualQuotaBeforeGroup-wantBefore) > 1e-6 {
  298. t.Errorf("before group: got %f, want %f", result.ActualQuotaBeforeGroup, wantBefore)
  299. }
  300. if result.MatchedTier != "long_context" {
  301. t.Errorf("tier = %q, want %q", result.MatchedTier, "long_context")
  302. }
  303. if !result.CrossedTier {
  304. t.Error("expected crossed_tier=true (estimated standard, actual long_context)")
  305. }
  306. }
  307. func TestComputeTieredQuota_SameTier(t *testing.T) {
  308. snap := &billingexpr.BillingSnapshot{
  309. BillingMode: "tiered_expr",
  310. ExprString: claudeExpr,
  311. ExprHash: billingexpr.ExprHashString(claudeExpr),
  312. GroupRatio: 1.5,
  313. EstimatedPromptTokens: 50000,
  314. EstimatedCompletionTokens: 1000,
  315. EstimatedQuotaBeforeGroup: (50000*1.5 + 1000*7.5) / 1_000_000 * 500_000,
  316. EstimatedQuotaAfterGroup: billingexpr.QuotaRound((50000*1.5 + 1000*7.5) / 1_000_000 * 500_000 * 1.5),
  317. EstimatedTier: "standard",
  318. QuotaPerUnit: 500_000,
  319. }
  320. result, err := billingexpr.ComputeTieredQuota(snap, billingexpr.TokenParams{P: 80000, C: 2000})
  321. if err != nil {
  322. t.Fatal(err)
  323. }
  324. wantBefore := (80000*1.5 + 2000*7.5) / 1_000_000 * 500_000
  325. wantAfter := billingexpr.QuotaRound(wantBefore * 1.5)
  326. if result.ActualQuotaAfterGroup != wantAfter {
  327. t.Errorf("after group: got %d, want %d", result.ActualQuotaAfterGroup, wantAfter)
  328. }
  329. if result.CrossedTier {
  330. t.Error("expected crossed_tier=false (both standard)")
  331. }
  332. }
  333. // ---------------------------------------------------------------------------
  334. // Compile errors
  335. // ---------------------------------------------------------------------------
  336. func TestCompileError(t *testing.T) {
  337. _, _, err := billingexpr.RunExpr("invalid +-+ syntax", billingexpr.TokenParams{})
  338. if err == nil {
  339. t.Error("expected compile error")
  340. }
  341. }
  342. // ---------------------------------------------------------------------------
  343. // Compile Cache
  344. // ---------------------------------------------------------------------------
  345. func TestCompileCache_SameResult(t *testing.T) {
  346. r1, _, err := billingexpr.RunExpr("p * 0.5", billingexpr.TokenParams{P: 100})
  347. if err != nil {
  348. t.Fatal(err)
  349. }
  350. r2, _, err := billingexpr.RunExpr("p * 0.5", billingexpr.TokenParams{P: 100})
  351. if err != nil {
  352. t.Fatal(err)
  353. }
  354. if r1 != r2 {
  355. t.Errorf("cached and uncached results differ: %f != %f", r1, r2)
  356. }
  357. }
  358. func TestInvalidateCache(t *testing.T) {
  359. billingexpr.InvalidateCache()
  360. r1, _, _ := billingexpr.RunExpr("p * 0.5", billingexpr.TokenParams{P: 100})
  361. billingexpr.InvalidateCache()
  362. r2, _, _ := billingexpr.RunExpr("p * 0.5", billingexpr.TokenParams{P: 100})
  363. if r1 != r2 {
  364. t.Errorf("post-invalidate results differ: %f != %f", r1, r2)
  365. }
  366. }
  367. // ---------------------------------------------------------------------------
  368. // Hash
  369. // ---------------------------------------------------------------------------
  370. func TestExprHashString_Deterministic(t *testing.T) {
  371. h1 := billingexpr.ExprHashString("p * 0.5")
  372. h2 := billingexpr.ExprHashString("p * 0.5")
  373. if h1 != h2 {
  374. t.Error("hash should be deterministic")
  375. }
  376. h3 := billingexpr.ExprHashString("p * 0.6")
  377. if h1 == h3 {
  378. t.Error("different expressions should have different hashes")
  379. }
  380. }
  381. // ---------------------------------------------------------------------------
  382. // Cache variables: present
  383. // ---------------------------------------------------------------------------
  384. const claudeWithCacheExpr = `p <= 200000 ? tier("standard", p * 1.5 + c * 7.5 + cr * 0.15 + cc * 1.875) : tier("long_context", p * 3.0 + c * 11.25 + cr * 0.3 + cc * 3.75)`
  385. func TestCachePresent_StandardTier(t *testing.T) {
  386. params := billingexpr.TokenParams{P: 100000, C: 5000, CR: 50000, CC: 10000}
  387. cost, trace, err := billingexpr.RunExpr(claudeWithCacheExpr, params)
  388. if err != nil {
  389. t.Fatal(err)
  390. }
  391. want := 100000*1.5 + 5000*7.5 + 50000*0.15 + 10000*1.875
  392. if math.Abs(cost-want) > 1e-6 {
  393. t.Errorf("cost = %f, want %f", cost, want)
  394. }
  395. if trace.MatchedTier != "standard" {
  396. t.Errorf("tier = %q, want %q", trace.MatchedTier, "standard")
  397. }
  398. }
  399. func TestCachePresent_LongContextTier(t *testing.T) {
  400. params := billingexpr.TokenParams{P: 300000, C: 10000, CR: 100000, CC: 20000}
  401. cost, trace, err := billingexpr.RunExpr(claudeWithCacheExpr, params)
  402. if err != nil {
  403. t.Fatal(err)
  404. }
  405. want := 300000*3.0 + 10000*11.25 + 100000*0.3 + 20000*3.75
  406. if math.Abs(cost-want) > 1e-6 {
  407. t.Errorf("cost = %f, want %f", cost, want)
  408. }
  409. if trace.MatchedTier != "long_context" {
  410. t.Errorf("tier = %q, want %q", trace.MatchedTier, "long_context")
  411. }
  412. }
  413. // ---------------------------------------------------------------------------
  414. // Cache variables: absent (all zero) — same expression still works
  415. // ---------------------------------------------------------------------------
  416. func TestCacheAbsent_ZeroCacheTokens(t *testing.T) {
  417. params := billingexpr.TokenParams{P: 100000, C: 5000}
  418. cost, trace, err := billingexpr.RunExpr(claudeWithCacheExpr, params)
  419. if err != nil {
  420. t.Fatal(err)
  421. }
  422. want := 100000*1.5 + 5000*7.5
  423. if math.Abs(cost-want) > 1e-6 {
  424. t.Errorf("cost = %f, want %f (cache terms should be 0)", cost, want)
  425. }
  426. if trace.MatchedTier != "standard" {
  427. t.Errorf("tier = %q, want %q", trace.MatchedTier, "standard")
  428. }
  429. }
  430. // ---------------------------------------------------------------------------
  431. // Mixed cache fields: cc and cc1h non-zero
  432. // ---------------------------------------------------------------------------
  433. const claudeCacheSplitExpr = `tier("default", p * 1.5 + c * 7.5 + cr * 0.15 + cc * 2.0 + cc1h * 3.0)`
  434. func TestMixedCacheFields(t *testing.T) {
  435. params := billingexpr.TokenParams{P: 100000, C: 5000, CR: 10000, CC: 5000, CC1h: 2000}
  436. cost, _, err := billingexpr.RunExpr(claudeCacheSplitExpr, params)
  437. if err != nil {
  438. t.Fatal(err)
  439. }
  440. want := 100000*1.5 + 5000*7.5 + 10000*0.15 + 5000*2.0 + 2000*3.0
  441. if math.Abs(cost-want) > 1e-6 {
  442. t.Errorf("cost = %f, want %f", cost, want)
  443. }
  444. }
  445. func TestMixedCacheFields_AllCacheZero(t *testing.T) {
  446. params := billingexpr.TokenParams{P: 100000, C: 5000}
  447. cost, _, err := billingexpr.RunExpr(claudeCacheSplitExpr, params)
  448. if err != nil {
  449. t.Fatal(err)
  450. }
  451. want := 100000*1.5 + 5000*7.5
  452. if math.Abs(cost-want) > 1e-6 {
  453. t.Errorf("cost = %f, want %f (all cache zero)", cost, want)
  454. }
  455. }
  456. // ---------------------------------------------------------------------------
  457. // Backward compatibility: p+c only expressions still work with TokenParams
  458. // ---------------------------------------------------------------------------
  459. func TestBackwardCompat_OldExprWithTokenParams(t *testing.T) {
  460. params := billingexpr.TokenParams{P: 100000, C: 5000, CR: 99999, CC: 88888}
  461. cost, trace, err := billingexpr.RunExpr(claudeExpr, params)
  462. if err != nil {
  463. t.Fatal(err)
  464. }
  465. want := 100000*1.5 + 5000*7.5
  466. if math.Abs(cost-want) > 1e-6 {
  467. t.Errorf("cost = %f, want %f (old expr ignores cache fields)", cost, want)
  468. }
  469. if trace.MatchedTier != "standard" {
  470. t.Errorf("tier = %q, want %q", trace.MatchedTier, "standard")
  471. }
  472. }
  473. // ---------------------------------------------------------------------------
  474. // Settlement with cache tokens
  475. // ---------------------------------------------------------------------------
  476. func TestComputeTieredQuota_WithCache(t *testing.T) {
  477. snap := &billingexpr.BillingSnapshot{
  478. BillingMode: "tiered_expr",
  479. ExprString: claudeWithCacheExpr,
  480. ExprHash: billingexpr.ExprHashString(claudeWithCacheExpr),
  481. GroupRatio: 1.0,
  482. EstimatedPromptTokens: 100000,
  483. EstimatedCompletionTokens: 5000,
  484. EstimatedQuotaBeforeGroup: (100000*1.5 + 5000*7.5) / 1_000_000 * 500_000,
  485. EstimatedQuotaAfterGroup: billingexpr.QuotaRound((100000*1.5 + 5000*7.5) / 1_000_000 * 500_000),
  486. EstimatedTier: "standard",
  487. QuotaPerUnit: 500_000,
  488. }
  489. params := billingexpr.TokenParams{P: 100000, C: 5000, CR: 50000, CC: 10000}
  490. result, err := billingexpr.ComputeTieredQuota(snap, params)
  491. if err != nil {
  492. t.Fatal(err)
  493. }
  494. wantBefore := (100000*1.5 + 5000*7.5 + 50000*0.15 + 10000*1.875) / 1_000_000 * 500_000
  495. if math.Abs(result.ActualQuotaBeforeGroup-wantBefore) > 1e-6 {
  496. t.Errorf("before group: got %f, want %f", result.ActualQuotaBeforeGroup, wantBefore)
  497. }
  498. if result.MatchedTier != "standard" {
  499. t.Errorf("tier = %q, want %q", result.MatchedTier, "standard")
  500. }
  501. if result.CrossedTier {
  502. t.Error("expected crossed_tier=false (same tier)")
  503. }
  504. }
  505. func TestComputeTieredQuota_WithCacheCrossTier(t *testing.T) {
  506. snap := &billingexpr.BillingSnapshot{
  507. BillingMode: "tiered_expr",
  508. ExprString: claudeWithCacheExpr,
  509. ExprHash: billingexpr.ExprHashString(claudeWithCacheExpr),
  510. GroupRatio: 2.0,
  511. EstimatedPromptTokens: 100000,
  512. EstimatedCompletionTokens: 5000,
  513. EstimatedQuotaBeforeGroup: (100000*1.5 + 5000*7.5) / 1_000_000 * 500_000,
  514. EstimatedQuotaAfterGroup: billingexpr.QuotaRound((100000*1.5 + 5000*7.5) / 1_000_000 * 500_000 * 2.0),
  515. EstimatedTier: "standard",
  516. QuotaPerUnit: 500_000,
  517. }
  518. params := billingexpr.TokenParams{P: 300000, C: 10000, CR: 50000, CC: 10000}
  519. result, err := billingexpr.ComputeTieredQuota(snap, params)
  520. if err != nil {
  521. t.Fatal(err)
  522. }
  523. wantBefore := (300000*3.0 + 10000*11.25 + 50000*0.3 + 10000*3.75) / 1_000_000 * 500_000
  524. wantAfter := billingexpr.QuotaRound(wantBefore * 2.0)
  525. if math.Abs(result.ActualQuotaBeforeGroup-wantBefore) > 1e-6 {
  526. t.Errorf("before group: got %f, want %f", result.ActualQuotaBeforeGroup, wantBefore)
  527. }
  528. if result.ActualQuotaAfterGroup != wantAfter {
  529. t.Errorf("after group: got %d, want %d", result.ActualQuotaAfterGroup, wantAfter)
  530. }
  531. if !result.CrossedTier {
  532. t.Error("expected crossed_tier=true (estimated standard, actual long_context)")
  533. }
  534. }
  535. // ---------------------------------------------------------------------------
  536. // Fuzz: random p/c/cache, verify non-negative result
  537. // ---------------------------------------------------------------------------
  538. func TestFuzz_NonNegativeResults(t *testing.T) {
  539. exprs := []string{
  540. claudeExpr,
  541. claudeWithCacheExpr,
  542. glmExpr,
  543. "p * 0.5 + c * 1.0",
  544. "max(p, c) * 0.1",
  545. "p * 0.5 + cr * 0.1 + cc * 0.2",
  546. }
  547. rng := rand.New(rand.NewSource(42))
  548. for _, exprStr := range exprs {
  549. for i := 0; i < 500; i++ {
  550. params := billingexpr.TokenParams{
  551. P: math.Round(rng.Float64() * 1000000),
  552. C: math.Round(rng.Float64() * 500000),
  553. CR: math.Round(rng.Float64() * 200000),
  554. CC: math.Round(rng.Float64() * 50000),
  555. CC1h: math.Round(rng.Float64() * 10000),
  556. }
  557. cost, _, err := billingexpr.RunExpr(exprStr, params)
  558. if err != nil {
  559. t.Fatalf("expr=%q params=%+v: %v", exprStr, params, err)
  560. }
  561. if cost < 0 {
  562. t.Errorf("expr=%q params=%+v: negative cost %f", exprStr, params, cost)
  563. }
  564. }
  565. }
  566. }
  567. func TestFuzz_SettlementConsistency(t *testing.T) {
  568. rng := rand.New(rand.NewSource(99))
  569. for i := 0; i < 200; i++ {
  570. estParams := billingexpr.TokenParams{
  571. P: math.Round(rng.Float64() * 500000),
  572. C: math.Round(rng.Float64() * 100000),
  573. CR: math.Round(rng.Float64() * 100000),
  574. CC: math.Round(rng.Float64() * 30000),
  575. }
  576. actParams := billingexpr.TokenParams{
  577. P: math.Round(rng.Float64() * 500000),
  578. C: math.Round(rng.Float64() * 100000),
  579. CR: math.Round(rng.Float64() * 100000),
  580. CC: math.Round(rng.Float64() * 30000),
  581. }
  582. groupRatio := 0.5 + rng.Float64()*2.0
  583. estCost, estTrace, _ := billingexpr.RunExpr(claudeWithCacheExpr, estParams)
  584. const qpu = 500_000.0
  585. snap := &billingexpr.BillingSnapshot{
  586. BillingMode: "tiered_expr",
  587. ExprString: claudeWithCacheExpr,
  588. ExprHash: billingexpr.ExprHashString(claudeWithCacheExpr),
  589. GroupRatio: groupRatio,
  590. EstimatedPromptTokens: int(estParams.P),
  591. EstimatedCompletionTokens: int(estParams.C),
  592. EstimatedQuotaBeforeGroup: estCost / 1_000_000 * qpu,
  593. EstimatedQuotaAfterGroup: billingexpr.QuotaRound(estCost / 1_000_000 * qpu * groupRatio),
  594. EstimatedTier: estTrace.MatchedTier,
  595. QuotaPerUnit: qpu,
  596. }
  597. result, err := billingexpr.ComputeTieredQuota(snap, actParams)
  598. if err != nil {
  599. t.Fatalf("iter %d: %v", i, err)
  600. }
  601. directCost, _, _ := billingexpr.RunExpr(claudeWithCacheExpr, actParams)
  602. directQuota := billingexpr.QuotaRound(directCost / 1_000_000 * qpu * groupRatio)
  603. if result.ActualQuotaAfterGroup != directQuota {
  604. t.Errorf("iter %d: settlement %d != direct %d", i, result.ActualQuotaAfterGroup, directQuota)
  605. }
  606. }
  607. }
  608. // ---------------------------------------------------------------------------
  609. // Settlement-level tests for ComputeTieredQuota
  610. // ---------------------------------------------------------------------------
  611. func TestComputeTieredQuota_BasicSettlement(t *testing.T) {
  612. exprStr := `tier("default", p + c)`
  613. snap := &billingexpr.BillingSnapshot{
  614. BillingMode: "tiered_expr",
  615. ExprString: exprStr,
  616. ExprHash: billingexpr.ExprHashString(exprStr),
  617. GroupRatio: 1.0,
  618. QuotaPerUnit: 500_000,
  619. }
  620. result, err := billingexpr.ComputeTieredQuota(snap, billingexpr.TokenParams{P: 3000, C: 2000})
  621. if err != nil {
  622. t.Fatal(err)
  623. }
  624. // exprOutput = 5000; quota = 5000 / 1M * 500K = 2500
  625. if math.Abs(result.ActualQuotaBeforeGroup-2500) > 1e-6 {
  626. t.Errorf("before group = %f, want 2500", result.ActualQuotaBeforeGroup)
  627. }
  628. if result.ActualQuotaAfterGroup != 2500 {
  629. t.Errorf("after group = %d, want 2500", result.ActualQuotaAfterGroup)
  630. }
  631. if result.MatchedTier != "default" {
  632. t.Errorf("tier = %q, want default", result.MatchedTier)
  633. }
  634. }
  635. func TestComputeTieredQuota_WithGroupRatio(t *testing.T) {
  636. exprStr := `tier("default", p + c)`
  637. snap := &billingexpr.BillingSnapshot{
  638. BillingMode: "tiered_expr",
  639. ExprString: exprStr,
  640. ExprHash: billingexpr.ExprHashString(exprStr),
  641. GroupRatio: 2.0,
  642. QuotaPerUnit: 500_000,
  643. }
  644. result, err := billingexpr.ComputeTieredQuota(snap, billingexpr.TokenParams{P: 1000, C: 500})
  645. if err != nil {
  646. t.Fatal(err)
  647. }
  648. // exprOutput = 1500; quotaBeforeGroup = 750; afterGroup = round(750 * 2.0) = 1500
  649. if result.ActualQuotaAfterGroup != 1500 {
  650. t.Errorf("after group = %d, want 1500", result.ActualQuotaAfterGroup)
  651. }
  652. }
  653. func TestComputeTieredQuota_ZeroTokens(t *testing.T) {
  654. exprStr := `tier("default", p * 2 + c * 10)`
  655. snap := &billingexpr.BillingSnapshot{
  656. BillingMode: "tiered_expr",
  657. ExprString: exprStr,
  658. ExprHash: billingexpr.ExprHashString(exprStr),
  659. GroupRatio: 1.0,
  660. QuotaPerUnit: 500_000,
  661. }
  662. result, err := billingexpr.ComputeTieredQuota(snap, billingexpr.TokenParams{})
  663. if err != nil {
  664. t.Fatal(err)
  665. }
  666. if result.ActualQuotaAfterGroup != 0 {
  667. t.Errorf("after group = %d, want 0", result.ActualQuotaAfterGroup)
  668. }
  669. }
  670. func TestComputeTieredQuota_RoundingEdge(t *testing.T) {
  671. exprStr := `tier("default", p * 0.5)` // 3 * 0.5 = 1.5 (expr); 1.5 / 1M * 500K = 0.75; round(0.75) = 1
  672. snap := &billingexpr.BillingSnapshot{
  673. BillingMode: "tiered_expr",
  674. ExprString: exprStr,
  675. ExprHash: billingexpr.ExprHashString(exprStr),
  676. GroupRatio: 1.0,
  677. QuotaPerUnit: 500_000,
  678. }
  679. result, err := billingexpr.ComputeTieredQuota(snap, billingexpr.TokenParams{P: 3})
  680. if err != nil {
  681. t.Fatal(err)
  682. }
  683. // 3 * 0.5 = 1.5 (expr); quota = 1.5 / 1M * 500K = 0.75; round(0.75) = 1
  684. if result.ActualQuotaAfterGroup != 1 {
  685. t.Errorf("after group = %d, want 1 (round 0.75 up)", result.ActualQuotaAfterGroup)
  686. }
  687. }
  688. func TestComputeTieredQuota_RoundingEdgeDown(t *testing.T) {
  689. exprStr := `tier("default", p * 0.4)` // 3 * 0.4 = 1.2 (expr); 1.2 / 1M * 500K = 0.6; round(0.6) = 1
  690. snap := &billingexpr.BillingSnapshot{
  691. BillingMode: "tiered_expr",
  692. ExprString: exprStr,
  693. ExprHash: billingexpr.ExprHashString(exprStr),
  694. GroupRatio: 1.0,
  695. QuotaPerUnit: 500_000,
  696. }
  697. result, err := billingexpr.ComputeTieredQuota(snap, billingexpr.TokenParams{P: 3})
  698. if err != nil {
  699. t.Fatal(err)
  700. }
  701. // 3 * 0.4 = 1.2 (expr); quota = 1.2 / 1M * 500K = 0.6; round(0.6) = 1
  702. if result.ActualQuotaAfterGroup != 1 {
  703. t.Errorf("after group = %d, want 1 (round 0.6 up)", result.ActualQuotaAfterGroup)
  704. }
  705. }
  706. func TestComputeTieredQuotaWithRequest_ProbeAffectsQuota(t *testing.T) {
  707. exprStr := `param("fast") == true ? tier("fast", p * 4) : tier("normal", p * 2)`
  708. snap := &billingexpr.BillingSnapshot{
  709. BillingMode: "tiered_expr",
  710. ExprString: exprStr,
  711. ExprHash: billingexpr.ExprHashString(exprStr),
  712. GroupRatio: 1.0,
  713. EstimatedTier: "normal",
  714. QuotaPerUnit: 500_000,
  715. }
  716. // Without request: normal tier
  717. r1, err := billingexpr.ComputeTieredQuota(snap, billingexpr.TokenParams{P: 1000})
  718. if err != nil {
  719. t.Fatal(err)
  720. }
  721. // normal: p*2 = 2000; quota = 2000 / 1M * 500K = 1000
  722. if r1.ActualQuotaAfterGroup != 1000 {
  723. t.Errorf("normal = %d, want 1000", r1.ActualQuotaAfterGroup)
  724. }
  725. // With request: fast tier
  726. r2, err := billingexpr.ComputeTieredQuotaWithRequest(snap, billingexpr.TokenParams{P: 1000}, billingexpr.RequestInput{
  727. Body: []byte(`{"fast":true}`),
  728. })
  729. if err != nil {
  730. t.Fatal(err)
  731. }
  732. // fast: p*4 = 4000; quota = 4000 / 1M * 500K = 2000
  733. if r2.ActualQuotaAfterGroup != 2000 {
  734. t.Errorf("fast = %d, want 2000", r2.ActualQuotaAfterGroup)
  735. }
  736. if !r2.CrossedTier {
  737. t.Error("expected CrossedTier = true when probe changes tier")
  738. }
  739. }
  740. func TestComputeTieredQuota_BoundaryTierCrossing(t *testing.T) {
  741. exprStr := `p <= 100000 ? tier("small", p * 1) : tier("large", p * 2)`
  742. snap := &billingexpr.BillingSnapshot{
  743. BillingMode: "tiered_expr",
  744. ExprString: exprStr,
  745. ExprHash: billingexpr.ExprHashString(exprStr),
  746. GroupRatio: 1.0,
  747. EstimatedTier: "small",
  748. QuotaPerUnit: 500_000,
  749. }
  750. // At boundary: small, p*1 = 100000; quota = 100000 / 1M * 500K = 50000
  751. r1, err := billingexpr.ComputeTieredQuota(snap, billingexpr.TokenParams{P: 100000})
  752. if err != nil {
  753. t.Fatal(err)
  754. }
  755. if r1.MatchedTier != "small" {
  756. t.Errorf("at boundary: tier = %s, want small", r1.MatchedTier)
  757. }
  758. if r1.ActualQuotaAfterGroup != 50000 {
  759. t.Errorf("at boundary: quota = %d, want 50000", r1.ActualQuotaAfterGroup)
  760. }
  761. // Past boundary: large, p*2 = 200002; quota = 200002 / 1M * 500K = 100001
  762. r2, err := billingexpr.ComputeTieredQuota(snap, billingexpr.TokenParams{P: 100001})
  763. if err != nil {
  764. t.Fatal(err)
  765. }
  766. if r2.MatchedTier != "large" {
  767. t.Errorf("past boundary: tier = %s, want large", r2.MatchedTier)
  768. }
  769. if r2.ActualQuotaAfterGroup != 100001 {
  770. t.Errorf("past boundary: quota = %d, want 100001", r2.ActualQuotaAfterGroup)
  771. }
  772. if !r2.CrossedTier {
  773. t.Error("expected CrossedTier = true")
  774. }
  775. }
  776. // ---------------------------------------------------------------------------
  777. // Time function tests
  778. // ---------------------------------------------------------------------------
  779. func TestTimeFunctions_ValidTimezone(t *testing.T) {
  780. exprStr := `tier("default", p) * (hour("UTC") >= 0 ? 1 : 1)`
  781. cost, _, err := billingexpr.RunExpr(exprStr, billingexpr.TokenParams{P: 100})
  782. if err != nil {
  783. t.Fatal(err)
  784. }
  785. if cost != 100 {
  786. t.Errorf("cost = %f, want 100", cost)
  787. }
  788. }
  789. func TestTimeFunctions_AllFunctionsCompile(t *testing.T) {
  790. exprStr := `tier("default", p) * (hour("Asia/Shanghai") >= 0 ? 1 : 1) * (minute("UTC") >= 0 ? 1 : 1) * (weekday("UTC") >= 0 ? 1 : 1) * (month("UTC") >= 1 ? 1 : 1) * (day("UTC") >= 1 ? 1 : 1)`
  791. cost, _, err := billingexpr.RunExpr(exprStr, billingexpr.TokenParams{P: 500})
  792. if err != nil {
  793. t.Fatal(err)
  794. }
  795. if cost != 500 {
  796. t.Errorf("cost = %f, want 500", cost)
  797. }
  798. }
  799. func TestTimeFunctions_InvalidTimezone(t *testing.T) {
  800. exprStr := `tier("default", p) * (hour("Invalid/Zone") >= 0 ? 1 : 2)`
  801. cost, _, err := billingexpr.RunExpr(exprStr, billingexpr.TokenParams{P: 100})
  802. if err != nil {
  803. t.Fatal(err)
  804. }
  805. // Invalid timezone falls back to UTC; hour is 0-23, so condition is always true
  806. if cost != 100 {
  807. t.Errorf("cost = %f, want 100 (fallback to UTC)", cost)
  808. }
  809. }
  810. func TestTimeFunctions_EmptyTimezone(t *testing.T) {
  811. exprStr := `tier("default", p) * (hour("") >= 0 ? 1 : 2)`
  812. cost, _, err := billingexpr.RunExpr(exprStr, billingexpr.TokenParams{P: 100})
  813. if err != nil {
  814. t.Fatal(err)
  815. }
  816. if cost != 100 {
  817. t.Errorf("cost = %f, want 100 (empty tz -> UTC)", cost)
  818. }
  819. }
  820. func TestTimeFunctions_NightDiscountPattern(t *testing.T) {
  821. exprStr := `tier("default", p * 2 + c * 10) * (hour("UTC") >= 21 || hour("UTC") < 6 ? 0.5 : 1)`
  822. cost, _, err := billingexpr.RunExpr(exprStr, billingexpr.TokenParams{P: 1000, C: 500})
  823. if err != nil {
  824. t.Fatal(err)
  825. }
  826. // Base = 1000*2 + 500*10 = 7000; multiplier is either 0.5 or 1 depending on current UTC hour
  827. if cost != 7000 && cost != 3500 {
  828. t.Errorf("cost = %f, want 7000 or 3500", cost)
  829. }
  830. }
  831. func TestTimeFunctions_WeekdayRange(t *testing.T) {
  832. exprStr := `tier("default", p) * (weekday("UTC") >= 0 && weekday("UTC") <= 6 ? 1 : 999)`
  833. cost, _, err := billingexpr.RunExpr(exprStr, billingexpr.TokenParams{P: 100})
  834. if err != nil {
  835. t.Fatal(err)
  836. }
  837. // weekday is always 0-6, so multiplier is always 1
  838. if cost != 100 {
  839. t.Errorf("cost = %f, want 100", cost)
  840. }
  841. }
  842. func TestTimeFunctions_MonthDayPattern(t *testing.T) {
  843. exprStr := `tier("default", p) * (month("Asia/Shanghai") == 1 && day("Asia/Shanghai") == 1 ? 0.5 : 1)`
  844. cost, _, err := billingexpr.RunExpr(exprStr, billingexpr.TokenParams{P: 1000})
  845. if err != nil {
  846. t.Fatal(err)
  847. }
  848. // Either 1000 (not Jan 1) or 500 (Jan 1) — both are valid
  849. if cost != 1000 && cost != 500 {
  850. t.Errorf("cost = %f, want 1000 or 500", cost)
  851. }
  852. }
  853. // ---------------------------------------------------------------------------
  854. // Image and audio token tests
  855. // ---------------------------------------------------------------------------
  856. func TestImageTokenVariable(t *testing.T) {
  857. exprStr := `tier("base", p * 2 + c * 10 + img * 5)`
  858. cost, _, err := billingexpr.RunExpr(exprStr, billingexpr.TokenParams{P: 1000, C: 500, Img: 200})
  859. if err != nil {
  860. t.Fatal(err)
  861. }
  862. // 1000*2 + 500*10 + 200*5 = 2000 + 5000 + 1000 = 8000
  863. if math.Abs(cost-8000) > 1e-6 {
  864. t.Errorf("cost = %f, want 8000", cost)
  865. }
  866. }
  867. func TestAudioTokenVariables(t *testing.T) {
  868. exprStr := `tier("base", p * 2 + c * 10 + ai * 50 + ao * 100)`
  869. cost, _, err := billingexpr.RunExpr(exprStr, billingexpr.TokenParams{P: 1000, C: 500, AI: 100, AO: 50})
  870. if err != nil {
  871. t.Fatal(err)
  872. }
  873. // 1000*2 + 500*10 + 100*50 + 50*100 = 2000 + 5000 + 5000 + 5000 = 17000
  874. if math.Abs(cost-17000) > 1e-6 {
  875. t.Errorf("cost = %f, want 17000", cost)
  876. }
  877. }
  878. func TestImageAudioVariables(t *testing.T) {
  879. exprStr := `tier("base", p * 1 + img * 3 + ai * 5 + ao * 10)`
  880. cost, _, err := billingexpr.RunExpr(exprStr, billingexpr.TokenParams{P: 100, Img: 50, AI: 20, AO: 10})
  881. if err != nil {
  882. t.Fatal(err)
  883. }
  884. // 100*1 + 50*3 + 20*5 + 10*10 = 100 + 150 + 100 + 100 = 450
  885. if math.Abs(cost-450) > 1e-6 {
  886. t.Errorf("cost = %f, want 450", cost)
  887. }
  888. }
  889. func TestImageAudioZero(t *testing.T) {
  890. exprStr := `tier("base", p * 2 + img * 5 + ai * 50 + ao * 100)`
  891. cost, _, err := billingexpr.RunExpr(exprStr, billingexpr.TokenParams{P: 1000})
  892. if err != nil {
  893. t.Fatal(err)
  894. }
  895. // img, ai, ao default to 0
  896. if math.Abs(cost-2000) > 1e-6 {
  897. t.Errorf("cost = %f, want 2000", cost)
  898. }
  899. }
  900. // ---------------------------------------------------------------------------
  901. // Benchmarks: compile vs cached execution
  902. // ---------------------------------------------------------------------------
  903. const benchComplexExpr = `p <= 200000 ? tier("standard", p * 3 + c * 15 + cr * 0.3 + cc * 3.75 + cc1h * 6 + img * 3 + img_o * 30 + ai * 10 + ao * 40) : tier("long_context", p * 6 + c * 22.5 + cr * 0.6 + cc * 7.5 + cc1h * 12 + img * 6 + img_o * 60 + ai * 20 + ao * 80)`
  904. func BenchmarkExprCompile(b *testing.B) {
  905. for i := 0; i < b.N; i++ {
  906. billingexpr.InvalidateCache()
  907. billingexpr.CompileFromCache(benchComplexExpr)
  908. }
  909. }
  910. func BenchmarkExprRunCached(b *testing.B) {
  911. billingexpr.CompileFromCache(benchComplexExpr)
  912. params := billingexpr.TokenParams{P: 150000, C: 10000, CR: 30000, CC: 5000, Img: 2000, AI: 1000, AO: 500}
  913. b.ResetTimer()
  914. for i := 0; i < b.N; i++ {
  915. billingexpr.RunExpr(benchComplexExpr, params)
  916. }
  917. }