/** * SEO 端到端检测脚本 * 模拟爬虫与普通用户请求,验证是否返回正确的页面 * * 使用前请先启动服务器: npm run start * 然后运行: npm run test 或 node test-seo.js */ const http = require('http') const https = require('https') const BASE_URL = process.env.SEO_TEST_URL || 'http://localhost:3000' // 要检测的路由及期望的 SEO 标题关键词(与 useSEO.js 一致,天远数据) const ROUTES = [ { path: '/', titleKeyword: '天远数据' }, { path: '/agent', titleKeyword: '天远数据代理' }, { path: '/help', titleKeyword: '天远数据帮助中心' }, { path: '/inquire/personalData', titleKeyword: '个人综合风险报告' }, { path: '/agent/promote', titleKeyword: '推广码' }, { path: '/historyQuery', titleKeyword: '我的报告' } ] function request(url, userAgent) { return new Promise((resolve, reject) => { const lib = url.startsWith('https') ? https : http const req = lib.get(url, { headers: { 'User-Agent': userAgent }, timeout: 10000 }, res => { const chunks = [] res.on('data', chunk => chunks.push(chunk)) res.on('end', () => { resolve({ statusCode: res.statusCode, headers: res.headers, body: Buffer.concat(chunks).toString('utf-8') }) }) }) req.on('error', reject) req.on('timeout', () => { req.destroy() reject(new Error('请求超时')) }) }) } function extractTitle(html) { const match = html.match(/]*>([^<]+)<\/title>/i) return match ? match[1].trim() : null } function hasMetaDescription(html) { return /