Initial import of zappier-edge from zapier monorepo

This commit is contained in:
George Lambert 2026-09-11 13:37:15 -04:00
commit d6b86e4284
120 changed files with 19867 additions and 0 deletions

40
tests/db-usage.test.ts Normal file
View file

@ -0,0 +1,40 @@
import Database from 'better-sqlite3';
import { SqliteUsageRepo } from '../src/db/usage-repo';
import { 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('SqliteUsageRepo', () => {
it('records and lists entries per customer', () => {
const repo = new SqliteUsageRepo(new Database(':memory:'));
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 SqliteUsageRepo(new Database(':memory:'));
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 SqliteUsageRepo(new Database(':memory:'));
repo.record(entry());
repo.record(entry({ endpointId: 'storage', cents: 112 }));
const s = repo.summaryFor('cust_1');
expect(s.calls).toBe(2);
expect(s.totalCents).toBe(116);
expect(s.byEndpoint.storage).toEqual({ calls: 1, cents: 112 });
});
});