feat(admin): 添加用户管理相关文件
添加用户管理视图、API和状态管理文件
This commit is contained in:
@@ -0,0 +1,155 @@
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
|
||||
export interface TestResult {
|
||||
testId: string;
|
||||
testName: string;
|
||||
status: 'passed' | 'failed' | 'skipped';
|
||||
duration: number;
|
||||
error?: string;
|
||||
stackTrace?: string;
|
||||
retries: number;
|
||||
timestamp: string;
|
||||
}
|
||||
|
||||
export interface TestSuite {
|
||||
suiteId: string;
|
||||
suiteName: string;
|
||||
tests: TestResult[];
|
||||
duration: number;
|
||||
passed: number;
|
||||
failed: number;
|
||||
skipped: number;
|
||||
timestamp: string;
|
||||
}
|
||||
|
||||
export interface TestReport {
|
||||
reportId: string;
|
||||
reportName: string;
|
||||
testSuites: TestSuite[];
|
||||
totalTests: number;
|
||||
totalPassed: number;
|
||||
totalFailed: number;
|
||||
totalSkipped: number;
|
||||
totalDuration: number;
|
||||
passRate: number;
|
||||
timestamp: string;
|
||||
environment: {
|
||||
node: string;
|
||||
platform: string;
|
||||
ci: boolean;
|
||||
};
|
||||
metadata?: Record<string, any>;
|
||||
}
|
||||
|
||||
export interface TrendData {
|
||||
date: string;
|
||||
totalTests: number;
|
||||
passed: number;
|
||||
failed: number;
|
||||
skipped: number;
|
||||
passRate: number;
|
||||
duration: number;
|
||||
}
|
||||
|
||||
export interface ReportGeneratorOptions {
|
||||
outputDir: string;
|
||||
reportName: string;
|
||||
includeTrend?: boolean;
|
||||
trendDataDays?: number;
|
||||
}
|
||||
|
||||
export abstract class BaseReportGenerator {
|
||||
protected outputDir: string;
|
||||
protected reportName: string;
|
||||
|
||||
constructor(options: ReportGeneratorOptions) {
|
||||
this.outputDir = options.outputDir;
|
||||
this.reportName = options.reportName;
|
||||
this.ensureOutputDir();
|
||||
}
|
||||
|
||||
protected ensureOutputDir(): void {
|
||||
if (!fs.existsSync(this.outputDir)) {
|
||||
fs.mkdirSync(this.outputDir, { recursive: true });
|
||||
}
|
||||
}
|
||||
|
||||
abstract generate(report: TestReport): string;
|
||||
|
||||
abstract getExtension(): string;
|
||||
|
||||
protected getOutputPath(): string {
|
||||
return path.join(this.outputDir, `${this.reportName}.${this.getExtension()}`);
|
||||
}
|
||||
|
||||
protected writeToFile(content: string): string {
|
||||
const outputPath = this.getOutputPath();
|
||||
fs.writeFileSync(outputPath, content, 'utf-8');
|
||||
return outputPath;
|
||||
}
|
||||
|
||||
protected formatDuration(ms: number): string {
|
||||
if (ms < 1000) {
|
||||
return `${ms}ms`;
|
||||
} else if (ms < 60000) {
|
||||
return `${(ms / 1000).toFixed(2)}s`;
|
||||
} else {
|
||||
const minutes = Math.floor(ms / 60000);
|
||||
const seconds = ((ms % 60000) / 1000).toFixed(0);
|
||||
return `${minutes}m ${seconds}s`;
|
||||
}
|
||||
}
|
||||
|
||||
protected formatTimestamp(timestamp: string): string {
|
||||
const date = new Date(timestamp);
|
||||
return date.toLocaleString('zh-CN', {
|
||||
year: 'numeric',
|
||||
month: '2-digit',
|
||||
day: '2-digit',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
second: '2-digit',
|
||||
});
|
||||
}
|
||||
|
||||
protected calculatePassRate(passed: number, total: number): number {
|
||||
if (total === 0) return 0;
|
||||
return Math.round((passed / total) * 100 * 100) / 100;
|
||||
}
|
||||
|
||||
protected getStatusIcon(status: string): string {
|
||||
switch (status) {
|
||||
case 'passed':
|
||||
return '✅';
|
||||
case 'failed':
|
||||
return '❌';
|
||||
case 'skipped':
|
||||
return '⏭️';
|
||||
default:
|
||||
return '❓';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class ReportGeneratorFactory {
|
||||
static create(
|
||||
type: 'html' | 'json' | 'junit',
|
||||
options: ReportGeneratorOptions
|
||||
): BaseReportGenerator {
|
||||
switch (type) {
|
||||
case 'html':
|
||||
return new HTMLReportGenerator(options);
|
||||
case 'json':
|
||||
return new JSONReportGenerator(options);
|
||||
case 'junit':
|
||||
return new JUnitReportGenerator(options);
|
||||
default:
|
||||
throw new Error(`Unsupported report type: ${type}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
import { HTMLReportGenerator } from './html-report-generator';
|
||||
import { JSONReportGenerator } from './json-report-generator';
|
||||
import { JUnitReportGenerator } from './junit-report-generator';
|
||||
@@ -0,0 +1,401 @@
|
||||
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}
|
||||
`;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import {
|
||||
BaseReportGenerator,
|
||||
TestReport,
|
||||
ReportGeneratorOptions,
|
||||
} from './base-report-generator';
|
||||
|
||||
export class JSONReportGenerator extends BaseReportGenerator {
|
||||
constructor(options: ReportGeneratorOptions) {
|
||||
super(options);
|
||||
}
|
||||
|
||||
getExtension(): string {
|
||||
return 'json';
|
||||
}
|
||||
|
||||
generate(report: TestReport): string {
|
||||
const json = JSON.stringify(report, null, 2);
|
||||
return this.writeToFile(json);
|
||||
}
|
||||
|
||||
generateCompact(report: TestReport): string {
|
||||
const json = JSON.stringify(report);
|
||||
return this.writeToFile(json);
|
||||
}
|
||||
|
||||
generateWithMetadata(report: TestReport, metadata: Record<string, any>): string {
|
||||
const reportWithMetadata = {
|
||||
...report,
|
||||
metadata,
|
||||
};
|
||||
const json = JSON.stringify(reportWithMetadata, null, 2);
|
||||
return this.writeToFile(json);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
import {
|
||||
BaseReportGenerator,
|
||||
TestReport,
|
||||
ReportGeneratorOptions,
|
||||
} from './base-report-generator';
|
||||
|
||||
export class JUnitReportGenerator extends BaseReportGenerator {
|
||||
constructor(options: ReportGeneratorOptions) {
|
||||
super(options);
|
||||
}
|
||||
|
||||
getExtension(): string {
|
||||
return 'xml';
|
||||
}
|
||||
|
||||
generate(report: TestReport): string {
|
||||
const xml = this.generateJUnitXML(report);
|
||||
return this.writeToFile(xml);
|
||||
}
|
||||
|
||||
private generateJUnitXML(report: TestReport): string {
|
||||
const xmlHeader = '<?xml version="1.0" encoding="UTF-8"?>\n';
|
||||
const testsuites = this.generateTestSuitesXML(report);
|
||||
return xmlHeader + testsuites;
|
||||
}
|
||||
|
||||
private generateTestSuitesXML(report: TestReport): string {
|
||||
const testsuites = report.testSuites
|
||||
.map(suite => this.generateTestSuiteXML(suite))
|
||||
.join('\n');
|
||||
|
||||
return `<testsuites name="${this.escapeXML(report.reportName)}" tests="${report.totalTests}" failures="${report.totalFailed}" skipped="${report.totalSkipped}" time="${report.totalDuration / 1000}" timestamp="${report.timestamp}">
|
||||
${testsuites}
|
||||
</testsuites>`;
|
||||
}
|
||||
|
||||
private generateTestSuiteXML(suite: any): string {
|
||||
const testcases = suite.tests
|
||||
.map((test: any) => this.generateTestCaseXML(test))
|
||||
.join('\n');
|
||||
|
||||
const failedTests = suite.tests.filter((t: any) => t.status === 'failed');
|
||||
|
||||
return ` <testsuite name="${this.escapeXML(suite.suiteName)}" tests="${suite.tests.length}" failures="${suite.failed}" skipped="${suite.skipped}" time="${suite.duration / 1000}" timestamp="${suite.timestamp}">
|
||||
${testcases}
|
||||
</testsuite>`;
|
||||
}
|
||||
|
||||
private generateTestCaseXML(test: any): string {
|
||||
const testcase = ` <testcase name="${this.escapeXML(test.testName)}" classname="${this.escapeXML(test.testId)}" time="${test.duration / 1000}"`;
|
||||
|
||||
if (test.status === 'skipped') {
|
||||
return `${testcase}>
|
||||
<skipped/>
|
||||
</testcase>`;
|
||||
}
|
||||
|
||||
if (test.status === 'failed') {
|
||||
const failureMessage = test.error
|
||||
? this.escapeXML(test.error)
|
||||
: 'Test failed';
|
||||
const failureDetails = test.stackTrace
|
||||
? this.escapeXML(test.stackTrace)
|
||||
: '';
|
||||
|
||||
return `${testcase}>
|
||||
<failure message="${failureMessage}">${failureDetails}</failure>
|
||||
</testcase>`;
|
||||
}
|
||||
|
||||
return `${testcase}/>`;
|
||||
}
|
||||
|
||||
private escapeXML(str: string): string {
|
||||
return str
|
||||
.replace(/&/g, '&')
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>')
|
||||
.replace(/"/g, '"')
|
||||
.replace(/'/g, ''');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user