billing-logic.test.mjs
sha256:65ccb454656ea5acdea0a10e559b78bcde1eb6ff753ecc2911bc99d1c3d7cadd
feat(calendar): enforce agent context tiers in retrieval AP…
Human
minor
⚠ breaking
2 days ago
| 1 | import { test } from 'node:test'; |
| 2 | import assert from 'node:assert/strict'; |
| 3 | import { tryDeduct, inferPackConsolidationPassesFromIndexingTokenBalance } from '../hub/gateway/billing-logic.mjs'; |
| 4 | |
| 5 | test('tryDeduct: beta tier never blocks', () => { |
| 6 | const u = { |
| 7 | tier: 'beta', |
| 8 | monthly_included_cents: 0, |
| 9 | monthly_used_cents: 0, |
| 10 | addon_cents: 0, |
| 11 | }; |
| 12 | assert.equal(tryDeduct(u, 999999).ok, true); |
| 13 | }); |
| 14 | |
| 15 | test('tryDeduct: monthly pool only', () => { |
| 16 | const u = { |
| 17 | tier: 'starter', |
| 18 | monthly_included_cents: 1000, |
| 19 | monthly_used_cents: 0, |
| 20 | addon_cents: 0, |
| 21 | }; |
| 22 | assert.equal(tryDeduct(u, 400).ok, true); |
| 23 | assert.equal(u.monthly_used_cents, 400); |
| 24 | assert.equal(tryDeduct(u, 600).ok, true); |
| 25 | assert.equal(u.monthly_used_cents, 1000); |
| 26 | }); |
| 27 | |
| 28 | test('tryDeduct: spills to addon rollover', () => { |
| 29 | const u = { |
| 30 | tier: 'starter', |
| 31 | monthly_included_cents: 1000, |
| 32 | monthly_used_cents: 800, |
| 33 | addon_cents: 500, |
| 34 | }; |
| 35 | assert.equal(tryDeduct(u, 500).ok, true); |
| 36 | assert.equal(u.monthly_used_cents, 1000); |
| 37 | assert.equal(u.addon_cents, 200); |
| 38 | }); |
| 39 | |
| 40 | test('tryDeduct: free tier uses small monthly pool', () => { |
| 41 | const u = { |
| 42 | tier: 'free', |
| 43 | monthly_included_cents: 0, |
| 44 | monthly_used_cents: 0, |
| 45 | addon_cents: 0, |
| 46 | }; |
| 47 | assert.equal(tryDeduct(u, 100).ok, true); |
| 48 | assert.equal(u.monthly_used_cents, 100); |
| 49 | assert.equal(tryDeduct(u, 200).ok, true); |
| 50 | assert.equal(u.monthly_used_cents, 300); |
| 51 | assert.equal(tryDeduct(u, 1).ok, false); |
| 52 | }); |
| 53 | |
| 54 | test('tryDeduct: QUOTA_EXHAUSTED when both pools insufficient', () => { |
| 55 | const u = { |
| 56 | tier: 'starter', |
| 57 | monthly_included_cents: 100, |
| 58 | monthly_used_cents: 100, |
| 59 | addon_cents: 50, |
| 60 | }; |
| 61 | const r = tryDeduct(u, 100); |
| 62 | assert.equal(r.ok, false); |
| 63 | assert.equal(r.code, 'QUOTA_EXHAUSTED'); |
| 64 | }); |
| 65 | |
| 66 | test('inferPackConsolidationPassesFromIndexingTokenBalance: 80M → 200 (medium + small)', () => { |
| 67 | assert.equal(inferPackConsolidationPassesFromIndexingTokenBalance(80_000_000), 200); |
| 68 | }); |
| 69 | |
| 70 | test('inferPackConsolidationPassesFromIndexingTokenBalance: 60M → 150 (one medium)', () => { |
| 71 | assert.equal(inferPackConsolidationPassesFromIndexingTokenBalance(60_000_000), 150); |
| 72 | }); |
| 73 | |
| 74 | test('inferPackConsolidationPassesFromIndexingTokenBalance: 20M → 50 (one small)', () => { |
| 75 | assert.equal(inferPackConsolidationPassesFromIndexingTokenBalance(20_000_000), 50); |
| 76 | }); |
| 77 | |
| 78 | test('inferPackConsolidationPassesFromIndexingTokenBalance: remainder uses 400k tokens/pass', () => { |
| 79 | assert.equal(inferPackConsolidationPassesFromIndexingTokenBalance(10_000_000), 25); |
| 80 | }); |
| 81 | |
| 82 | test('inferPackConsolidationPassesFromIndexingTokenBalance: zero / NaN', () => { |
| 83 | assert.equal(inferPackConsolidationPassesFromIndexingTokenBalance(0), 0); |
| 84 | assert.equal(inferPackConsolidationPassesFromIndexingTokenBalance(NaN), 0); |
| 85 | }); |
File History
2 commits
sha256:65ccb454656ea5acdea0a10e559b78bcde1eb6ff753ecc2911bc99d1c3d7cadd
feat(calendar): enforce agent context tiers in retrieval AP…
Human
minor
⚠
2 days ago
sha256:9103f98c89257ed2b01c237cea895dabb3e85ea337dccb1161c175e4422355b6
docs: accept Calendar Events v0 spec with Phase 0 security …
Human
2 days ago