agentsqripts
Version:
Comprehensive static code analysis toolkit for identifying technical debt, security vulnerabilities, performance issues, and code quality problems
132 lines (107 loc) • 4.51 kB
JavaScript
/**
* @file AST Helpers test bridge
* @description Bridge test file to import from ast-helpers module
*/
const qtests = require('qtests');
const { parseCode, findNodes, getNodeContext, getScopeVariables, walkAST } = require('./ast-helpers');
async function runTests() {
console.log('\n=== Testing AST Helper Utilities ===');
const results = { total: 0, passed: 0 };
// Test parseCode function
results.total++;
try {
const code = 'function test() { return 42; }';
const ast = parseCode(code);
qtests.assert(typeof ast === 'object', 'parseCode should return AST object');
qtests.assert(ast.type === 'Program', 'AST should have Program type');
console.log('✓ parseCode correctly parses JavaScript code');
results.passed++;
} catch (error) {
console.log(`✗ parseCode test failed: ${error.message}`);
}
// Test parseCode error handling
results.total++;
try {
qtests.assert(typeof parseCode === 'function', 'parseCode should be a function');
try {
parseCode('invalid syntax {');
console.log('✗ parseCode error handling test failed: Should throw on invalid syntax');
} catch (parseError) {
console.log('✓ parseCode correctly handles parsing errors');
results.passed++;
}
} catch (error) {
console.log(`✗ parseCode error handling test failed: ${error.message}`);
}
// Test findNodes function
results.total++;
try {
const code = 'function test() { return 42; }';
const ast = parseCode(code);
const functionNodes = findNodes(ast, node => node.type === 'FunctionDeclaration');
qtests.assert(Array.isArray(functionNodes), 'findNodes should return array');
qtests.assert(functionNodes.length === 1, 'Should find one function declaration');
console.log('✓ findNodes correctly finds nodes by predicate');
results.passed++;
} catch (error) {
console.log(`✗ findNodes test failed: ${error.message}`);
}
// Test findNodes with predicate
results.total++;
try {
const code = 'const a = 1; const b = 2;';
const ast = parseCode(code);
const varNodes = findNodes(ast, node => node.type === 'VariableDeclaration');
qtests.assert(varNodes.length === 2, 'Should find two variable declarations');
console.log('✓ findNodes correctly filters nodes with predicate');
results.passed++;
} catch (error) {
console.log(`✗ findNodes predicate test failed: ${error.message}`);
}
// Test getNodeContext function
results.total++;
try {
const code = 'function test() { return 42; }';
const ast = parseCode(code);
const functionNode = findNodes(ast, node => node.type === 'FunctionDeclaration')[0];
const context = getNodeContext(functionNode, code);
qtests.assert(typeof context === 'object', 'getNodeContext should return object');
qtests.assert(typeof context.type === 'string', 'Context should have type');
qtests.assert(typeof context.line === 'number', 'Context should have line number');
console.log('✓ getNodeContext correctly extracts node context');
results.passed++;
} catch (error) {
console.log(`✗ getNodeContext test failed: ${error.message}`);
}
// Test getScopeVariables function
results.total++;
try {
const code = 'const a = 1; let b = 2; var c = 3;';
const ast = parseCode(code);
const variables = getScopeVariables(ast);
qtests.assert(Array.isArray(variables), 'getScopeVariables should return array');
qtests.assert(variables.length >= 1, 'Should find at least one variable');
console.log('✓ getScopeVariables correctly extracts variable declarations');
results.passed++;
} catch (error) {
console.log(`✗ getScopeVariables test failed: ${error.message}`);
}
// Test walkAST function
results.total++;
try {
const code = 'function test() { const x = 1; }';
const ast = parseCode(code);
let nodeCount = 0;
walkAST(ast, () => nodeCount++);
qtests.assert(nodeCount > 0, 'walkAST should visit nodes');
console.log('✓ walkAST correctly traverses AST nodes');
results.passed++;
} catch (error) {
console.log(`✗ walkAST test failed: ${error.message}`);
}
console.log(`=== AST Helpers Test Results ===`);
console.log(`Tests passed: ${results.passed}/${results.total}`);
console.log(`Success rate: ${((results.passed / results.total) * 100).toFixed(1)}%\n`);
return results;
}
module.exports = { runTests };