import Database from 'better-sqlite3'; import { SqliteUsageRepo } from '../src/db/usage-repo'; import { UsageEntry } from '../src/usage'; const entry = (over: Partial = {}): 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 }); }); });