729fb2d42a
- 新增 vitest 测试框架配置 - 添加 54 个测试文件,共 951 个测试用例 - 覆盖核心模块: - Agent: executor, registry, config-loader, permission-merger - Context: manager, compaction, prune, token-counter - Permission: manager, bash/file/git/web checkers, wildcard - Session: manager, storage - Tools: filesystem (12个), git (10个), web, shell, todo, task - LSP: client, server, language - Utils: config, diff - UI: terminal
189 lines
5.7 KiB
TypeScript
189 lines
5.7 KiB
TypeScript
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
|
|
|
// Mock fs/promises
|
|
vi.mock('fs/promises', () => ({
|
|
stat: vi.fn(),
|
|
readlink: vi.fn(),
|
|
readdir: vi.fn(),
|
|
}));
|
|
|
|
// Mock permission manager
|
|
vi.mock('../../../../src/permission/index.js', () => ({
|
|
getPermissionManager: vi.fn(() => ({
|
|
checkFilePermission: vi.fn().mockResolvedValue({
|
|
allowed: true,
|
|
action: 'allow',
|
|
}),
|
|
})),
|
|
}));
|
|
|
|
// Mock loadDescription
|
|
vi.mock('../../../../src/tools/load_description.js', () => ({
|
|
loadDescription: vi.fn(() => '获取文件信息'),
|
|
}));
|
|
|
|
import { getFileInfoTool } from '../../../../src/tools/filesystem/get_file_info.js';
|
|
import * as fs from 'fs/promises';
|
|
import { getPermissionManager } from '../../../../src/permission/index.js';
|
|
|
|
describe('getFileInfoTool - 获取文件信息工具', () => {
|
|
const mockStats = {
|
|
isDirectory: () => false,
|
|
isFile: () => true,
|
|
isSymbolicLink: () => false,
|
|
size: 1024,
|
|
mode: 0o100644,
|
|
birthtime: new Date('2024-01-01'),
|
|
mtime: new Date('2024-01-15'),
|
|
atime: new Date('2024-01-20'),
|
|
ino: 12345,
|
|
nlink: 1,
|
|
};
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
vi.mocked(fs.stat).mockResolvedValue(mockStats as any);
|
|
});
|
|
|
|
describe('工具定义', () => {
|
|
it('有正确的名称', () => {
|
|
expect(getFileInfoTool.name).toBe('get_file_info');
|
|
});
|
|
|
|
it('有正确的元数据', () => {
|
|
expect(getFileInfoTool.metadata.category).toBe('filesystem');
|
|
expect(getFileInfoTool.metadata.keywords).toContain('file');
|
|
expect(getFileInfoTool.metadata.keywords).toContain('info');
|
|
expect(getFileInfoTool.metadata.keywords).toContain('stat');
|
|
});
|
|
|
|
it('定义了必需的 path 参数', () => {
|
|
expect(getFileInfoTool.parameters.path.required).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe('execute - 执行', () => {
|
|
it('成功获取文件信息', async () => {
|
|
const result = await getFileInfoTool.execute({ path: 'test.txt' });
|
|
|
|
expect(result.success).toBe(true);
|
|
expect(result.output).toContain('路径:');
|
|
expect(result.output).toContain('类型: 文件');
|
|
expect(result.output).toContain('大小:');
|
|
expect(result.output).toContain('权限:');
|
|
expect(result.output).toContain('创建时间:');
|
|
expect(result.output).toContain('修改时间:');
|
|
expect(result.output).toContain('inode:');
|
|
});
|
|
|
|
it('正确显示目录信息', async () => {
|
|
vi.mocked(fs.stat).mockResolvedValue({
|
|
...mockStats,
|
|
isDirectory: () => true,
|
|
isFile: () => false,
|
|
} as any);
|
|
vi.mocked(fs.readdir).mockResolvedValue(['file1', 'file2', 'dir1'] as any);
|
|
|
|
const result = await getFileInfoTool.execute({ path: 'test_dir' });
|
|
|
|
expect(result.success).toBe(true);
|
|
expect(result.output).toContain('类型: 目录');
|
|
expect(result.output).toContain('子项数量: 3');
|
|
});
|
|
|
|
it('正确显示符号链接信息', async () => {
|
|
vi.mocked(fs.stat).mockResolvedValue({
|
|
...mockStats,
|
|
isSymbolicLink: () => true,
|
|
isFile: () => false,
|
|
} as any);
|
|
vi.mocked(fs.readlink).mockResolvedValue('/real/path');
|
|
|
|
const result = await getFileInfoTool.execute({ path: 'link' });
|
|
|
|
expect(result.success).toBe(true);
|
|
expect(result.output).toContain('类型: 符号链接');
|
|
expect(result.output).toContain('链接目标: /real/path');
|
|
});
|
|
|
|
it('正确格式化文件大小', async () => {
|
|
// 测试不同大小
|
|
const sizes = [
|
|
{ size: 500, expected: 'B' },
|
|
{ size: 1024, expected: 'KB' },
|
|
{ size: 1024 * 1024, expected: 'MB' },
|
|
{ size: 1024 * 1024 * 1024, expected: 'GB' },
|
|
];
|
|
|
|
for (const { size, expected } of sizes) {
|
|
vi.mocked(fs.stat).mockResolvedValue({
|
|
...mockStats,
|
|
size,
|
|
} as any);
|
|
|
|
const result = await getFileInfoTool.execute({ path: 'test.txt' });
|
|
|
|
expect(result.output).toContain(expected);
|
|
}
|
|
});
|
|
|
|
it('权限被拒绝时返回错误', async () => {
|
|
vi.mocked(getPermissionManager).mockReturnValue({
|
|
checkFilePermission: vi.fn().mockResolvedValue({
|
|
allowed: false,
|
|
action: 'deny',
|
|
reason: '不允许获取信息',
|
|
}),
|
|
} as any);
|
|
|
|
const result = await getFileInfoTool.execute({ path: '/protected/file' });
|
|
|
|
expect(result.success).toBe(false);
|
|
expect(result.error).toContain('权限被拒绝');
|
|
});
|
|
|
|
it('需要确认时返回提示', async () => {
|
|
vi.mocked(getPermissionManager).mockReturnValue({
|
|
checkFilePermission: vi.fn().mockResolvedValue({
|
|
allowed: false,
|
|
action: 'ask',
|
|
needsConfirmation: true,
|
|
}),
|
|
} as any);
|
|
|
|
const result = await getFileInfoTool.execute({ path: 'file.txt' });
|
|
|
|
expect(result.success).toBe(false);
|
|
expect(result.error).toContain('需要用户确认');
|
|
});
|
|
|
|
it('文件不存在返回错误', async () => {
|
|
vi.mocked(getPermissionManager).mockReturnValue({
|
|
checkFilePermission: vi.fn().mockResolvedValue({ allowed: true }),
|
|
} as any);
|
|
|
|
vi.mocked(fs.stat).mockRejectedValue(new Error('ENOENT'));
|
|
|
|
const result = await getFileInfoTool.execute({ path: 'nonexistent.txt' });
|
|
|
|
expect(result.success).toBe(false);
|
|
expect(result.error).toContain('ENOENT');
|
|
});
|
|
|
|
it('传递正确参数给权限检查', async () => {
|
|
const mockCheck = vi.fn().mockResolvedValue({ allowed: true });
|
|
vi.mocked(getPermissionManager).mockReturnValue({
|
|
checkFilePermission: mockCheck,
|
|
} as any);
|
|
|
|
await getFileInfoTool.execute({ path: 'test.txt' });
|
|
|
|
expect(mockCheck).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
operation: 'info',
|
|
})
|
|
);
|
|
});
|
|
});
|
|
});
|