codewithgarry
Version:
Girish Sharma's NPX business card - DevOps Engineer & Cloud Architect - Connect with me directly via terminal!
287 lines (236 loc) โข 11 kB
JavaScript
/**
* Test Suite for CodeWithGarry NPX Card
* Basic testing framework without external dependencies
*/
class TestRunner {
constructor() {
this.tests = [];
this.results = {
passed: 0,
failed: 0,
total: 0
};
this.startTime = Date.now();
}
describe(description, testFn) {
console.log(`\n๐งช ${description}`);
testFn();
}
it(description, testFn) {
this.results.total++;
try {
testFn();
this.results.passed++;
console.log(` โ
${description}`);
} catch (error) {
this.results.failed++;
console.log(` โ ${description}`);
console.log(` Error: ${error.message}`);
}
}
expect(actual) {
return {
toBe: (expected) => {
if (actual !== expected) {
throw new Error(`Expected ${expected}, but got ${actual}`);
}
},
toEqual: (expected) => {
if (JSON.stringify(actual) !== JSON.stringify(expected)) {
throw new Error(`Expected ${JSON.stringify(expected)}, but got ${JSON.stringify(actual)}`);
}
},
toBeTruthy: () => {
if (!actual) {
throw new Error(`Expected truthy value, but got ${actual}`);
}
},
toBeFalsy: () => {
if (actual) {
throw new Error(`Expected falsy value, but got ${actual}`);
}
},
toThrow: () => {
if (typeof actual !== 'function') {
throw new Error('Expected a function');
}
try {
actual();
throw new Error('Expected function to throw, but it did not');
} catch (error) {
// Expected to throw
}
},
toContain: (expected) => {
if (!actual.includes(expected)) {
throw new Error(`Expected ${actual} to contain ${expected}`);
}
}
};
}
run() {
console.log('\n๐ Running CodeWithGarry NPX Card Tests...\n');
// Run all tests
this.runBasicTests();
this.runSecurityTests();
this.runPerformanceTests();
this.runIntegrationTests();
// Show results and return success status
return this.showResults();
}
runBasicTests() {
this.describe('Basic Functionality', () => {
this.it('should have required dependencies', () => {
const fs = require('fs');
const path = require('path');
this.expect(fs.existsSync(path.join(__dirname, '../package.json'))).toBeTruthy();
this.expect(fs.existsSync(path.join(__dirname, '../index.js'))).toBeTruthy();
});
this.it('should have valid package.json', () => {
const path = require('path');
const pkg = require(path.join(__dirname, '../package.json'));
this.expect(pkg.name).toBe('codewithgarry');
this.expect(pkg.version).toBeTruthy();
this.expect(pkg.main).toBe('index.js');
this.expect(pkg.bin.codewithgarry).toBe('./index.js');
});
this.it('should load main file without errors', () => {
const fs = require('fs');
const path = require('path');
const content = fs.readFileSync(path.join(__dirname, '../index.js'), 'utf8');
this.expect(content).toContain('#!/usr/bin/env node');
this.expect(content).toContain('codewithgarry');
});
this.it('should have valid Node.js version check', () => {
const fs = require('fs');
const path = require('path');
const content = fs.readFileSync(path.join(__dirname, '../index.js'), 'utf8');
this.expect(content).toContain('majorVersion');
this.expect(content).toContain('Node.js Update Required');
});
this.it('should handle missing dependencies gracefully', () => {
const fs = require('fs');
const path = require('path');
const content = fs.readFileSync(path.join(__dirname, '../index.js'), 'utf8');
this.expect(content).toContain('try {');
this.expect(content).toContain('MODULE_NOT_FOUND');
});
});
}
runSecurityTests() {
this.describe('Security', () => {
this.it('should have security configuration', () => {
const fs = require('fs');
const path = require('path');
this.expect(fs.existsSync(path.join(__dirname, '../security.js'))).toBeTruthy();
});
this.it('should have Content Security Policy', () => {
const fs = require('fs');
const path = require('path');
// Check HTML files for CSP headers
if (fs.existsSync(path.join(__dirname, '../advanced-learning-platform.html'))) {
const content = fs.readFileSync(path.join(__dirname, '../advanced-learning-platform.html'), 'utf8');
this.expect(content).toContain('Content-Security-Policy');
}
});
this.it('should have HTTPS enforcement', () => {
const fs = require('fs');
const path = require('path');
const securityContent = fs.readFileSync(path.join(__dirname, '../security.js'), 'utf8');
this.expect(securityContent).toContain('enforceHTTPS');
this.expect(securityContent).toContain('https:');
});
this.it('should have input sanitization', () => {
const fs = require('fs');
const path = require('path');
const securityContent = fs.readFileSync(path.join(__dirname, '../security.js'), 'utf8');
this.expect(securityContent).toContain('sanitizeInput');
});
});
}
runPerformanceTests() {
this.describe('Performance', () => {
this.it('should have performance monitoring', () => {
const fs = require('fs');
const path = require('path');
this.expect(fs.existsSync(path.join(__dirname, '../performance.js'))).toBeTruthy();
this.expect(fs.existsSync(path.join(__dirname, '../monitoring.js'))).toBeTruthy();
});
this.it('should have lazy loading implementation', () => {
const fs = require('fs');
const path = require('path');
const perfContent = fs.readFileSync(path.join(__dirname, '../performance.js'), 'utf8');
this.expect(perfContent).toContain('LazyLoader');
this.expect(perfContent).toContain('IntersectionObserver');
});
this.it('should have caching implementation', () => {
const fs = require('fs');
const path = require('path');
const perfContent = fs.readFileSync(path.join(__dirname, '../performance.js'), 'utf8');
this.expect(perfContent).toContain('CacheManager');
this.expect(perfContent).toContain('localStorage');
});
this.it('should have bundle size monitoring', () => {
const path = require('path');
const pkg = require(path.join(__dirname, '../package.json'));
this.expect(pkg.scripts['bundle-size']).toBeTruthy();
});
});
}
runIntegrationTests() {
this.describe('Integration', () => {
this.it('should have Docker configuration', () => {
const fs = require('fs');
const path = require('path');
this.expect(fs.existsSync(path.join(__dirname, '../Dockerfile'))).toBeTruthy();
this.expect(fs.existsSync(path.join(__dirname, '../docker-compose.yml'))).toBeTruthy();
this.expect(fs.existsSync(path.join(__dirname, '../.dockerignore'))).toBeTruthy();
});
this.it('should have CI/CD configuration', () => {
const fs = require('fs');
const path = require('path');
this.expect(fs.existsSync(path.join(__dirname, '../.github/workflows/deploy.yml'))).toBeTruthy();
});
this.it('should have build scripts', () => {
const fs = require('fs');
const path = require('path');
this.expect(fs.existsSync(path.join(__dirname, '../build.sh'))).toBeTruthy();
});
this.it('should have environment configuration', () => {
const fs = require('fs');
const path = require('path');
this.expect(fs.existsSync(path.join(__dirname, '../.env.example'))).toBeTruthy();
this.expect(fs.existsSync(path.join(__dirname, '../.env.production'))).toBeTruthy();
});
this.it('should have monitoring configuration', () => {
const fs = require('fs');
const path = require('path');
this.expect(fs.existsSync(path.join(__dirname, '../monitoring/prometheus.yml'))).toBeTruthy();
});
});
}
showResults() {
const duration = Date.now() - this.startTime;
console.log('\n' + '='.repeat(50));
console.log('๐ Test Results');
console.log('='.repeat(50));
console.log(`โ
Passed: ${this.results.passed}`);
console.log(`โ Failed: ${this.results.failed}`);
console.log(`๐ Total: ${this.results.total}`);
console.log(`โฑ๏ธ Duration: ${duration}ms`);
if (this.results.failed === 0) {
console.log('\n๐ All tests passed! Ready for production!');
} else {
console.log(`\nโ ๏ธ ${this.results.failed} test(s) failed. Please fix before deploying.`);
}
console.log('='.repeat(50));
return this.results.failed === 0;
}
}
// Run tests if this file is executed directly
if (require.main === module) {
const runner = new TestRunner();
const success = runner.run();
process.exit(success ? 0 : 1);
}
module.exports = TestRunner;