Files
opsxshell/test/components/sftp/utils.test.ts
T
Logan.Li 484d850abd feat: v0.3.0 版本 - SSH终端、SFTP、端口转发功能实现
核心功能:
- SSH终端: 密码/密钥认证、XTerm.js终端、多主题配色、IP高亮
- SFTP管理: 双栏文件管理、上传下载、传输队列
- 端口转发: 本地/远程转发、SOCKS5动态代理、规则管理

架构改进:
- 拆分大型组件 (SFTPView/TabBar/Sidebar)
- 添加统一日志和错误处理
- 修复React严格模式下的终端黑屏问题

UI更新:
- 新增端口转发视图
- 更新UI预览文档
- 左侧栏右键菜单支持SFTP快捷打开
2026-04-06 01:20:47 +08:00

115 lines
3.9 KiB
TypeScript

/**
* 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)
})
})
})