node-os-utils
Version:
Advanced cross-platform operating system monitoring utilities with TypeScript support
186 lines • 8.04 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const platform_adapter_1 = require("../../../src/core/platform-adapter");
const errors_1 = require("../../../src/types/errors");
class TestPlatformAdapter extends platform_adapter_1.BasePlatformAdapter {
constructor() {
super('test');
}
initializeSupportedFeatures() {
return {
cpu: { info: true, usage: true, temperature: false, frequency: false, cache: false, perCore: true, cores: true },
memory: { info: true, usage: true, swap: false, pressure: false, detailed: false, virtual: false },
disk: { info: true, io: false, health: false, smart: false, filesystem: false, usage: false, stats: false, mounts: false, filesystems: false },
network: { interfaces: true, stats: true, connections: false, bandwidth: false, gateway: false },
process: { list: true, details: false, tree: false, monitor: false, info: false, kill: false, openFiles: false, environment: false },
system: { info: true, load: true, uptime: true, users: false, services: false }
};
}
async executeCommand(command) {
if (command === 'throw') {
throw new Error('failed');
}
if (command === 'fail') {
return {
stdout: '',
stderr: 'failed',
exitCode: 1,
platform: this.getPlatform(),
executionTime: 5,
command
};
}
if (command === 'empty') {
return {
stdout: '',
stderr: '',
exitCode: 0,
platform: this.getPlatform(),
executionTime: 1,
command
};
}
return {
stdout: 'ok',
stderr: '',
exitCode: 0,
platform: this.getPlatform(),
executionTime: 1,
command
};
}
async readFile() { return ''; }
async fileExists() { return true; }
async getCPUInfo() { return {}; }
async getCPUUsage() { return {}; }
async getCPUTemperature() { return {}; }
async getMemoryInfo() { return {}; }
async getMemoryUsage() { return {}; }
async getDiskInfo() { return {}; }
async getDiskIO() { return {}; }
async getNetworkInterfaces() { return {}; }
async getNetworkStats() { return {}; }
async getProcesses() { return []; }
async getProcessInfo() { return {}; }
async getSystemInfo() { return {}; }
async getSystemLoad() { return {}; }
async getDiskUsage() { return {}; }
async getDiskStats() { return {}; }
async getMounts() { return {}; }
async getFileSystems() { return {}; }
async getNetworkConnections() { return {}; }
async getDefaultGateway() { return {}; }
async getProcessList() { return []; }
async killProcess() { return true; }
async getProcessOpenFiles() { return []; }
async getProcessEnvironment() { return {}; }
async getSystemUptime() { return {}; }
async getSystemUsers() { return []; }
async getSystemServices() { return []; }
safeExecutePublic(command) {
return this.safeExecute(command);
}
validateCommandResultPublic(result) {
return this.validateCommandResult(result, result.command);
}
convertToBytesPublic(value, unit) {
return this.convertToBytes(value, unit);
}
parseKeyValueOutputPublic(output, separator) {
return this.parseKeyValueOutput(output, separator);
}
parseTableOutputPublic(output, hasHeader) {
return this.parseTableOutput(output, hasHeader);
}
splitTableRowPublic(row) {
return this.splitTableRow(row);
}
safeParseJSONPublic(data, fallback) {
return this.safeParseJSON(data, fallback);
}
safeParseNumberPublic(value, fallback) {
return this.safeParseNumber(value, fallback);
}
safeParseIntPublic(value, fallback, radix) {
return this.safeParseInt(value, fallback, radix);
}
}
describe('BasePlatformAdapter', () => {
it('isSupported 根据 supportedFeatures 判断功能支持', () => {
const adapter = new TestPlatformAdapter();
(0, chai_1.expect)(adapter.isSupported('cpu.info')).to.be.true;
(0, chai_1.expect)(adapter.isSupported('cpu.temperature')).to.be.false;
(0, chai_1.expect)(adapter.isSupported('memory.swap')).to.be.false;
});
it('getSupportedFeatures 返回浅拷贝', () => {
const adapter = new TestPlatformAdapter();
const features = adapter.getSupportedFeatures();
const featuresAgain = adapter.getSupportedFeatures();
(0, chai_1.expect)(features).to.not.equal(featuresAgain);
features.cpu = { ...features.cpu, info: false };
(0, chai_1.expect)(adapter.isSupported('cpu.info')).to.be.true;
});
it('safeExecute 会捕获执行异常并返回失败结果', async () => {
const adapter = new TestPlatformAdapter();
const result = await adapter.safeExecutePublic('throw');
(0, chai_1.expect)(result.exitCode).to.equal(1);
(0, chai_1.expect)(result.stderr).to.include('failed');
});
it('validateCommandResult 遇到非零退出码会抛出错误', () => {
const adapter = new TestPlatformAdapter();
(0, chai_1.expect)(() => adapter.validateCommandResultPublic({
stdout: '',
stderr: 'failed',
exitCode: 1,
platform: 'test',
executionTime: 1,
command: 'fail'
})).to.throw(errors_1.MonitorError);
});
it('validateCommandResult 输出为空时抛出错误', () => {
const adapter = new TestPlatformAdapter();
(0, chai_1.expect)(() => adapter.validateCommandResultPublic({
stdout: '',
stderr: '',
exitCode: 0,
platform: 'test',
executionTime: 1,
command: 'empty'
})).to.throw(errors_1.MonitorError);
});
it('convertToBytes 可以解析带单位的字符串', () => {
const adapter = new TestPlatformAdapter();
(0, chai_1.expect)(adapter.convertToBytesPublic('1 GB')).to.equal(1024 * 1024 * 1024);
(0, chai_1.expect)(adapter.convertToBytesPublic(2, 'MB')).to.equal(2 * 1024 * 1024);
});
it('parseKeyValueOutput 能解析键值对文本', () => {
const adapter = new TestPlatformAdapter();
const result = adapter.parseKeyValueOutputPublic('Key: Value\nOther: Data');
(0, chai_1.expect)(result).to.deep.equal({ Key: 'Value', Other: 'Data' });
});
it('parseTableOutput 能按表头映射数据', () => {
const adapter = new TestPlatformAdapter();
const table = adapter.parseTableOutputPublic('NAME VALUE\nCPU 20\nMEM 30');
(0, chai_1.expect)(table).to.deep.equal([
{ NAME: 'CPU', VALUE: '20' },
{ NAME: 'MEM', VALUE: '30' }
]);
});
it('splitTableRow 可分割多空格分隔的行', () => {
const adapter = new TestPlatformAdapter();
(0, chai_1.expect)(adapter.splitTableRowPublic('CPU 20 %')).to.deep.equal(['CPU', '20', '%']);
});
it('safeParseJSON 在解析失败时返回 fallback', () => {
const adapter = new TestPlatformAdapter();
(0, chai_1.expect)(adapter.safeParseJSONPublic('not json', { ok: false })).to.deep.equal({ ok: false });
});
it('safeParseNumber 与 safeParseInt 按需返回默认值', () => {
const adapter = new TestPlatformAdapter();
(0, chai_1.expect)(adapter.safeParseNumberPublic('12.5')).to.equal(12.5);
(0, chai_1.expect)(adapter.safeParseNumberPublic('bad', 3)).to.equal(3);
(0, chai_1.expect)(adapter.safeParseIntPublic('10', 0, 10)).to.equal(10);
(0, chai_1.expect)(adapter.safeParseIntPublic('zz', 7, 10)).to.equal(7);
});
});
//# sourceMappingURL=platform-adapter.test.js.map