billing-logic.test.mjs
sha256:b5f647cb9c409f563d4671fe3fc05ddea01fabfed9b41fc11cb923588e1c1baf
mirror: GitHub Phase A durable MCP OAuth (#270)
Human
minor
⚠ breaking
13 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
4 commits
sha256:b5f647cb9c409f563d4671fe3fc05ddea01fabfed9b41fc11cb923588e1c1baf
mirror: GitHub Phase A durable MCP OAuth (#270)
Human
minor
⚠
13 days ago
sha256:d8c648b20a4d53b2673c5c082ee7edfa7b2fc9b11080832da1f38807b6bf940b
fix(7C-L1b): route hosted delegation proposals through cani…
Human
minor
⚠
33 days ago
sha256:2827ba9e7632a4b141c50caf1e8f7d77abbc3515be20e7465f2bccb0ac4edf91
fix: repair endpoint now sets has_active_subscription when …
Human
minor
⚠
53 days ago
sha256:6a102aafafdfe7e70a24f4e59740200f0ee713ce7915f1b53e9d4ba5ee8b4410
Initial Muse snapshot
Human
85 days ago