export class TestLogger { private logs: LogEntry[] = []; private currentTest: string | null = null; private currentStep: string | null = null; startTest(testName: string): void { this.currentTest = testName; this.currentStep = null; this.log('info', `开始测试: ${testName}`); } endTest(testName: string, status: 'passed' | 'failed', error?: Error): void { this.log('info', `结束测试: ${testName} - ${status}`); if (error) { this.log('error', `测试失败: ${error.message}`, error); } this.currentTest = null; this.currentStep = null; } startStep(stepName: string): void { this.currentStep = stepName; this.log('info', ` 开始步骤: ${stepName}`); } endStep(stepName: string, status: 'passed' | 'failed', error?: Error): void { this.log('info', ` 结束步骤: ${stepName} - ${status}`); if (error) { this.log('error', ` 步骤失败: ${error.message}`, error); } this.currentStep = null; } debug(message: string): void { this.log('debug', message); } info(message: string): void { this.log('info', message); } warn(message: string): void { this.log('warn', message); } error(message: string, error?: Error): void { this.log('error', message, error); } success(message: string): void { this.log('info', `✅ ${message}`); } private log(level: LogLevel, message: string, error?: Error): void { const entry: LogEntry = { timestamp: new Date().toISOString(), level, message, test: this.currentTest, step: this.currentStep, error }; this.logs.push(entry); this.printLog(entry); } private printLog(entry: LogEntry): void { const timestamp = entry.timestamp.split('T')[1].split('.')[0]; const prefix = entry.step ? ` ${entry.step}` : entry.test || 'SYSTEM'; const levelIcon = { debug: '🔍', info: 'ℹ️', warn: '⚠️', error: '❌' }[entry.level]; console.log(`${timestamp} ${levelIcon} [${prefix}] ${entry.message}`); if (entry.error) { const errorMessage = entry.error.stack || entry.error.message || String(entry.error); console.error(errorMessage); } } getLogs(): LogEntry[] { return this.logs; } clearLogs(): void { this.logs = []; } exportLogs(): string { return JSON.stringify(this.logs, null, 2); } } export interface LogEntry { timestamp: string; level: LogLevel; message: string; test?: string | null; step?: string | null; error?: Error; } export type LogLevel = 'debug' | 'info' | 'warn' | 'error'; export const testLogger = new TestLogger();