|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * generate-test.js |
| 4 | + * AI 삼위일체 테스트 자동 생성 스크립트 |
| 5 | + * |
| 6 | + * Usage: |
| 7 | + * node scripts/generate-test.js <moduleName> <category> |
| 8 | + * |
| 9 | + * Example: |
| 10 | + * node scripts/generate-test.js myModule core |
| 11 | + */ |
| 12 | + |
| 13 | +const fs = require('fs'); |
| 14 | +const path = require('path'); |
| 15 | + |
| 16 | +// ANSI 색상 |
| 17 | +const colors = { |
| 18 | + reset: '\x1b[0m', |
| 19 | + bright: '\x1b[1m', |
| 20 | + green: '\x1b[32m', |
| 21 | + yellow: '\x1b[33m', |
| 22 | + blue: '\x1b[34m', |
| 23 | + magenta: '\x1b[35m', |
| 24 | + cyan: '\x1b[36m', |
| 25 | +}; |
| 26 | + |
| 27 | +async function generateTest() { |
| 28 | + console.log('\n' + colors.bright + colors.cyan + '🤖 AI 삼위일체 테스트 생성기' + colors.reset + '\n'); |
| 29 | + |
| 30 | + // 커맨드 라인 인자 파싱 |
| 31 | + const args = process.argv.slice(2); |
| 32 | + |
| 33 | + if (args.length < 2) { |
| 34 | + printUsage(); |
| 35 | + process.exit(1); |
| 36 | + } |
| 37 | + |
| 38 | + const moduleName = args[0]; |
| 39 | + const category = args[1]; // core, services, handlers |
| 40 | + const description = args[2] || ''; |
| 41 | + |
| 42 | + // 유효성 검사 |
| 43 | + if (!['core', 'services', 'handlers'].includes(category)) { |
| 44 | + console.log(colors.yellow + '❌ Category must be: core, services, or handlers' + colors.reset); |
| 45 | + process.exit(1); |
| 46 | + } |
| 47 | + |
| 48 | + console.log(colors.blue + '📝 Module Information:' + colors.reset); |
| 49 | + console.log(` Name: ${colors.bright}${moduleName}${colors.reset}`); |
| 50 | + console.log(` Category: ${colors.bright}${category}${colors.reset}`); |
| 51 | + console.log(` Description: ${description || 'Auto-generated'}` + '\n'); |
| 52 | + |
| 53 | + // 파일 경로 생성 |
| 54 | + const testPath = path.join(__dirname, `../tests/${category}/${moduleName}.test.js`); |
| 55 | + const srcPath = path.join(__dirname, `../src/${category}/${moduleName}.js`); |
| 56 | + |
| 57 | + // 이미 파일이 존재하는지 확인 |
| 58 | + if (fs.existsSync(testPath)) { |
| 59 | + console.log(colors.yellow + `⚠️ Test file already exists: ${testPath}` + colors.reset); |
| 60 | + console.log(colors.yellow + 'Overwrite? (y/N): ' + colors.reset); |
| 61 | + // 간단하게 진행 |
| 62 | + } |
| 63 | + |
| 64 | + console.log(colors.green + '\n🧠 Step 1: Generating BDD Specification...' + colors.reset); |
| 65 | + console.log(colors.cyan + ' (Gemini would analyze requirements here)' + colors.reset); |
| 66 | + |
| 67 | + // BDD 템플릿 생성 |
| 68 | + const bddTemplate = generateBDDTemplate(moduleName, category); |
| 69 | + console.log(colors.green + '✅ BDD specification generated' + colors.reset); |
| 70 | + |
| 71 | + console.log(colors.green + '\n⚙️ Step 2: Generating Jest Test Code...' + colors.reset); |
| 72 | + console.log(colors.cyan + ' (Qwen would generate test code here)' + colors.reset); |
| 73 | + |
| 74 | + // Jest 테스트 템플릿 생성 |
| 75 | + const testCode = generateJestTemplate(moduleName, category, bddTemplate); |
| 76 | + console.log(colors.green + '✅ Jest test code generated' + colors.reset); |
| 77 | + |
| 78 | + console.log(colors.green + '\n💾 Step 3: Saving Files...' + colors.reset); |
| 79 | + |
| 80 | + // 테스트 파일 저장 |
| 81 | + fs.writeFileSync(testPath, testCode); |
| 82 | + console.log(colors.green + `✅ Test saved: ${testPath}` + colors.reset); |
| 83 | + |
| 84 | + // 소스 파일 템플릿 생성 (아직 없는 경우) |
| 85 | + if (!fs.existsSync(srcPath)) { |
| 86 | + const srcTemplate = generateSourceTemplate(moduleName, category); |
| 87 | + fs.writeFileSync(srcPath, srcTemplate); |
| 88 | + console.log(colors.green + `✅ Source template saved: ${srcPath}` + colors.reset); |
| 89 | + } |
| 90 | + |
| 91 | + console.log(colors.bright + colors.magenta + '\n🎉 Test generation complete!' + colors.reset); |
| 92 | + console.log(colors.cyan + '\nNext steps:' + colors.reset); |
| 93 | + console.log(` 1. Review the generated test: ${testPath}`); |
| 94 | + console.log(` 2. Run: ${colors.bright}npm test ${testPath}${colors.reset}`); |
| 95 | + console.log(` 3. Implement: ${srcPath}`); |
| 96 | + console.log(` 4. Follow TDD cycle: 🔴 RED → 🟢 GREEN → 🔵 REFACTOR\n`); |
| 97 | +} |
| 98 | + |
| 99 | +function printUsage() { |
| 100 | + console.log(colors.bright + 'Usage:' + colors.reset); |
| 101 | + console.log(` node scripts/generate-test.js <moduleName> <category> [description]\n`); |
| 102 | + console.log(colors.bright + 'Arguments:' + colors.reset); |
| 103 | + console.log(` moduleName - Name of the module (e.g., myHandler)`); |
| 104 | + console.log(` category - core | services | handlers`); |
| 105 | + console.log(` description - Optional module description\n`); |
| 106 | + console.log(colors.bright + 'Example:' + colors.reset); |
| 107 | + console.log(` node scripts/generate-test.js authHandler handlers "Authentication handler"\n`); |
| 108 | +} |
| 109 | + |
| 110 | +function generateBDDTemplate(moduleName, category) { |
| 111 | + return ` |
| 112 | +BDD Specification for ${moduleName}: |
| 113 | +
|
| 114 | +Scenario 1: Basic functionality |
| 115 | + Given: Module is initialized |
| 116 | + When: Primary function is called |
| 117 | + Then: Expected result is returned |
| 118 | +
|
| 119 | +Scenario 2: Error handling |
| 120 | + Given: Invalid input |
| 121 | + When: Function is called |
| 122 | + Then: Error is thrown |
| 123 | +
|
| 124 | +Scenario 3: Edge case |
| 125 | + Given: Boundary condition |
| 126 | + When: Function is called |
| 127 | + Then: Handles gracefully |
| 128 | +`; |
| 129 | +} |
| 130 | + |
| 131 | +function generateJestTemplate(moduleName, category, bddSpec) { |
| 132 | + const className = moduleName.charAt(0).toUpperCase() + moduleName.slice(1); |
| 133 | + |
| 134 | + return `// tests/${category}/${moduleName}.test.js |
| 135 | +// Auto-generated by generate-test.js |
| 136 | +
|
| 137 | +const { ${className} } = require('../../src/${category}/${moduleName}'); |
| 138 | +
|
| 139 | +describe('${className}', () => { |
| 140 | + let instance; |
| 141 | +
|
| 142 | + beforeEach(() => { |
| 143 | + instance = new ${className}(); |
| 144 | + }); |
| 145 | +
|
| 146 | + describe('Basic functionality', () => { |
| 147 | + test('should initialize correctly', () => { |
| 148 | + expect(instance).toBeDefined(); |
| 149 | + }); |
| 150 | +
|
| 151 | + test('should perform primary operation', () => { |
| 152 | + // TODO: Implement based on BDD specification |
| 153 | + // Given: Module is initialized |
| 154 | + // When: Primary function is called |
| 155 | + // Then: Expected result is returned |
| 156 | +
|
| 157 | + // Example: |
| 158 | + // const result = instance.execute(); |
| 159 | + // expect(result).toBeDefined(); |
| 160 | + }); |
| 161 | + }); |
| 162 | +
|
| 163 | + describe('Error handling', () => { |
| 164 | + test('should throw error on invalid input', () => { |
| 165 | + // TODO: Implement error handling test |
| 166 | + expect(() => { |
| 167 | + // instance.execute(null); |
| 168 | + }).toThrow(); |
| 169 | + }); |
| 170 | + }); |
| 171 | +
|
| 172 | + describe('Edge cases', () => { |
| 173 | + test('should handle edge case', () => { |
| 174 | + // TODO: Implement edge case test |
| 175 | + }); |
| 176 | + }); |
| 177 | +}); |
| 178 | +
|
| 179 | +/* |
| 180 | +BDD Specification: |
| 181 | +${bddSpec} |
| 182 | +
|
| 183 | +Next Steps: |
| 184 | +1. Run: npm test tests/${category}/${moduleName}.test.js |
| 185 | +2. Implement the module to make tests pass |
| 186 | +3. Refactor and improve |
| 187 | +*/ |
| 188 | +`; |
| 189 | +} |
| 190 | + |
| 191 | +function generateSourceTemplate(moduleName, category) { |
| 192 | + const className = moduleName.charAt(0).toUpperCase() + moduleName.slice(1); |
| 193 | + |
| 194 | + return `/** |
| 195 | + * ${moduleName}.js |
| 196 | + * Auto-generated by generate-test.js |
| 197 | + * |
| 198 | + * @module ${moduleName} |
| 199 | + */ |
| 200 | +
|
| 201 | +/** |
| 202 | + * ${className} class |
| 203 | + */ |
| 204 | +class ${className} { |
| 205 | + constructor(options = {}) { |
| 206 | + // TODO: Initialize |
| 207 | + } |
| 208 | +
|
| 209 | + /** |
| 210 | + * Primary method |
| 211 | + * |
| 212 | + * @returns {*} |
| 213 | + */ |
| 214 | + execute() { |
| 215 | + // TODO: Implement |
| 216 | + throw new Error('Not implemented'); |
| 217 | + } |
| 218 | +} |
| 219 | +
|
| 220 | +module.exports = { ${className} }; |
| 221 | +`; |
| 222 | +} |
| 223 | + |
| 224 | +// 실행 |
| 225 | +generateTest().catch(error => { |
| 226 | + console.error(colors.yellow + '\n❌ Error:', error.message + colors.reset); |
| 227 | + process.exit(1); |
| 228 | +}); |
0 commit comments