59 lines
2.1 KiB
TypeScript
59 lines
2.1 KiB
TypeScript
import {
|
|
createBillingCatalog,
|
|
parseStripeCatalogPrice,
|
|
} from './billing-catalog.ts'
|
|
|
|
function assert(condition: unknown, message: string): asserts condition {
|
|
if (!condition) throw new Error(message)
|
|
}
|
|
|
|
Deno.test('Stripe catalog accepts only the exact active recurring price', () => {
|
|
const parsed = parseStripeCatalogPrice({
|
|
id: 'price_pro',
|
|
active: true,
|
|
type: 'recurring',
|
|
unit_amount: 990,
|
|
currency: 'usd',
|
|
recurring: { interval: 'month', interval_count: 1 },
|
|
}, 'price_pro')
|
|
assert(parsed?.unit_amount === 990, 'amount must be preserved')
|
|
assert(parsed?.currency === 'USD', 'currency must be normalized')
|
|
assert(parsed?.interval === 'month', 'interval must be preserved')
|
|
})
|
|
|
|
Deno.test('Stripe catalog rejects mismatched, inactive, free, malformed, and one-time prices', () => {
|
|
const base = {
|
|
id: 'price_pro',
|
|
active: true,
|
|
type: 'recurring',
|
|
unit_amount: 990,
|
|
currency: 'usd',
|
|
recurring: { interval: 'month', interval_count: 1 },
|
|
}
|
|
const invalid = [
|
|
{ ...base, id: 'price_other' },
|
|
{ ...base, active: false },
|
|
{ ...base, type: 'one_time', recurring: null },
|
|
{ ...base, unit_amount: 0 },
|
|
{ ...base, currency: 'US$' },
|
|
{ ...base, recurring: { interval: 'minute', interval_count: 1 } },
|
|
{ ...base, recurring: { interval: 'month', interval_count: 0 } },
|
|
]
|
|
for (const value of invalid) {
|
|
assert(parseStripeCatalogPrice(value, 'price_pro') === null, 'invalid Stripe price must fail closed')
|
|
}
|
|
})
|
|
|
|
Deno.test('catalog exposes only configured provider prices and never invents a fallback', () => {
|
|
const catalog = createBillingCatalog({
|
|
payple: { pro: 9900, pro_plus: 29900 },
|
|
stripe: {
|
|
pro: { unit_amount: 990, currency: 'USD', interval: 'month', interval_count: 1 },
|
|
},
|
|
})
|
|
assert(catalog.plans[0].prices.length === 2, 'pro must expose two verified providers')
|
|
assert(catalog.plans[1].prices.length === 1, 'pro plus must omit unavailable Stripe')
|
|
|
|
const unavailable = createBillingCatalog({})
|
|
assert(unavailable.plans.every((plan) => plan.prices.length === 0), 'missing configuration must stay unavailable')
|
|
})
|