-
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathbilling.test.js
More file actions
168 lines (135 loc) · 5.7 KB
/
Copy pathbilling.test.js
File metadata and controls
168 lines (135 loc) · 5.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { describe, it, expect } from 'vitest';
import { readFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import { dirname, resolve } from 'node:path';
const __dirname = dirname(fileURLToPath(import.meta.url));
const repoRoot = resolve(__dirname, '..');
const p = (...parts) => resolve(repoRoot, ...parts);
describe('billing — vercel.json routing', () => {
const vercel = JSON.parse(readFileSync(p('vercel.json'), 'utf8'));
const routes = vercel.routes || [];
it('routes /api/billing/summary to the serverless endpoint', () => {
const r = routes.find((x) => x.src === '/api/billing/summary');
expect(r).toBeTruthy();
expect(r.dest).toBe('/api/billing/summary');
});
});
describe('billing — dashboard tab', () => {
const dashjs = readFileSync(p('src/dashboard/dashboard.js'), 'utf8');
it('renderBilling is async and fetches billing data', () => {
expect(dashjs).toContain('async function renderBilling');
expect(dashjs).toContain('/api/billing/summary');
});
it('billing tab renders quota meters for avatars, storage, MCP', () => {
expect(dashjs).toContain('usage.mcp_calls_24h');
expect(dashjs).toContain('usage.total_bytes');
expect(dashjs).toContain('usage.avatar_count');
});
});
describe('billing — endpoint file', () => {
const src = readFileSync(p('api/billing/summary.js'), 'utf8');
it('exports a default handler', () => {
expect(src).toContain('export default');
});
it('queries plan_quotas joined with users', () => {
expect(src).toContain('plan_quotas');
expect(src).toContain('user.id');
});
it('returns usage fields for avatars, agents, mcp, llm', () => {
expect(src).toContain('avatar_count');
expect(src).toContain('agent_count');
expect(src).toContain('mcp_calls_24h');
expect(src).toContain('llm_calls_month');
});
it('requires authentication', () => {
expect(src).toContain('getSessionUser');
expect(src).toContain('401');
});
});
describe('billing — pricing + invoices routing', () => {
const vercel = JSON.parse(readFileSync(p('vercel.json'), 'utf8'));
const routes = vercel.routes || [];
const route = (src) => routes.find((x) => x.src === src);
it('routes /api/pricing to the aggregate pricing endpoint', () => {
expect(route('/api/pricing')?.dest).toBe('/api/pricing');
});
it('routes /api/billing/invoices to the invoice statement endpoint', () => {
expect(route('/api/billing/invoices')?.dest).toBe('/api/billing/invoices');
});
it('routes /billing to the statement page', () => {
expect(route('/billing/?')?.dest).toBe('/billing.html');
});
});
describe('billing — /api/pricing serves server truth', () => {
const src = readFileSync(p('api/pricing.js'), 'utf8');
it('aggregates the catalog, platform fee, and holder tier ladder', () => {
expect(src).toContain('publicCatalog');
expect(src).toContain('getFeeBps');
expect(src).toContain('TIERS');
});
it('personalizes the holder price via the real charge-rail price function', () => {
expect(src).toContain('priceForAction');
expect(src).toContain('your_usd');
expect(src).toContain('resolveUserTier');
});
it('never blocks the page on the balance read (degrades to public price)', () => {
// The personalization is wrapped in try/catch so an RPC hiccup falls back.
expect(src).toMatch(/try\s*{[\s\S]*resolveUserTier[\s\S]*catch/);
});
});
describe('billing — invoices endpoint rolls usage into a statement', () => {
const src = readFileSync(p('api/billing/invoices.js'), 'utf8');
it('uses the metering rollup + reconciliation helpers', () => {
expect(src).toContain('rollupInvoice');
expect(src).toContain('reconciliationStatus');
});
it('requires authentication', () => {
expect(src).toContain('getSessionUser');
expect(src).toContain('401');
});
it('supports a CSV download of the statement', () => {
expect(src).toContain('format');
expect(src).toContain('text/csv');
});
});
describe('billing — receipts serves a per-charge metered receipt', () => {
const src = readFileSync(p('api/billing/receipts.js'), 'utf8');
it('returns a metered receipt by event_id via getReceipt', () => {
expect(src).toContain('event_id');
expect(src).toContain('getReceipt');
});
it('keeps the existing signed purchase receipt path', () => {
expect(src).toContain('purchase_id');
expect(src).toContain('purchase_receipts');
});
});
describe('billing — metering is wired into the one charge choke point', () => {
const charge = readFileSync(p('api/_lib/pricing/charge-three.js'), 'utf8');
it('meters after a settled $THREE charge', () => {
expect(charge).toContain('recordUsageSafe');
expect(charge).toContain('meterSettledCharge');
});
it('meters the allowance rail too, keyed by the settlement id', () => {
// Both the settle and allowance success paths call the meter helper.
const calls = charge.match(/meterSettledCharge\(/g) || [];
expect(calls.length).toBeGreaterThanOrEqual(3); // 1 def + 2 call sites
});
});
describe('billing — revenue dashboard surfaces reconciliation status', () => {
const src = readFileSync(p('api/billing/revenue.js'), 'utf8');
it('includes reconciliation in the dashboard payload', () => {
expect(src).toContain('reconciliationStatus');
expect(src).toContain('reconciliation');
});
});
describe('billing — usage metering migration', () => {
const sqlText = readFileSync(p('api/_lib/migrations/20260623170000_usage_metering.sql'), 'utf8');
it('adds the metering columns to usage_events', () => {
for (const col of ['meter_action', 'price_usdc_atomics', 'fee_usdc_atomics', 'settlement_ref', 'idempotency_key']) {
expect(sqlText).toContain(col);
}
});
it('enforces idempotency with a unique index on the idempotency key', () => {
expect(sqlText).toContain('create unique index if not exists usage_events_idem');
});
});