agmission/Development/server/tests/test_coupon_endpoint.js

117 lines
4.1 KiB
JavaScript

#!/usr/bin/env node
/**
* Test Coupon Endpoint
*
* Verifies that /api/admin/subscriptionPromos/coupons returns both
* 'forever' and 'repeating' coupons, but excludes 'once' coupons.
*/
const path = require('path');
const { CouponDuration } = require('../helpers/constants');
// Parse --env argument
const args = process.argv.slice(2);
let envFile = './environment.env';
for (let i = 0; i < args.length; i++) {
if (args[i] === '--env' && args[i + 1]) {
envFile = args[i + 1];
i++;
}
}
// Load environment
const envPath = path.resolve(process.cwd(), envFile);
require('dotenv').config({ path: envPath });
console.log('=== Coupon Endpoint Test ===\n');
async function testCouponEndpoint() {
const stripe = require('stripe')(process.env.STRIPE_SEC_KEY);
try {
console.log('1. Fetching all coupons from Stripe...');
const allCoupons = await stripe.coupons.list({ limit: 100 });
console.log(` Total coupons in Stripe: ${allCoupons.data.length}`);
// Count by duration
const byDuration = {
forever: allCoupons.data.filter(c => c.duration === CouponDuration.FOREVER).length,
repeating: allCoupons.data.filter(c => c.duration === CouponDuration.REPEATING).length,
once: allCoupons.data.filter(c => c.duration === CouponDuration.ONCE).length
};
console.log(` - Forever: ${byDuration.forever}`);
console.log(` - Repeating: ${byDuration.repeating}`);
console.log(` - Once: ${byDuration.once}`);
console.log('');
console.log('2. Testing endpoint filter logic...');
const validCoupons = allCoupons.data.filter(c =>
!c.deleted && (c.duration === CouponDuration.FOREVER || c.duration === CouponDuration.REPEATING)
);
console.log(` Valid coupons (forever + repeating): ${validCoupons.length}`);
console.log(` Excluded coupons (once): ${byDuration.once}`);
console.log('');
console.log('3. Verifying response format...');
const formattedCoupons = validCoupons.map(c => ({
id: c.id,
name: c.name || c.id,
percent_off: c.percent_off,
amount_off: c.amount_off,
currency: c.currency,
duration: c.duration,
duration_in_months: c.duration_in_months,
valid: c.valid,
created: c.created
}));
console.log(' Sample coupons:');
formattedCoupons.slice(0, 5).forEach(c => {
const discount = c.percent_off ? `${c.percent_off}% off` :
c.amount_off ? `$${c.amount_off / 100} off` : 'Unknown';
const duration = c.duration === 'repeating' ?
`${c.duration} (${c.duration_in_months} months)` :
c.duration;
console.log(` - ${c.id}: ${discount}, ${duration}`);
});
console.log('');
console.log('4. Test Results:');
const hasForever = formattedCoupons.some(c => c.duration === CouponDuration.FOREVER);
const hasRepeating = formattedCoupons.some(c => c.duration === CouponDuration.REPEATING);
const hasOnce = formattedCoupons.some(c => c.duration === CouponDuration.ONCE);
console.log(` ✓ Forever coupons included: ${hasForever ? 'YES' : 'NO'}`);
console.log(` ✓ Repeating coupons included: ${hasRepeating ? 'YES' : 'NO'}`);
console.log(` ✓ Once coupons excluded: ${hasOnce ? 'NO (FAIL)' : 'YES'}`);
if (hasRepeating) {
const repeatingCoupon = formattedCoupons.find(c => c.duration === CouponDuration.REPEATING);
const hasMonthField = repeatingCoupon.duration_in_months !== undefined;
console.log(` ✓ Repeating coupons have duration_in_months: ${hasMonthField ? 'YES' : 'NO'}`);
}
console.log('');
console.log('=== Test Summary ===');
if (!hasOnce && (hasForever || hasRepeating)) {
console.log('✓ All tests passed!');
console.log('Endpoint correctly returns forever/repeating coupons and excludes once coupons.');
return 0;
} else {
console.log('✗ Test failed!');
if (hasOnce) console.log(' - Once coupons should be excluded');
if (!hasForever && !hasRepeating) console.log(' - No valid coupons found');
return 1;
}
} catch (err) {
console.error('ERROR:', err.message);
return 1;
}
}
testCouponEndpoint().then(code => process.exit(code));