/** * SFTP Utils Tests - SFTP 工具函数测试 */ import { describe, it, expect } from 'vitest' import { formatSize, formatSpeed, formatDate, getFileExtension, sortFiles } from '../../../src/components/sftp/utils' import type { FileItem } from '../../../src/components/sftp/types' describe('SFTP Utils', () => { describe('formatSize', () => { it('应该正确格式化字节', () => { expect(formatSize(0)).toBe('--') expect(formatSize(512)).toBe('512 B') expect(formatSize(1024)).toBe('1 KB') expect(formatSize(1536)).toBe('1.5 KB') expect(formatSize(1048576)).toBe('1 MB') expect(formatSize(1073741824)).toBe('1 GB') }) it('应该保留一位小数', () => { expect(formatSize(1500)).toBe('1.5 KB') expect(formatSize(1572864)).toBe('1.5 MB') }) }) describe('formatSpeed', () => { it('应该正确格式化传输速度', () => { expect(formatSpeed(0)).toBe('0 B/s') expect(formatSpeed(1024)).toBe('1 KB/s') expect(formatSpeed(1048576)).toBe('1 MB/s') }) it('应该保留一位小数', () => { expect(formatSpeed(1536)).toBe('1.5 KB/s') }) }) describe('formatDate', () => { it('应该显示今天', () => { const today = Date.now() / 1000 expect(formatDate(today)).toBe('今天') }) it('应该显示昨天', () => { const yesterday = Date.now() / 1000 - 86400 expect(formatDate(yesterday)).toBe('昨天') }) it('应该显示天数', () => { const threeDaysAgo = Date.now() / 1000 - 3 * 86400 expect(formatDate(threeDaysAgo)).toBe('3 天前') }) }) describe('getFileExtension', () => { it('应该返回正确的文件类型描述', () => { expect(getFileExtension('test.pdf')).toBe('PDF') expect(getFileExtension('test.json')).toBe('JSON') expect(getFileExtension('test.ts')).toBe('TypeScript') expect(getFileExtension('test.MD')).toBe('Markdown') }) it('应该处理无扩展名的文件', () => { expect(getFileExtension('Makefile')).toBe('文件') }) it('应该返回大写扩展名', () => { expect(getFileExtension('test.PNG')).toBe('PNG') }) }) describe('sortFiles', () => { const mockFiles: FileItem[] = [ { name: 'test.txt', path: '/test.txt', type: 'file', size: 100, modified: 1000, permissions: '-rw-r--r--' }, { name: 'docs', path: '/docs', type: 'directory', size: 0, modified: 2000, permissions: 'drwxr-xr-x' }, { name: 'image.png', path: '/image.png', type: 'file', size: 500, modified: 500, permissions: '-rw-r--r--' }, { name: 'backup.zip', path: '/backup.zip', type: 'file', size: 1000, modified: 1500, permissions: '-rw-r--r--' }, ] it('应该按名称排序且目录优先', () => { const sorted = sortFiles(mockFiles, 'name', 'asc') expect(sorted[0].name).toBe('docs') // 目录优先 expect(sorted[1].name).toBe('backup.zip') }) it('应该按大小排序且目录优先', () => { const sorted = sortFiles(mockFiles, 'size', 'asc') expect(sorted[0].name).toBe('docs') // 目录优先 expect(sorted[sorted.length - 1].name).toBe('backup.zip') // 最大 }) it('应该按修改时间排序且目录优先', () => { const sorted = sortFiles(mockFiles, 'modified', 'asc') expect(sorted[0].name).toBe('docs') // 目录优先 expect(sorted[1].name).toBe('image.png') // 第二新 }) it('应该反转排序顺序', () => { const sorted = sortFiles(mockFiles, 'name', 'desc') expect(sorted[0].name).toBe('docs') // 目录优先,即使降序 }) it('应该总是目录优先', () => { const sorted = sortFiles(mockFiles, 'size', 'desc') expect(sorted[0].type).toBe('directory') }) it('不应该修改原始数组', () => { const original = [...mockFiles] sortFiles(mockFiles, 'name', 'asc') expect(mockFiles).toEqual(original) }) }) })