UNPKG

node-os-utils

Version:

Advanced cross-platform operating system monitoring utilities with TypeScript support

184 lines 10.3 kB
"use strict"; /** * 错误处理单元测试 * 测试错误类型、错误码和错误处理机制 */ Object.defineProperty(exports, "__esModule", { value: true }); const chai_1 = require("chai"); const errors_1 = require("../../../src/types/errors"); describe('Error Handling Unit Tests', function () { describe('ErrorCode枚举', function () { it('应该包含所有预期的错误码', function () { const expectedCodes = [ 'PLATFORM_NOT_SUPPORTED', 'COMMAND_FAILED', 'PARSE_ERROR', 'PERMISSION_DENIED', 'TIMEOUT', 'INVALID_CONFIG', 'NOT_AVAILABLE', 'FILE_NOT_FOUND', 'NETWORK_ERROR' ]; expectedCodes.forEach(code => { (0, chai_1.expect)(errors_1.ErrorCode).to.have.property(code); (0, chai_1.expect)(errors_1.ErrorCode[code]).to.be.a('string'); }); }); it('错误码应该是字符串类型', function () { Object.values(errors_1.ErrorCode).forEach(code => { (0, chai_1.expect)(code).to.be.a('string'); (0, chai_1.expect)(code.length).to.be.greaterThan(0); }); }); }); describe('MonitorError创建', function () { it('应该能够创建基本的MonitorError', function () { const error = new errors_1.MonitorError('Test error message', errors_1.ErrorCode.COMMAND_FAILED, 'test-platform'); (0, chai_1.expect)(error.code).to.equal(errors_1.ErrorCode.COMMAND_FAILED); (0, chai_1.expect)(error.message).to.equal('Test error message'); (0, chai_1.expect)(error.platform).to.equal('test-platform'); (0, chai_1.expect)(error.name).to.equal('MonitorError'); (0, chai_1.expect)(error).to.be.instanceof(Error); }); it('应该能够创建包含详细信息的MonitorError', function () { const details = { command: 'ps aux', exitCode: 1, stderr: 'Permission denied' }; const error = new errors_1.MonitorError('Failed to execute command', errors_1.ErrorCode.PERMISSION_DENIED, 'linux', details); (0, chai_1.expect)(error.details).to.deep.equal(details); (0, chai_1.expect)(error.code).to.equal(errors_1.ErrorCode.PERMISSION_DENIED); (0, chai_1.expect)(error.platform).to.equal('linux'); }); }); describe('静态工厂方法', function () { it('应该能够创建平台不支持错误', function () { const error = errors_1.MonitorError.createPlatformNotSupported('win32', 'temperature'); (0, chai_1.expect)(error.code).to.equal(errors_1.ErrorCode.PLATFORM_NOT_SUPPORTED); (0, chai_1.expect)(error.platform).to.equal('win32'); (0, chai_1.expect)(error.message).to.include('temperature'); (0, chai_1.expect)(error.details).to.have.property('feature', 'temperature'); }); it('应该能够创建命令执行失败错误', function () { const error = errors_1.MonitorError.createCommandFailed('linux', 'ps aux', { exitCode: 1 }); (0, chai_1.expect)(error.code).to.equal(errors_1.ErrorCode.COMMAND_FAILED); (0, chai_1.expect)(error.platform).to.equal('linux'); (0, chai_1.expect)(error.message).to.include('ps aux'); (0, chai_1.expect)(error.details).to.have.property('command', 'ps aux'); (0, chai_1.expect)(error.details).to.have.property('exitCode', 1); }); it('应该能够创建解析错误', function () { const error = errors_1.MonitorError.createParseError('darwin', 'invalid data', 'malformed JSON'); (0, chai_1.expect)(error.code).to.equal(errors_1.ErrorCode.PARSE_ERROR); (0, chai_1.expect)(error.platform).to.equal('darwin'); (0, chai_1.expect)(error.message).to.include('malformed JSON'); (0, chai_1.expect)(error.details).to.have.property('data', 'invalid data'); (0, chai_1.expect)(error.details).to.have.property('reason', 'malformed JSON'); }); }); describe('错误序列化', function () { it('应该能够序列化为JSON', function () { const error = new errors_1.MonitorError('Serialization test', errors_1.ErrorCode.COMMAND_FAILED, 'test', { extra: 'data' }); const json = error.toJSON(); (0, chai_1.expect)(json).to.have.property('name', 'MonitorError'); (0, chai_1.expect)(json).to.have.property('message', 'Serialization test'); (0, chai_1.expect)(json).to.have.property('code', errors_1.ErrorCode.COMMAND_FAILED); (0, chai_1.expect)(json).to.have.property('platform', 'test'); (0, chai_1.expect)(json).to.have.property('details'); (0, chai_1.expect)(json.details).to.deep.equal({ extra: 'data' }); }); it('应该能够完整序列化到JSON字符串', function () { const error = new errors_1.MonitorError('JSON string test', errors_1.ErrorCode.TIMEOUT, 'network'); const jsonString = JSON.stringify(error); const parsed = JSON.parse(jsonString); (0, chai_1.expect)(parsed.name).to.equal('MonitorError'); (0, chai_1.expect)(parsed.message).to.equal('JSON string test'); (0, chai_1.expect)(parsed.code).to.equal(errors_1.ErrorCode.TIMEOUT); (0, chai_1.expect)(parsed.platform).to.equal('network'); }); }); describe('错误比较和匹配', function () { it('应该能够比较错误类型', function () { const error1 = new errors_1.MonitorError('First error', errors_1.ErrorCode.TIMEOUT, 'cpu'); const error2 = new errors_1.MonitorError('Second error', errors_1.ErrorCode.TIMEOUT, 'memory'); const error3 = new errors_1.MonitorError('Third error', errors_1.ErrorCode.PERMISSION_DENIED, 'cpu'); (0, chai_1.expect)(error1.code).to.equal(error2.code); (0, chai_1.expect)(error1.code).to.not.equal(error3.code); (0, chai_1.expect)(error1.platform).to.equal(error3.platform); (0, chai_1.expect)(error1.platform).to.not.equal(error2.platform); }); it('应该能够匹配错误模式', function () { const errors = [ new errors_1.MonitorError('CPU timeout', errors_1.ErrorCode.TIMEOUT, 'cpu'), new errors_1.MonitorError('Memory access denied', errors_1.ErrorCode.PERMISSION_DENIED, 'memory'), new errors_1.MonitorError('Disk command failed', errors_1.ErrorCode.COMMAND_FAILED, 'disk'), new errors_1.MonitorError('Network timeout', errors_1.ErrorCode.TIMEOUT, 'network') ]; // 找出所有超时错误 const timeoutErrors = errors.filter(error => error.code === errors_1.ErrorCode.TIMEOUT); (0, chai_1.expect)(timeoutErrors).to.have.length(2); // 找出所有CPU相关错误 const cpuErrors = errors.filter(error => error.platform === 'cpu'); (0, chai_1.expect)(cpuErrors).to.have.length(1); (0, chai_1.expect)(cpuErrors[0].code).to.equal(errors_1.ErrorCode.TIMEOUT); }); }); describe('边界情况', function () { it('应该处理空消息', function () { const error = new errors_1.MonitorError('', errors_1.ErrorCode.PARSE_ERROR, 'test'); (0, chai_1.expect)(error.message).to.equal(''); (0, chai_1.expect)(error.code).to.equal(errors_1.ErrorCode.PARSE_ERROR); }); it('应该处理特殊字符', function () { const specialMessage = 'Error with 特殊字符 and émojis 🚫'; const error = new errors_1.MonitorError(specialMessage, errors_1.ErrorCode.INVALID_CONFIG, 'test'); (0, chai_1.expect)(error.message).to.equal(specialMessage); }); it('应该处理长错误消息', function () { const longMessage = 'A'.repeat(1000); const error = new errors_1.MonitorError(longMessage, errors_1.ErrorCode.COMMAND_FAILED, 'test'); (0, chai_1.expect)(error.message).to.equal(longMessage); (0, chai_1.expect)(error.message.length).to.equal(1000); }); it('应该处理复杂的详细信息对象', function () { const complexDetails = { nested: { object: true, array: [1, 2, 3], null_value: null, undefined_value: undefined } }; (0, chai_1.expect)(() => { new errors_1.MonitorError('Complex details', errors_1.ErrorCode.INVALID_CONFIG, 'test', complexDetails); }).to.not.throw(); }); }); describe('错误继承', function () { it('应该正确继承Error类', function () { const error = new errors_1.MonitorError('Inheritance test', errors_1.ErrorCode.COMMAND_FAILED, 'test'); (0, chai_1.expect)(error).to.be.instanceof(Error); (0, chai_1.expect)(error).to.be.instanceof(errors_1.MonitorError); (0, chai_1.expect)(error.name).to.equal('MonitorError'); (0, chai_1.expect)(error.stack).to.be.a('string'); }); }); describe('多语言错误消息', function () { it('应该支持不同语言的错误消息', function () { const messages = { en: 'File not found', zh: '文件未找到', ja: 'ファイルが見つかりません', fr: 'Fichier non trouvé' }; Object.entries(messages).forEach(([lang, message]) => { const error = new errors_1.MonitorError(message, errors_1.ErrorCode.FILE_NOT_FOUND, 'file-system'); (0, chai_1.expect)(error.message).to.equal(message); (0, chai_1.expect)(error.code).to.equal(errors_1.ErrorCode.FILE_NOT_FOUND); }); }); }); }); //# sourceMappingURL=errors.test.js.map