build: 为项目创建单元测试框架 #45

Manually merged
remilia merged 8 commits from build/test into master 2025-10-27 14:16:26 +08:00
5 changed files with 454 additions and 16 deletions
Showing only changes of commit f2634ca0f4 - Show all commits

View File

@ -3,34 +3,49 @@ import {
getFileExtension, getFileExtension,
formatFileExtension, formatFileExtension,
} from '../../../app/utils/file'; } from '../../../app/utils/file';
import { expect, test } from 'vitest'; import { expect, test, describe } from 'vitest';
/** /**
* 单元测试: formatFileSize * 单元测试: formatFileSize
*/ */
test('formatFileSize - format bytes correctly', () => { describe('formatFileSize', () => {
test('format bytes correctly', () => {
expect(formatFileSize(500)).toBe('500.00 B'); expect(formatFileSize(500)).toBe('500.00 B');
}); });
test('formatFileSize - format kilobytes correctly', () => { test('format kilobytes correctly', () => {
expect(formatFileSize(2048)).toBe('2.00 KB'); expect(formatFileSize(2048)).toBe('2.00 KB');
}); });
test('formatFileSize - format megabytes correctly', () => { test('format megabytes correctly', () => {
expect(formatFileSize(5 * 1024 * 1024)).toBe('5.00 MB'); expect(formatFileSize(5 * 1024 * 1024)).toBe('5.00 MB');
}); });
});
/** /**
* 单元测试: getFileExtension, formatFileExtension * 单元测试: getFileExtension
*/ */
test('getFileExtension - extract extension from filename', () => { describe('getFileExtension', () => {
test('extract extension from filename', () => {
expect(getFileExtension('document.pdf')).toBe('pdf'); expect(getFileExtension('document.pdf')).toBe('pdf');
}); });
test('getFileExtension - handle filenames without extension', () => { test('handle filenames without extension', () => {
expect(getFileExtension('README')).toBe(''); expect(getFileExtension('README')).toBe('');
}); });
test('handle multiple dots in filename', () => {
expect(getFileExtension('archive.tar.gz')).toBe('gz');
});
test('handle empty filename', () => {
expect(getFileExtension('')).toBe('');
});
});
test('formatFileExtension - format extension without dot', () => { /**
* 单元测试: formatFileExtension
*/
describe('formatFileExtension', () => {
test('format extension without dot', () => {
expect(formatFileExtension('txt')).toBe('TXT'); expect(formatFileExtension('txt')).toBe('TXT');
}); });
test('formatFileExtension - format extension with dot', () => { test('format extension with dot', () => {
expect(formatFileExtension('.jpg')).toBe('JPG'); expect(formatFileExtension('.jpg')).toBe('JPG');
}); });
});