Files
张翔 08ea5fbe98 feat(admin): 添加用户管理相关文件
添加用户管理视图、API和状态管理文件
2026-03-28 14:37:29 +08:00

402 lines
11 KiB
TypeScript

import {
BaseReportGenerator,
TestReport,
ReportGeneratorOptions,
} from './base-report-generator';
export class HTMLReportGenerator extends BaseReportGenerator {
constructor(options: ReportGeneratorOptions) {
super(options);
}
getExtension(): string {
return 'html';
}
generate(report: TestReport): string {
const html = this.generateHTML(report);
return this.writeToFile(html);
}
private generateHTML(report: TestReport): string {
return `
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>${report.reportName} - 测试报告</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
padding: 20px;
}
.container {
max-width: 1200px;
margin: 0 auto;
background: white;
border-radius: 12px;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
overflow: hidden;
}
.header {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 40px;
text-align: center;
}
.header h1 {
font-size: 2.5em;
margin-bottom: 10px;
font-weight: 700;
}
.header .timestamp {
font-size: 1.1em;
opacity: 0.9;
}
.summary {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 20px;
padding: 40px;
background: #f8f9fa;
}
.summary-card {
background: white;
padding: 25px;
border-radius: 10px;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
text-align: center;
transition: transform 0.3s ease;
}
.summary-card:hover {
transform: translateY(-5px);
}
.summary-card .icon {
font-size: 2.5em;
margin-bottom: 10px;
}
.summary-card .value {
font-size: 2em;
font-weight: 700;
color: #333;
margin-bottom: 5px;
}
.summary-card .label {
color: #666;
font-size: 0.9em;
text-transform: uppercase;
letter-spacing: 1px;
}
.summary-card.passed .value { color: #28a745; }
.summary-card.failed .value { color: #dc3545; }
.summary-card.skipped .value { color: #ffc107; }
.summary-card.pass-rate .value { color: #17a2b8; }
.progress-bar {
width: 100%;
height: 30px;
background: #e9ecef;
border-radius: 15px;
overflow: hidden;
margin: 20px 0;
box-shadow: inset 0 2px 5px rgba(0, 0, 0, 0.1);
}
.progress-fill {
height: 100%;
background: linear-gradient(90deg, #28a745 0%, #20c997 100%);
transition: width 0.5s ease;
display: flex;
align-items: center;
justify-content: center;
color: white;
font-weight: 600;
}
.test-suites {
padding: 40px;
}
.test-suites h2 {
font-size: 1.8em;
margin-bottom: 20px;
color: #333;
}
.suite {
background: #f8f9fa;
border-radius: 10px;
margin-bottom: 20px;
overflow: hidden;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
}
.suite-header {
background: white;
padding: 20px;
border-bottom: 2px solid #e9ecef;
display: flex;
justify-content: space-between;
align-items: center;
}
.suite-header h3 {
font-size: 1.3em;
color: #333;
}
.suite-stats {
display: flex;
gap: 15px;
}
.suite-stats span {
padding: 5px 12px;
border-radius: 20px;
font-size: 0.9em;
font-weight: 600;
}
.suite-stats .passed { background: #d4edda; color: #155724; }
.suite-stats .failed { background: #f8d7da; color: #721c24; }
.suite-stats .skipped { background: #fff3cd; color: #856404; }
.test-list {
padding: 20px;
}
.test-item {
padding: 15px;
margin-bottom: 10px;
background: white;
border-radius: 8px;
display: flex;
justify-content: space-between;
align-items: center;
border-left: 4px solid #e9ecef;
transition: all 0.3s ease;
}
.test-item:hover {
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
transform: translateX(5px);
}
.test-item.passed { border-left-color: #28a745; }
.test-item.failed { border-left-color: #dc3545; }
.test-item.skipped { border-left-color: #ffc107; }
.test-name {
font-weight: 600;
color: #333;
flex: 1;
}
.test-duration {
color: #666;
font-size: 0.9em;
margin: 0 20px;
}
.test-status {
font-size: 1.5em;
}
.error-details {
background: #fff5f5;
padding: 15px;
margin: 10px 0;
border-radius: 8px;
border-left: 4px solid #dc3545;
font-family: 'Courier New', monospace;
font-size: 0.9em;
color: #721c24;
white-space: pre-wrap;
word-wrap: break-word;
}
.footer {
background: #f8f9fa;
padding: 20px;
text-align: center;
color: #666;
font-size: 0.9em;
border-top: 1px solid #e9ecef;
}
.environment-info {
padding: 20px 40px;
background: #f8f9fa;
border-top: 1px solid #e9ecef;
}
.environment-info h3 {
font-size: 1.2em;
margin-bottom: 10px;
color: #333;
}
.environment-info ul {
list-style: none;
display: flex;
gap: 20px;
flex-wrap: wrap;
}
.environment-info li {
background: white;
padding: 8px 15px;
border-radius: 5px;
font-size: 0.9em;
color: #666;
}
@media (max-width: 768px) {
.header h1 {
font-size: 1.8em;
}
.summary {
grid-template-columns: 1fr;
}
.suite-header {
flex-direction: column;
align-items: flex-start;
gap: 10px;
}
.test-item {
flex-direction: column;
align-items: flex-start;
gap: 10px;
}
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>${report.reportName}</h1>
<div class="timestamp">生成时间: ${this.formatTimestamp(report.timestamp)}</div>
</div>
<div class="summary">
<div class="summary-card">
<div class="icon">📊</div>
<div class="value">${report.totalTests}</div>
<div class="label">总测试数</div>
</div>
<div class="summary-card passed">
<div class="icon">✅</div>
<div class="value">${report.totalPassed}</div>
<div class="label">通过</div>
</div>
<div class="summary-card failed">
<div class="icon">❌</div>
<div class="value">${report.totalFailed}</div>
<div class="label">失败</div>
</div>
<div class="summary-card skipped">
<div class="icon">⏭️</div>
<div class="value">${report.totalSkipped}</div>
<div class="label">跳过</div>
</div>
<div class="summary-card pass-rate">
<div class="icon">📈</div>
<div class="value">${report.passRate}%</div>
<div class="label">通过率</div>
</div>
<div class="summary-card">
<div class="icon">⏱️</div>
<div class="value">${this.formatDuration(report.totalDuration)}</div>
<div class="label">总耗时</div>
</div>
</div>
<div style="padding: 0 40px;">
<div class="progress-bar">
<div class="progress-fill" style="width: ${report.passRate}%">
${report.passRate}%
</div>
</div>
</div>
<div class="environment-info">
<h3>环境信息</h3>
<ul>
<li>Node.js: ${report.environment.node}</li>
<li>平台: ${report.environment.platform}</li>
<li>CI环境: ${report.environment.ci ? '是' : '否'}</li>
</ul>
</div>
<div class="test-suites">
<h2>测试套件详情</h2>
${report.testSuites.map(suite => this.generateSuiteHTML(suite)).join('')}
</div>
<div class="footer">
<p>测试报告由自动化测试框架生成 | 报告ID: ${report.reportId}</p>
</div>
</div>
</body>
</html>
`.trim();
}
private generateSuiteHTML(suite: any): string {
return `
<div class="suite">
<div class="suite-header">
<h3>${suite.suiteName}</h3>
<div class="suite-stats">
<span class="passed">✅ ${suite.passed}</span>
<span class="failed">❌ ${suite.failed}</span>
<span class="skipped">⏭️ ${suite.skipped}</span>
<span>⏱️ ${this.formatDuration(suite.duration)}</span>
</div>
</div>
<div class="test-list">
${suite.tests.map((test: any) => this.generateTestHTML(test)).join('')}
</div>
</div>
`;
}
private generateTestHTML(test: any): string {
const errorDetails = test.status === 'failed' && test.error
? `<div class="error-details">${test.error}${test.stackTrace ? '\n\n' + test.stackTrace : ''}</div>`
: '';
return `
<div class="test-item ${test.status}">
<div class="test-name">${test.testName}</div>
<div class="test-duration">${this.formatDuration(test.duration)}</div>
<div class="test-status">${this.getStatusIcon(test.status)}</div>
</div>
${errorDetails}
`;
}
}