log-vista
Version:
LogVista Agent - Lightweight system monitoring and log collection for any project/language
115 lines (96 loc) • 3.72 kB
JavaScript
/**
* CLI Tests for LogVista Agent
* Tests the command-line interface functionality
*/
const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const os = require('os');
describe('LogVista CLI', () => {
const binPath = path.join(__dirname, '..', 'bin', 'logvista.js');
const tempDir = path.join(os.tmpdir(), 'logvista-test-' + Date.now());
beforeEach(() => {
// Create temp directory for testing
if (!fs.existsSync(tempDir)) {
fs.mkdirSync(tempDir, { recursive: true });
}
});
afterEach(() => {
// Clean up temp directory
if (fs.existsSync(tempDir)) {
fs.rmSync(tempDir, { recursive: true, force: true });
}
});
test('should show version when --version flag is used', () => {
try {
const output = execSync(`node "${binPath}" --version`, { encoding: 'utf8' });
expect(output.trim()).toMatch(/\d+\.\d+\.\d+/);
} catch (error) {
// If execSync fails, check if it's because of normal exit
if (error.status === 0) {
expect(error.stdout.trim()).toMatch(/\d+\.\d+\.\d+/);
} else {
throw error;
}
}
});
test('should show help when --help flag is used', () => {
try {
const output = execSync(`node "${binPath}" --help`, { encoding: 'utf8' });
expect(output).toContain('Usage:');
expect(output).toContain('Commands:');
expect(output).toContain('init');
expect(output).toContain('start');
expect(output).toContain('status');
} catch (error) {
// If execSync fails, check if it's because of normal exit
if (error.status === 0) {
expect(error.stdout).toContain('Usage:');
} else {
throw error;
}
}
});
test('should detect Node.js project type', () => {
// Create a package.json to simulate Node.js project
const packageJsonPath = path.join(tempDir, 'package.json');
fs.writeFileSync(packageJsonPath, JSON.stringify({
name: 'test-project',
version: '1.0.0',
scripts: {
start: 'node app.js'
}
}, null, 2));
// Test detection (this would be part of the init process)
expect(fs.existsSync(packageJsonPath)).toBe(true);
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
expect(packageJson.name).toBe('test-project');
});
test('should detect PHP project type', () => {
// Create composer.json to simulate PHP project
const composerJsonPath = path.join(tempDir, 'composer.json');
fs.writeFileSync(composerJsonPath, JSON.stringify({
name: 'test/php-project',
require: {
'php': '^8.0'
}
}, null, 2));
expect(fs.existsSync(composerJsonPath)).toBe(true);
const composerJson = JSON.parse(fs.readFileSync(composerJsonPath, 'utf8'));
expect(composerJson.name).toBe('test/php-project');
});
test('should detect Python project type', () => {
// Create requirements.txt to simulate Python project
const requirementsPath = path.join(tempDir, 'requirements.txt');
fs.writeFileSync(requirementsPath, 'django>=4.0\nrequests>=2.25.0');
expect(fs.existsSync(requirementsPath)).toBe(true);
const content = fs.readFileSync(requirementsPath, 'utf8');
expect(content).toContain('django');
});
test('should create .logvista directory structure', () => {
const logvistaDir = path.join(tempDir, '.logvista');
// Simulate creating .logvista directory
fs.mkdirSync(logvistaDir, { recursive: true });
expect(fs.existsSync(logvistaDir)).toBe(true);
});
});