auto-account-machine/browser-automation-ts/test-card-validity.mjs
2025-11-27 10:34:59 +08:00

91 lines
2.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 测试生成的卡号是否有效
*/
import pkg from './src/tools/card/CardGeneratorTool.js';
const { CardGeneratorTool } = pkg;
// Luhn算法验证
function validateLuhn(cardNumber) {
let sum = 0;
let isEven = false;
for (let i = cardNumber.length - 1; i >= 0; i--) {
let digit = parseInt(cardNumber[i]);
if (isEven) {
digit *= 2;
if (digit > 9) digit -= 9;
}
sum += digit;
isEven = !isEven;
}
return sum % 10 === 0;
}
async function test() {
console.log('生成20张卡验证有效性...\n');
const cardGen = new CardGeneratorTool();
await cardGen.initialize({});
const cards = await cardGen.generateBatch(20, 'unionpay');
let luhnPass = 0;
let luhnFail = 0;
console.log('=== 卡号验证 ===\n');
cards.forEach((card, i) => {
const isValid = validateLuhn(card.number);
const status = isValid ? '✅' : '❌';
if (isValid) luhnPass++;
else luhnFail++;
console.log(`${i + 1}. ${status} ${card.number}`);
console.log(` 有效期: ${card.month}/${card.year}`);
console.log(` CVV: ${card.cvv}`);
console.log(` 发卡行: ${card.issuer} (${card.country})`);
console.log();
});
console.log('=== 验证结果 ===');
console.log(`Luhn校验通过: ${luhnPass}/20`);
console.log(`Luhn校验失败: ${luhnFail}/20`);
// 验证有效期范围
console.log('\n=== 有效期检查 ===');
const months = cards.map(c => parseInt(c.month));
const years = cards.map(c => parseInt(c.year));
const validMonths = months.filter(m => m >= 1 && m <= 12).length;
const validYears = years.filter(y => y >= 26 && y <= 30).length;
console.log(`月份有效: ${validMonths}/20 (范围: 01-12)`);
console.log(`年份有效: ${validYears}/20 (范围: 26-30)`);
// 验证CVV长度
console.log('\n=== CVV检查 ===');
const validCVV = cards.filter(c => c.cvv.length === 3).length;
console.log(`CVV长度正确: ${validCVV}/20 (应为3位)`);
// 显示有效期和CVV的分布
console.log('\n=== 有效期分布 ===');
const expiryDist = {};
cards.forEach(c => {
const key = `${c.month}/${c.year}`;
expiryDist[key] = (expiryDist[key] || 0) + 1;
});
Object.entries(expiryDist)
.sort((a, b) => b[1] - a[1])
.forEach(([expiry, count]) => {
console.log(` ${expiry}: ${count}`);
});
}
test().catch(err => {
console.error('错误:', err.message);
console.error(err.stack);
});