Files
mcp-proc/tests/procfs-reader.test.ts
2025-10-11 03:22:03 +02:00

102 líneas
3.3 KiB
TypeScript

/**
* Basic tests for ProcFS Reader
*/
import { ProcFSReader } from '../src/lib/procfs-reader';
describe('ProcFSReader', () => {
let reader: ProcFSReader;
beforeEach(() => {
reader = new ProcFSReader();
});
describe('System Information', () => {
test('should read CPU information', async () => {
const info = await reader.getCPUInfo();
expect(info).toBeDefined();
expect(info).toHaveProperty('model');
expect(info).toHaveProperty('cores');
expect(info).toHaveProperty('processors');
expect(typeof info.cores).toBe('number');
expect(info.cores).toBeGreaterThan(0);
});
test('should read memory information', async () => {
const info = await reader.getMemInfo();
expect(info).toBeDefined();
expect(info).toHaveProperty('total');
expect(info).toHaveProperty('free');
expect(info).toHaveProperty('available');
expect(typeof info.total).toBe('number');
expect(info.total).toBeGreaterThan(0);
});
test('should read load average', async () => {
const info = await reader.getLoadAvg();
expect(info).toBeDefined();
expect(info).toHaveProperty('one');
expect(info).toHaveProperty('five');
expect(info).toHaveProperty('fifteen');
expect(typeof info.one).toBe('number');
expect(info.one).toBeGreaterThanOrEqual(0);
});
test('should read network statistics', async () => {
const stats = await reader.getNetDevStats();
expect(Array.isArray(stats)).toBe(true);
if (stats.length > 0) {
const first = stats[0];
expect(first).toHaveProperty('interface');
expect(first).toHaveProperty('rxBytes');
expect(first).toHaveProperty('txBytes');
}
});
test('should read disk statistics', async () => {
const stats = await reader.getDiskStats();
expect(Array.isArray(stats)).toBe(true);
if (stats.length > 0) {
const first = stats[0];
expect(first).toHaveProperty('device');
expect(first).toHaveProperty('readsCompleted');
expect(first).toHaveProperty('writesCompleted');
}
});
});
describe('Process Information', () => {
test('should list PIDs', async () => {
const pids = await reader.listPIDs();
expect(Array.isArray(pids)).toBe(true);
expect(pids.length).toBeGreaterThan(0);
expect(pids).toContain(1); // init/systemd should always exist
});
test('should read process information for PID 1', async () => {
const info = await reader.getProcessInfo(1);
expect(info).toBeDefined();
expect(info.pid).toBe(1);
expect(info).toHaveProperty('name');
expect(info).toHaveProperty('state');
expect(info).toHaveProperty('ppid');
});
test('should throw error for non-existent PID', async () => {
await expect(reader.getProcessInfo(999999)).rejects.toThrow();
});
});
describe('Raw File Reading', () => {
test('should read raw procfs file', async () => {
const content = await reader.readRaw('version');
expect(typeof content).toBe('string');
expect(content.length).toBeGreaterThan(0);
});
test('should throw error for non-existent file', async () => {
await expect(reader.readRaw('nonexistent')).rejects.toThrow();
});
});
});