文件
Henglianshutong/tests/partner/verify_partner_reward_to_money_spec.js

192 行
12 KiB
JavaScript

/**
* 收益分成直接加到钱包余额专项验证测试
* 覆盖范围:
* 1. PartnerConfig (rewardWalletType 默认 money)
* 2. PartnerRewardService (account 走 money 入账,refund 智能回滚)
* 3. DailySettleService (cashShare 走 money 入账,clearDaily 智能回滚)
* 4. 后台 Reward 控制器 (add 走 money 入账,back 智能回滚)
* 5. WalletLog 模型 (eventMap['money'] 补全收益事件,getEventTextAttr 动态解析)
* 6. 用户端组件 account-info.vue (展示钱包余额并跳转 /pages/user/wallet/money)
* 7. 业务逻辑与回滚算法行为模拟断言
*/
const fs = require('fs');
const path = require('path');
let passCount = 0;
let failCount = 0;
function assert(condition, message) {
if (condition) {
console.log(` \x1b[32m[PASS]\x1b[0m ${message}`);
passCount++;
} else {
console.error(` \x1b[31m[FAIL]\x1b[0m ${message}`);
failCount++;
}
}
console.log('========================================================');
console.log(' 收益分成直接加到钱包余额专项测试');
console.log('========================================================\n');
// ----------------------------------------------------
// 1. PartnerConfig.php 验证
// ----------------------------------------------------
console.log('--- 1. PartnerConfig.php 规范验证 ---');
const partnerConfigPath = path.resolve(__dirname, '../../addons/shopro/service/partner/PartnerConfig.php');
assert(fs.existsSync(partnerConfigPath), 'PartnerConfig.php 文件存在');
if (fs.existsSync(partnerConfigPath)) {
const content = fs.readFileSync(partnerConfigPath, 'utf-8');
assert(content.includes('public static function rewardWalletType()'), '包含 rewardWalletType() 收益入账钱包解析方法');
assert(content.includes("self::get('partner_reward_wallet', 'money')"), '默认收益入账钱包缺省为 money (钱包余额)');
assert(content.includes("in_array($type, ['money', 'commission'])"), '安全限定钱包类型在合法区间');
}
// ----------------------------------------------------
// 2. PartnerRewardService.php 验证
// ----------------------------------------------------
console.log('\n--- 2. PartnerRewardService.php 调度验证 ---');
const partnerRewardServicePath = path.resolve(__dirname, '../../addons/shopro/service/partner/PartnerRewardService.php');
assert(fs.existsSync(partnerRewardServicePath), 'PartnerRewardService.php 文件存在');
if (fs.existsSync(partnerRewardServicePath)) {
const content = fs.readFileSync(partnerRewardServicePath, 'utf-8');
assert(content.includes('$walletType = PartnerConfig::rewardWalletType();'), 'account() 动态获取收益入账钱包类型');
assert(content.includes('Wallet::change($userId, $walletType, $amount, $type,'), 'account() 使用动态钱包入账(直接入钱包余额)');
assert(content.includes("AgentModel::where('user_id', $userId)->setInc('total_income', $amount);"), '保留分销商累计收益 total_income 累加');
assert(content.includes('$walletLog = \\app\\admin\\model\\shopro\\user\\WalletLog::where('), 'refund() 退款时查询原入账钱包记录');
assert(content.includes("$targetWallet = ($walletLog && $walletLog->type === 'commission') ? 'commission' : PartnerConfig::rewardWalletType();"), 'refund() 智能根据原入账类型回滚对应钱包');
assert(content.includes("Wallet::change($reward->user_id, $targetWallet, -$reward->amount, 'reward_back'"), 'refund() 精准回滚目标钱包');
}
// ----------------------------------------------------
// 3. DailySettleService.php 验证
// ----------------------------------------------------
console.log('\n--- 3. DailySettleService.php 日结加权分润验证 ---');
const dailySettleServicePath = path.resolve(__dirname, '../../addons/shopro/service/partner/DailySettleService.php');
assert(fs.existsSync(dailySettleServicePath), 'DailySettleService.php 文件存在');
if (fs.existsSync(dailySettleServicePath)) {
const content = fs.readFileSync(dailySettleServicePath, 'utf-8');
assert(content.includes('$walletType = PartnerConfig::rewardWalletType();'), 'cashShare() 获取收益入账钱包类型');
assert(content.includes('Wallet::change($userId, $walletType, $each, $type,'), 'cashShare() 日结酒道馆/区县分成直接加到钱包余额');
assert(content.includes("AgentModel::where('user_id', $userId)->setInc('total_income', $each);"), 'cashShare() 累计更新 total_income');
assert(content.includes("$targetWallet = ($walletLog && $walletLog->type === 'commission') ? 'commission' : PartnerConfig::rewardWalletType();"), 'clearDaily() 重跑日结时智能识别并回滚对应钱包');
}
// ----------------------------------------------------
// 4. 后台 Reward 控制器验证
// ----------------------------------------------------
console.log('\n--- 4. 后台 Reward.php 控制器验证 ---');
const rewardCtrlPath = path.resolve(__dirname, '../../application/admin/controller/shopro/partner/Reward.php');
assert(fs.existsSync(rewardCtrlPath), 'Reward.php 文件存在');
if (fs.existsSync(rewardCtrlPath)) {
const content = fs.readFileSync(rewardCtrlPath, 'utf-8');
assert(content.includes('$walletType = PartnerConfig::rewardWalletType();'), 'add() 手工补发获取收益入账钱包类型');
assert(content.includes('Wallet::change($params[\'user_id\'], $walletType, $amount, \'reward_income\''), 'add() 手工补发直接加到钱包余额');
assert(content.includes("$targetWallet = ($walletLog && $walletLog->type === 'commission') ? 'commission' : PartnerConfig::rewardWalletType();"), 'back() 手工撤销智能回滚对应钱包');
}
// ----------------------------------------------------
// 5. WalletLog.php 模型验证
// ----------------------------------------------------
console.log('\n--- 5. WalletLog.php 模型事件映射验证 ---');
const walletLogPath = path.resolve(__dirname, '../../application/admin/model/shopro/user/WalletLog.php');
assert(fs.existsSync(walletLogPath), 'WalletLog.php 文件存在');
if (fs.existsSync(walletLogPath)) {
const content = fs.readFileSync(walletLogPath, 'utf-8');
assert(content.includes("'push3' => '推荐提成'"), 'money 事件映射包含 push3 (推荐提成)');
assert(content.includes("'maker_diff' => '创客进阶奖'"), 'money 事件映射包含 maker_diff (创客进阶奖)');
assert(content.includes("'maker_peer' => '创客协同奖'"), 'money 事件映射包含 maker_peer (创客协同奖)');
assert(content.includes("'maker_overtaken' => '创客卓越奖'"), 'money 事件映射包含 maker_overtaken (创客卓越奖)');
assert(content.includes("'team_venue' => '酒道馆管理奖'"), 'money 事件映射包含 team_venue (酒道馆管理奖)');
assert(content.includes("'team_county' => '区县管理奖'"), 'money 事件映射包含 team_county (区县管理奖)');
assert(content.includes("'venue_peer' => '酒道馆平级奖'"), 'money 事件映射包含 venue_peer (酒道馆平级奖)');
assert(content.includes("'county_peer' => '区县代平级奖'"), 'money 事件映射包含 county_peer (区县代平级奖)');
assert(content.includes("'venue_share' => '酒道馆加权奖'"), 'money 事件映射包含 venue_share (酒道馆加权奖)');
assert(content.includes("'county_share' => '区县加权奖'"), 'money 事件映射包含 county_share (区县加权奖)');
assert(content.includes("'reward_income' => '收益分成-入账'"), 'money 事件映射包含 reward_income (收益分成-入账)');
assert(content.includes("'reward_back' => '收益分成-退还'"), 'money 事件映射包含 reward_back (收益分成-退还)');
assert(content.includes('PartnerConfig::rewardTypeMap()'), 'WalletLog 支持联动 PartnerConfig 动态解析文案');
}
// ----------------------------------------------------
// 6. 前端视图 account-info.vue 验证
// ----------------------------------------------------
console.log('\n--- 6. 前端视图 account-info.vue 验证 ---');
const accountInfoPath = path.resolve(__dirname, '../../uniapp/pages/commission/components/account-info.vue');
assert(fs.existsSync(accountInfoPath), 'account-info.vue 文件存在');
if (fs.existsSync(accountInfoPath)) {
const content = fs.readFileSync(accountInfoPath, 'utf-8');
assert(content.includes('<view class="item-title">钱包余额(元)</view>'), '卡片标签已变更为「钱包余额(元)」');
assert(content.includes("userInfo?.money || '0.00'"), '读取 userInfo.money 钱包余额');
assert(content.includes("sheep.$router.go('/pages/user/wallet/money')"), '点击钱包余额卡片直达 /pages/user/wallet/money');
assert(content.includes("sheep.$router.go('/pages/commission/rewards')"), '查看明细直达收益明细');
}
// ----------------------------------------------------
// 7. 模拟行为逻辑断言
// ----------------------------------------------------
console.log('\n--- 7. 模拟算法与资金业务流断言 ---');
// 模拟场景 A: 默认配置读取
function mockGetWalletType(config) {
const val = config['partner_reward_wallet'] || 'money';
return ['money', 'commission'].includes(val) ? val : 'money';
}
assert(mockGetWalletType({}) === 'money', '未配置 partner_reward_wallet 时,默认返回 money');
assert(mockGetWalletType({ partner_reward_wallet: 'commission' }) === 'commission', '配置 commission 时,支持切换为 commission');
assert(mockGetWalletType({ partner_reward_wallet: 'invalid_type' }) === 'money', '配置非法类型时,安全回退至 money');
// 模拟场景 B: 收益入账模拟
function mockAccountReward(user, agent, amount, event) {
const walletType = mockGetWalletType({});
const beforeMoney = user[walletType];
user[walletType] = (parseFloat(user[walletType]) + amount).toFixed(2);
agent.total_income = (parseFloat(agent.total_income) + amount).toFixed(2);
const walletLog = {
user_id: user.id,
type: walletType,
amount: amount,
before: beforeMoney,
after: user[walletType],
event: event,
};
return walletLog;
}
const mockUser = { id: 10, money: '100.00', commission: '0.00' };
const mockAgent = { user_id: 10, total_income: '500.00' };
const log1 = mockAccountReward(mockUser, mockAgent, 398.00, 'push3');
assert(mockUser.money === '498.00', '模拟即时奖入账:用户钱包余额从 100 增至 498.00');
assert(mockUser.commission === '0.00', '模拟即时奖入账:佣金钱包维持 0.00 未动');
assert(mockAgent.total_income === '898.00', '模拟即时奖入账:分销商累计收益从 500 累加至 898.00');
assert(log1.type === 'money' && log1.event === 'push3', '产生正确的 money 钱包变更流水');
// 模拟场景 C: 退款智能回滚判断
function mockDetermineRefundWallet(walletLogs, rewardId) {
const found = walletLogs.find(l => l.partner_reward_id === rewardId);
if (found && found.type === 'commission') {
return 'commission';
}
return 'money';
}
const testLogs = [
{ id: 1, type: 'commission', partner_reward_id: 101 }, // 历史老订单
{ id: 2, type: 'money', partner_reward_id: 102 }, // 新订单
];
assert(mockDetermineRefundWallet(testLogs, 101) === 'commission', '历史老订单收益退款:精准定位回滚 commission 钱包');
assert(mockDetermineRefundWallet(testLogs, 102) === 'money', '新订单收益退款:精准定位回滚 money 钱包');
assert(mockDetermineRefundWallet(testLogs, 999) === 'money', '无流水记录极端情况:默认回退扣减当前生效的 money 钱包');
console.log('\n========================================================');
console.log(`测试执行结果: 通过 ${passCount} 项, 失败 ${failCount} 项`);
console.log('========================================================');
if (failCount > 0) {
process.exit(1);
}