agentsqripts
Version:
Comprehensive static code analysis toolkit for identifying technical debt, security vulnerabilities, performance issues, and code quality problems
178 lines (140 loc) • 5.78 kB
JavaScript
/**
* @file Unit tests for CLI help formatter utility
* @description Tests help message generation and formatting for CLI tools
*/
const { createHelpFunction } = require('./helpFormatter');
const qtests = require('qtests');
/**
* Test runner for help formatter
*/
async function runTests() {
console.log('=== Testing CLI Help Formatter ===');
const results = {
total: 0,
passed: 0
};
// Test basic help function creation
results.total++;
try {
const showHelp = createHelpFunction({
command: 'test-command.js',
description: 'Test command description',
options: [
{ flag: '--test-flag', description: 'Test flag description' }
]
});
qtests.assert(typeof showHelp === 'function', 'createHelpFunction should return a function');
console.log('✓ createHelpFunction returns valid help function');
results.passed++;
} catch (error) {
console.log(`✗ createHelpFunction basic test failed: ${error.message}`);
}
// Test help message content
results.total++;
try {
const originalConsoleLog = console.log;
let helpOutput = '';
console.log = (message) => { helpOutput += message + '\n'; };
const showHelp = createHelpFunction({
command: 'test-command.js',
description: 'Test command description',
options: [
{ flag: '--output-format <fmt>', description: 'Output format: json, summary (default: summary)' },
{ flag: '--help', description: 'Show this help message' }
],
examples: [
'node test-command.js .',
'node test-command.js --output-format json src/'
]
});
showHelp();
console.log = originalConsoleLog;
qtests.assert(helpOutput.includes('test-command.js'), 'Help should include command name');
qtests.assert(helpOutput.includes('Test command description'), 'Help should include description');
qtests.assert(helpOutput.includes('--output-format'), 'Help should include option flags');
qtests.assert(helpOutput.includes('Examples:'), 'Help should include examples section');
console.log('✓ createHelpFunction generates complete help content');
results.passed++;
} catch (error) {
console.log(`✗ createHelpFunction content test failed: ${error.message}`);
}
// Test help with sections
results.total++;
try {
const originalConsoleLog = console.log;
let helpOutput = '';
console.log = (message) => { helpOutput += message + '\n'; };
const showHelp = createHelpFunction({
command: 'analyze-test.js',
description: 'Analyze test files',
options: [
{ flag: '--severity <level>', description: 'Minimum severity: LOW, MEDIUM, HIGH' }
],
sections: {
'SEVERITY LEVELS': ' LOW: Minor issues\n MEDIUM: Notable issues\n HIGH: Critical issues'
}
});
showHelp();
console.log = originalConsoleLog;
qtests.assert(helpOutput.includes('SEVERITY LEVELS'), 'Help should include custom sections');
qtests.assert(helpOutput.includes('Critical issues'), 'Help should include section content');
console.log('✓ createHelpFunction handles custom sections correctly');
results.passed++;
} catch (error) {
console.log(`✗ createHelpFunction sections test failed: ${error.message}`);
}
// Test help with modes
results.total++;
try {
const originalConsoleLog = console.log;
let helpOutput = '';
console.log = (message) => { helpOutput += message + '\n'; };
const showHelp = createHelpFunction({
command: 'analyze-files.js',
description: 'Analyze files or projects',
modes: ' file Analyze single file\n project Analyze entire project',
options: [
{ flag: '--mode <mode>', description: 'Analysis mode: file, project' }
]
});
showHelp();
console.log = originalConsoleLog;
qtests.assert(helpOutput.includes('Analysis Modes:'), 'Help should include modes section');
qtests.assert(helpOutput.includes('Analyze single file'), 'Help should include mode descriptions');
console.log('✓ createHelpFunction handles modes correctly');
results.passed++;
} catch (error) {
console.log(`✗ createHelpFunction modes test failed: ${error.message}`);
}
// Test help formatting consistency
results.total++;
try {
const originalConsoleLog = console.log;
let helpOutput = '';
console.log = (message) => { helpOutput += message + '\n'; };
const showHelp = createHelpFunction({
command: 'format-test.js',
description: 'Test formatting consistency',
options: [
{ flag: '--flag1', description: 'First flag' },
{ flag: '--very-long-flag-name <value>', description: 'Flag with very long name for alignment testing' }
]
});
showHelp();
console.log = originalConsoleLog;
// Check that options are properly aligned
const lines = helpOutput.split('\n');
const optionLines = lines.filter(line => line.includes('--'));
qtests.assert(optionLines.length >= 2, 'Help should format multiple options');
qtests.assert(optionLines.every(line => line.includes(' ')), 'Options should be properly indented');
console.log('✓ createHelpFunction maintains formatting consistency');
results.passed++;
} catch (error) {
console.log(`✗ createHelpFunction formatting test failed: ${error.message}`);
}
console.log(`=== Help Formatter Test Results ===`);
console.log(`Tests passed: ${results.passed}/${results.total}`);
console.log(`Success rate: ${((results.passed / results.total) * 100).toFixed(1)}%`);
return results;
}
module.exports = { runTests };