log-vista
Version:
LogVista Agent - Lightweight system monitoring and log collection for any project/language
91 lines (74 loc) • 2.97 kB
JavaScript
/**
* Metrics Collector Tests
* Tests the system metrics collection functionality
*/
const MetricsCollector = require('../src/metricsCollector');
describe('MetricsCollector', () => {
let metricsCollector;
beforeEach(() => {
metricsCollector = new MetricsCollector();
});
test('should initialize with valid configuration', () => {
expect(metricsCollector).toBeDefined();
expect(metricsCollector.lastNetworkStats).toBeNull();
});
test('should collect system metrics', async () => {
try {
const metrics = await metricsCollector.collectSystemMetrics();
expect(metrics).toBeDefined();
expect(typeof metrics.cpuUsage).toBe('number');
expect(metrics.cpuUsage).toBeGreaterThanOrEqual(0);
expect(metrics.cpuUsage).toBeLessThanOrEqual(100);
expect(typeof metrics.ramUsage).toBe('number');
expect(typeof metrics.ramTotal).toBe('number');
expect(metrics.ramTotal).toBeGreaterThan(0);
} catch (error) {
// If system info is not available in test environment, that's okay
expect(error).toBeDefined();
}
}, 15000);
test('should collect process-specific metrics', async () => {
const projectConfig = {
pwd_path: process.cwd(),
project_name: 'Test Project'
};
try {
const processMetrics = await metricsCollector.collectProjectMetrics(projectConfig);
expect(processMetrics).toBeDefined();
expect(typeof processMetrics.cpuUsage).toBe('number');
expect(typeof processMetrics.ramUsage).toBe('number');
expect(processMetrics.ramUsage).toBeGreaterThan(0);
} catch (error) {
// If project metrics are not available in test environment, that's okay
expect(error).toBeDefined();
}
}, 15000);
test('should format metrics with timestamp', () => {
const rawMetrics = {
cpuUsage: 50.5,
ramUsage: 1024,
ramTotal: 2048
};
// Test that metrics can be formatted with timestamp
const withTimestamp = {
...rawMetrics,
timestamp: new Date().toISOString()
};
expect(withTimestamp).toBeDefined();
expect(withTimestamp.timestamp).toBeDefined();
expect(withTimestamp.cpuUsage).toBe(50.5);
expect(withTimestamp.ramUsage).toBe(1024);
});
test('should handle metrics collection errors gracefully', async () => {
// Mock a scenario where system info is not available
const originalCollectSystemMetrics = metricsCollector.collectSystemMetrics;
metricsCollector.collectSystemMetrics = jest.fn().mockRejectedValue(new Error('System info unavailable'));
try {
await metricsCollector.collectSystemMetrics();
} catch (error) {
expect(error.message).toBe('System info unavailable');
}
// Restore original method
metricsCollector.collectSystemMetrics = originalCollectSystemMetrics;
});
});