Files
autoAiWorkSys/api/tests/invite_register.test.js
张成 60f1c5e628 11
2025-12-20 00:07:32 +08:00

145 lines
6.2 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.
/**
* 邀请注册功能测试
* 测试新用户注册试用期和邀请人奖励逻辑
*/
const dayjs = require('dayjs');
// 运行所有测试
function runTests() {
console.log('\n========================================');
console.log('开始测试邀请注册功能');
console.log('========================================');
const tests = [
{ name: '新用户注册应该获得3天试用期', fn: () => {
console.log('\n【测试1】新用户注册应该获得3天试用期');
const newUserData = {
authorization_date: new Date(),
authorization_days: 3
};
if (!newUserData.authorization_date) throw new Error('授权日期为空');
if (newUserData.authorization_days !== 3) throw new Error('授权天数不是3天');
console.log('✅ 通过: 新用户获得3天试用期');
console.log(' - 授权日期:', dayjs(newUserData.authorization_date).format('YYYY-MM-DD'));
console.log(' - 授权天数:', newUserData.authorization_days, '天');
}},
{ name: '邀请人授权未过期时应该累加3天', fn: () => {
console.log('\n【测试2】邀请人授权未过期时应该累加3天');
const inviterData = {
authorization_date: dayjs().subtract(2, 'day').toDate(),
authorization_days: 7
};
const currentEndDate = dayjs(inviterData.authorization_date).add(inviterData.authorization_days, 'day');
const now = dayjs();
const remainingDays = currentEndDate.diff(now, 'day');
console.log(' 邀请人当前状态:');
console.log(' - 授权开始日期:', dayjs(inviterData.authorization_date).format('YYYY-MM-DD'));
console.log(' - 授权总天数:', inviterData.authorization_days, '天');
console.log(' - 剩余天数:', remainingDays, '天');
if (currentEndDate.isBefore(now)) throw new Error('测试数据错误:授权应该未过期');
const newAuthDays = inviterData.authorization_days + 3;
if (newAuthDays !== 10) throw new Error('累加计算错误');
console.log('✅ 通过: 未过期授权累加 7天 + 3天 = 10天');
}},
{ name: '邀请人授权已过期时应该重新激活给3天', fn: () => {
console.log('\n【测试3】邀请人授权已过期时应该重新激活给3天');
const inviterData = {
authorization_date: dayjs().subtract(10, 'day').toDate(),
authorization_days: 5
};
const currentEndDate = dayjs(inviterData.authorization_date).add(inviterData.authorization_days, 'day');
const now = dayjs();
const daysExpired = now.diff(currentEndDate, 'day');
console.log(' 邀请人当前状态:');
console.log(' - 授权开始日期:', dayjs(inviterData.authorization_date).format('YYYY-MM-DD'));
console.log(' - 授权天数:', inviterData.authorization_days, '天');
console.log(' - 过期日期:', currentEndDate.format('YYYY-MM-DD'));
console.log(' - 已过期天数:', daysExpired, '天');
if (!currentEndDate.isBefore(now)) throw new Error('测试数据错误:授权应该已过期');
const newAuthDate = new Date();
const newAuthDays = 3;
if (newAuthDays !== 3) throw new Error('重新激活计算错误');
console.log('✅ 通过: 过期授权重新激活给3天');
console.log(' - 新授权开始日期:', dayjs(newAuthDate).format('YYYY-MM-DD'));
}},
{ name: '邀请人没有授权日期时,应该从今天开始累加', fn: () => {
console.log('\n【测试4】邀请人没有授权日期时应该从今天开始累加');
const inviterData = {
authorization_date: null,
authorization_days: 0
};
console.log(' 邀请人当前状态:');
console.log(' - 授权开始日期: 无');
console.log(' - 授权天数:', inviterData.authorization_days, '天');
if (inviterData.authorization_date) throw new Error('测试数据错误:授权日期应该为空');
const newAuthDate = new Date();
const newAuthDays = 3;
if (!newAuthDate) throw new Error('授权日期为空');
if (newAuthDays !== 3) throw new Error('天数计算错误');
console.log('✅ 通过: 首次授权从今天开始给3天');
console.log(' - 新授权开始日期:', dayjs(newAuthDate).format('YYYY-MM-DD'));
console.log(' - 新授权天数:', newAuthDays, '天');
}},
{ name: '邀请记录应该正确保存', fn: () => {
console.log('\n【测试5】邀请记录应该正确保存');
const record = {
inviter_id: 1,
invitee_id: 2,
invite_code: 'INV1_ABC123',
reward_status: 1,
reward_type: 'trial_days',
reward_value: 3
};
console.log(' 邀请记录内容:');
console.log(' - 邀请人ID:', record.inviter_id);
console.log(' - 被邀请人ID:', record.invitee_id);
console.log(' - 邀请码:', record.invite_code);
console.log(' - 奖励状态:', record.reward_status === 1 ? '已发放' : '未发放');
console.log(' - 奖励类型:', record.reward_type);
console.log(' - 奖励值:', record.reward_value, '天');
if (record.reward_status !== 1) throw new Error('奖励状态错误');
if (record.reward_type !== 'trial_days') throw new Error('奖励类型错误');
if (record.reward_value !== 3) throw new Error('奖励值错误');
console.log('✅ 通过: 邀请记录字段正确');
}}
];
let passed = 0;
let failed = 0;
tests.forEach(test => {
try {
test.fn();
passed++;
} catch (error) {
failed++;
console.error(`❌ 失败: ${error.message}`);
}
});
console.log('\n========================================');
console.log('测试完成');
console.log('========================================');
console.log(`✅ 通过: ${passed}/${tests.length}`);
console.log(`❌ 失败: ${failed}/${tests.length}`);
console.log(`成功率: ${(passed / tests.length * 100).toFixed(0)}%`);
console.log('========================================\n');
return failed === 0;
}
// 如果直接运行此文件,执行测试
if (require.main === module) {
const success = runTests();
process.exit(success ? 0 : 1);
}
module.exports = { runTests };