user-prompt-validation.js•5.29 kB
/**
* User Prompt Schema Validation Demo
* Demonstrates the validation capabilities for user prompt input
*/
import Ajv from 'ajv';
const ajv = new Ajv({ allErrors: true });
// Main schema for processing user prompts
const processTestScenarioSchema = {
type: 'object',
properties: {
scenario_title: {
type: 'string',
description: 'Title of the test scenario',
minLength: 1,
},
tags: {
type: 'array',
items: { type: 'string' },
description: 'Test ID tags for the scenario (e.g., ["@login", "@smoke", "@TEST-001"])',
},
gherkin_syntax: {
type: 'string',
description: 'Complete Gherkin syntax with Given/When/Then steps',
minLength: 1,
},
selectors: {
type: 'object',
description: 'UI element selectors as key-value pairs',
},
data_items: {
type: 'object',
description: 'Test data items and configurations (optional)',
},
output_directory: {
type: 'string',
description: 'Base directory where all generated files should be saved',
minLength: 1,
},
repo_path: {
type: 'string',
description: 'Path to existing repository for pattern analysis (optional)',
},
},
required: ['scenario_title', 'gherkin_syntax', 'selectors', 'output_directory'],
additionalProperties: false,
};
function validateAndLog(schemaName, schema, testData, shouldPass = true) {
const validate = ajv.compile(schema);
const valid = validate(testData);
if (valid) {
console.log(`✅ ${shouldPass ? 'PASS' : 'UNEXPECTED PASS'}: ${schemaName}`);
if (!shouldPass) {
console.log(` Data: ${JSON.stringify(testData, null, 2)}`);
}
} else {
console.log(`${shouldPass ? '❌ UNEXPECTED FAIL' : '✅ EXPECTED FAIL'}: ${schemaName}`);
const errors = validate.errors
.map(err => `${err.instancePath || 'root'}: ${err.message}`)
.join(', ');
console.log(` Errors: ${errors}`);
if (shouldPass) {
console.log(` Data: ${JSON.stringify(testData, null, 2)}\n`);
}
}
}
console.log('🧪 User Prompt Schema Validation Demo\n');
// Test process_test_scenario
console.log('📋 Testing process_test_scenario schema:');
validateAndLog('Complete valid scenario', processTestScenarioSchema, {
scenario_title: 'User Login Functionality',
tags: ['@login', '@smoke', '@TEST-001'],
gherkin_syntax: `Feature: User Login
Scenario: Successful login with valid credentials
Given I am on the login page
When I enter valid username "user@example.com"
And I enter valid password "password123"
And I click the login button
Then I should be redirected to the dashboard
And I should see the welcome message`,
selectors: {
usernameInput: '[data-testid="username-input"]',
passwordInput: '[data-testid="password-input"]',
loginButton: '[data-testid="login-button"]',
welcomeMessage: '.welcome-message'
},
data_items: {
validUser: {
username: 'user@example.com',
password: 'password123'
},
invalidUser: {
username: 'invalid@example.com',
password: 'wrongpassword'
}
},
output_directory: './generated-tests',
repo_path: './existing-tests'
});
validateAndLog('Minimal valid scenario', processTestScenarioSchema, {
scenario_title: 'Simple Test',
gherkin_syntax: 'Given I am on the page\nWhen I click button\nThen I see result',
selectors: {
button: '#submit-btn'
},
output_directory: './tests'
});
validateAndLog('Missing required field', processTestScenarioSchema, {
scenario_title: 'Test Scenario',
gherkin_syntax: 'Given I am on the page',
// Missing selectors and output_directory
}, false);
validateAndLog('Empty scenario title', processTestScenarioSchema, {
scenario_title: '',
gherkin_syntax: 'Given I am on the page',
selectors: { button: '#btn' },
output_directory: './tests'
}, false);
validateAndLog('Invalid selectors type', processTestScenarioSchema, {
scenario_title: 'Test',
gherkin_syntax: 'Given I am on the page',
selectors: 'not an object',
output_directory: './tests'
}, false);
console.log('\n🎯 User Prompt Processing Benefits:');
console.log(' • No external API dependencies');
console.log(' • Direct input from user prompts');
console.log(' • Complete scenario processing in one call');
console.log(' • Automatic file generation and organization');
console.log(' • Pattern analysis from existing repositories');
console.log(' • Built-in code review and enhancement');
console.log('\n📝 Example User Prompt:');
console.log(`"Generate WDIO tests for login functionality with the following:
- Title: User Login
- Tags: @login, @smoke, @TEST-001
- Gherkin: Given I am on login page, When I enter credentials, Then I see dashboard
- Selectors: username=#user, password=#pass, loginBtn=.login-button
- Output to: ./my-tests
- Analyze patterns from: ./existing-repo"`);