import { nanoid } from 'nanoid' import * as Runtime from '../../../test/runtime.js' import * as Db from '../Db.js' import * as BillingSettings from './billingSettings.js' const create = () => Db.postgres({ connectionString: Runtime.postgresUrl, schema: `t_${nanoid()}` }) /** Redacts nondeterministic row fields (timestamps). */ function redact(record: BillingSettings.Record) { return { ...record, createdAt: '', updatedAt: '' } } describe('upsert', () => { test('behavior: inserts defaults for fields not provided', async () => { const db = create() await db.migrate() const record = await BillingSettings.upsert(db, { orgId: 'org_1', spendLimit: '250' }) expect(redact(record)).toMatchInlineSnapshot(` { "createdAt": "", "currency": "usd", "environment": "production", "orgId": "org_1", "period": "month", "spendLimit": "250", "txFeeLimit": null, "updatedAt": "", } `) await db.close() }) test('behavior: absent fields keep their stored value', async () => { const db = create() await db.migrate() await BillingSettings.upsert(db, { orgId: 'org_1', spendLimit: '250' }) const record = await BillingSettings.upsert(db, { orgId: 'org_1', txFeeLimit: '0.50' }) expect(record.spendLimit).toBe('250') expect(record.txFeeLimit).toBe('0.50') await db.close() }) test('behavior: null clears a limit without touching the other', async () => { const db = create() await db.migrate() await BillingSettings.upsert(db, { orgId: 'org_1', spendLimit: '250', txFeeLimit: '0.50' }) // prettier-ignore const record = await BillingSettings.upsert(db, { orgId: 'org_1', spendLimit: null }) expect(record.spendLimit).toBeNull() expect(record.txFeeLimit).toBe('0.50') await db.close() }) }) describe('get', () => { test('behavior: returns the stored row, undefined for orgs without settings', async () => { const db = create() await db.migrate() expect(await BillingSettings.get(db, 'org_1')).toBeUndefined() const record = await BillingSettings.upsert(db, { orgId: 'org_1', txFeeLimit: '1' }) expect(await BillingSettings.get(db, 'org_1')).toStrictEqual(record) await db.close() }) test('behavior: production and sandbox settings coexist per org and read disjointly', async () => { const db = create() await db.migrate() const production = await BillingSettings.upsert(db, { orgId: 'org_1', spendLimit: '250' }) const sandbox = await BillingSettings.upsert(db, { environment: 'sandbox', orgId: 'org_1', spendLimit: '10' }) // prettier-ignore expect(sandbox.environment).toBe('sandbox') expect(await BillingSettings.get(db, 'org_1')).toStrictEqual(production) expect(await BillingSettings.get(db, 'org_1', 'sandbox')).toStrictEqual(sandbox) await db.close() }) }) describe('deleteByOrg', () => { test('behavior: deletes the row and reports absence', async () => { const db = create() await db.migrate() await BillingSettings.upsert(db, { orgId: 'org_1', spendLimit: '250' }) expect(await BillingSettings.deleteByOrg(db, 'org_1')).toBe(true) expect(await BillingSettings.deleteByOrg(db, 'org_1')).toBe(false) expect(await BillingSettings.get(db, 'org_1')).toBeUndefined() await db.close() }) })