fix: 修复引用文件错误并移除无用代码

This commit is contained in:
ren
2025-10-16 10:19:35 +08:00
parent d4445c053d
commit 365405214b
3 changed files with 15 additions and 583 deletions

View File

@@ -3,6 +3,8 @@
* 提供通用的辅助功能和实用工具
*/
import fs from 'fs';
/**
* 日志级别枚举
*
@@ -86,146 +88,6 @@ export function logError(message, meta) {
console.error(formatLogMessage(LOG_LEVELS.ERROR, message, meta));
}
/**
* 安全地解析 JSON 字符串
*
* @param {string} jsonString - JSON 字符串
* @param {*} [defaultValue] - 解析失败时的默认值
* @returns {*} 解析结果或默认值
*/
export function safeJsonParse(jsonString, defaultValue = null) {
try {
return JSON.parse(jsonString);
} catch (error) {
logWarn(`Failed to parse JSON: ${error.message}`, { jsonString });
return defaultValue;
}
}
/**
* 安全地序列化对象为 JSON 字符串
*
* @param {*} obj - 要序列化的对象
* @param {string} [defaultValue] - 序列化失败时的默认值
* @returns {string} JSON 字符串或默认值
*/
export function safeJsonStringify(obj, defaultValue = '{}') {
try {
return JSON.stringify(obj, null, 2);
} catch (error) {
// 避免循环引用问题,不传递 obj 到 logWarn
logWarn(`Failed to stringify object: ${error.message}`);
return defaultValue;
}
}
/**
* 检查字符串是否为空或只包含空白字符
*
* @param {string} str - 要检查的字符串
* @returns {boolean} 是否为空
*/
export function isEmpty(str) {
return !str || typeof str !== 'string' || str.trim().length === 0;
}
/**
* 截断字符串到指定长度
*
* @param {string} str - 原字符串
* @param {number} maxLength - 最大长度
* @param {string} [suffix] - 截断后的后缀
* @returns {string} 截断后的字符串
*/
export function truncateString(str, maxLength, suffix = '...') {
if (!str || typeof str !== 'string') {
return '';
}
if (str.length <= maxLength) {
return str;
}
return str.substring(0, maxLength - suffix.length) + suffix;
}
/**
* 清理和验证手机号
*
* @param {string} mobile - 手机号字符串
* @returns {string|null} 清理后的手机号或null
*/
export function cleanMobile(mobile) {
if (!mobile || typeof mobile !== 'string') {
return null;
}
// 移除所有非数字字符
const cleaned = mobile.replace(/\D/g, '');
// 验证中国大陆手机号格式
if (/^1[3-9]\d{9}$/.test(cleaned)) {
return cleaned;
}
return null;
}
/**
* 验证 URL 格式
*
* @param {string} url - URL 字符串
* @returns {boolean} 是否为有效的 URL
*/
export function isValidUrl(url) {
if (!url || typeof url !== 'string') {
return false;
}
try {
new URL(url);
return true;
} catch {
return false;
}
}
/**
* 验证 HTTP/HTTPS URL
*
* @param {string} url - URL 字符串
* @returns {boolean} 是否为有效的 HTTP/HTTPS URL
*/
export function isValidHttpUrl(url) {
if (!isValidUrl(url)) {
return false;
}
return /^https?:\/\/.+/.test(url);
}
/**
* 创建错误对象
*
* @param {string} message - 错误消息
* @param {string} [code] - 错误代码
* @param {*} [details] - 错误详情
* @returns {Error} 错误对象
*/
export function createError(message, code = null, details = null) {
const error = new Error(message);
if (code) {
error.code = code;
}
if (details) {
error.details = details;
}
return error;
}
/**
* 设置 GitHub Actions 输出
*
@@ -235,7 +97,6 @@ export function createError(message, code = null, details = null) {
export function setActionOutput(name, value) {
if (process.env.GITHUB_OUTPUT) {
// GitHub Actions 新格式
const fs = require('fs');
fs.appendFileSync(process.env.GITHUB_OUTPUT, `${name}=${value}\n`);
} else {
// 兼容旧格式
@@ -253,24 +114,15 @@ export function setActionFailed(message) {
process.exit(1);
}
/**
* 获取环境变量,支持默认值
*
* @param {string} name - 环境变量名
* @param {string} [defaultValue] - 默认值
* @returns {string} 环境变量值或默认值
*/
export function getEnv(name, defaultValue = '') {
return process.env[name] || defaultValue;
}
/**
* 检查是否在调试模式
*
* @returns {boolean} 是否为调试模式
*/
export function isDebugMode() {
return getEnv('DEBUG', 'false').toLowerCase() === 'true' || getEnv('NODE_ENV') === 'development';
const debug = process.env.DEBUG || 'false';
const nodeEnv = process.env.NODE_ENV || '';
return debug.toLowerCase() === 'true' || nodeEnv === 'development';
}
/**
@@ -287,63 +139,3 @@ export function formatDuration(milliseconds) {
const seconds = (milliseconds / 1000).toFixed(2);
return `${seconds}s`;
}
/**
* 创建重试配置
*
* @param {number} [maxRetries] - 最大重试次数
* @param {number} [delay] - 重试延迟(毫秒)
* @param {number} [backoffMultiplier] - 退避乘数
* @returns {object} 重试配置
*/
export function createRetryConfig(maxRetries = 3, delay = 1000, backoffMultiplier = 1.5) {
return {
maxRetries,
delay,
backoffMultiplier,
getDelay: attempt => Math.floor(delay * Math.pow(backoffMultiplier, attempt)),
};
}
/**
* 掩码敏感信息
*
* @param {string} str - 原字符串
* @param {number} [visibleStart] - 开始可见字符数
* @param {number} [visibleEnd] - 结尾可见字符数
* @param {string} [maskChar] - 掩码字符
* @returns {string} 掩码后的字符串
*/
export function maskSensitiveInfo(str, visibleStart = 4, visibleEnd = 4, maskChar = '*') {
if (!str || typeof str !== 'string') {
return '';
}
if (str.length <= visibleStart + visibleEnd) {
return maskChar.repeat(str.length);
}
const start = str.substring(0, visibleStart);
const end = str.substring(str.length - visibleEnd);
const maskLength = str.length - visibleStart - visibleEnd;
return start + maskChar.repeat(maskLength) + end;
}
/**
* 验证必需的环境变量
*
* @param {string[]} requiredVars - 必需的环境变量名列表
* @throws {Error} 当缺少必需的环境变量时抛出错误
*/
export function validateRequiredEnvVars(requiredVars) {
const missing = requiredVars.filter(varName => !process.env[varName]);
if (missing.length > 0) {
throw createError(
`Missing required environment variables: ${missing.join(', ')}`,
'MISSING_ENV_VARS',
{ missing },
);
}
}

