39 lines
1.2 KiB
TypeScript
39 lines
1.2 KiB
TypeScript
import { MeterEventClient, reportUsage, METER_EVENT_NAME } from '../src/billing/stripe';
|
|
import { UsageEntry } from '../src/usage';
|
|
|
|
const entry = (cents: number): UsageEntry => ({
|
|
customerId: 'cust_1',
|
|
endpointId: 'storage',
|
|
cents,
|
|
metadataBytes: 0,
|
|
attachmentBytes: 0,
|
|
timestamp: new Date(),
|
|
});
|
|
|
|
const fakeClient = () => {
|
|
const calls: { eventName: string; customerId: string; value: string }[] = [];
|
|
const client: MeterEventClient = {
|
|
createMeterEvent: async (params) => {
|
|
calls.push(params);
|
|
},
|
|
};
|
|
return { client, calls };
|
|
};
|
|
|
|
describe('reportUsage', () => {
|
|
it('reports usage minus the monthly credit as one meter event', async () => {
|
|
const { client, calls } = fakeClient();
|
|
const reported = await reportUsage(client, 'cus_123', [entry(112), entry(4)], 100);
|
|
expect(reported).toBe(16); // 116 - 100 credit
|
|
expect(calls).toEqual([
|
|
{ eventName: METER_EVENT_NAME, customerId: 'cus_123', value: '16' },
|
|
]);
|
|
});
|
|
|
|
it('reports nothing when the credit covers all usage', async () => {
|
|
const { client, calls } = fakeClient();
|
|
const reported = await reportUsage(client, 'cus_123', [entry(4)], 100);
|
|
expect(reported).toBe(0);
|
|
expect(calls).toHaveLength(0);
|
|
});
|
|
});
|