billingexpr_test.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  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. `prompt_tokens * 0.5 + completion_tokens * 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,
  288. EstimatedQuotaAfterGroup: billingexpr.QuotaRound(100000*1.5 + 5000*7.5),
  289. EstimatedTier: "standard",
  290. }
  291. result, err := billingexpr.ComputeTieredQuota(snap, billingexpr.TokenParams{P: 300000, C: 10000})
  292. if err != nil {
  293. t.Fatal(err)
  294. }
  295. wantBefore := 300000*3.0 + 10000*11.25
  296. if math.Abs(result.ActualQuotaBeforeGroup-wantBefore) > 1e-6 {
  297. t.Errorf("before group: got %f, want %f", result.ActualQuotaBeforeGroup, wantBefore)
  298. }
  299. if result.MatchedTier != "long_context" {
  300. t.Errorf("tier = %q, want %q", result.MatchedTier, "long_context")
  301. }
  302. if !result.CrossedTier {
  303. t.Error("expected crossed_tier=true (estimated standard, actual long_context)")
  304. }
  305. }
  306. func TestComputeTieredQuota_SameTier(t *testing.T) {
  307. snap := &billingexpr.BillingSnapshot{
  308. BillingMode: "tiered_expr",
  309. ExprString: claudeExpr,
  310. ExprHash: billingexpr.ExprHashString(claudeExpr),
  311. GroupRatio: 1.5,
  312. EstimatedPromptTokens: 50000,
  313. EstimatedCompletionTokens: 1000,
  314. EstimatedQuotaBeforeGroup: 50000*1.5 + 1000*7.5,
  315. EstimatedQuotaAfterGroup: billingexpr.QuotaRound((50000*1.5 + 1000*7.5) * 1.5),
  316. EstimatedTier: "standard",
  317. }
  318. result, err := billingexpr.ComputeTieredQuota(snap, billingexpr.TokenParams{P: 80000, C: 2000})
  319. if err != nil {
  320. t.Fatal(err)
  321. }
  322. wantBefore := 80000*1.5 + 2000*7.5
  323. wantAfter := billingexpr.QuotaRound(wantBefore * 1.5)
  324. if result.ActualQuotaAfterGroup != wantAfter {
  325. t.Errorf("after group: got %d, want %d", result.ActualQuotaAfterGroup, wantAfter)
  326. }
  327. if result.CrossedTier {
  328. t.Error("expected crossed_tier=false (both standard)")
  329. }
  330. }
  331. // ---------------------------------------------------------------------------
  332. // Compile errors
  333. // ---------------------------------------------------------------------------
  334. func TestCompileError(t *testing.T) {
  335. _, _, err := billingexpr.RunExpr("invalid +-+ syntax", billingexpr.TokenParams{})
  336. if err == nil {
  337. t.Error("expected compile error")
  338. }
  339. }
  340. // ---------------------------------------------------------------------------
  341. // Compile Cache
  342. // ---------------------------------------------------------------------------
  343. func TestCompileCache_SameResult(t *testing.T) {
  344. r1, _, err := billingexpr.RunExpr("p * 0.5", billingexpr.TokenParams{P: 100})
  345. if err != nil {
  346. t.Fatal(err)
  347. }
  348. r2, _, err := billingexpr.RunExpr("p * 0.5", billingexpr.TokenParams{P: 100})
  349. if err != nil {
  350. t.Fatal(err)
  351. }
  352. if r1 != r2 {
  353. t.Errorf("cached and uncached results differ: %f != %f", r1, r2)
  354. }
  355. }
  356. func TestInvalidateCache(t *testing.T) {
  357. billingexpr.InvalidateCache()
  358. r1, _, _ := billingexpr.RunExpr("p * 0.5", billingexpr.TokenParams{P: 100})
  359. billingexpr.InvalidateCache()
  360. r2, _, _ := billingexpr.RunExpr("p * 0.5", billingexpr.TokenParams{P: 100})
  361. if r1 != r2 {
  362. t.Errorf("post-invalidate results differ: %f != %f", r1, r2)
  363. }
  364. }
  365. // ---------------------------------------------------------------------------
  366. // Hash
  367. // ---------------------------------------------------------------------------
  368. func TestExprHashString_Deterministic(t *testing.T) {
  369. h1 := billingexpr.ExprHashString("p * 0.5")
  370. h2 := billingexpr.ExprHashString("p * 0.5")
  371. if h1 != h2 {
  372. t.Error("hash should be deterministic")
  373. }
  374. h3 := billingexpr.ExprHashString("p * 0.6")
  375. if h1 == h3 {
  376. t.Error("different expressions should have different hashes")
  377. }
  378. }
  379. // ---------------------------------------------------------------------------
  380. // Cache variables: present
  381. // ---------------------------------------------------------------------------
  382. 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)`
  383. func TestCachePresent_StandardTier(t *testing.T) {
  384. params := billingexpr.TokenParams{P: 100000, C: 5000, CR: 50000, CC: 10000}
  385. cost, trace, err := billingexpr.RunExpr(claudeWithCacheExpr, params)
  386. if err != nil {
  387. t.Fatal(err)
  388. }
  389. want := 100000*1.5 + 5000*7.5 + 50000*0.15 + 10000*1.875
  390. if math.Abs(cost-want) > 1e-6 {
  391. t.Errorf("cost = %f, want %f", cost, want)
  392. }
  393. if trace.MatchedTier != "standard" {
  394. t.Errorf("tier = %q, want %q", trace.MatchedTier, "standard")
  395. }
  396. }
  397. func TestCachePresent_LongContextTier(t *testing.T) {
  398. params := billingexpr.TokenParams{P: 300000, C: 10000, CR: 100000, CC: 20000}
  399. cost, trace, err := billingexpr.RunExpr(claudeWithCacheExpr, params)
  400. if err != nil {
  401. t.Fatal(err)
  402. }
  403. want := 300000*3.0 + 10000*11.25 + 100000*0.3 + 20000*3.75
  404. if math.Abs(cost-want) > 1e-6 {
  405. t.Errorf("cost = %f, want %f", cost, want)
  406. }
  407. if trace.MatchedTier != "long_context" {
  408. t.Errorf("tier = %q, want %q", trace.MatchedTier, "long_context")
  409. }
  410. }
  411. // ---------------------------------------------------------------------------
  412. // Cache variables: absent (all zero) — same expression still works
  413. // ---------------------------------------------------------------------------
  414. func TestCacheAbsent_ZeroCacheTokens(t *testing.T) {
  415. params := billingexpr.TokenParams{P: 100000, C: 5000}
  416. cost, trace, err := billingexpr.RunExpr(claudeWithCacheExpr, params)
  417. if err != nil {
  418. t.Fatal(err)
  419. }
  420. want := 100000*1.5 + 5000*7.5
  421. if math.Abs(cost-want) > 1e-6 {
  422. t.Errorf("cost = %f, want %f (cache terms should be 0)", cost, want)
  423. }
  424. if trace.MatchedTier != "standard" {
  425. t.Errorf("tier = %q, want %q", trace.MatchedTier, "standard")
  426. }
  427. }
  428. // ---------------------------------------------------------------------------
  429. // Mixed cache fields: cc and cc1h non-zero
  430. // ---------------------------------------------------------------------------
  431. const claudeCacheSplitExpr = `tier("default", p * 1.5 + c * 7.5 + cr * 0.15 + cc * 2.0 + cc1h * 3.0)`
  432. func TestMixedCacheFields(t *testing.T) {
  433. params := billingexpr.TokenParams{P: 100000, C: 5000, CR: 10000, CC: 5000, CC1h: 2000}
  434. cost, _, err := billingexpr.RunExpr(claudeCacheSplitExpr, params)
  435. if err != nil {
  436. t.Fatal(err)
  437. }
  438. want := 100000*1.5 + 5000*7.5 + 10000*0.15 + 5000*2.0 + 2000*3.0
  439. if math.Abs(cost-want) > 1e-6 {
  440. t.Errorf("cost = %f, want %f", cost, want)
  441. }
  442. }
  443. func TestMixedCacheFields_AllCacheZero(t *testing.T) {
  444. params := billingexpr.TokenParams{P: 100000, C: 5000}
  445. cost, _, err := billingexpr.RunExpr(claudeCacheSplitExpr, params)
  446. if err != nil {
  447. t.Fatal(err)
  448. }
  449. want := 100000*1.5 + 5000*7.5
  450. if math.Abs(cost-want) > 1e-6 {
  451. t.Errorf("cost = %f, want %f (all cache zero)", cost, want)
  452. }
  453. }
  454. // ---------------------------------------------------------------------------
  455. // Backward compatibility: p+c only expressions still work with TokenParams
  456. // ---------------------------------------------------------------------------
  457. func TestBackwardCompat_OldExprWithTokenParams(t *testing.T) {
  458. params := billingexpr.TokenParams{P: 100000, C: 5000, CR: 99999, CC: 88888}
  459. cost, trace, err := billingexpr.RunExpr(claudeExpr, params)
  460. if err != nil {
  461. t.Fatal(err)
  462. }
  463. want := 100000*1.5 + 5000*7.5
  464. if math.Abs(cost-want) > 1e-6 {
  465. t.Errorf("cost = %f, want %f (old expr ignores cache fields)", cost, want)
  466. }
  467. if trace.MatchedTier != "standard" {
  468. t.Errorf("tier = %q, want %q", trace.MatchedTier, "standard")
  469. }
  470. }
  471. // ---------------------------------------------------------------------------
  472. // Settlement with cache tokens
  473. // ---------------------------------------------------------------------------
  474. func TestComputeTieredQuota_WithCache(t *testing.T) {
  475. snap := &billingexpr.BillingSnapshot{
  476. BillingMode: "tiered_expr",
  477. ExprString: claudeWithCacheExpr,
  478. ExprHash: billingexpr.ExprHashString(claudeWithCacheExpr),
  479. GroupRatio: 1.0,
  480. EstimatedPromptTokens: 100000,
  481. EstimatedCompletionTokens: 5000,
  482. EstimatedQuotaBeforeGroup: 100000*1.5 + 5000*7.5,
  483. EstimatedQuotaAfterGroup: billingexpr.QuotaRound(100000*1.5 + 5000*7.5),
  484. EstimatedTier: "standard",
  485. }
  486. params := billingexpr.TokenParams{P: 100000, C: 5000, CR: 50000, CC: 10000}
  487. result, err := billingexpr.ComputeTieredQuota(snap, params)
  488. if err != nil {
  489. t.Fatal(err)
  490. }
  491. wantBefore := 100000*1.5 + 5000*7.5 + 50000*0.15 + 10000*1.875
  492. if math.Abs(result.ActualQuotaBeforeGroup-wantBefore) > 1e-6 {
  493. t.Errorf("before group: got %f, want %f", result.ActualQuotaBeforeGroup, wantBefore)
  494. }
  495. if result.MatchedTier != "standard" {
  496. t.Errorf("tier = %q, want %q", result.MatchedTier, "standard")
  497. }
  498. if result.CrossedTier {
  499. t.Error("expected crossed_tier=false (same tier)")
  500. }
  501. }
  502. func TestComputeTieredQuota_WithCacheCrossTier(t *testing.T) {
  503. snap := &billingexpr.BillingSnapshot{
  504. BillingMode: "tiered_expr",
  505. ExprString: claudeWithCacheExpr,
  506. ExprHash: billingexpr.ExprHashString(claudeWithCacheExpr),
  507. GroupRatio: 2.0,
  508. EstimatedPromptTokens: 100000,
  509. EstimatedCompletionTokens: 5000,
  510. EstimatedQuotaBeforeGroup: 100000*1.5 + 5000*7.5,
  511. EstimatedQuotaAfterGroup: billingexpr.QuotaRound((100000*1.5 + 5000*7.5) * 2.0),
  512. EstimatedTier: "standard",
  513. }
  514. params := billingexpr.TokenParams{P: 300000, C: 10000, CR: 50000, CC: 10000}
  515. result, err := billingexpr.ComputeTieredQuota(snap, params)
  516. if err != nil {
  517. t.Fatal(err)
  518. }
  519. wantBefore := 300000*3.0 + 10000*11.25 + 50000*0.3 + 10000*3.75
  520. wantAfter := billingexpr.QuotaRound(wantBefore * 2.0)
  521. if math.Abs(result.ActualQuotaBeforeGroup-wantBefore) > 1e-6 {
  522. t.Errorf("before group: got %f, want %f", result.ActualQuotaBeforeGroup, wantBefore)
  523. }
  524. if result.ActualQuotaAfterGroup != wantAfter {
  525. t.Errorf("after group: got %d, want %d", result.ActualQuotaAfterGroup, wantAfter)
  526. }
  527. if !result.CrossedTier {
  528. t.Error("expected crossed_tier=true (estimated standard, actual long_context)")
  529. }
  530. }
  531. // ---------------------------------------------------------------------------
  532. // Fuzz: random p/c/cache, verify non-negative result
  533. // ---------------------------------------------------------------------------
  534. func TestFuzz_NonNegativeResults(t *testing.T) {
  535. exprs := []string{
  536. claudeExpr,
  537. claudeWithCacheExpr,
  538. glmExpr,
  539. "p * 0.5 + c * 1.0",
  540. "max(p, c) * 0.1",
  541. "p * 0.5 + cr * 0.1 + cc * 0.2",
  542. }
  543. rng := rand.New(rand.NewSource(42))
  544. for _, exprStr := range exprs {
  545. for i := 0; i < 500; i++ {
  546. params := billingexpr.TokenParams{
  547. P: math.Round(rng.Float64() * 1000000),
  548. C: math.Round(rng.Float64() * 500000),
  549. CR: math.Round(rng.Float64() * 200000),
  550. CC: math.Round(rng.Float64() * 50000),
  551. CC1h: math.Round(rng.Float64() * 10000),
  552. }
  553. cost, _, err := billingexpr.RunExpr(exprStr, params)
  554. if err != nil {
  555. t.Fatalf("expr=%q params=%+v: %v", exprStr, params, err)
  556. }
  557. if cost < 0 {
  558. t.Errorf("expr=%q params=%+v: negative cost %f", exprStr, params, cost)
  559. }
  560. }
  561. }
  562. }
  563. func TestFuzz_SettlementConsistency(t *testing.T) {
  564. rng := rand.New(rand.NewSource(99))
  565. for i := 0; i < 200; i++ {
  566. estParams := billingexpr.TokenParams{
  567. P: math.Round(rng.Float64() * 500000),
  568. C: math.Round(rng.Float64() * 100000),
  569. CR: math.Round(rng.Float64() * 100000),
  570. CC: math.Round(rng.Float64() * 30000),
  571. }
  572. actParams := billingexpr.TokenParams{
  573. P: math.Round(rng.Float64() * 500000),
  574. C: math.Round(rng.Float64() * 100000),
  575. CR: math.Round(rng.Float64() * 100000),
  576. CC: math.Round(rng.Float64() * 30000),
  577. }
  578. groupRatio := 0.5 + rng.Float64()*2.0
  579. estCost, estTrace, _ := billingexpr.RunExpr(claudeWithCacheExpr, estParams)
  580. snap := &billingexpr.BillingSnapshot{
  581. BillingMode: "tiered_expr",
  582. ExprString: claudeWithCacheExpr,
  583. ExprHash: billingexpr.ExprHashString(claudeWithCacheExpr),
  584. GroupRatio: groupRatio,
  585. EstimatedPromptTokens: int(estParams.P),
  586. EstimatedCompletionTokens: int(estParams.C),
  587. EstimatedQuotaBeforeGroup: estCost,
  588. EstimatedQuotaAfterGroup: billingexpr.QuotaRound(estCost * groupRatio),
  589. EstimatedTier: estTrace.MatchedTier,
  590. }
  591. result, err := billingexpr.ComputeTieredQuota(snap, actParams)
  592. if err != nil {
  593. t.Fatalf("iter %d: %v", i, err)
  594. }
  595. directCost, _, _ := billingexpr.RunExpr(claudeWithCacheExpr, actParams)
  596. directQuota := billingexpr.QuotaRound(directCost * groupRatio)
  597. if result.ActualQuotaAfterGroup != directQuota {
  598. t.Errorf("iter %d: settlement %d != direct %d", i, result.ActualQuotaAfterGroup, directQuota)
  599. }
  600. }
  601. }
  602. // ---------------------------------------------------------------------------
  603. // Settlement-level tests for ComputeTieredQuota
  604. // ---------------------------------------------------------------------------
  605. func TestComputeTieredQuota_BasicSettlement(t *testing.T) {
  606. exprStr := `tier("default", p + c)`
  607. snap := &billingexpr.BillingSnapshot{
  608. BillingMode: "tiered_expr",
  609. ExprString: exprStr,
  610. ExprHash: billingexpr.ExprHashString(exprStr),
  611. GroupRatio: 1.0,
  612. }
  613. result, err := billingexpr.ComputeTieredQuota(snap, billingexpr.TokenParams{P: 3000, C: 2000})
  614. if err != nil {
  615. t.Fatal(err)
  616. }
  617. if math.Abs(result.ActualQuotaBeforeGroup-5000) > 1e-6 {
  618. t.Errorf("before group = %f, want 5000", result.ActualQuotaBeforeGroup)
  619. }
  620. if result.ActualQuotaAfterGroup != 5000 {
  621. t.Errorf("after group = %d, want 5000", result.ActualQuotaAfterGroup)
  622. }
  623. if result.MatchedTier != "default" {
  624. t.Errorf("tier = %q, want default", result.MatchedTier)
  625. }
  626. }
  627. func TestComputeTieredQuota_WithGroupRatio(t *testing.T) {
  628. exprStr := `tier("default", p + c)`
  629. snap := &billingexpr.BillingSnapshot{
  630. BillingMode: "tiered_expr",
  631. ExprString: exprStr,
  632. ExprHash: billingexpr.ExprHashString(exprStr),
  633. GroupRatio: 2.0,
  634. }
  635. result, err := billingexpr.ComputeTieredQuota(snap, billingexpr.TokenParams{P: 1000, C: 500})
  636. if err != nil {
  637. t.Fatal(err)
  638. }
  639. // cost = 1500, after group = round(1500 * 2.0) = 3000
  640. if result.ActualQuotaAfterGroup != 3000 {
  641. t.Errorf("after group = %d, want 3000", result.ActualQuotaAfterGroup)
  642. }
  643. }
  644. func TestComputeTieredQuota_ZeroTokens(t *testing.T) {
  645. exprStr := `tier("default", p * 2 + c * 10)`
  646. snap := &billingexpr.BillingSnapshot{
  647. BillingMode: "tiered_expr",
  648. ExprString: exprStr,
  649. ExprHash: billingexpr.ExprHashString(exprStr),
  650. GroupRatio: 1.0,
  651. }
  652. result, err := billingexpr.ComputeTieredQuota(snap, billingexpr.TokenParams{})
  653. if err != nil {
  654. t.Fatal(err)
  655. }
  656. if result.ActualQuotaAfterGroup != 0 {
  657. t.Errorf("after group = %d, want 0", result.ActualQuotaAfterGroup)
  658. }
  659. }
  660. func TestComputeTieredQuota_RoundingEdge(t *testing.T) {
  661. exprStr := `tier("default", p * 0.5)` // 3 * 0.5 = 1.5 -> round to 2
  662. snap := &billingexpr.BillingSnapshot{
  663. BillingMode: "tiered_expr",
  664. ExprString: exprStr,
  665. ExprHash: billingexpr.ExprHashString(exprStr),
  666. GroupRatio: 1.0,
  667. }
  668. result, err := billingexpr.ComputeTieredQuota(snap, billingexpr.TokenParams{P: 3})
  669. if err != nil {
  670. t.Fatal(err)
  671. }
  672. // 3 * 0.5 = 1.5, round(1.5) = 2
  673. if result.ActualQuotaAfterGroup != 2 {
  674. t.Errorf("after group = %d, want 2 (round 1.5 up)", result.ActualQuotaAfterGroup)
  675. }
  676. }
  677. func TestComputeTieredQuota_RoundingEdgeDown(t *testing.T) {
  678. exprStr := `tier("default", p * 0.4)` // 3 * 0.4 = 1.2 -> round to 1
  679. snap := &billingexpr.BillingSnapshot{
  680. BillingMode: "tiered_expr",
  681. ExprString: exprStr,
  682. ExprHash: billingexpr.ExprHashString(exprStr),
  683. GroupRatio: 1.0,
  684. }
  685. result, err := billingexpr.ComputeTieredQuota(snap, billingexpr.TokenParams{P: 3})
  686. if err != nil {
  687. t.Fatal(err)
  688. }
  689. // 3 * 0.4 = 1.2, round(1.2) = 1
  690. if result.ActualQuotaAfterGroup != 1 {
  691. t.Errorf("after group = %d, want 1 (round 1.2 down)", result.ActualQuotaAfterGroup)
  692. }
  693. }
  694. func TestComputeTieredQuotaWithRequest_ProbeAffectsQuota(t *testing.T) {
  695. exprStr := `param("fast") == true ? tier("fast", p * 4) : tier("normal", p * 2)`
  696. snap := &billingexpr.BillingSnapshot{
  697. BillingMode: "tiered_expr",
  698. ExprString: exprStr,
  699. ExprHash: billingexpr.ExprHashString(exprStr),
  700. GroupRatio: 1.0,
  701. EstimatedTier: "normal",
  702. }
  703. // Without request: normal tier
  704. r1, err := billingexpr.ComputeTieredQuota(snap, billingexpr.TokenParams{P: 1000})
  705. if err != nil {
  706. t.Fatal(err)
  707. }
  708. if r1.ActualQuotaAfterGroup != 2000 {
  709. t.Errorf("normal = %d, want 2000", r1.ActualQuotaAfterGroup)
  710. }
  711. // With request: fast tier
  712. r2, err := billingexpr.ComputeTieredQuotaWithRequest(snap, billingexpr.TokenParams{P: 1000}, billingexpr.RequestInput{
  713. Body: []byte(`{"fast":true}`),
  714. })
  715. if err != nil {
  716. t.Fatal(err)
  717. }
  718. if r2.ActualQuotaAfterGroup != 4000 {
  719. t.Errorf("fast = %d, want 4000", r2.ActualQuotaAfterGroup)
  720. }
  721. if !r2.CrossedTier {
  722. t.Error("expected CrossedTier = true when probe changes tier")
  723. }
  724. }
  725. func TestComputeTieredQuota_BoundaryTierCrossing(t *testing.T) {
  726. exprStr := `p <= 100000 ? tier("small", p * 1) : tier("large", p * 2)`
  727. snap := &billingexpr.BillingSnapshot{
  728. BillingMode: "tiered_expr",
  729. ExprString: exprStr,
  730. ExprHash: billingexpr.ExprHashString(exprStr),
  731. GroupRatio: 1.0,
  732. EstimatedTier: "small",
  733. }
  734. // At boundary
  735. r1, err := billingexpr.ComputeTieredQuota(snap, billingexpr.TokenParams{P: 100000})
  736. if err != nil {
  737. t.Fatal(err)
  738. }
  739. if r1.MatchedTier != "small" {
  740. t.Errorf("at boundary: tier = %s, want small", r1.MatchedTier)
  741. }
  742. if r1.ActualQuotaAfterGroup != 100000 {
  743. t.Errorf("at boundary: quota = %d, want 100000", r1.ActualQuotaAfterGroup)
  744. }
  745. // Past boundary
  746. r2, err := billingexpr.ComputeTieredQuota(snap, billingexpr.TokenParams{P: 100001})
  747. if err != nil {
  748. t.Fatal(err)
  749. }
  750. if r2.MatchedTier != "large" {
  751. t.Errorf("past boundary: tier = %s, want large", r2.MatchedTier)
  752. }
  753. if r2.ActualQuotaAfterGroup != 200002 {
  754. t.Errorf("past boundary: quota = %d, want 200002", r2.ActualQuotaAfterGroup)
  755. }
  756. if !r2.CrossedTier {
  757. t.Error("expected CrossedTier = true")
  758. }
  759. }
  760. // ---------------------------------------------------------------------------
  761. // Time function tests
  762. // ---------------------------------------------------------------------------
  763. func TestTimeFunctions_ValidTimezone(t *testing.T) {
  764. exprStr := `tier("default", p) * (hour("UTC") >= 0 ? 1 : 1)`
  765. cost, _, err := billingexpr.RunExpr(exprStr, billingexpr.TokenParams{P: 100})
  766. if err != nil {
  767. t.Fatal(err)
  768. }
  769. if cost != 100 {
  770. t.Errorf("cost = %f, want 100", cost)
  771. }
  772. }
  773. func TestTimeFunctions_AllFunctionsCompile(t *testing.T) {
  774. 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)`
  775. cost, _, err := billingexpr.RunExpr(exprStr, billingexpr.TokenParams{P: 500})
  776. if err != nil {
  777. t.Fatal(err)
  778. }
  779. if cost != 500 {
  780. t.Errorf("cost = %f, want 500", cost)
  781. }
  782. }
  783. func TestTimeFunctions_InvalidTimezone(t *testing.T) {
  784. exprStr := `tier("default", p) * (hour("Invalid/Zone") >= 0 ? 1 : 2)`
  785. cost, _, err := billingexpr.RunExpr(exprStr, billingexpr.TokenParams{P: 100})
  786. if err != nil {
  787. t.Fatal(err)
  788. }
  789. // Invalid timezone falls back to UTC; hour is 0-23, so condition is always true
  790. if cost != 100 {
  791. t.Errorf("cost = %f, want 100 (fallback to UTC)", cost)
  792. }
  793. }
  794. func TestTimeFunctions_EmptyTimezone(t *testing.T) {
  795. exprStr := `tier("default", p) * (hour("") >= 0 ? 1 : 2)`
  796. cost, _, err := billingexpr.RunExpr(exprStr, billingexpr.TokenParams{P: 100})
  797. if err != nil {
  798. t.Fatal(err)
  799. }
  800. if cost != 100 {
  801. t.Errorf("cost = %f, want 100 (empty tz -> UTC)", cost)
  802. }
  803. }
  804. func TestTimeFunctions_NightDiscountPattern(t *testing.T) {
  805. exprStr := `tier("default", p * 2 + c * 10) * (hour("UTC") >= 21 || hour("UTC") < 6 ? 0.5 : 1)`
  806. cost, _, err := billingexpr.RunExpr(exprStr, billingexpr.TokenParams{P: 1000, C: 500})
  807. if err != nil {
  808. t.Fatal(err)
  809. }
  810. // Base = 1000*2 + 500*10 = 7000; multiplier is either 0.5 or 1 depending on current UTC hour
  811. if cost != 7000 && cost != 3500 {
  812. t.Errorf("cost = %f, want 7000 or 3500", cost)
  813. }
  814. }
  815. func TestTimeFunctions_WeekdayRange(t *testing.T) {
  816. exprStr := `tier("default", p) * (weekday("UTC") >= 0 && weekday("UTC") <= 6 ? 1 : 999)`
  817. cost, _, err := billingexpr.RunExpr(exprStr, billingexpr.TokenParams{P: 100})
  818. if err != nil {
  819. t.Fatal(err)
  820. }
  821. // weekday is always 0-6, so multiplier is always 1
  822. if cost != 100 {
  823. t.Errorf("cost = %f, want 100", cost)
  824. }
  825. }
  826. func TestTimeFunctions_MonthDayPattern(t *testing.T) {
  827. exprStr := `tier("default", p) * (month("Asia/Shanghai") == 1 && day("Asia/Shanghai") == 1 ? 0.5 : 1)`
  828. cost, _, err := billingexpr.RunExpr(exprStr, billingexpr.TokenParams{P: 1000})
  829. if err != nil {
  830. t.Fatal(err)
  831. }
  832. // Either 1000 (not Jan 1) or 500 (Jan 1) — both are valid
  833. if cost != 1000 && cost != 500 {
  834. t.Errorf("cost = %f, want 1000 or 500", cost)
  835. }
  836. }