ctrlshiftleft
Version:
AI-powered toolkit for embedding QA and security testing into development workflows
149 lines (126 loc) • 4.4 kB
JavaScript
/**
* ctrl.shift.left API Usage Examples
*
* This file demonstrates how to use the ctrl.shift.left API programmatically
* in your applications, IDE plugins, or CI/CD pipelines.
*/
// Import the ctrl.shift.left API
const ctrlShiftLeft = require('ctrlshiftleft');
// For TypeScript users, import types:
// import ctrlShiftLeft, { TestGenerationOptions, SecurityAnalysisOptions } from 'ctrlshiftleft';
/**
* Example 1: Generate tests for a component
*/
async function generateTestsExample() {
console.log('Generating tests for a component...');
try {
const result = await ctrlShiftLeft.generateTests('./src/components/LoginForm.jsx', {
format: 'playwright',
outputDir: './tests/generated',
pageObjects: true
});
console.log(`Generated ${result.testCount} tests in ${result.generationTime}ms`);
console.log('Test files:', result.files);
} catch (error) {
console.error('Error generating tests:', error);
}
}
/**
* Example 2: Run security analysis with AI
*/
async function securityAnalysisExample() {
console.log('Running AI-enhanced security analysis...');
try {
const result = await ctrlShiftLeft.analyzeSecurity('./src/components/PaymentForm.tsx', {
useAI: true,
outputFile: './security-reports/payment-form.md',
format: 'markdown'
});
console.log(`Security Score: ${result.score}/100`);
console.log(`Found ${result.issuesFound} issues`);
console.log('Issues by severity:', result.issuesBySeverity);
console.log(`Report saved to: ${result.reportPath}`);
} catch (error) {
console.error('Error analyzing security:', error);
}
}
/**
* Example 3: Generate QA checklist
*/
async function generateChecklistExample() {
console.log('Generating QA & security checklist...');
try {
const result = await ctrlShiftLeft.generateChecklist('./src/components/UserProfile.jsx', {
type: 'all',
format: 'markdown',
outputFile: './checklists/user-profile.md'
});
console.log(`Generated checklist with ${result.itemCount} items`);
console.log(`Passing: ${result.passingCount}, Failing: ${result.failingCount}, Review: ${result.reviewCount}`);
console.log(`Checklist saved to: ${result.filePath}`);
} catch (error) {
console.error('Error generating checklist:', error);
}
}
/**
* Example 4: Watch for file changes
*/
function watchExample() {
console.log('Watching for file changes...');
// Start watching the src directory
const stopWatching = ctrlShiftLeft.watch('./src', {
include: ['**/*.{jsx,tsx}'],
exclude: ['**/tests/**', '**/node_modules/**'],
generateTests: true,
analyzeSecurity: true,
onChange: (filePath) => {
console.log(`File changed: ${filePath}`);
}
});
// Stop watching after 1 hour
setTimeout(() => {
console.log('Stopping file watcher...');
stopWatching();
}, 60 * 60 * 1000);
}
/**
* Example 5: Integration with Cursor AI or other code generators
*/
async function cursorAiIntegrationExample(filePath) {
console.log(`File created/updated by Cursor AI: ${filePath}`);
// Run full suite of analyses
try {
// Generate tests
const testResult = await ctrlShiftLeft.generateTests(filePath);
console.log(`Generated ${testResult.testCount} tests`);
// Run security analysis
const securityResult = await ctrlShiftLeft.analyzeSecurity(filePath, { useAI: true });
console.log(`Security Score: ${securityResult.score}/100`);
// Generate checklist
const checklistResult = await ctrlShiftLeft.generateChecklist(filePath);
console.log(`Generated checklist with ${checklistResult.itemCount} items`);
return {
testResult,
securityResult,
checklistResult
};
} catch (error) {
console.error('Error processing file:', error);
throw error;
}
}
// Run examples
(async () => {
try {
// Uncomment to run examples
// await generateTestsExample();
// await securityAnalysisExample();
// await generateChecklistExample();
// watchExample();
// Example of processing a file created by Cursor AI
// await cursorAiIntegrationExample('./src/components/NewComponent.jsx');
console.log('To run examples, uncomment the function calls in the main block');
} catch (error) {
console.error('Error running examples:', error);
}
})();