250 lines
7.6 KiB
JavaScript
250 lines
7.6 KiB
JavaScript
/**
|
||
* 账号工具函数测试
|
||
*/
|
||
|
||
const {
|
||
calculateRemainingDays,
|
||
isAuthorizationValid,
|
||
addRemainingDays,
|
||
addRemainingDaysToAccounts
|
||
} = require('../utils/account_utils');
|
||
|
||
// 测试剩余天数计算
|
||
function testCalculateRemainingDays() {
|
||
console.log('\n===== 测试剩余天数计算 =====');
|
||
|
||
try {
|
||
const dayjs = require('dayjs');
|
||
|
||
// 测试 1: 未来有效期
|
||
const futureDate = dayjs().subtract(5, 'day').toDate();
|
||
const remaining1 = calculateRemainingDays(futureDate, 30);
|
||
console.log('✓ 未来有效期 (5天前授权30天):', remaining1, '天');
|
||
console.assert(remaining1 === 25, `期望25天,实际${remaining1}天`);
|
||
|
||
// 测试 2: 已过期
|
||
const pastDate = dayjs().subtract(40, 'day').toDate();
|
||
const remaining2 = calculateRemainingDays(pastDate, 30);
|
||
console.log('✓ 已过期 (40天前授权30天):', remaining2, '天');
|
||
console.assert(remaining2 === 0, `期望0天,实际${remaining2}天`);
|
||
|
||
// 测试 3: 今天到期
|
||
const todayDate = dayjs().startOf('day').toDate();
|
||
const remaining3 = calculateRemainingDays(todayDate, 0);
|
||
console.log('✓ 今天到期:', remaining3, '天');
|
||
|
||
// 测试 4: 空值处理
|
||
const remaining4 = calculateRemainingDays(null, 30);
|
||
console.log('✓ 空授权日期:', remaining4, '天');
|
||
console.assert(remaining4 === 0, '空值应返回0');
|
||
|
||
const remaining5 = calculateRemainingDays(futureDate, 0);
|
||
console.log('✓ 0天授权:', remaining5, '天');
|
||
console.assert(remaining5 === 0, '0天授权应返回0');
|
||
|
||
return true;
|
||
} catch (error) {
|
||
console.error('✗ 剩余天数计算测试失败:', error.message);
|
||
return false;
|
||
}
|
||
}
|
||
|
||
// 测试授权有效性检查
|
||
function testIsAuthorizationValid() {
|
||
console.log('\n===== 测试授权有效性检查 =====');
|
||
|
||
try {
|
||
const dayjs = require('dayjs');
|
||
|
||
// 测试 1: 有效授权
|
||
const validDate = dayjs().subtract(5, 'day').toDate();
|
||
const isValid = isAuthorizationValid(validDate, 30);
|
||
console.log('✓ 有效授权 (5天前授权30天):', isValid ? '有效' : '无效');
|
||
console.assert(isValid === true, '应该是有效的');
|
||
|
||
// 测试 2: 过期授权
|
||
const expiredDate = dayjs().subtract(40, 'day').toDate();
|
||
const isExpired = isAuthorizationValid(expiredDate, 30);
|
||
console.log('✓ 过期授权 (40天前授权30天):', isExpired ? '有效' : '无效');
|
||
console.assert(isExpired === false, '应该是无效的');
|
||
|
||
// 测试 3: 空值处理
|
||
const isNull = isAuthorizationValid(null, 30);
|
||
console.log('✓ 空授权日期:', isNull ? '有效' : '无效');
|
||
console.assert(isNull === false, '空值应该无效');
|
||
|
||
return true;
|
||
} catch (error) {
|
||
console.error('✗ 授权有效性检查测试失败:', error.message);
|
||
return false;
|
||
}
|
||
}
|
||
|
||
// 测试添加剩余天数
|
||
function testAddRemainingDays() {
|
||
console.log('\n===== 测试添加剩余天数 =====');
|
||
|
||
try {
|
||
const dayjs = require('dayjs');
|
||
|
||
// 测试 1: 普通对象
|
||
const account1 = {
|
||
id: 1,
|
||
sn_code: 'SN001',
|
||
authorization_date: dayjs().subtract(5, 'day').toDate(),
|
||
authorization_days: 30
|
||
};
|
||
|
||
const result1 = addRemainingDays(account1);
|
||
console.log('✓ 普通对象添加剩余天数:', result1.remaining_days, '天');
|
||
console.assert(result1.remaining_days === 25, `期望25天,实际${result1.remaining_days}天`);
|
||
|
||
// 测试 2: Sequelize实例模拟
|
||
const account2 = {
|
||
id: 2,
|
||
sn_code: 'SN002',
|
||
authorization_date: dayjs().subtract(10, 'day').toDate(),
|
||
authorization_days: 15,
|
||
toJSON: function() {
|
||
return {
|
||
id: this.id,
|
||
sn_code: this.sn_code,
|
||
authorization_date: this.authorization_date,
|
||
authorization_days: this.authorization_days
|
||
};
|
||
}
|
||
};
|
||
|
||
const result2 = addRemainingDays(account2);
|
||
console.log('✓ Sequelize实例添加剩余天数:', result2.remaining_days, '天');
|
||
console.assert(result2.remaining_days === 5, `期望5天,实际${result2.remaining_days}天`);
|
||
|
||
return true;
|
||
} catch (error) {
|
||
console.error('✗ 添加剩余天数测试失败:', error.message);
|
||
return false;
|
||
}
|
||
}
|
||
|
||
// 测试批量添加剩余天数
|
||
function testAddRemainingDaysToAccounts() {
|
||
console.log('\n===== 测试批量添加剩余天数 =====');
|
||
|
||
try {
|
||
const dayjs = require('dayjs');
|
||
|
||
const accounts = [
|
||
{
|
||
id: 1,
|
||
authorization_date: dayjs().subtract(5, 'day').toDate(),
|
||
authorization_days: 30
|
||
},
|
||
{
|
||
id: 2,
|
||
authorization_date: dayjs().subtract(10, 'day').toDate(),
|
||
authorization_days: 15
|
||
},
|
||
{
|
||
id: 3,
|
||
authorization_date: dayjs().subtract(50, 'day').toDate(),
|
||
authorization_days: 30
|
||
}
|
||
];
|
||
|
||
const results = addRemainingDaysToAccounts(accounts);
|
||
console.log('✓ 批量添加剩余天数:');
|
||
results.forEach((acc, index) => {
|
||
console.log(` 账号${index + 1}: ${acc.remaining_days}天`);
|
||
});
|
||
|
||
console.assert(results.length === 3, '数组长度应该是3');
|
||
console.assert(results[0].remaining_days === 25, '第1个账号剩余天数错误');
|
||
console.assert(results[1].remaining_days === 5, '第2个账号剩余天数错误');
|
||
console.assert(results[2].remaining_days === 0, '第3个账号剩余天数错误');
|
||
|
||
// 测试空数组
|
||
const emptyResults = addRemainingDaysToAccounts([]);
|
||
console.log('✓ 空数组处理:', emptyResults.length === 0 ? '正确' : '错误');
|
||
|
||
return true;
|
||
} catch (error) {
|
||
console.error('✗ 批量添加剩余天数测试失败:', error.message);
|
||
return false;
|
||
}
|
||
}
|
||
|
||
// 测试时区处理
|
||
function testTimezoneHandling() {
|
||
console.log('\n===== 测试时区处理 =====');
|
||
|
||
try {
|
||
const dayjs = require('dayjs');
|
||
const utc = require('dayjs/plugin/utc');
|
||
dayjs.extend(utc);
|
||
|
||
// 创建不同时区的日期
|
||
const localDate = dayjs().subtract(5, 'day').toDate();
|
||
const utcDate = dayjs().utc().subtract(5, 'day').toDate();
|
||
|
||
const remaining1 = calculateRemainingDays(localDate, 30);
|
||
const remaining2 = calculateRemainingDays(utcDate, 30);
|
||
|
||
console.log('✓ 本地时区日期剩余天数:', remaining1, '天');
|
||
console.log('✓ UTC时区日期剩余天数:', remaining2, '天');
|
||
|
||
// 剩余天数应该接近(可能相差1天因为时区转换)
|
||
const diff = Math.abs(remaining1 - remaining2);
|
||
console.log('✓ 时区差异:', diff, '天');
|
||
console.assert(diff <= 1, '时区差异应该不超过1天');
|
||
|
||
return true;
|
||
} catch (error) {
|
||
console.error('✗ 时区处理测试失败:', error.message);
|
||
return false;
|
||
}
|
||
}
|
||
|
||
// 运行所有测试
|
||
async function runAllTests() {
|
||
console.log('\n==================== 开始测试 ====================\n');
|
||
|
||
const results = [];
|
||
|
||
results.push(testCalculateRemainingDays());
|
||
results.push(testIsAuthorizationValid());
|
||
results.push(testAddRemainingDays());
|
||
results.push(testAddRemainingDaysToAccounts());
|
||
results.push(testTimezoneHandling());
|
||
|
||
console.log('\n==================== 测试总结 ====================\n');
|
||
|
||
const passed = results.filter(r => r).length;
|
||
const total = results.length;
|
||
|
||
console.log(`测试通过: ${passed}/${total}`);
|
||
|
||
if (passed === total) {
|
||
console.log('\n✓ 所有测试通过!\n');
|
||
process.exit(0);
|
||
} else {
|
||
console.log('\n✗ 部分测试失败\n');
|
||
process.exit(1);
|
||
}
|
||
}
|
||
|
||
// 执行测试
|
||
if (require.main === module) {
|
||
runAllTests().catch(error => {
|
||
console.error('测试执行失败:', error);
|
||
process.exit(1);
|
||
});
|
||
}
|
||
|
||
module.exports = {
|
||
testCalculateRemainingDays,
|
||
testIsAuthorizationValid,
|
||
testAddRemainingDays,
|
||
testAddRemainingDaysToAccounts,
|
||
testTimezoneHandling
|
||
};
|