UNPKG

node-os-utils

Version:

Advanced cross-platform operating system monitoring utilities with TypeScript support

316 lines 10 kB
"use strict"; /** * 测试基础工具类 * 提供跨平台的测试工具函数和平台检测功能 */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PerformanceMonitor = exports.TestAssertions = exports.skipOnPlatform = exports.platformTest = exports.longTest = exports.asyncTest = exports.TestConfig = exports.TestValidators = exports.PlatformUtils = void 0; const os = __importStar(require("os")); const chai_1 = require("chai"); /** * 平台检测工具类 */ class PlatformUtils { /** * 检查是否为Linux系统 */ static isLinux() { return os.platform() === 'linux'; } /** * 检查是否为macOS系统 */ static isMacOS() { return os.platform() === 'darwin'; } /** * 检查是否为Windows系统 */ static isWindows() { return os.platform() === 'win32'; } /** * 获取当前平台 */ static getCurrentPlatform() { return os.platform(); } /** * 获取平台友好名称 */ static getPlatformName() { const platform = os.platform(); switch (platform) { case 'linux': return 'Linux'; case 'darwin': return 'macOS'; case 'win32': return 'Windows'; default: return platform; } } } exports.PlatformUtils = PlatformUtils; /** * 测试验证工具类 */ class TestValidators { /** * 检查是否为有效的数字 */ static isValidNumber(num) { return typeof num === 'number' && !isNaN(num) && isFinite(num); } /** * 检查是否为有效的百分比 (0-100) */ static isValidPercentage(num) { return this.isValidNumber(num) && num >= 0 && num <= 100; } /** * 检查是否为正数 */ static isPositiveNumber(num) { return this.isValidNumber(num) && num >= 0; } /** * 检查是否为非空字符串 */ static isNonEmptyString(str) { return typeof str === 'string' && str.length > 0; } /** * 检查是否为有效的数组 */ static isValidArray(arr) { return Array.isArray(arr); } /** * 检查是否为有效的对象 */ static isValidObject(obj) { return typeof obj === 'object' && obj !== null && !Array.isArray(obj); } } exports.TestValidators = TestValidators; /** * 测试配置类 */ class TestConfig { } exports.TestConfig = TestConfig; /** * 默认超时时间 */ TestConfig.DEFAULT_TIMEOUT = 10000; /** * 长时间运行测试的超时时间 */ TestConfig.LONG_TIMEOUT = 30000; /** * 性能测试的最大执行时间 */ TestConfig.PERF_MAX_DURATION = 5000; /** * 内存测试的最大内存增长(MB) */ TestConfig.MAX_MEMORY_INCREASE_MB = 10; /** * 异步测试装饰器 */ function asyncTest(testFn) { return async function () { this.timeout(TestConfig.DEFAULT_TIMEOUT); await testFn.call(this); }; } exports.asyncTest = asyncTest; /** * 长时间测试装饰器 */ function longTest(testFn) { return async function () { this.timeout(TestConfig.LONG_TIMEOUT); await testFn.call(this); }; } exports.longTest = longTest; /** * 平台条件测试装饰器 */ function platformTest(platforms, testFn) { return function () { const currentPlatform = os.platform(); if (!platforms.includes(currentPlatform)) { this.skip(); return; } return testFn.call(this); }; } exports.platformTest = platformTest; /** * 跳过特定平台的测试装饰器 */ function skipOnPlatform(platforms, testFn) { return function () { const currentPlatform = os.platform(); if (platforms.includes(currentPlatform)) { this.skip(); return; } return testFn.call(this); }; } exports.skipOnPlatform = skipOnPlatform; /** * 测试数据验证助手 */ class TestAssertions { /** * 断言系统信息对象的有效性 */ static assertSystemInfo(info, requiredFields) { (0, chai_1.expect)(info).to.be.an('object'); for (const field of requiredFields) { // 检查属性是否存在 (0, chai_1.expect)(info).to.have.property(field); // 检查字段值是否为有效数字字符串或数字 const value = info[field]; if (typeof value === 'string') { (0, chai_1.expect)(TestValidators.isValidNumber(parseFloat(value))).to.be.true; (0, chai_1.expect)(parseFloat(value)).to.be.at.least(0, `${field} should be non-negative`); } else if (typeof value === 'number') { (0, chai_1.expect)(TestValidators.isValidNumber(value)).to.be.true; (0, chai_1.expect)(value).to.be.at.least(0, `${field} should be non-negative`); } else { chai_1.expect.fail(`${field} should be a number or numeric string, got ${typeof value}: ${value}`); } } } /** * 断言百分比字段的有效性 */ static assertPercentageFields(info, percentageFields) { for (const field of percentageFields) { const value = parseFloat(info[field]); (0, chai_1.expect)(TestValidators.isValidPercentage(value)).to.be.true; } } /** * 断言内存信息的一致性 */ static assertMemoryConsistency(info) { const used = parseFloat(info.usedMemMb || info.usedGb); const free = parseFloat(info.freeMemMb || info.freeGb); const total = parseFloat(info.totalMemMb || info.totalGb); const usedPercentage = parseFloat(info.usedMemPercentage || info.usedPercentage); const freePercentage = parseFloat(info.freeMemPercentage || info.freePercentage); // 验证总和关系 - 增加容差以处理浮点数精度问题 (0, chai_1.expect)(Math.abs((used + free) - total)).to.be.lessThan(2, 'Used + Free should approximately equal Total'); (0, chai_1.expect)(Math.abs((usedPercentage + freePercentage) - 100)).to.be.lessThan(2, 'Percentages should sum to approximately 100'); } /** * 断言网络统计信息的有效性 */ static assertNetworkStats(stats) { (0, chai_1.expect)(stats).to.be.an('array'); for (const stat of stats) { (0, chai_1.expect)(stat).to.have.property('interface'); (0, chai_1.expect)(stat).to.have.property('inputBytes'); (0, chai_1.expect)(stat).to.have.property('outputBytes'); (0, chai_1.expect)(TestValidators.isNonEmptyString(stat.interface)).to.be.true; (0, chai_1.expect)(TestValidators.isValidNumber(parseInt(stat.inputBytes))).to.be.true; (0, chai_1.expect)(TestValidators.isValidNumber(parseInt(stat.outputBytes))).to.be.true; (0, chai_1.expect)(parseInt(stat.inputBytes)).to.be.at.least(0); (0, chai_1.expect)(parseInt(stat.outputBytes)).to.be.at.least(0); } } } exports.TestAssertions = TestAssertions; /** * 性能监控工具 */ class PerformanceMonitor { constructor() { this.timings = {}; this.startTime = Date.now(); this.startMemory = process.memoryUsage().heapUsed; } /** * 获取执行时间(毫秒) */ getExecutionTime() { return Date.now() - this.startTime; } /** * 获取内存使用增长(字节) */ getMemoryIncrease() { return process.memoryUsage().heapUsed - this.startMemory; } /** * 验证性能是否在可接受范围内 */ assertPerformance(maxTimeMs = TestConfig.PERF_MAX_DURATION) { const executionTime = this.getExecutionTime(); (0, chai_1.expect)(executionTime).to.be.lessThan(maxTimeMs, `Execution time ${executionTime}ms exceeded maximum ${maxTimeMs}ms`); } /** * 验证内存使用是否在可接受范围内 */ assertMemoryUsage(maxIncreaseMB = TestConfig.MAX_MEMORY_INCREASE_MB) { const memoryIncrease = this.getMemoryIncrease(); const memoryIncreaseMB = memoryIncrease / (1024 * 1024); (0, chai_1.expect)(memoryIncreaseMB).to.be.lessThan(maxIncreaseMB, `Memory increase ${memoryIncreaseMB.toFixed(2)}MB exceeded maximum ${maxIncreaseMB}MB`); } /** * 计时方法 - 执行函数并记录执行时间 */ async time(label, fn) { const start = Date.now(); const result = await fn(); const end = Date.now(); this.timings[label] = end - start; return result; } /** * 获取所有计时报告 */ getReport() { return { ...this.timings }; } /** * 重置所有计时记录 */ reset() { this.timings = {}; this.startTime = Date.now(); this.startMemory = process.memoryUsage().heapUsed; } } exports.PerformanceMonitor = PerformanceMonitor; //# sourceMappingURL=test-base.js.map