const { execSync } = require('child_process'); 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(" 商品添加/编辑在页面选项卡(Tab)中打开功能自动化验证"); console.log("========================================================\n"); // 1. 本地代码文件逻辑检查 console.log("一、本地前端代码检查:"); const goodsJsPath = path.resolve(__dirname, '../../public/assets/js/backend/shopro/goods/goods.js'); const goodsJsContent = fs.readFileSync(goodsJsPath, 'utf-8'); assert(goodsJsContent.includes("function openTab("), 'goods.js 封装了统一的 openTab 方法'); assert(goodsJsContent.includes("top.Backend.api.addtabs(url, title, icon)"), 'openTab 优先调用 top.Backend.api.addtabs 选项卡方法'); assert(!goodsJsContent.includes('Fast.api.open("shopro/goods/goods/add?type=add"'), 'onAdd 不再通过 Fast.api.open 弹窗打开'); assert(!goodsJsContent.includes('Fast.api.open(`shopro/goods/goods/edit?type=edit'), 'onEdit 不再通过 Fast.api.open 弹窗打开'); assert(!goodsJsContent.includes('Fast.api.open(`shopro/goods/goods/add?type=copy'), 'onCopy 不再通过 Fast.api.open 弹窗打开'); assert(goodsJsContent.includes('openTab("shopro/goods/goods/add?type=add&ref=addtabs"'), 'onAdd 采用 openTab 打开添加页面'); assert(goodsJsContent.includes('openTab(`shopro/goods/goods/edit/id/${id}?type=edit&id=${id}&ref=addtabs`'), 'onEdit 采用 openTab 打开编辑页面'); assert(goodsJsContent.includes('openTab(`shopro/goods/goods/add?type=copy&id=${id}&ref=addtabs`'), 'onCopy 采用 openTab 打开复制添加页面'); assert(goodsJsContent.includes('top.Backend.api.closetabs()'), '表单保存后支持自动关闭当前 Tab 选项卡'); assert(goodsJsContent.includes('function onCancel()'), '表单模块实现并导出了 onCancel 取消方法'); const addHtmlPath = path.resolve(__dirname, '../../application/admin/view/shopro/goods/goods/add.html'); const addHtmlContent = fs.readFileSync(addHtmlPath, 'utf-8'); assert(addHtmlContent.includes('@click="onCancel"'), 'add.html 底部包含绑定 @click="onCancel" 的取消按钮'); // 2. 线上服务器部署一致性验证 console.log("\n二、线上服务器部署一致性检查:"); try { const remoteCheckCmd = `sshpass -p 'Bjsslxz1' ssh -o StrictHostKeyChecking=no root@118.184.172.94 " grep -c 'openTab' /www/wwwroot/henglian.gynhc.com/public/assets/js/backend/shopro/goods/goods.js grep -c 'onCancel' /www/wwwroot/henglian.gynhc.com/application/admin/view/shopro/goods/goods/add.html "`; const remoteOutput = execSync(remoteCheckCmd, { encoding: 'utf-8' }); const lines = remoteOutput.trim().split('\n'); const openTabMatches = parseInt(lines[lines.length - 2] || '0', 10); const onCancelMatches = parseInt(lines[lines.length - 1] || '0', 10); assert(openTabMatches >= 4, `线上 goods.js 已生效 openTab 逻辑 (匹配数: ${openTabMatches})`); assert(onCancelMatches >= 1, `线上 add.html 已生效 onCancel 取消按钮 (匹配数: ${onCancelMatches})`); } catch (err) { assert(false, '线上部署一致性检查', err.message); } // 3. 线上添加与编辑页面模板内容检查 console.log("\n三、线上模板状态验证:"); try { const remoteRenderCmd = `sshpass -p 'Bjsslxz1' ssh -o StrictHostKeyChecking=no root@118.184.172.94 "grep 'onCancel' /www/wwwroot/henglian.gynhc.com/application/admin/view/shopro/goods/goods/add.html"`; const renderOutput = execSync(remoteRenderCmd, { encoding: 'utf-8' }); assert(renderOutput.includes('onCancel'), '线上商品添加/编辑模板包含 onCancel 取消按钮'); } catch (err) { assert(false, '线上模板状态验证', err.message); } console.log("\n========================================================"); console.log(` 测试完成:通过 ${passed} 项,失败 ${failed} 项`); console.log("========================================================"); if (failed > 0) { process.exit(1); } else { process.exit(0); }