397777089e
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
96 lines
3.9 KiB
JavaScript
96 lines
3.9 KiB
JavaScript
import { describe, it, expect } from 'vitest';
|
|
import { parsePowerShellOutput, computeSystemHealthBlock } from './system-health.mjs';
|
|
|
|
describe('parsePowerShellOutput', () => {
|
|
it('parses CSV output from PowerShell Get-Process', () => {
|
|
const csv = `Id,ProcessName,CPU,StartTime
|
|
6444,python,25435.12,2026-05-27T18:30:00
|
|
1234,node,120.5,2026-05-28T07:00:00`;
|
|
const parsed = parsePowerShellOutput(csv, new Date('2026-05-28T08:00:00'));
|
|
expect(parsed).toHaveLength(2);
|
|
expect(parsed[0]).toMatchObject({ pid: 6444, name: 'python', cpuSeconds: 25435.12 });
|
|
expect(parsed[0].ageMinutes).toBeGreaterThan(800);
|
|
});
|
|
|
|
it('returns empty array on empty input', () => {
|
|
expect(parsePowerShellOutput('', new Date())).toEqual([]);
|
|
});
|
|
|
|
it('returns empty array when only header row present', () => {
|
|
expect(parsePowerShellOutput('Id,ProcessName,CPU,StartTime', new Date())).toEqual([]);
|
|
});
|
|
|
|
it('skips rows with insufficient columns', () => {
|
|
const csv = `Id,ProcessName,CPU,StartTime
|
|
6444,python`;
|
|
const result = parsePowerShellOutput(csv, new Date());
|
|
expect(result).toHaveLength(0);
|
|
});
|
|
|
|
it('computes ageMinutes from StartTime relative to now', () => {
|
|
const now = new Date('2026-05-28T10:00:00Z');
|
|
const csv = `Id,ProcessName,CPU,StartTime
|
|
100,myproc,500.0,2026-05-28T09:00:00Z`;
|
|
const result = parsePowerShellOutput(csv, now);
|
|
expect(result).toHaveLength(1);
|
|
expect(result[0].ageMinutes).toBe(60);
|
|
});
|
|
});
|
|
|
|
describe('computeSystemHealthBlock', () => {
|
|
it('returns block with top long-running processes (CPU > 3600s)', () => {
|
|
const procs = [
|
|
{ pid: 6444, name: 'python', cpuSeconds: 25435, ageMinutes: 815 },
|
|
{ pid: 1234, name: 'node', cpuSeconds: 120, ageMinutes: 60 },
|
|
];
|
|
const block = computeSystemHealthBlock(procs);
|
|
expect(block).toContain('System Health');
|
|
expect(block).toContain('6444');
|
|
expect(block).toContain('python');
|
|
expect(block).not.toContain('1234');
|
|
});
|
|
|
|
it('returns "Долго работающих процессов нет" when no procs > 3600s', () => {
|
|
const block = computeSystemHealthBlock([{ pid: 1, name: 'x', cpuSeconds: 100, ageMinutes: 10 }]);
|
|
expect(block).toContain('Долго работающих процессов нет');
|
|
});
|
|
|
|
it('returns "Долго работающих процессов нет" for empty array', () => {
|
|
const block = computeSystemHealthBlock([]);
|
|
expect(block).toContain('Долго работающих процессов нет');
|
|
});
|
|
|
|
it('returns "Долго работающих процессов нет" for null input', () => {
|
|
const block = computeSystemHealthBlock(null);
|
|
expect(block).toContain('Долго работающих процессов нет');
|
|
});
|
|
|
|
it('limits output to top 3 processes sorted by CPU descending', () => {
|
|
const procs = [
|
|
{ pid: 1, name: 'a', cpuSeconds: 4000, ageMinutes: 100 },
|
|
{ pid: 2, name: 'b', cpuSeconds: 8000, ageMinutes: 200 },
|
|
{ pid: 3, name: 'c', cpuSeconds: 5000, ageMinutes: 150 },
|
|
{ pid: 4, name: 'd', cpuSeconds: 6000, ageMinutes: 180 },
|
|
];
|
|
const block = computeSystemHealthBlock(procs);
|
|
// Should contain top 3 by CPU: b(8000), d(6000), c(5000)
|
|
expect(block).toContain('| 2 |'); // pid 2 = b
|
|
expect(block).toContain('| 4 |'); // pid 4 = d
|
|
expect(block).toContain('| 3 |'); // pid 3 = c
|
|
// pid 1 (lowest CPU among heavy) should be excluded
|
|
expect(block).not.toContain('| 1 |');
|
|
});
|
|
|
|
it('includes the orphan process warning', () => {
|
|
const procs = [{ pid: 9999, name: 'zombie', cpuSeconds: 7200, ageMinutes: 120 }];
|
|
const block = computeSystemHealthBlock(procs);
|
|
expect(block).toContain('осиротевш');
|
|
});
|
|
|
|
it('formats CPU hours correctly', () => {
|
|
const procs = [{ pid: 42, name: 'worker', cpuSeconds: 7200, ageMinutes: 200 }];
|
|
const block = computeSystemHealthBlock(procs);
|
|
expect(block).toContain('2.00ч'); // 7200 / 3600 = 2.00
|
|
});
|
|
});
|