281 lines
9.0 KiB
JavaScript
281 lines
9.0 KiB
JavaScript
/**
|
|
* 静态页面生成器测试脚本
|
|
*/
|
|
|
|
import { generateAllPages, generatePage } from './generateStaticPages.js';
|
|
import { generateStaticHTML } from './pageTemplates.js';
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
|
|
console.log('='.repeat(60));
|
|
console.log(' 天远查 - 静态页面生成器测试');
|
|
console.log('='.repeat(60));
|
|
console.log();
|
|
|
|
// 测试结果统计
|
|
let passedTests = 0;
|
|
let failedTests = 0;
|
|
|
|
// 测试函数
|
|
function test(name, fn) {
|
|
try {
|
|
fn();
|
|
console.log(`✅ ${name}`);
|
|
passedTests++;
|
|
} catch (error) {
|
|
console.log(`❌ ${name}`);
|
|
console.log(` 错误: ${error.message}`);
|
|
failedTests++;
|
|
}
|
|
}
|
|
|
|
// 断言函数
|
|
function assert(condition, message) {
|
|
if (!condition) {
|
|
throw new Error(message || '断言失败');
|
|
}
|
|
}
|
|
|
|
console.log('🧪 开始测试...\n');
|
|
|
|
// 测试 1: 测试基本的 HTML 生成
|
|
test('测试基本的 HTML 生成', () => {
|
|
const seoConfig = {
|
|
title: '测试标题',
|
|
description: '测试描述',
|
|
keywords: '测试关键词'
|
|
};
|
|
|
|
const html = generateStaticHTML(seoConfig, { path: '/test' });
|
|
|
|
assert(html.includes('<!DOCTYPE html>'), '应该包含 DOCTYPE 声明');
|
|
assert(html.includes('测试标题'), '应该包含标题');
|
|
assert(html.includes('测试描述'), '应该包含描述');
|
|
assert(html.includes('测试关键词'), '应该包含关键词');
|
|
assert(html.includes('og:title'), '应该包含 Open Graph 标签');
|
|
assert(html.includes('twitter:card'), '应该包含 Twitter Cards 标签');
|
|
assert(html.includes('application/ld+json'), '应该包含结构化数据');
|
|
});
|
|
|
|
// 测试 2: 测试默认内容生成
|
|
test('测试默认内容生成', () => {
|
|
const seoConfig = {
|
|
title: '测试页面',
|
|
description: '这是一个测试页面',
|
|
keywords: '测试'
|
|
};
|
|
|
|
const html = generateStaticHTML(seoConfig, { path: '/test' });
|
|
|
|
assert(html.includes('关于测试页面'), '应该包含关于标题');
|
|
assert(html.includes('核心功能'), '应该包含核心功能');
|
|
assert(html.includes('为什么选择天远查?'), '应该包含选择理由');
|
|
assert(html.includes('应用场景'), '应该包含应用场景');
|
|
});
|
|
|
|
// 测试 3: 测试路由内容生成
|
|
test('测试路由内容生成', () => {
|
|
const seoConfig = {
|
|
title: '司法涉诉核验',
|
|
description: '司法风险检测中心',
|
|
keywords: '司法,诉讼'
|
|
};
|
|
|
|
const html = generateStaticHTML(seoConfig, {
|
|
path: '/inquire/category/lawsuit',
|
|
content: generateContentByRoute('/inquire/category/lawsuit', seoConfig)
|
|
});
|
|
|
|
assert(html.includes('司法涉诉核验服务'), '应该包含司法涉诉标题');
|
|
assert(html.includes('开庭公告查询'), '应该包含开庭公告');
|
|
assert(html.includes('裁判文书查询'), '应该包含裁判文书');
|
|
});
|
|
|
|
// 测试 4: 测试文件名生成
|
|
test('测试文件名生成', () => {
|
|
const paths = [
|
|
{ input: '/', expected: 'index.html' },
|
|
{ input: '/help', expected: 'help.html' },
|
|
{ input: '/inquire/category/lawsuit', expected: 'inquire-category-lawsuit.html' },
|
|
{ input: '/agent/promote', expected: 'agent-promote.html' }
|
|
];
|
|
|
|
paths.forEach(({ input, expected }) => {
|
|
const filename = input
|
|
.replace(/^\//, '')
|
|
.replace(/\/+/g, '-')
|
|
.replace(/:/g, '-');
|
|
const result = filename ? `${filename}.html` : 'index.html';
|
|
|
|
assert(result === expected, `路径 ${input} 应该生成文件名 ${expected}`);
|
|
});
|
|
});
|
|
|
|
// 测试 5: 测试静态页面目录创建
|
|
test('测试静态页面目录创建', () => {
|
|
const outputDir = path.join(__dirname, '../static-pages-test');
|
|
|
|
// 清理测试目录(如果存在)
|
|
if (fs.existsSync(outputDir)) {
|
|
fs.rmSync(outputDir, { recursive: true, force: true });
|
|
}
|
|
|
|
// 创建目录
|
|
if (!fs.existsSync(outputDir)) {
|
|
fs.mkdirSync(outputDir, { recursive: true });
|
|
}
|
|
|
|
assert(fs.existsSync(outputDir), '应该创建输出目录');
|
|
|
|
// 清理测试目录
|
|
fs.rmSync(outputDir, { recursive: true, force: true });
|
|
});
|
|
|
|
// 测试 6: 测试单个页面生成
|
|
test('测试单个页面生成', () => {
|
|
const testDir = path.join(__dirname, '../static-pages-test');
|
|
|
|
// 清理测试目录(如果存在)
|
|
if (fs.existsSync(testDir)) {
|
|
fs.rmSync(testDir, { recursive: true, force: true });
|
|
}
|
|
|
|
fs.mkdirSync(testDir, { recursive: true });
|
|
|
|
const seoConfig = {
|
|
title: '测试页面',
|
|
description: '测试描述',
|
|
keywords: '测试'
|
|
};
|
|
|
|
const filepath = path.join(testDir, 'test.html');
|
|
|
|
// 临时修改 generatePage 的输出目录
|
|
const originalDir = path.join(__dirname, '../static-pages');
|
|
|
|
// 生成测试文件
|
|
const content = generateStaticHTML(seoConfig, { path: '/test' });
|
|
fs.writeFileSync(filepath, content, 'utf-8');
|
|
|
|
assert(fs.existsSync(filepath), '应该生成测试文件');
|
|
|
|
// 验证文件内容
|
|
const fileContent = fs.readFileSync(filepath, 'utf-8');
|
|
assert(fileContent.includes('测试页面'), '文件应该包含标题');
|
|
|
|
// 清理测试目录
|
|
fs.rmSync(testDir, { recursive: true, force: true });
|
|
});
|
|
|
|
// 测试 7: 测试 SEO 配置完整性
|
|
test('测试 SEO 配置完整性', () => {
|
|
const seoConfigs = {
|
|
'/': {
|
|
title: '天远查官网_企业与婚姻关联风险核验_综合履约背景核验',
|
|
description: '天远查官网(TianYuanCha)聚合官方公示数据...',
|
|
keywords: '天远查,婚姻状态风险, 配偶背景核验'
|
|
},
|
|
'/inquire/category/lawsuit': {
|
|
title: '司法涉诉核验_个人及企业法律诉讼记录_履约风险评估_天远查',
|
|
description: '天远查司法风险检测中心...',
|
|
keywords: '司法案件核验,法律诉讼记录'
|
|
}
|
|
};
|
|
|
|
for (const [path, config] of Object.entries(seoConfigs)) {
|
|
assert(config.title, `路由 ${path} 应该有 title`);
|
|
assert(config.description, `路由 ${path} 应该有 description`);
|
|
assert(config.keywords, `路由 ${path} 应该有 keywords`);
|
|
}
|
|
});
|
|
|
|
// 测试 8: 测试 Open Graph 标签
|
|
test('测试 Open Graph 标签', () => {
|
|
const seoConfig = {
|
|
title: '测试标题',
|
|
description: '测试描述',
|
|
keywords: '测试'
|
|
};
|
|
|
|
const html = generateStaticHTML(seoConfig, { path: '/test' });
|
|
|
|
assert(html.includes('og:type'), '应该包含 og:type');
|
|
assert(html.includes('og:url'), '应该包含 og:url');
|
|
assert(html.includes('og:title'), '应该包含 og:title');
|
|
assert(html.includes('og:description'), '应该包含 og:description');
|
|
assert(html.includes('og:site_name'), '应该包含 og:site_name');
|
|
assert(html.includes('og:locale'), '应该包含 og:locale');
|
|
});
|
|
|
|
// 测试 9: 测试结构化数据
|
|
test('测试结构化数据', () => {
|
|
const seoConfig = {
|
|
title: '测试标题',
|
|
description: '测试描述',
|
|
keywords: '测试'
|
|
};
|
|
|
|
const html = generateStaticHTML(seoConfig, { path: '/test' });
|
|
|
|
assert(html.includes('@context'), '应该包含 @context');
|
|
assert(html.includes('@type'), '应该包含 @type');
|
|
assert(html.includes('WebPage'), '应该包含 WebPage 类型');
|
|
assert(html.includes('Organization'), '应该包含 Organization 类型');
|
|
});
|
|
|
|
// 测试 10: 测试 canonical URL
|
|
test('测试 canonical URL', () => {
|
|
const seoConfig = {
|
|
title: '测试标题',
|
|
description: '测试描述',
|
|
keywords: '测试'
|
|
};
|
|
|
|
const html = generateStaticHTML(seoConfig, {
|
|
path: '/test/page',
|
|
canonical: 'https://www.tianyuancha.cn'
|
|
});
|
|
|
|
assert(html.includes('https://www.tianyuancha.cn/test/page'), '应该包含完整的 canonical URL');
|
|
});
|
|
|
|
// 运行实际生成测试
|
|
console.log('\n🚀 运行实际生成测试...\n');
|
|
|
|
test('生成所有静态页面', () => {
|
|
try {
|
|
generateAllPages();
|
|
|
|
const staticDir = path.join(__dirname, '../static-pages');
|
|
assert(fs.existsSync(staticDir), '应该生成 static-pages 目录');
|
|
|
|
const files = fs.readdirSync(staticDir);
|
|
assert(files.length > 0, '应该生成至少一个文件');
|
|
assert(files.includes('index.html'), '应该生成 index.html');
|
|
assert(files.includes('sitemap.xml'), '应该生成 sitemap.xml');
|
|
assert(files.includes('robots.txt'), '应该生成 robots.txt');
|
|
|
|
console.log(` 生成了 ${files.length} 个文件`);
|
|
} catch (error) {
|
|
console.log(` 错误: ${error.message}`);
|
|
throw error;
|
|
}
|
|
});
|
|
|
|
// 输出测试结果
|
|
console.log('\n' + '='.repeat(60));
|
|
console.log(' 测试结果');
|
|
console.log('='.repeat(60));
|
|
console.log(`✅ 通过: ${passedTests}`);
|
|
console.log(`❌ 失败: ${failedTests}`);
|
|
console.log(`📊 总计: ${passedTests + failedTests}`);
|
|
console.log();
|
|
|
|
if (failedTests === 0) {
|
|
console.log('🎉 所有测试通过!');
|
|
process.exit(0);
|
|
} else {
|
|
console.log('⚠️ 部分测试失败,请检查错误信息');
|
|
process.exit(1);
|
|
}
|