Initial import of zappier-edge from zapier monorepo

This commit is contained in:
George Lambert 2026-09-11 17:15:28 -04:00
commit b9dca6cd0f
130 changed files with 20724 additions and 0 deletions

41
tests/usage.test.ts Normal file
View file

@ -0,0 +1,41 @@
import { InMemoryUsageRepo, UsageEntry } from '../src/usage';
const entry = (over: Partial<UsageEntry> = {}): UsageEntry => ({
customerId: 'cust_1',
endpointId: 'transform',
cents: 4,
metadataBytes: 0,
attachmentBytes: 0,
timestamp: new Date('2026-07-27T10:00:00Z'),
...over,
});
describe('InMemoryUsageRepo', () => {
it('records and lists entries per customer', () => {
const repo = new InMemoryUsageRepo();
repo.record(entry());
repo.record(entry({ customerId: 'cust_2' }));
expect(repo.listFor('cust_1')).toHaveLength(1);
expect(repo.listFor('cust_2')).toHaveLength(1);
});
it('filters entries by since date', () => {
const repo = new InMemoryUsageRepo();
repo.record(entry({ timestamp: new Date('2026-07-01T00:00:00Z') }));
repo.record(entry({ timestamp: new Date('2026-07-27T00:00:00Z') }));
expect(repo.listFor('cust_1', new Date('2026-07-15T00:00:00Z'))).toHaveLength(1);
});
it('summarizes totals by endpoint', () => {
const repo = new InMemoryUsageRepo();
repo.record(entry());
repo.record(entry({ endpointId: 'storage', cents: 112 }));
repo.record(entry());
const s = repo.summaryFor('cust_1');
expect(s.customerId).toBe('cust_1');
expect(s.calls).toBe(3);
expect(s.totalCents).toBe(120);
expect(s.byEndpoint.transform).toEqual({ calls: 2, cents: 8 });
expect(s.byEndpoint.storage).toEqual({ calls: 1, cents: 112 });
});
});