UNPKG

node-os-utils

Version:

Advanced cross-platform operating system monitoring utilities with TypeScript support

147 lines 5.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const chai_1 = require("chai"); const adapter_factory_1 = require("../../../src/adapters/adapter-factory"); const errors_1 = require("../../../src/types/errors"); function createStubSupportedFeatures() { return { cpu: { info: true, usage: true, temperature: true, frequency: true, cache: true, perCore: true, cores: true }, memory: { info: true, usage: true, swap: true, pressure: true, detailed: true, virtual: true }, disk: { info: true, io: true, health: true, smart: true, filesystem: true, usage: true, stats: true, mounts: true, filesystems: true }, network: { interfaces: true, stats: true, connections: true, bandwidth: true, gateway: true }, process: { list: true, details: true, tree: true, monitor: true, info: true, kill: true, openFiles: true, environment: true }, system: { info: true, load: true, uptime: true, users: true, services: true } }; } function createStubAdapter() { const features = createStubSupportedFeatures(); return { getPlatform: () => 'linux', isSupported: () => true, executeCommand: async (command) => ({ stdout: `/usr/bin/${command.split(/\s+/).pop()}`, stderr: '', exitCode: 0, platform: 'linux', executionTime: 1, command }), readFile: async () => '', fileExists: async () => true, getCPUInfo: async () => ({}), getCPUUsage: async () => ({}), getCPUTemperature: async () => ({}), getMemoryInfo: async () => ({}), getMemoryUsage: async () => ({}), getDiskInfo: async () => ({}), getDiskIO: async () => ({}), getNetworkInterfaces: async () => ({}), getNetworkStats: async () => ({}), getProcesses: async () => ({}), getProcessInfo: async () => ({}), getSystemInfo: async () => ({}), getSystemLoad: async () => ({}), getDiskUsage: async () => ({}), getDiskStats: async () => ({}), getMounts: async () => ({}), getFileSystems: async () => ({}), getNetworkConnections: async () => ({}), getDefaultGateway: async () => ({}), getProcessList: async () => ({}), killProcess: async () => true, getProcessOpenFiles: async () => [], getProcessEnvironment: async () => ({}), getSystemUptime: async () => ({}), getSystemUsers: async () => ({}), getSystemServices: async () => ({}), getSupportedFeatures: () => features }; } describe('AdapterFactory', () => { afterEach(() => { adapter_factory_1.AdapterFactory.clearCache(); }); it('缓存已创建的平台适配器实例', () => { const first = adapter_factory_1.AdapterFactory.create('darwin'); const second = adapter_factory_1.AdapterFactory.create('darwin'); (0, chai_1.expect)(second).to.equal(first); }); it('不支持的平台会抛出 MonitorError', () => { (0, chai_1.expect)(() => adapter_factory_1.AdapterFactory.create('solaris')).to.throw(errors_1.MonitorError) .and.have.property('code', errors_1.ErrorCode.PLATFORM_NOT_SUPPORTED); }); it('支持常见平台别名转换', () => { const adapter = adapter_factory_1.AdapterFactory.create('macos'); (0, chai_1.expect)(adapter.getPlatform()).to.equal('darwin'); }); it('checkPlatformCapabilities 能汇总适配器能力', async () => { const originalCreate = adapter_factory_1.AdapterFactory.create.bind(adapter_factory_1.AdapterFactory); adapter_factory_1.AdapterFactory.create = () => createStubAdapter(); try { const result = await adapter_factory_1.AdapterFactory.checkPlatformCapabilities('linux'); (0, chai_1.expect)(result.supported).to.be.true; (0, chai_1.expect)(result.capabilities.features).to.include('cpu.info'); (0, chai_1.expect)(result.capabilities.commands).to.include('ps'); (0, chai_1.expect)(result.capabilities.files).to.include('/proc/cpuinfo'); } finally { adapter_factory_1.AdapterFactory.create = originalCreate; } }); it('checkPlatformCapabilities 在不支持的平台返回问题列表', async () => { const result = await adapter_factory_1.AdapterFactory.checkPlatformCapabilities('aix'); (0, chai_1.expect)(result.supported).to.be.false; (0, chai_1.expect)(result.issues[0]).to.include('not supported'); }); it('getPlatformDisplayName 返回友好名称', () => { (0, chai_1.expect)(adapter_factory_1.AdapterFactory.getPlatformDisplayName('mac')).to.equal('macOS'); (0, chai_1.expect)(adapter_factory_1.AdapterFactory.getPlatformDisplayName('win')).to.equal('Windows'); }); }); //# sourceMappingURL=adapter-factory.test.js.map