chore: add prettier config and prettify
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
/**
|
||||
* test-claude-errors.js
|
||||
*
|
||||
*
|
||||
* A test script to verify the error handling and retry logic in the callClaude function.
|
||||
* This script creates a modified version of dev.js that simulates different error scenarios.
|
||||
*/
|
||||
@@ -22,7 +22,7 @@ dotenv.config();
|
||||
|
||||
// Create a simple PRD for testing
|
||||
const createTestPRD = () => {
|
||||
return `# Test PRD for Error Handling
|
||||
return `# Test PRD for Error Handling
|
||||
|
||||
## Overview
|
||||
This is a simple test PRD to verify the error handling in the callClaude function.
|
||||
@@ -36,21 +36,22 @@ This is a simple test PRD to verify the error handling in the callClaude functio
|
||||
|
||||
// Create a modified version of dev.js that simulates errors
|
||||
function createErrorSimulationScript(errorType, failureCount = 2) {
|
||||
// Read the original dev.js file
|
||||
const devJsPath = path.join(__dirname, 'dev.js');
|
||||
const devJsContent = fs.readFileSync(devJsPath, 'utf8');
|
||||
|
||||
// Create a modified version that simulates errors
|
||||
let modifiedContent = devJsContent;
|
||||
|
||||
// Find the anthropic.messages.create call and replace it with our mock
|
||||
const anthropicCallRegex = /const response = await anthropic\.messages\.create\(/;
|
||||
|
||||
let mockCode = '';
|
||||
|
||||
switch (errorType) {
|
||||
case 'network':
|
||||
mockCode = `
|
||||
// Read the original dev.js file
|
||||
const devJsPath = path.join(__dirname, 'dev.js');
|
||||
const devJsContent = fs.readFileSync(devJsPath, 'utf8');
|
||||
|
||||
// Create a modified version that simulates errors
|
||||
let modifiedContent = devJsContent;
|
||||
|
||||
// Find the anthropic.messages.create call and replace it with our mock
|
||||
const anthropicCallRegex =
|
||||
/const response = await anthropic\.messages\.create\(/;
|
||||
|
||||
let mockCode = '';
|
||||
|
||||
switch (errorType) {
|
||||
case 'network':
|
||||
mockCode = `
|
||||
// Mock for network error simulation
|
||||
let currentAttempt = 0;
|
||||
const failureCount = ${failureCount};
|
||||
@@ -65,10 +66,10 @@ function createErrorSimulationScript(errorType, failureCount = 2) {
|
||||
}
|
||||
|
||||
const response = await anthropic.messages.create(`;
|
||||
break;
|
||||
|
||||
case 'timeout':
|
||||
mockCode = `
|
||||
break;
|
||||
|
||||
case 'timeout':
|
||||
mockCode = `
|
||||
// Mock for timeout error simulation
|
||||
let currentAttempt = 0;
|
||||
const failureCount = ${failureCount};
|
||||
@@ -83,10 +84,10 @@ function createErrorSimulationScript(errorType, failureCount = 2) {
|
||||
}
|
||||
|
||||
const response = await anthropic.messages.create(`;
|
||||
break;
|
||||
|
||||
case 'invalid-json':
|
||||
mockCode = `
|
||||
break;
|
||||
|
||||
case 'invalid-json':
|
||||
mockCode = `
|
||||
// Mock for invalid JSON response
|
||||
let currentAttempt = 0;
|
||||
const failureCount = ${failureCount};
|
||||
@@ -107,10 +108,10 @@ function createErrorSimulationScript(errorType, failureCount = 2) {
|
||||
}
|
||||
|
||||
const response = await anthropic.messages.create(`;
|
||||
break;
|
||||
|
||||
case 'empty-tasks':
|
||||
mockCode = `
|
||||
break;
|
||||
|
||||
case 'empty-tasks':
|
||||
mockCode = `
|
||||
// Mock for empty tasks array
|
||||
let currentAttempt = 0;
|
||||
const failureCount = ${failureCount};
|
||||
@@ -131,82 +132,87 @@ function createErrorSimulationScript(errorType, failureCount = 2) {
|
||||
}
|
||||
|
||||
const response = await anthropic.messages.create(`;
|
||||
break;
|
||||
|
||||
default:
|
||||
// No modification
|
||||
mockCode = `const response = await anthropic.messages.create(`;
|
||||
}
|
||||
|
||||
// Replace the anthropic call with our mock
|
||||
modifiedContent = modifiedContent.replace(anthropicCallRegex, mockCode);
|
||||
|
||||
// Write the modified script to a temporary file
|
||||
const tempScriptPath = path.join(__dirname, `temp-dev-${errorType}.js`);
|
||||
fs.writeFileSync(tempScriptPath, modifiedContent, 'utf8');
|
||||
|
||||
return tempScriptPath;
|
||||
break;
|
||||
|
||||
default:
|
||||
// No modification
|
||||
mockCode = `const response = await anthropic.messages.create(`;
|
||||
}
|
||||
|
||||
// Replace the anthropic call with our mock
|
||||
modifiedContent = modifiedContent.replace(anthropicCallRegex, mockCode);
|
||||
|
||||
// Write the modified script to a temporary file
|
||||
const tempScriptPath = path.join(__dirname, `temp-dev-${errorType}.js`);
|
||||
fs.writeFileSync(tempScriptPath, modifiedContent, 'utf8');
|
||||
|
||||
return tempScriptPath;
|
||||
}
|
||||
|
||||
// Function to run a test with a specific error type
|
||||
async function runErrorTest(errorType, numTasks = 5, failureCount = 2) {
|
||||
console.log(`\n=== Test: ${errorType.toUpperCase()} Error Simulation ===`);
|
||||
|
||||
// Create a test PRD
|
||||
const testPRD = createTestPRD();
|
||||
const testPRDPath = path.join(__dirname, `test-prd-${errorType}.txt`);
|
||||
fs.writeFileSync(testPRDPath, testPRD, 'utf8');
|
||||
|
||||
// Create a modified dev.js that simulates the specified error
|
||||
const tempScriptPath = createErrorSimulationScript(errorType, failureCount);
|
||||
|
||||
console.log(`Created test PRD at ${testPRDPath}`);
|
||||
console.log(`Created error simulation script at ${tempScriptPath}`);
|
||||
console.log(`Running with error type: ${errorType}, failure count: ${failureCount}, tasks: ${numTasks}`);
|
||||
|
||||
try {
|
||||
// Run the modified script
|
||||
execSync(`node ${tempScriptPath} parse-prd --input=${testPRDPath} --tasks=${numTasks}`, {
|
||||
stdio: 'inherit'
|
||||
});
|
||||
console.log(`${errorType} error test completed successfully`);
|
||||
} catch (error) {
|
||||
console.error(`${errorType} error test failed:`, error.message);
|
||||
} finally {
|
||||
// Clean up temporary files
|
||||
if (fs.existsSync(tempScriptPath)) {
|
||||
fs.unlinkSync(tempScriptPath);
|
||||
}
|
||||
if (fs.existsSync(testPRDPath)) {
|
||||
fs.unlinkSync(testPRDPath);
|
||||
}
|
||||
}
|
||||
console.log(`\n=== Test: ${errorType.toUpperCase()} Error Simulation ===`);
|
||||
|
||||
// Create a test PRD
|
||||
const testPRD = createTestPRD();
|
||||
const testPRDPath = path.join(__dirname, `test-prd-${errorType}.txt`);
|
||||
fs.writeFileSync(testPRDPath, testPRD, 'utf8');
|
||||
|
||||
// Create a modified dev.js that simulates the specified error
|
||||
const tempScriptPath = createErrorSimulationScript(errorType, failureCount);
|
||||
|
||||
console.log(`Created test PRD at ${testPRDPath}`);
|
||||
console.log(`Created error simulation script at ${tempScriptPath}`);
|
||||
console.log(
|
||||
`Running with error type: ${errorType}, failure count: ${failureCount}, tasks: ${numTasks}`
|
||||
);
|
||||
|
||||
try {
|
||||
// Run the modified script
|
||||
execSync(
|
||||
`node ${tempScriptPath} parse-prd --input=${testPRDPath} --tasks=${numTasks}`,
|
||||
{
|
||||
stdio: 'inherit'
|
||||
}
|
||||
);
|
||||
console.log(`${errorType} error test completed successfully`);
|
||||
} catch (error) {
|
||||
console.error(`${errorType} error test failed:`, error.message);
|
||||
} finally {
|
||||
// Clean up temporary files
|
||||
if (fs.existsSync(tempScriptPath)) {
|
||||
fs.unlinkSync(tempScriptPath);
|
||||
}
|
||||
if (fs.existsSync(testPRDPath)) {
|
||||
fs.unlinkSync(testPRDPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Function to run all error tests
|
||||
async function runAllErrorTests() {
|
||||
console.log('Starting error handling tests for callClaude function...');
|
||||
|
||||
// Test 1: Network error with automatic retry
|
||||
await runErrorTest('network', 5, 2);
|
||||
|
||||
// Test 2: Timeout error with automatic retry
|
||||
await runErrorTest('timeout', 5, 2);
|
||||
|
||||
// Test 3: Invalid JSON response with task reduction
|
||||
await runErrorTest('invalid-json', 10, 2);
|
||||
|
||||
// Test 4: Empty tasks array with task reduction
|
||||
await runErrorTest('empty-tasks', 15, 2);
|
||||
|
||||
// Test 5: Exhausted retries (more failures than MAX_RETRIES)
|
||||
await runErrorTest('network', 5, 4);
|
||||
|
||||
console.log('\nAll error tests completed!');
|
||||
console.log('Starting error handling tests for callClaude function...');
|
||||
|
||||
// Test 1: Network error with automatic retry
|
||||
await runErrorTest('network', 5, 2);
|
||||
|
||||
// Test 2: Timeout error with automatic retry
|
||||
await runErrorTest('timeout', 5, 2);
|
||||
|
||||
// Test 3: Invalid JSON response with task reduction
|
||||
await runErrorTest('invalid-json', 10, 2);
|
||||
|
||||
// Test 4: Empty tasks array with task reduction
|
||||
await runErrorTest('empty-tasks', 15, 2);
|
||||
|
||||
// Test 5: Exhausted retries (more failures than MAX_RETRIES)
|
||||
await runErrorTest('network', 5, 4);
|
||||
|
||||
console.log('\nAll error tests completed!');
|
||||
}
|
||||
|
||||
// Run the tests
|
||||
runAllErrorTests().catch(error => {
|
||||
console.error('Error running tests:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
runAllErrorTests().catch((error) => {
|
||||
console.error('Error running tests:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user