109 lines
3.2 KiB
TypeScript
109 lines
3.2 KiB
TypeScript
import { formatVersion, formatFileSize, formatDateTime, getProjectDisplayName } from '../utils';
|
|
|
|
// Mock fs module
|
|
const fs = require('fs');
|
|
jest.mock('fs', () => ({
|
|
...jest.requireActual('fs'),
|
|
readFileSync: jest.fn(),
|
|
}));
|
|
|
|
describe('formatVersion', () => {
|
|
test('应该将下划线替换为点号', () => {
|
|
expect(formatVersion('0_42')).toBe('0.42');
|
|
expect(formatVersion('1_23_4')).toBe('1.23.4');
|
|
expect(formatVersion('2_0')).toBe('2.0');
|
|
});
|
|
|
|
test('应该处理没有下划线的版本号', () => {
|
|
expect(formatVersion('1.0')).toBe('1.0');
|
|
expect(formatVersion('2.3.4')).toBe('2.3.4');
|
|
});
|
|
|
|
test('应该处理空字符串', () => {
|
|
expect(formatVersion('')).toBe('');
|
|
});
|
|
});
|
|
|
|
describe('formatFileSize', () => {
|
|
test('应该格式化字节数', () => {
|
|
expect(formatFileSize(0)).toBe('0 B');
|
|
expect(formatFileSize(100)).toBe('100 B');
|
|
expect(formatFileSize(1023)).toBe('1023 B');
|
|
});
|
|
|
|
test('应该格式化KB', () => {
|
|
expect(formatFileSize(1024)).toBe('1.00 KB');
|
|
expect(formatFileSize(2048)).toBe('2.00 KB');
|
|
expect(formatFileSize(1536)).toBe('1.50 KB');
|
|
});
|
|
|
|
test('应该格式化MB', () => {
|
|
expect(formatFileSize(1024 * 1024)).toBe('1.00 MB');
|
|
expect(formatFileSize(2 * 1024 * 1024)).toBe('2.00 MB');
|
|
expect(formatFileSize(1.5 * 1024 * 1024)).toBe('1.50 MB');
|
|
});
|
|
|
|
test('应该格式化GB', () => {
|
|
expect(formatFileSize(1024 * 1024 * 1024)).toBe('1.00 GB');
|
|
expect(formatFileSize(2 * 1024 * 1024 * 1024)).toBe('2.00 GB');
|
|
});
|
|
});
|
|
|
|
describe('formatDateTime', () => {
|
|
test('应该格式化日期时间为标准格式', () => {
|
|
const date = new Date('2024-01-15T09:30:00');
|
|
expect(formatDateTime(date)).toBe('2024-01-15 09:30');
|
|
});
|
|
|
|
test('应该正确处理午夜时间', () => {
|
|
const date = new Date('2024-12-31T00:00:00');
|
|
expect(formatDateTime(date)).toBe('2024-12-31 00:00');
|
|
});
|
|
|
|
test('应该正确处理月末日期', () => {
|
|
const date = new Date('2024-12-31T23:59:00');
|
|
expect(formatDateTime(date)).toBe('2024-12-31 23:59');
|
|
});
|
|
|
|
test('应该正确处理单数月份和日期', () => {
|
|
const date = new Date('2024-01-05T09:05:00');
|
|
expect(formatDateTime(date)).toBe('2024-01-05 09:05');
|
|
});
|
|
});
|
|
|
|
describe('getProjectDisplayName', () => {
|
|
const originalAliases = {
|
|
'FT': '外放构建',
|
|
'FT_DEV': '开发构建',
|
|
};
|
|
|
|
beforeEach(() => {
|
|
// Mock the aliases file
|
|
fs.readFileSync.mockReturnValue(JSON.stringify(originalAliases));
|
|
});
|
|
|
|
afterEach(() => {
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
it('should return display name for aliased project', () => {
|
|
expect(getProjectDisplayName('FT')).toBe('外放构建');
|
|
});
|
|
|
|
it('should return directory name when no alias exists', () => {
|
|
expect(getProjectDisplayName('UNKNOWN')).toBe('UNKNOWN');
|
|
});
|
|
|
|
it('should return directory name when aliases file is empty', () => {
|
|
fs.readFileSync.mockReturnValue('{}');
|
|
expect(getProjectDisplayName('FT')).toBe('FT');
|
|
});
|
|
|
|
it('should return directory name when aliases file cannot be read', () => {
|
|
fs.readFileSync.mockImplementation(() => {
|
|
throw new Error('File not found');
|
|
});
|
|
expect(getProjectDisplayName('FT')).toBe('FT');
|
|
});
|
|
});
|