187 lines
6.9 KiB
TypeScript
187 lines
6.9 KiB
TypeScript
/**
|
|
* SFTP Utils Tests - SFTP 工具函数测试
|
|
*/
|
|
|
|
import { describe, it, expect } from 'vitest'
|
|
import {
|
|
formatSize,
|
|
formatSpeed,
|
|
formatDate,
|
|
getFileExtension,
|
|
sortFiles,
|
|
filterFilesByName,
|
|
isHiddenFileName,
|
|
filterHiddenFiles,
|
|
} 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)
|
|
})
|
|
})
|
|
|
|
describe('filterFilesByName', () => {
|
|
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--' },
|
|
{ name: 'Test_Report.PDF', path: '/Test_Report.PDF', type: 'file', size: 200, modified: 1200, permissions: '-rw-r--r--' },
|
|
]
|
|
|
|
it('空查询应返回原列表引用', () => {
|
|
expect(filterFilesByName(mockFiles, '')).toBe(mockFiles)
|
|
expect(filterFilesByName(mockFiles, ' ')).toBe(mockFiles)
|
|
})
|
|
|
|
it('应按子串匹配文件名', () => {
|
|
const result = filterFilesByName(mockFiles, 'image')
|
|
expect(result.map((f) => f.name)).toEqual(['image.png'])
|
|
})
|
|
|
|
it('应不区分大小写', () => {
|
|
const result = filterFilesByName(mockFiles, 'test')
|
|
expect(result.map((f) => f.name).sort()).toEqual(['Test_Report.PDF', 'test.txt'])
|
|
})
|
|
|
|
it('可匹配扩展名', () => {
|
|
const result = filterFilesByName(mockFiles, '.png')
|
|
expect(result).toHaveLength(1)
|
|
expect(result[0].name).toBe('image.png')
|
|
})
|
|
|
|
it('无匹配时返回空数组', () => {
|
|
expect(filterFilesByName(mockFiles, 'xyz-not-exist')).toEqual([])
|
|
})
|
|
})
|
|
|
|
describe('isHiddenFileName / filterHiddenFiles', () => {
|
|
const mockFiles: FileItem[] = [
|
|
{ name: 'readme.md', path: '/readme.md', type: 'file', size: 10, modified: 1, permissions: '-rw-r--r--' },
|
|
{ name: '.gitignore', path: '/.gitignore', type: 'file', size: 20, modified: 2, permissions: '-rw-r--r--' },
|
|
{ name: '.config', path: '/.config', type: 'directory', size: 0, modified: 3, permissions: 'drwxr-xr-x' },
|
|
{ name: '.', path: '/.', type: 'directory', size: 0, modified: 4, permissions: 'drwxr-xr-x' },
|
|
{ name: '..', path: '/..', type: 'directory', size: 0, modified: 5, permissions: 'drwxr-xr-x' },
|
|
{ name: 'src', path: '/src', type: 'directory', size: 0, modified: 6, permissions: 'drwxr-xr-x' },
|
|
]
|
|
|
|
it('应识别点文件与 . / ..', () => {
|
|
expect(isHiddenFileName('.gitignore')).toBe(true)
|
|
expect(isHiddenFileName('.')).toBe(true)
|
|
expect(isHiddenFileName('..')).toBe(true)
|
|
expect(isHiddenFileName('readme.md')).toBe(false)
|
|
})
|
|
|
|
it('默认应隐藏点文件,并始终排除 . / ..', () => {
|
|
const result = filterHiddenFiles(mockFiles, false)
|
|
expect(result.map((f) => f.name).sort()).toEqual(['readme.md', 'src'])
|
|
})
|
|
|
|
it('显示隐藏文件时仍排除 . / ..', () => {
|
|
const result = filterHiddenFiles(mockFiles, true)
|
|
expect(result.map((f) => f.name).sort()).toEqual(['.config', '.gitignore', 'readme.md', 'src'])
|
|
})
|
|
})
|
|
})
|