#!/usr/bin/env node /** * 测试 dict-generator 修复 * 验证文件命名和重叠名问题 */ const DictGenerator = require('./generators/dict-generator'); class DictFixTester { constructor() { this.errors = []; this.passed = []; } async run() { console.log('🧪 测试 dict-generator 修复...\n'); // 测试1: 继承 BaseGenerator this.testInheritance(); // 测试2: 文件命名规范 this.testFileNaming(); // 测试3: 避免重叠名 this.testNoOverlappingNames(); // 输出结果 this.printResults(); } testInheritance() { console.log('📝 测试1: 继承 BaseGenerator'); const generator = new DictGenerator(); if (typeof generator.writeFile === 'function') { this.passed.push('DictGenerator 继承了 BaseGenerator.writeFile'); console.log(' ✅ 继承了 BaseGenerator.writeFile'); } else { this.errors.push('DictGenerator 未继承 BaseGenerator.writeFile'); console.log(' ❌ 未继承 BaseGenerator.writeFile'); } if (typeof generator.printStats === 'function') { this.passed.push('DictGenerator 继承了 BaseGenerator.printStats'); console.log(' ✅ 继承了 BaseGenerator.printStats'); } else { this.errors.push('DictGenerator 未继承 BaseGenerator.printStats'); console.log(' ❌ 未继承 BaseGenerator.printStats'); } if (generator.dryRun !== undefined) { this.passed.push('DictGenerator 支持 dry-run 模式'); console.log(' ✅ 支持 dry-run 模式'); } else { this.errors.push('DictGenerator 不支持 dry-run 模式'); console.log(' ❌ 不支持 dry-run 模式'); } } testFileNaming() { console.log('\n📝 测试2: 文件命名规范(kebab-case)'); const generator = new DictGenerator(); // 模拟生成内容并检查 const testCases = [ { input: 'Dict', expected: 'dict.enum.ts' }, { input: 'MemberLevel', expected: 'member-level.enum.ts' }, { input: 'PayChannel', expected: 'pay-channel.enum.ts' }, { input: 'dict_service', expected: 'dict-service.enum.ts' } ]; for (const testCase of testCases) { const kebabName = generator.toKebabCase(testCase.input); const fileName = `${kebabName}.enum.ts`; if (fileName === testCase.expected) { this.passed.push(`文件命名正确: ${testCase.input} → ${fileName}`); console.log(` ✅ ${testCase.input} → ${fileName}`); } else { this.errors.push(`文件命名错误: ${testCase.input} 应为 ${testCase.expected},实际为 ${fileName}`); console.log(` ❌ ${testCase.input} 应为 ${testCase.expected},实际为 ${fileName}`); } } } testNoOverlappingNames() { console.log('\n📝 测试3: 避免重叠名问题'); const generator = new DictGenerator(); const content = generator.generateDictContent('test', 'Dict'); // 检查1: 应该生成 DictEnum 而不是 DictDict if (content.includes('export enum DictEnum')) { this.passed.push('使用 DictEnum 而不是 DictDict'); console.log(' ✅ 使用 DictEnum(避免重叠名)'); } else if (content.includes('export enum DictDict')) { this.errors.push('错误使用了 DictDict(重叠名)'); console.log(' ❌ 错误使用了 DictDict(重叠名)'); } else { this.errors.push('未找到枚举定义'); console.log(' ❌ 未找到枚举定义'); } // 检查2: dictDict 变量名是合理的 if (content.includes('export const dictDict')) { this.passed.push('dictDict 变量名符合预期'); console.log(' ✅ dictDict 变量名符合预期'); } else { this.errors.push('dictDict 变量名不正确'); console.log(' ❌ dictDict 变量名不正确'); } // 检查3: 工具类命名 if (content.includes('export class DictEnumUtil')) { this.passed.push('工具类使用 DictEnumUtil'); console.log(' ✅ 工具类使用 DictEnumUtil'); } else { this.errors.push('工具类命名不正确'); console.log(' ❌ 工具类命名不正确'); } // 检查4: 文件名建议 console.log('\n 💡 推荐文件名模式:'); console.log(' - dict.enum.ts (kebab-case + .enum.ts 后缀)'); console.log(' - member-level.enum.ts'); console.log(' - pay-channel.enum.ts'); console.log('\n ❌ 禁止的文件名:'); console.log(' - DictDict.ts (PascalCase + 重叠名)'); console.log(' - Dict.ts (无后缀)'); } printResults() { console.log('\n\n📊 测试结果汇总'); console.log('='.repeat(60)); console.log(`\n✅ 通过项 (${this.passed.length}):`); this.passed.forEach(item => console.log(` - ${item}`)); if (this.errors.length > 0) { console.log(`\n❌ 错误项 (${this.errors.length}):`); this.errors.forEach(item => console.log(` - ${item}`)); } console.log('\n' + '='.repeat(60)); const totalChecks = this.passed.length + this.errors.length; const successRate = totalChecks > 0 ? ((this.passed.length / totalChecks) * 100).toFixed(2) : '0.00'; console.log(`📈 成功率: ${successRate}% (${this.passed.length}/${totalChecks})`); if (this.errors.length === 0) { console.log('\n🎉 dict-generator 修复验证通过!'); return true; } else { console.log(`\n💔 发现 ${this.errors.length} 个错误,需要修复`); return false; } } } // 运行测试 if (require.main === module) { const tester = new DictFixTester(); tester.run().then(passed => { process.exit(passed ? 0 : 1); }); } module.exports = DictFixTester;