文件
Henglianshutong/tests/verify_agent_url_routing_spec.js

165 行
8.0 KiB
JavaScript

const fs = require('fs');
const path = require('path');
let passed = 0;
let failed = 0;
function assert(condition, testName, detail = '') {
if (condition) {
console.log(` [PASS] ${testName}`);
passed++;
} else {
console.error(` [FAIL] ${testName} - ${detail}`);
failed++;
}
}
console.log("========================================================");
console.log(" 分销商成员档案 URL 路由 & 参数回显自动化规范测试");
console.log("========================================================\n");
// 1. 验证 dashboard.js 中 onOpenAgent 修复
const dashboardJsPath = path.resolve(__dirname, '../public/assets/js/backend/shopro/dashboard.js');
assert(fs.existsSync(dashboardJsPath), 'dashboard.js 文件存在');
const dashboardContent = fs.readFileSync(dashboardJsPath, 'utf-8');
assert(!dashboardContent.includes('`shopro/commission/agent/index` + (level ? `&level=${level}` : \'\')'),
'dashboard.js 中已彻底移除错误的 &level= 拼接语法');
assert(dashboardContent.includes('`shopro/commission/agent/index` + (level !== undefined && level !== null && level !== \'\' ? `?level=${level}` : \'\')'),
'dashboard.js 中使用健壮的 ?level= 拼接参数');
// 模拟测试 onOpenAgent 各种参数行为
function mockOnOpenAgent(level) {
let captured = {};
function onOpenTab(url, title) {
captured = { url, title };
}
let url = `shopro/commission/agent/index` + (level !== undefined && level !== null && level !== '' ? `?level=${level}` : '');
let title = (level == 1) ? "品酒师会员" : "成员档案";
onOpenTab(url, title);
return captured;
}
const res1 = mockOnOpenAgent(1);
assert(res1.url === 'shopro/commission/agent/index?level=1' && res1.title === '品酒师会员',
'正常场景: level=1 生成正确的 query string ?level=1 且标题为品酒师会员');
const resEmpty = mockOnOpenAgent('');
assert(resEmpty.url === 'shopro/commission/agent/index' && resEmpty.title === '成员档案',
'边界场景: level="" 生成基础无参数 URL');
const resNull = mockOnOpenAgent(null);
assert(resNull.url === 'shopro/commission/agent/index' && resNull.title === '成员档案',
'边界场景: level=null 生成基础无参数 URL');
const resUndef = mockOnOpenAgent(undefined);
assert(resUndef.url === 'shopro/commission/agent/index' && resUndef.title === '成员档案',
'边界场景: level=undefined 生成基础无参数 URL');
// 2. 验证 agent.js URL 参数解析与初始化
const agentJsPath = path.resolve(__dirname, '../public/assets/js/backend/shopro/commission/agent.js');
assert(fs.existsSync(agentJsPath), 'agent.js 文件存在');
const agentContent = fs.readFileSync(agentJsPath, 'utf-8');
assert(agentContent.includes('is_partner: \'\''), 'agent.js filter.data 包含 is_partner');
assert(agentContent.includes('is_partner: {') && agentContent.includes('label: \'合伙股东\''), 'agent.js filter.tools 包含 is_partner 筛选工具');
assert(agentContent.includes('{ value: \'partner\', label: \'合伙股东\' }'), 'agent.js filter.tools.identity 选项中包含合伙股东');
assert(agentContent.includes('new URLSearchParams(location.search)'), 'agent.js onMounted 中使用 URLSearchParams 解析参数');
assert(agentContent.includes('params.get(\'level\')'), 'agent.js onMounted 读取 level 参数');
assert(agentContent.includes('params.get(\'is_venue\')'), 'agent.js onMounted 读取 is_venue 参数');
assert(agentContent.includes('params.get(\'is_county\')'), 'agent.js onMounted 读取 is_county 参数');
assert(agentContent.includes('params.get(\'is_partner\')'), 'agent.js onMounted 读取 is_partner 参数');
assert(agentContent.includes('params.get(\'identity\')'), 'agent.js onMounted 读取 identity 参数');
assert(agentContent.includes('params.get(\'user_id\')'), 'agent.js onMounted 读取 user_id 参数');
// 模拟测试 agent.js onMounted 参数解析与赋值
function mockAgentMount(searchString) {
const params = new URLSearchParams(searchString);
let state = {
filter: {
data: { level: '', is_venue: '', is_county: '', is_partner: '', identity: '', user: { field: 'user_id', value: '' } },
tools: {
level: { value: '' },
is_venue: { value: '' },
is_county: { value: '' },
is_partner: { value: '' },
identity: { value: '' },
user: { value: { field: 'user_id', value: '' } }
}
}
};
const levelParam = params.get('level');
if (levelParam !== null && levelParam !== '') {
const parsedLevel = !isNaN(Number(levelParam)) ? Number(levelParam) : levelParam;
state.filter.data.level = parsedLevel;
if (state.filter.tools && state.filter.tools.level) {
state.filter.tools.level.value = parsedLevel;
}
}
const isVenueParam = params.get('is_venue');
if (isVenueParam !== null && isVenueParam !== '') {
state.filter.data.is_venue = isVenueParam;
if (state.filter.tools && state.filter.tools.is_venue) {
state.filter.tools.is_venue.value = isVenueParam;
}
}
const isCountyParam = params.get('is_county');
if (isCountyParam !== null && isCountyParam !== '') {
state.filter.data.is_county = isCountyParam;
if (state.filter.tools && state.filter.tools.is_county) {
state.filter.tools.is_county.value = isCountyParam;
}
}
const isPartnerParam = params.get('is_partner');
if (isPartnerParam !== null && isPartnerParam !== '') {
state.filter.data.is_partner = isPartnerParam;
if (state.filter.tools && state.filter.tools.is_partner) {
state.filter.tools.is_partner.value = isPartnerParam;
}
}
const identityParam = params.get('identity');
if (identityParam !== null && identityParam !== '') {
state.filter.data.identity = identityParam;
if (state.filter.tools && state.filter.tools.identity) {
state.filter.tools.identity.value = identityParam;
}
}
const userIdParam = params.get('user_id');
if (userIdParam !== null && userIdParam !== '') {
state.filter.data.user = { field: 'user_id', value: userIdParam };
if (state.filter.tools && state.filter.tools.user) {
state.filter.tools.user.value = { field: 'user_id', value: userIdParam };
}
}
return state;
}
const mockState = mockAgentMount('?level=1&ref=addtabs');
assert(mockState.filter.data.level === 1 && mockState.filter.tools.level.value === 1,
'agent.js 正确从 ?level=1 解析数字等级 1 并同步至 filter.data 与 filter.tools');
const mockPartnerState = mockAgentMount('?is_partner=1');
assert(mockPartnerState.filter.data.is_partner === '1' && mockPartnerState.filter.tools.is_partner.value === '1',
'agent.js 正确从 ?is_partner=1 解析合伙股东标记');
// 3. 验证 AgentFilter.php 过滤能力
const filterPhpPath = path.resolve(__dirname, '../addons/shopro/filter/commission/AgentFilter.php');
assert(fs.existsSync(filterPhpPath), 'AgentFilter.php 文件存在');
const filterPhpContent = fs.readFileSync(filterPhpPath, 'utf-8');
assert(filterPhpContent.includes('public function level($query, $data)'), 'AgentFilter 显式定义 level 过滤方法');
assert(filterPhpContent.includes('public function isPartner($query, $data)'), 'AgentFilter 显式定义 isPartner 过滤方法');
assert(filterPhpContent.includes('public function isVenue($query, $data)'), 'AgentFilter 显式定义 isVenue 过滤方法');
assert(filterPhpContent.includes('public function isCounty($query, $data)'), 'AgentFilter 显式定义 isCounty 过滤方法');
assert(filterPhpContent.includes('if ($value === \'partner\') {') && filterPhpContent.includes('where(\'is_partner\', 1)'),
'AgentFilter identity 方法支持合伙股东 (partner)');
console.log("\n--------------------------------------------------------");
console.log(`测试执行结果: 通过 ${passed} 项, 失败 ${failed} 项`);
console.log("========================================================");
process.exit(failed === 0 ? 0 : 1);