import { nanoid } from 'nanoid' import * as Runtime from '../../../test/runtime.js' import * as Db from '../Db.js' import * as StripeCustomers from './stripeCustomers.js' const create = () => Db.postgres({ connectionString: Runtime.postgresUrl, schema: `t_${nanoid()}` }) /** Redacts nondeterministic row fields (timestamps). */ function redact(record: StripeCustomers.Record) { return { ...record, createdAt: '', updatedAt: '' } } describe('create', () => { test('behavior: inserts with status none', async () => { const db = create() await db.migrate() const record = await StripeCustomers.create(db, { orgId: 'org_1', stripeCustomerId: 'cus_a', }) expect(redact(record)).toMatchInlineSnapshot(` { "createdAt": "", "environment": "production", "orgId": "org_1", "status": "none", "stripeCustomerId": "cus_a", "updatedAt": "", } `) await db.close() }) test('behavior: a concurrent duplicate resolves to the winning row', async () => { const db = create() await db.migrate() const winner = await StripeCustomers.create(db, { orgId: 'org_1', stripeCustomerId: 'cus_a' }) // Loser of the first-checkout race: same org, its own Stripe customer. const resolved = await StripeCustomers.create(db, { orgId: 'org_1', stripeCustomerId: 'cus_b' }) // prettier-ignore expect(resolved).toStrictEqual(winner) expect(await StripeCustomers.getByCustomer(db, 'cus_b')).toBeUndefined() await db.close() }) }) describe('get', () => { test('behavior: reads by org; undefined when absent', async () => { const db = create() await db.migrate() const record = await StripeCustomers.create(db, { orgId: 'org_1', stripeCustomerId: 'cus_a' }) expect(await StripeCustomers.get(db, 'org_1')).toStrictEqual(record) expect(await StripeCustomers.get(db, 'org_missing')).toBeUndefined() await db.close() }) test('behavior: production and sandbox rows coexist per org and read disjointly', async () => { const db = create() await db.migrate() const production = await StripeCustomers.create(db, { orgId: 'org_1', stripeCustomerId: 'cus_live' }) // prettier-ignore const sandbox = await StripeCustomers.create(db, { environment: 'sandbox', orgId: 'org_1', stripeCustomerId: 'cus_test' }) // prettier-ignore expect(sandbox.environment).toBe('sandbox') expect(await StripeCustomers.get(db, 'org_1')).toStrictEqual(production) expect(await StripeCustomers.get(db, 'org_1', 'sandbox')).toStrictEqual(sandbox) await db.close() }) }) describe('getByCustomer', () => { test('behavior: reads by Stripe customer id; undefined for unknown customers', async () => { const db = create() await db.migrate() const record = await StripeCustomers.create(db, { orgId: 'org_1', stripeCustomerId: 'cus_a' }) expect(await StripeCustomers.getByCustomer(db, 'cus_a')).toStrictEqual(record) expect(await StripeCustomers.getByCustomer(db, 'cus_unknown')).toBeUndefined() await db.close() }) }) describe('setStatus', () => { test('behavior: persists the status and bumps updatedAt', async () => { const db = create() await db.migrate() const record = await StripeCustomers.create(db, { orgId: 'org_1', stripeCustomerId: 'cus_a' }) const updated = await StripeCustomers.setStatus(db, 'org_1', 'active') expect(updated?.status).toMatchInlineSnapshot(`"active"`) expect(updated?.createdAt).toBe(record.createdAt) await db.close() }) test('behavior: undefined when the org has no billing account', async () => { const db = create() await db.migrate() expect(await StripeCustomers.setStatus(db, 'org_missing', 'active')).toBeUndefined() await db.close() }) }) describe('deleteByOrg', () => { test('behavior: deletes the row; false when absent', async () => { const db = create() await db.migrate() await StripeCustomers.create(db, { orgId: 'org_1', stripeCustomerId: 'cus_a' }) expect(await StripeCustomers.deleteByOrg(db, 'org_1')).toBe(true) expect(await StripeCustomers.get(db, 'org_1')).toBeUndefined() expect(await StripeCustomers.deleteByOrg(db, 'org_1')).toBe(false) await db.close() }) })