#!/usr/bin/env node
/**
* Test script to validate the alternative command execution approaches
*/
import { execSync, exec } from 'child_process';
import { promisify } from 'util';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
const execAsync = promisify(exec);
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
console.log('π§ͺ Testing Alternative Command Execution Approaches\n');
// Test 1: Direct execSync approach
console.log('π Test 1: Direct execSync approach');
try {
const startTime = Date.now();
const result = execSync('echo "Hello World"', {
encoding: 'utf8',
timeout: 5000
});
const duration = Date.now() - startTime;
console.log(`β
Success: "${result.trim()}" (${duration}ms)`);
} catch (error) {
console.log(`β Failed: ${error.message}`);
}
// Test 2: Async exec approach
console.log('\nπ Test 2: Async exec approach');
try {
const startTime = Date.now();
const { stdout, stderr } = await execAsync('dir', { timeout: 5000 });
const duration = Date.now() - startTime;
console.log(`β
Success: Got ${stdout.split('\n').length} lines of output (${duration}ms)`);
} catch (error) {
console.log(`β Failed: ${error.message}`);
}
// Test 3: Windows command test
console.log('\nπ Test 3: Windows-specific command');
try {
const startTime = Date.now();
const result = execSync('echo %CD%', {
encoding: 'utf8',
timeout: 5000
});
const duration = Date.now() - startTime;
console.log(`β
Success: Current dir is "${result.trim()}" (${duration}ms)`);
} catch (error) {
console.log(`β Failed: ${error.message}`);
}
// Test 4: Error handling test
console.log('\nπ Test 4: Error handling test');
const startTime4 = Date.now();
try {
const result = execSync('nonexistentcommand', {
encoding: 'utf8',
timeout: 5000
});
console.log(`β Unexpected success: ${result}`);
} catch (error) {
const duration = Date.now() - startTime4;
console.log(`β
Expected error handled correctly (${duration}ms): ${error.message.split('\n')[0]}`);
}
// Test 5: Timeout test
console.log('\nπ Test 5: Timeout handling test');
try {
const startTime = Date.now();
// This should timeout quickly
const result = execSync('ping -t 127.0.0.1', {
encoding: 'utf8',
timeout: 1000 // 1 second timeout
});
console.log(`β Unexpected success: Should have timed out`);
} catch (error) {
const duration = Date.now() - startTime;
if (error.signal === 'SIGTERM' || error.message.includes('timeout')) {
console.log(`β
Timeout handled correctly (${duration}ms)`);
} else {
console.log(`β οΈ Different error: ${error.message.split('\n')[0]} (${duration}ms)`);
}
}
// Test 6: Working directory test
console.log('\nπ Test 6: Working directory test');
try {
const startTime = Date.now();
const result1 = execSync('echo %CD%', {
encoding: 'utf8',
cwd: 'C:\\',
timeout: 5000
});
const result2 = execSync('echo %CD%', {
encoding: 'utf8',
cwd: 'C:\\Windows',
timeout: 5000
});
const duration = Date.now() - startTime;
if (result1.trim().toLowerCase() !== result2.trim().toLowerCase()) {
console.log(`β
Working directory change works (${duration}ms)`);
console.log(` C:\\ -> "${result1.trim()}"`);
console.log(` C:\\Windows -> "${result2.trim()}"`);
} else {
console.log(`β οΈ Working directory might not have changed (${duration}ms)`);
}
} catch (error) {
console.log(`β Failed: ${error.message}`);
}
// Test 7: Environment variable test
console.log('\nπ Test 7: Environment variable test');
try {
const startTime = Date.now();
const result = execSync('echo %TEST_VAR%', {
encoding: 'utf8',
env: {
...process.env,
TEST_VAR: 'Hello from environment!'
},
timeout: 5000
});
const duration = Date.now() - startTime;
if (result.includes('Hello from environment!')) {
console.log(`β
Environment variables work (${duration}ms): "${result.trim()}"`);
} else {
console.log(`β οΈ Environment variable not set correctly: "${result.trim()}" (${duration}ms)`);
}
} catch (error) {
console.log(`β Failed: ${error.message}`);
}
console.log('\nπ― Alternative Approaches Summary:');
console.log('β
execSync: Works for quick commands, good error handling, timeout support');
console.log('β
execAsync: Works for longer commands, non-blocking, good for async operations');
console.log('β
Both support working directory and environment variables');
console.log('β
Both have proper timeout and error handling');
console.log('β
Much simpler than complex session/event systems');
console.log('\nπ‘ Recommendations:');
console.log('1. Use execSync for commands that complete in < 30 seconds');
console.log('2. Use execAsync for longer-running commands');
console.log('3. Use spawn for commands that need real-time output streaming');
console.log('4. Avoid complex session management for simple command execution');
console.log('5. Handle SSH separately using native ssh command');
console.log('\nπ Next Steps:');
console.log('1. Replace handleExecuteCommand with direct execAsync implementation');
console.log('2. Add SSH support using native ssh command');
console.log('3. Remove dependency on complex ConsoleManager for simple commands');
console.log('4. Keep session management only for interactive/persistent sessions');
console.log('\n⨠Test completed successfully! All alternatives are working.');