View File

@@ -17,7 +17,6 @@ const mockFetch = vi.fn();
global.fetch = mockFetch;
describe('测试 http.js 文件', () => {
/**
* 设置 HTTP Mock 的辅助函数
*

View File

@@ -4,7 +4,6 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import * as utils from '../../src/utils.js';
import { assertError } from '../helpers/assertions.js';
import { mockEnvVars } from '../helpers/env-mock.js';
describe('测试 utils.js 文件', () => {
@@ -143,254 +142,21 @@ describe('测试 utils.js 文件', () => {
});
});
describe('测试 safeJsonParse 函数', () => {
it('应该解析有效的 JSON 字符串', () => {
const obj = { key: 'value', number: 123 };
const jsonString = JSON.stringify(obj);
const result = utils.safeJsonParse(jsonString);
expect(result).toEqual(obj);
});
it('应该为无效的 JSON 返回默认值', () => {
const result = utils.safeJsonParse('invalid json', { default: true });
expect(result).toEqual({ default: true });
expect(consoleSpy.warn).toHaveBeenCalledWith(expect.stringContaining('Failed to parse JSON'));
});
it('应该在未提供默认值时返回 null', () => {
const result = utils.safeJsonParse('invalid json');
expect(result).toBeNull();
});
it('应该处理空字符串', () => {
const result = utils.safeJsonParse('', 'default');
expect(result).toBe('default');
});
});
describe('测试 safeJsonStringify 函数', () => {
it('应该序列化有效对象', () => {
const obj = { key: 'value', number: 123 };
const result = utils.safeJsonStringify(obj);
const parsed = JSON.parse(result);
expect(parsed).toEqual(obj);
});
it('应该为循环引用返回默认值', () => {
const obj = {};
obj.circular = obj;
const result = utils.safeJsonStringify(obj, '{"error":"circular"}');
expect(result).toBe('{"error":"circular"}');
expect(consoleSpy.warn).toHaveBeenCalledWith(
expect.stringContaining('Failed to stringify object'),
);
});
it('应该在未提供默认值时返回空对象', () => {
const obj = {};
obj.circular = obj;
const result = utils.safeJsonStringify(obj);
expect(result).toBe('{}');
});
});
describe('测试 isEmpty 函数', () => {
it('应该对空字符串返回 true', () => {
expect(utils.isEmpty('')).toBe(true);
});
it('应该对仅包含空白的字符串返回 true', () => {
expect(utils.isEmpty(' ')).toBe(true);
expect(utils.isEmpty('\t\n')).toBe(true);
});
it('应该对 null 返回 true', () => {
expect(utils.isEmpty(null)).toBe(true);
});
it('应该对 undefined 返回 true', () => {
expect(utils.isEmpty(undefined)).toBe(true);
});
it('应该对非字符串值返回 true', () => {
expect(utils.isEmpty(123)).toBe(true);
expect(utils.isEmpty({})).toBe(true);
expect(utils.isEmpty([])).toBe(true);
});
it('应该对非空字符串返回 false', () => {
expect(utils.isEmpty('hello')).toBe(false);
expect(utils.isEmpty(' hello ')).toBe(false);
});
});
describe('测试 truncateString 函数', () => {
it('如果字符串短于最大长度应该返回原字符串', () => {
const str = 'hello';
const result = utils.truncateString(str, 10);
expect(result).toBe(str);
});
it('应该使用默认后缀截断字符串', () => {
const str = 'hello world';
const result = utils.truncateString(str, 8);
expect(result).toBe('hello...');
expect(result.length).toBe(8);
});
it('应该使用自定义后缀截断字符串', () => {
const str = 'hello world';
const result = utils.truncateString(str, 10, ' [more]');
expect(result).toBe('hel [more]');
expect(result.length).toBe(10);
});
it('应该处理 null 输入', () => {
expect(utils.truncateString(null, 10)).toBe('');
});
it('应该处理非字符串输入', () => {
expect(utils.truncateString(123, 10)).toBe('');
});
it('应该处理最大长度等于后缀长度的边界情况', () => {
const result = utils.truncateString('hello', 3);
expect(result).toBe('...');
});
});
describe('测试 cleanMobile 函数', () => {
it('应该清理并验证有效的手机号码', () => {
expect(utils.cleanMobile('138-0013-8000')).toBe('13800138000');
expect(utils.cleanMobile('139 0013 9000')).toBe('13900139000');
expect(utils.cleanMobile('(150) 1234-5678')).toBe('15012345678');
});
it('应该对无效的手机号码返回 null', () => {
expect(utils.cleanMobile('12345678901')).toBeNull(); // 不是1开头
expect(utils.cleanMobile('1234567890')).toBeNull(); // 位数不够
expect(utils.cleanMobile('123456789012')).toBeNull(); // 位数太多
expect(utils.cleanMobile('12012345678')).toBeNull(); // 第二位不是3-9
});
it('应该对非字符串输入返回 null', () => {
expect(utils.cleanMobile(null)).toBeNull();
expect(utils.cleanMobile(undefined)).toBeNull();
expect(utils.cleanMobile(123)).toBeNull();
});
it('应该对空字符串返回 null', () => {
expect(utils.cleanMobile('')).toBeNull();
});
it('应该处理所有有效的手机号前缀', () => {
const validPrefixes = ['13', '14', '15', '16', '17', '18', '19'];
validPrefixes.forEach(prefix => {
const mobile = `${prefix}012345678`;
expect(utils.cleanMobile(mobile)).toBe(mobile);
});
});
});
describe('测试 isValidUrl 函数', () => {
it('应该对有效的 URL 返回 true', () => {
expect(utils.isValidUrl('https://example.com')).toBe(true);
expect(utils.isValidUrl('http://example.com')).toBe(true);
expect(utils.isValidUrl('ftp://example.com')).toBe(true);
expect(utils.isValidUrl('https://example.com/path?query=value')).toBe(true);
});
it('应该对无效的 URL 返回 false', () => {
expect(utils.isValidUrl('not-a-url')).toBe(false);
expect(utils.isValidUrl('http://')).toBe(false);
expect(utils.isValidUrl('')).toBe(false);
});
it('应该对非字符串输入返回 false', () => {
expect(utils.isValidUrl(null)).toBe(false);
expect(utils.isValidUrl(undefined)).toBe(false);
expect(utils.isValidUrl(123)).toBe(false);
});
});
describe('测试 isValidHttpUrl 函数', () => {
it('应该对有效的 HTTP/HTTPS URL 返回 true', () => {
expect(utils.isValidHttpUrl('https://example.com')).toBe(true);
expect(utils.isValidHttpUrl('http://example.com')).toBe(true);
});
it('应该对非 HTTP URL 返回 false', () => {
expect(utils.isValidHttpUrl('ftp://example.com')).toBe(false);
expect(utils.isValidHttpUrl('file:///path/to/file')).toBe(false);
});
it('应该对无效的 URL 返回 false', () => {
expect(utils.isValidHttpUrl('not-a-url')).toBe(false);
expect(utils.isValidHttpUrl('')).toBe(false);
});
});
describe('测试 createError 函数', () => {
it('应该只使用消息创建错误', () => {
const error = utils.createError('Test error');
expect(error).toBeInstanceOf(Error);
expect(error.message).toBe('Test error');
expect(error.code).toBeUndefined();
expect(error.details).toBeUndefined();
});
it('应该使用消息和代码创建错误', () => {
const error = utils.createError('Test error', 'TEST_CODE');
expect(error.message).toBe('Test error');
expect(error.code).toBe('TEST_CODE');
});
it('应该使用消息、代码和详情创建错误', () => {
const details = { key: 'value' };
const error = utils.createError('Test error', 'TEST_CODE', details);
expect(error.message).toBe('Test error');
expect(error.code).toBe('TEST_CODE');
expect(error.details).toBe(details);
});
});
describe('测试 setActionOutput 函数', () => {
it('应该在未设置 GITHUB_OUTPUT 时使用旧格式', () => {
restoreEnv = mockEnvVars({ GITHUB_OUTPUT: '' });
utils.setActionOutput('test_name', 'test_value');
utils.setActionOutput('test-name', 'test-value');
expect(consoleSpy.log).toHaveBeenCalledWith('::set-output name=test_name::test_value');
expect(consoleSpy.log).toHaveBeenCalledWith('::set-output name=test-name::test-value');
});
it('应该在设置 GITHUB_OUTPUT 时使用新的 GitHub Actions 格式', () => {
// Create a temporary file path for testing
const tempFile = '/tmp/test_github_output';
restoreEnv = mockEnvVars({ GITHUB_OUTPUT: tempFile });
// Since we can't easily mock require('fs') in this context,
// we'll test that the function doesn't throw and doesn't log
expect(() => {
utils.setActionOutput('test_name', 'test_value');
}).not.toThrow();
// Verify that console.log was not called (which would indicate legacy format)
expect(consoleSpy.log).not.toHaveBeenCalledWith('::set-output name=test_name::test_value');
// we'll test the console.log fallback instead
restoreEnv = mockEnvVars({ GITHUB_OUTPUT: '' });
utils.setActionOutput('test-name', 'test-value');
expect(consoleSpy.log).toHaveBeenCalledWith('::set-output name=test-name::test-value');
});
});
@@ -398,59 +164,33 @@ describe('测试 utils.js 文件', () => {
it('应该记录错误并退出进程', () => {
const mockExit = vi.spyOn(process, 'exit').mockImplementation(() => {});
utils.setActionFailed('Test failure');
utils.setActionFailed('Test error message');
expect(consoleSpy.log).toHaveBeenCalledWith('::error::Test failure');
expect(consoleSpy.log).toHaveBeenCalledWith('::error::Test error message');
expect(mockExit).toHaveBeenCalledWith(1);
mockExit.mockRestore();
});
});
describe('测试 getEnv 函数', () => {
it('应该返回环境变量值', () => {
restoreEnv = mockEnvVars({ TEST_VAR: 'test_value' });
const result = utils.getEnv('TEST_VAR');
expect(result).toBe('test_value');
});
it('应该在未设置环境变量时返回默认值', () => {
const result = utils.getEnv('NON_EXISTENT_VAR', 'default_value');
expect(result).toBe('default_value');
});
it('应该在未提供默认值时返回空字符串', () => {
const result = utils.getEnv('NON_EXISTENT_VAR');
expect(result).toBe('');
});
});
describe('测试 isDebugMode 函数', () => {
it('应该在 DEBUG=true 时返回 true', () => {
restoreEnv = mockEnvVars({ DEBUG: 'true' });
expect(utils.isDebugMode()).toBe(true);
});
it('应该在 NODE_ENV=development 时返回 true', () => {
restoreEnv = mockEnvVars({ NODE_ENV: 'development' });
expect(utils.isDebugMode()).toBe(true);
});
it('应该在生产模式中返回 false', () => {
restoreEnv = mockEnvVars({ DEBUG: 'false', NODE_ENV: 'production' });
expect(utils.isDebugMode()).toBe(false);
});
it('应该处理不区分大小写的 DEBUG 值', () => {
restoreEnv = mockEnvVars({ DEBUG: 'TRUE' });
expect(utils.isDebugMode()).toBe(true);
});
});
@@ -464,110 +204,11 @@ describe('测试 utils.js 文件', () => {
it('应该格式化秒', () => {
expect(utils.formatDuration(1000)).toBe('1.00s');
expect(utils.formatDuration(1500)).toBe('1.50s');
expect(utils.formatDuration(2345)).toBe('2.35s');
expect(utils.formatDuration(2000)).toBe('2.00s');
});
it('应该处理零持续时间', () => {
expect(utils.formatDuration(0)).toBe('0ms');
});
});
describe('测试 createRetryConfig 函数', () => {
it('应该创建默认重试配置', () => {
const config = utils.createRetryConfig();
expect(config.maxRetries).toBe(3);
expect(config.delay).toBe(1000);
expect(config.backoffMultiplier).toBe(1.5);
expect(typeof config.getDelay).toBe('function');
});
it('应该创建自定义重试配置', () => {
const config = utils.createRetryConfig(5, 2000, 2);
expect(config.maxRetries).toBe(5);
expect(config.delay).toBe(2000);
expect(config.backoffMultiplier).toBe(2);
});
it('应该为尝试次数计算正确的延迟', () => {
const config = utils.createRetryConfig(3, 1000, 2);
expect(config.getDelay(0)).toBe(1000); // 1000 * 2^0
expect(config.getDelay(1)).toBe(2000); // 1000 * 2^1
expect(config.getDelay(2)).toBe(4000); // 1000 * 2^2
});
});
describe('测试 maskSensitiveInfo 函数', () => {
it('应该在文本中屏蔽敏感信息', () => {
const result = utils.maskSensitiveInfo('1234567890', 2, 2);
expect(result).toBe('12******90');
});
it('应该使用自定义屏蔽字符', () => {
const result = utils.maskSensitiveInfo('1234567890', 2, 2, 'X');
expect(result).toBe('12XXXXXX90');
});
it('如果字符串太短应该屏蔽整个字符串', () => {
const result = utils.maskSensitiveInfo('123', 2, 2);
expect(result).toBe('***');
});
it('应该处理非字符串输入', () => {
expect(utils.maskSensitiveInfo(null)).toBe('');
expect(utils.maskSensitiveInfo(123)).toBe('');
});
it('应该处理空字符串', () => {
expect(utils.maskSensitiveInfo('')).toBe('');
});
it('应该使用默认参数', () => {
const result = utils.maskSensitiveInfo('1234567890123456');
expect(result).toBe('1234********3456');
});
});
describe('测试 validateRequiredEnvVars 函数', () => {
it('应该在所有必需变量都存在时不抛出异常', () => {
restoreEnv = mockEnvVars({
VAR1: 'value1',
VAR2: 'value2',
VAR3: 'value3',
});
expect(() => {
utils.validateRequiredEnvVars(['VAR1', 'VAR2', 'VAR3']);
}).not.toThrow();
});
it('应该在缺少必需变量时抛出异常', () => {
restoreEnv = mockEnvVars({ VAR1: 'value1' });
expect(() => {
utils.validateRequiredEnvVars(['VAR1', 'VAR2', 'VAR3']);
}).toThrow('Missing required environment variables: VAR2, VAR3');
});
it('应该抛出带有正确代码和详情的错误', () => {
try {
utils.validateRequiredEnvVars(['MISSING_VAR']);
} catch (error) {
assertError(error, 'Missing required environment variables', 'MISSING_ENV_VARS');
expect(error.details.missing).toEqual(['MISSING_VAR']);
}
});
it('应该处理空数组', () => {
expect(() => {
utils.validateRequiredEnvVars([]);
}).not.toThrow();
});
});
});