← back to Ventura Claw Leads
tests/stripe-lib.test.js
103 lines
// lib/stripe.js unit tests — config + tier resolution + cross-project filter.
// No live Stripe calls; everything is in-memory metadata logic.
require('dotenv').config({ path: require('path').resolve(__dirname, '..', '.env') });
process.env.NODE_ENV = 'test';
// Force missing-key path so client() returns null (don't accidentally hit live Stripe).
delete process.env.STRIPE_SECRET_KEY;
process.env.STRIPE_PRICE_VCL_STARTER_MONTH = 'price_test_starter';
process.env.STRIPE_PRICE_VCL_STANDARD_MONTH = 'price_test_standard';
process.env.STRIPE_PRICE_VCL_PREMIER_MONTH = 'price_test_premier';
const test = require('node:test');
const assert = require('node:assert/strict');
const s = require('../lib/stripe');
test('VCL_TIERS has 3 entries with required fields', () => {
const keys = Object.keys(s.VCL_TIERS);
assert.deepEqual(keys.sort(), ['premier', 'standard', 'starter']);
for (const k of keys) {
const t = s.VCL_TIERS[k];
assert.ok(t.name, `${k}.name`);
assert.ok(t.envMonth, `${k}.envMonth`);
assert.ok(t.cents > 0, `${k}.cents`);
assert.ok(t.label, `${k}.label`);
}
});
test('isLive returns false when STRIPE_SECRET_KEY missing', () => {
assert.equal(s.isLive(), false);
});
test('priceIdFor resolves env vars per tier', () => {
assert.equal(s.priceIdFor('starter'), 'price_test_starter');
assert.equal(s.priceIdFor('standard'), 'price_test_standard');
assert.equal(s.priceIdFor('premier'), 'price_test_premier');
assert.equal(s.priceIdFor('nope'), null);
});
test('tierFromPriceId reverse-resolves', () => {
assert.equal(s.tierFromPriceId('price_test_starter'), 'starter');
assert.equal(s.tierFromPriceId('price_test_standard'), 'standard');
assert.equal(s.tierFromPriceId('price_test_premier'), 'premier');
assert.equal(s.tierFromPriceId('price_unknown'), null);
assert.equal(s.tierFromPriceId(null), null);
assert.equal(s.tierFromPriceId(''), null);
});
test('eventBelongsToVCL: metadata.project=ventura-claw-leads ⇒ true', () => {
const event = {
type: 'customer.subscription.updated',
data: { object: { metadata: { project: 'ventura-claw-leads' } } }
};
assert.equal(s.eventBelongsToVCL(event), true);
});
test('eventBelongsToVCL: metadata.project=national-paper-hangers ⇒ false', () => {
const event = {
type: 'customer.subscription.updated',
data: { object: { metadata: { project: 'national-paper-hangers' } } }
};
assert.equal(s.eventBelongsToVCL(event), false);
});
test('eventBelongsToVCL: subscription items reference VCL price ⇒ true', () => {
const event = {
type: 'customer.subscription.created',
data: { object: {
metadata: {},
items: { data: [{ price: { id: 'price_test_premier' } }] }
} }
};
assert.equal(s.eventBelongsToVCL(event), true);
});
test('eventBelongsToVCL: invoice lines reference VCL price ⇒ true', () => {
const event = {
type: 'invoice.payment_succeeded',
data: { object: {
metadata: {},
lines: { data: [{ price: { id: 'price_test_starter' } }] }
} }
};
assert.equal(s.eventBelongsToVCL(event), true);
});
test('eventBelongsToVCL: no metadata + foreign price ⇒ false', () => {
const event = {
type: 'customer.subscription.created',
data: { object: {
metadata: {},
items: { data: [{ price: { id: 'price_npx_install_deposit' } }] }
} }
};
assert.equal(s.eventBelongsToVCL(event), false);
});
test('eventBelongsToVCL: malformed event ⇒ false (no throw)', () => {
assert.equal(s.eventBelongsToVCL(null), false);
assert.equal(s.eventBelongsToVCL({}), false);
assert.equal(s.eventBelongsToVCL({ data: null }), false);
assert.equal(s.eventBelongsToVCL({ data: {} }), false);
});