test-enhanced-methods.js•6.55 kB
/**
* Enhanced Core Methods Test
* Tests the newly implemented core methods for smart file analysis
*/
import { AutomationScriptGenerator } from './index.js';
import fs from 'fs-extra';
import path from 'path';
async function testEnhancedMethods() {
console.log('🧪 Testing Enhanced Core Methods\n');
const generator = new AutomationScriptGenerator();
// Create temporary test structure
const testDir = './test-repo';
await createTestRepository(testDir);
console.log('📁 Created test repository structure\n');
try {
// Test 1: findFilesByPattern
console.log('1️⃣ Testing findFilesByPattern...');
const featureFiles = await generator.findFilesByPattern(testDir, 'features');
const stepFiles = await generator.findFilesByPattern(testDir, 'steps');
const pageFiles = await generator.findFilesByPattern(testDir, 'pages');
console.log(` ✅ Found ${featureFiles.length} feature files`);
console.log(` ✅ Found ${stepFiles.length} step files`);
console.log(` ✅ Found ${pageFiles.length} page files`);
// Test 2: scanForPatterns
console.log('\n2️⃣ Testing scanForPatterns...');
const featurePatterns = await generator.scanForPatterns(testDir, 'features');
const stepPatterns = await generator.scanForPatterns(testDir, 'steps');
console.log(` ✅ Extracted patterns from ${featurePatterns.length} feature files`);
console.log(` ✅ Extracted patterns from ${stepPatterns.length} step files`);
if (featurePatterns.length > 0) {
const firstFeature = featurePatterns[0];
console.log(` 📋 Sample feature patterns:`, firstFeature.patterns);
}
// Test 3: scanForUtils
console.log('\n3️⃣ Testing scanForUtils...');
const utils = await generator.scanForUtils(testDir);
console.log(` ✅ Found ${Object.keys(utils).length} utility files`);
Object.keys(utils).forEach(utilFile => {
console.log(` 📦 ${utilFile}: ${utils[utilFile].functions.length} functions`);
});
// Test 4: extractNamingConventions
console.log('\n4️⃣ Testing extractNamingConventions...');
const conventions = await generator.extractNamingConventions(testDir);
console.log(` ✅ File naming: ${conventions.fileNaming} (${conventions.confidence.fileNaming}% confidence)`);
console.log(` ✅ Class naming: ${conventions.classNaming} (${conventions.confidence.classNaming}% confidence)`);
console.log(` ✅ Function naming: ${conventions.functionNaming} (${conventions.confidence.functionNaming}% confidence)`);
// Test 5: performCodeReview
console.log('\n5️⃣ Testing performCodeReview...');
const sampleCode = `
function testFunction() {
const data = getData();
return data;
}
class TestPage {
constructor() {
this.selector = '#test';
}
}`;
const review = await generator.performCodeReview(
sampleCode,
['docs', 'pom', 'functions', 'format'],
testDir,
'test.js'
);
console.log(` ✅ Review score: ${review.score}/100`);
console.log(` ⚠️ Issues found: ${review.issues.length}`);
console.log(` 💡 Suggestions: ${review.suggestions.length}`);
if (review.issues.length > 0) {
console.log(` 📝 Sample issue: ${review.issues[0]}`);
}
console.log('\n🎉 All enhanced methods are working correctly!');
console.log('\n🔍 Key Improvements Implemented:');
console.log(' • Real glob pattern matching for file discovery');
console.log(' • Comprehensive pattern extraction from code');
console.log(' • Advanced utility function analysis');
console.log(' • Smart naming convention detection');
console.log(' • Detailed code review with actionable feedback');
console.log(' • Error handling and graceful degradation');
} catch (error) {
console.error(`❌ Test failed: ${error.message}`);
console.error(error.stack);
} finally {
// Cleanup
await fs.remove(testDir);
console.log('\n🧹 Cleaned up test repository');
}
}
async function createTestRepository(testDir) {
// Create directory structure
await fs.ensureDir(path.join(testDir, 'features'));
await fs.ensureDir(path.join(testDir, 'step-definitions'));
await fs.ensureDir(path.join(testDir, 'pageobjects'));
await fs.ensureDir(path.join(testDir, 'utils'));
// Create sample feature file
await fs.writeFile(path.join(testDir, 'features', 'login.feature'), `
Feature: User Login
@login @smoke
Scenario: Successful login
Given I am on the login page
When I enter valid credentials
Then I should be logged in
`);
// Create sample step file
await fs.writeFile(path.join(testDir, 'step-definitions', 'login.steps.js'), `
import { Given, When, Then } from '@wdio/cucumber-framework';
Given('I am on the login page', async () => {
await browser.url('/login');
});
When('I enter valid credentials', async () => {
await $('#username').setValue('user@example.com');
await $('#password').setValue('password123');
await $('[data-testid="login-btn"]').click();
});
Then('I should be logged in', async () => {
await expect($('.welcome-message')).toBeDisplayed();
});
`);
// Create sample page object
await fs.writeFile(path.join(testDir, 'pageobjects', 'login.page.js'), `
class LoginPage {
get usernameInput() { return $('#username'); }
get passwordInput() { return $('#password'); }
get loginButton() { return '[data-testid="login-btn"]'; }
async login(username, password) {
await this.usernameInput.setValue(username);
await this.passwordInput.setValue(password);
await $(this.loginButton).click();
}
}
export default new LoginPage();
`);
// Create sample utility file
await fs.writeFile(path.join(testDir, 'utils', 'helpers.js'), `
export function waitForElement(selector, timeout = 5000) {
return browser.waitUntil(async () => {
return await $(selector).isDisplayed();
}, { timeout });
}
export async function takeScreenshot(name) {
await browser.saveScreenshot(\`./screenshots/\${name}.png\`);
}
export const testData = {
validUser: {
username: 'user@example.com',
password: 'password123'
}
};
`);
}
// Run the test
testEnhancedMethods().catch(console.error);