refactor(core): 移除不再需要的文件系统工具
删除以下工具及相关文件: - copy_file: 复制文件 - create_directory: 创建目录 - delete_file: 删除文件 - move_file: 移动文件 - search_files: 搜索文件 清理范围: - 工具实现文件 (5个) - 工具描述文件 (5个) - 单元测试文件 (6个) - Agent presets 中的引用 - Checkpoint 系统中的触发类型 - Hook 系统中的相关处理
This commit is contained in:
@@ -218,7 +218,6 @@ describe('CheckpointManager', () => {
|
||||
it('should determine if checkpoint should be created for tool', () => {
|
||||
expect(manager.shouldCreateCheckpoint('write_file')).toBe(true);
|
||||
expect(manager.shouldCreateCheckpoint('edit_file')).toBe(true);
|
||||
expect(manager.shouldCreateCheckpoint('delete_file')).toBe(true);
|
||||
expect(manager.shouldCreateCheckpoint('bash')).toBe(false); // 默认禁用
|
||||
expect(manager.shouldCreateCheckpoint('read_file')).toBe(false);
|
||||
});
|
||||
|
||||
@@ -328,25 +328,6 @@ describe('HookManager', () => {
|
||||
expect(triggered).toBe(true);
|
||||
});
|
||||
|
||||
it('should trigger file.deleted hook', async () => {
|
||||
let triggered = false;
|
||||
const hooks: Hooks = {
|
||||
'file.deleted': async (input, output) => {
|
||||
triggered = true;
|
||||
expect(input.path).toBe('/test/old-file.ts');
|
||||
},
|
||||
};
|
||||
|
||||
manager.registerHooks(hooks);
|
||||
|
||||
await manager.triggerFileDeleted({
|
||||
path: '/test/old-file.ts',
|
||||
tool: 'delete_file',
|
||||
sessionId: 'test-session',
|
||||
});
|
||||
|
||||
expect(triggered).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Config Hooks', () => {
|
||||
|
||||
@@ -1,192 +0,0 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
|
||||
// Mock fs/promises
|
||||
vi.mock('fs/promises', () => ({
|
||||
stat: vi.fn(),
|
||||
copyFile: vi.fn().mockResolvedValue(undefined),
|
||||
mkdir: vi.fn().mockResolvedValue(undefined),
|
||||
readdir: vi.fn().mockResolvedValue([]),
|
||||
}));
|
||||
|
||||
// 可变状态
|
||||
let mockCheckResults: Array<{
|
||||
allowed: boolean;
|
||||
action?: string;
|
||||
reason?: string;
|
||||
needsConfirmation?: boolean;
|
||||
}> = [];
|
||||
let checkCallIndex = 0;
|
||||
|
||||
// Mock permission manager
|
||||
vi.mock('../../../../src/permission/index.js', () => ({
|
||||
getPermissionManager: () => ({
|
||||
checkFilePermission: vi.fn(async () => {
|
||||
const result = mockCheckResults[checkCallIndex] || { allowed: true };
|
||||
checkCallIndex++;
|
||||
return result;
|
||||
}),
|
||||
}),
|
||||
}));
|
||||
|
||||
// Mock loadDescription
|
||||
vi.mock('../../../../src/tools/load_description.js', () => ({
|
||||
loadDescription: vi.fn(() => '复制文件'),
|
||||
}));
|
||||
|
||||
import { copyFileTool } from '../../../../src/tools/filesystem/copy_file.js';
|
||||
import * as fs from 'fs/promises';
|
||||
|
||||
describe('copyFileTool - 扩展测试', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mockCheckResults = [{ allowed: true }, { allowed: true }];
|
||||
checkCallIndex = 0;
|
||||
// 重置 mock 默认值
|
||||
vi.mocked(fs.stat).mockReset();
|
||||
vi.mocked(fs.copyFile).mockReset().mockResolvedValue(undefined);
|
||||
vi.mocked(fs.mkdir).mockReset().mockResolvedValue(undefined);
|
||||
vi.mocked(fs.readdir).mockReset().mockResolvedValue([]);
|
||||
});
|
||||
|
||||
describe('递归复制目录', () => {
|
||||
it('递归复制包含文件的目录', async () => {
|
||||
vi.mocked(fs.stat)
|
||||
.mockResolvedValueOnce({ isDirectory: () => true } as any)
|
||||
.mockRejectedValueOnce(new Error('ENOENT'))
|
||||
.mockResolvedValueOnce({ isDirectory: () => false } as any);
|
||||
|
||||
vi.mocked(fs.readdir).mockResolvedValueOnce(['file1.txt'] as any);
|
||||
|
||||
const result = await copyFileTool.execute({
|
||||
source: 'src_dir',
|
||||
destination: 'dest_dir',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(fs.mkdir).toHaveBeenCalled();
|
||||
expect(fs.copyFile).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('递归复制包含子目录的目录', async () => {
|
||||
// 调用顺序:
|
||||
// 1. execute: stat(source) - 检查源是否存在
|
||||
// 2. execute: stat(dest) - 检查目标(ENOENT)
|
||||
// 3. copyRecursive: stat(source) - 判断是否是目录
|
||||
// 4. copyRecursive: stat(source/subdir) - 递归判断子目录
|
||||
vi.mocked(fs.stat)
|
||||
.mockResolvedValueOnce({ isDirectory: () => true } as any) // source 存在且是目录
|
||||
.mockRejectedValueOnce(new Error('ENOENT')) // dest 不存在
|
||||
.mockResolvedValueOnce({ isDirectory: () => true } as any) // copyRecursive(source)
|
||||
.mockResolvedValueOnce({ isDirectory: () => true } as any); // copyRecursive(source/subdir)
|
||||
|
||||
vi.mocked(fs.readdir)
|
||||
.mockResolvedValueOnce(['subdir'] as any) // 第一层目录
|
||||
.mockResolvedValueOnce([] as any); // 子目录为空
|
||||
|
||||
const result = await copyFileTool.execute({
|
||||
source: 'src_dir',
|
||||
destination: 'dest_dir',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(fs.mkdir).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('目标位置权限', () => {
|
||||
it('目标位置需要确认时返回错误', async () => {
|
||||
mockCheckResults = [
|
||||
{ allowed: true },
|
||||
{ allowed: false, action: 'ask', needsConfirmation: true, reason: '首次复制到此位置' },
|
||||
];
|
||||
|
||||
vi.mocked(fs.stat).mockResolvedValueOnce({ isDirectory: () => false } as any);
|
||||
|
||||
const result = await copyFileTool.execute({
|
||||
source: 'src.txt',
|
||||
destination: '/new/location/dest.txt',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('需要用户确认');
|
||||
expect(result.error).toContain('首次复制到此位置');
|
||||
});
|
||||
});
|
||||
|
||||
describe('绝对路径处理', () => {
|
||||
it('源和目标都是绝对路径', async () => {
|
||||
vi.mocked(fs.stat)
|
||||
.mockResolvedValueOnce({ isDirectory: () => false } as any)
|
||||
.mockRejectedValueOnce(new Error('ENOENT'));
|
||||
|
||||
const result = await copyFileTool.execute({
|
||||
source: '/absolute/source/file.txt',
|
||||
destination: '/absolute/destination/file.txt',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('/absolute/source/file.txt');
|
||||
expect(result.output).toContain('/absolute/destination/file.txt');
|
||||
});
|
||||
});
|
||||
|
||||
describe('权限检查细节', () => {
|
||||
it('源文件权限被拒绝(无原因)', async () => {
|
||||
mockCheckResults = [
|
||||
{ allowed: false, action: 'deny' },
|
||||
];
|
||||
|
||||
const result = await copyFileTool.execute({
|
||||
source: 'src.txt',
|
||||
destination: 'dest.txt',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('不允许读取此文件');
|
||||
});
|
||||
|
||||
it('源文件需要确认(无原因)', async () => {
|
||||
mockCheckResults = [
|
||||
{ allowed: false, action: 'ask', needsConfirmation: true },
|
||||
];
|
||||
|
||||
const result = await copyFileTool.execute({
|
||||
source: 'src.txt',
|
||||
destination: 'dest.txt',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('需要权限确认');
|
||||
});
|
||||
|
||||
it('目标权限被拒绝(无原因)', async () => {
|
||||
mockCheckResults = [
|
||||
{ allowed: true },
|
||||
{ allowed: false, action: 'deny' },
|
||||
];
|
||||
|
||||
const result = await copyFileTool.execute({
|
||||
source: 'src.txt',
|
||||
destination: 'dest.txt',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('不允许复制到此位置');
|
||||
});
|
||||
|
||||
it('目标需要确认(无原因)', async () => {
|
||||
mockCheckResults = [
|
||||
{ allowed: true },
|
||||
{ allowed: false, action: 'ask', needsConfirmation: true },
|
||||
];
|
||||
|
||||
const result = await copyFileTool.execute({
|
||||
source: 'src.txt',
|
||||
destination: 'dest.txt',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('需要权限确认');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,173 +0,0 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
|
||||
// Mock fs/promises
|
||||
vi.mock('fs/promises', () => ({
|
||||
stat: vi.fn(),
|
||||
copyFile: vi.fn().mockResolvedValue(undefined),
|
||||
mkdir: vi.fn().mockResolvedValue(undefined),
|
||||
readdir: vi.fn().mockResolvedValue([]),
|
||||
}));
|
||||
|
||||
// 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 { copyFileTool } from '../../../../src/tools/filesystem/copy_file.js';
|
||||
import * as fs from 'fs/promises';
|
||||
import { getPermissionManager } from '../../../../src/permission/index.js';
|
||||
|
||||
describe('copyFileTool - 文件复制工具', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
vi.mocked(fs.stat).mockResolvedValue({
|
||||
isDirectory: () => false,
|
||||
isFile: () => true,
|
||||
} as any);
|
||||
});
|
||||
|
||||
describe('工具定义', () => {
|
||||
it('有正确的名称', () => {
|
||||
expect(copyFileTool.name).toBe('copy_file');
|
||||
});
|
||||
|
||||
it('有正确的元数据', () => {
|
||||
expect(copyFileTool.metadata.category).toBe('filesystem');
|
||||
expect(copyFileTool.metadata.keywords).toContain('copy');
|
||||
expect(copyFileTool.metadata.keywords).toContain('cp');
|
||||
});
|
||||
|
||||
it('定义了必需参数', () => {
|
||||
expect(copyFileTool.parameters.source.required).toBe(true);
|
||||
expect(copyFileTool.parameters.destination.required).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute - 执行', () => {
|
||||
it('成功复制文件', async () => {
|
||||
// 第一次调用检查源文件,第二次调用检查目标是否是目录
|
||||
vi.mocked(fs.stat)
|
||||
.mockResolvedValueOnce({ isDirectory: () => false } as any)
|
||||
.mockRejectedValueOnce(new Error('ENOENT')); // 目标不存在
|
||||
|
||||
const result = await copyFileTool.execute({
|
||||
source: 'src.txt',
|
||||
destination: 'dest.txt',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('已复制');
|
||||
expect(fs.copyFile).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('复制到已存在的目录', async () => {
|
||||
vi.mocked(fs.stat)
|
||||
.mockResolvedValueOnce({ isDirectory: () => false } as any) // 源文件
|
||||
.mockResolvedValueOnce({ isDirectory: () => true } as any); // 目标是目录
|
||||
|
||||
const result = await copyFileTool.execute({
|
||||
source: 'file.txt',
|
||||
destination: '/target/dir',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('file.txt');
|
||||
});
|
||||
|
||||
it('递归复制目录', async () => {
|
||||
vi.mocked(fs.stat)
|
||||
.mockResolvedValueOnce({ isDirectory: () => true } as any) // 源是目录
|
||||
.mockRejectedValueOnce(new Error('ENOENT')); // 目标不存在
|
||||
|
||||
vi.mocked(fs.readdir).mockResolvedValueOnce([]);
|
||||
|
||||
const result = await copyFileTool.execute({
|
||||
source: 'src_dir',
|
||||
destination: 'dest_dir',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(fs.mkdir).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('源文件读取权限被拒绝', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: false,
|
||||
action: 'deny',
|
||||
reason: '不允许读取',
|
||||
}),
|
||||
} as any);
|
||||
|
||||
const result = await copyFileTool.execute({
|
||||
source: '/etc/passwd',
|
||||
destination: 'copy.txt',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('权限被拒绝');
|
||||
});
|
||||
|
||||
it('目标位置写入权限被拒绝', async () => {
|
||||
const mockCheck = vi.fn()
|
||||
.mockResolvedValueOnce({ allowed: true }) // 读取权限
|
||||
.mockResolvedValueOnce({ allowed: false, action: 'deny', reason: '不允许写入' }); // 复制权限
|
||||
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: mockCheck,
|
||||
} as any);
|
||||
|
||||
const result = await copyFileTool.execute({
|
||||
source: 'src.txt',
|
||||
destination: '/protected/dest.txt',
|
||||
});
|
||||
|
||||
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 copyFileTool.execute({
|
||||
source: '/sensitive/file',
|
||||
destination: 'dest.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: no such file'));
|
||||
|
||||
const result = await copyFileTool.execute({
|
||||
source: 'nonexistent.txt',
|
||||
destination: 'dest.txt',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('ENOENT');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,156 +0,0 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
|
||||
// Mock fs/promises
|
||||
vi.mock('fs/promises', () => ({
|
||||
stat: vi.fn(),
|
||||
mkdir: vi.fn().mockResolvedValue(undefined),
|
||||
}));
|
||||
|
||||
// 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 { createDirectoryTool } from '../../../../src/tools/filesystem/create_directory.js';
|
||||
import * as fs from 'fs/promises';
|
||||
import { getPermissionManager } from '../../../../src/permission/index.js';
|
||||
|
||||
describe('createDirectoryTool - 创建目录工具', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
// 默认目录不存在
|
||||
vi.mocked(fs.stat).mockRejectedValue(new Error('ENOENT'));
|
||||
});
|
||||
|
||||
describe('工具定义', () => {
|
||||
it('有正确的名称', () => {
|
||||
expect(createDirectoryTool.name).toBe('create_directory');
|
||||
});
|
||||
|
||||
it('有正确的元数据', () => {
|
||||
expect(createDirectoryTool.metadata.category).toBe('filesystem');
|
||||
expect(createDirectoryTool.metadata.keywords).toContain('create');
|
||||
expect(createDirectoryTool.metadata.keywords).toContain('directory');
|
||||
expect(createDirectoryTool.metadata.keywords).toContain('mkdir');
|
||||
});
|
||||
|
||||
it('定义了必需的 path 参数', () => {
|
||||
expect(createDirectoryTool.parameters.path.required).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute - 执行', () => {
|
||||
it('成功创建目录', async () => {
|
||||
const result = await createDirectoryTool.execute({ path: 'new_dir' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('已创建目录');
|
||||
expect(fs.mkdir).toHaveBeenCalledWith(
|
||||
expect.any(String),
|
||||
{ recursive: true }
|
||||
);
|
||||
});
|
||||
|
||||
it('目录已存在返回成功', async () => {
|
||||
vi.mocked(fs.stat).mockResolvedValue({
|
||||
isDirectory: () => true,
|
||||
} as any);
|
||||
|
||||
const result = await createDirectoryTool.execute({ path: 'existing_dir' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('目录已存在');
|
||||
expect(fs.mkdir).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('路径是文件返回错误', async () => {
|
||||
vi.mocked(fs.stat).mockResolvedValue({
|
||||
isDirectory: () => false,
|
||||
} as any);
|
||||
|
||||
const result = await createDirectoryTool.execute({ path: 'file.txt' });
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('路径已存在且不是目录');
|
||||
});
|
||||
|
||||
it('权限被拒绝时返回错误', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: false,
|
||||
action: 'deny',
|
||||
reason: '不允许创建目录',
|
||||
}),
|
||||
} as any);
|
||||
|
||||
const result = await createDirectoryTool.execute({ path: '/protected/dir' });
|
||||
|
||||
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 createDirectoryTool.execute({ path: 'new_dir' });
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('需要用户确认');
|
||||
});
|
||||
|
||||
it('创建嵌套目录', async () => {
|
||||
// 确保权限检查通过
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({ allowed: true }),
|
||||
} as any);
|
||||
|
||||
const result = await createDirectoryTool.execute({ path: 'a/b/c/d' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(fs.mkdir).toHaveBeenCalledWith(
|
||||
expect.any(String),
|
||||
{ recursive: true }
|
||||
);
|
||||
});
|
||||
|
||||
it('传递正确参数给权限检查', async () => {
|
||||
const mockCheck = vi.fn().mockResolvedValue({ allowed: true });
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: mockCheck,
|
||||
} as any);
|
||||
|
||||
await createDirectoryTool.execute({ path: 'test_dir' });
|
||||
|
||||
expect(mockCheck).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
operation: 'mkdir',
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it('处理创建错误', async () => {
|
||||
vi.mocked(fs.mkdir).mockRejectedValue(new Error('Permission denied'));
|
||||
|
||||
const result = await createDirectoryTool.execute({ path: 'new_dir' });
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('Permission denied');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,173 +0,0 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
|
||||
// Mock fs/promises
|
||||
vi.mock('fs/promises', () => ({
|
||||
stat: vi.fn(),
|
||||
unlink: vi.fn().mockResolvedValue(undefined),
|
||||
rmdir: vi.fn().mockResolvedValue(undefined),
|
||||
rm: vi.fn().mockResolvedValue(undefined),
|
||||
readdir: vi.fn().mockResolvedValue([]),
|
||||
}));
|
||||
|
||||
// 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 { deleteFileTool } from '../../../../src/tools/filesystem/delete_file.js';
|
||||
import * as fs from 'fs/promises';
|
||||
import { getPermissionManager } from '../../../../src/permission/index.js';
|
||||
|
||||
describe('deleteFileTool - 文件删除工具', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('工具定义', () => {
|
||||
it('有正确的名称', () => {
|
||||
expect(deleteFileTool.name).toBe('delete_file');
|
||||
});
|
||||
|
||||
it('有正确的元数据', () => {
|
||||
expect(deleteFileTool.metadata.category).toBe('filesystem');
|
||||
expect(deleteFileTool.metadata.keywords).toContain('delete');
|
||||
expect(deleteFileTool.metadata.keywords).toContain('remove');
|
||||
expect(deleteFileTool.metadata.keywords).toContain('rm');
|
||||
});
|
||||
|
||||
it('定义了必需的 path 参数', () => {
|
||||
expect(deleteFileTool.parameters.path.required).toBe(true);
|
||||
});
|
||||
|
||||
it('定义了可选的 recursive 参数', () => {
|
||||
expect(deleteFileTool.parameters.recursive.required).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute - 执行', () => {
|
||||
it('成功删除文件', async () => {
|
||||
vi.mocked(fs.stat).mockResolvedValue({
|
||||
isDirectory: () => false,
|
||||
} as any);
|
||||
|
||||
const result = await deleteFileTool.execute({ path: 'file.txt' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('已删除文件');
|
||||
expect(fs.unlink).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('删除空目录', async () => {
|
||||
vi.mocked(fs.stat).mockResolvedValue({
|
||||
isDirectory: () => true,
|
||||
} as any);
|
||||
vi.mocked(fs.readdir).mockResolvedValue([]);
|
||||
|
||||
const result = await deleteFileTool.execute({ path: 'empty_dir' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('已删除目录');
|
||||
expect(fs.rmdir).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('非空目录无 recursive 返回错误', async () => {
|
||||
vi.mocked(fs.stat).mockResolvedValue({
|
||||
isDirectory: () => true,
|
||||
} as any);
|
||||
vi.mocked(fs.readdir).mockResolvedValue(['file1.txt', 'file2.txt'] as any);
|
||||
|
||||
const result = await deleteFileTool.execute({ path: 'nonempty_dir' });
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('目录不为空');
|
||||
expect(result.error).toContain('recursive: true');
|
||||
});
|
||||
|
||||
it('递归删除非空目录', async () => {
|
||||
vi.mocked(fs.stat).mockResolvedValue({
|
||||
isDirectory: () => true,
|
||||
} as any);
|
||||
|
||||
const result = await deleteFileTool.execute({
|
||||
path: 'nonempty_dir',
|
||||
recursive: true,
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('已删除目录');
|
||||
expect(fs.rm).toHaveBeenCalledWith(
|
||||
expect.any(String),
|
||||
{ recursive: true }
|
||||
);
|
||||
});
|
||||
|
||||
it('权限被拒绝时返回错误', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: false,
|
||||
action: 'deny',
|
||||
reason: '不允许删除',
|
||||
}),
|
||||
} as any);
|
||||
|
||||
const result = await deleteFileTool.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 deleteFileTool.execute({ path: 'important.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 deleteFileTool.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);
|
||||
vi.mocked(fs.stat).mockResolvedValue({ isDirectory: () => false } as any);
|
||||
|
||||
await deleteFileTool.execute({ path: 'test.txt' });
|
||||
|
||||
expect(mockCheck).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
operation: 'delete',
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,171 +0,0 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
|
||||
// Mock fs/promises
|
||||
vi.mock('fs/promises', () => ({
|
||||
access: vi.fn().mockResolvedValue(undefined),
|
||||
stat: vi.fn(),
|
||||
rename: vi.fn().mockResolvedValue(undefined),
|
||||
mkdir: vi.fn().mockResolvedValue(undefined),
|
||||
}));
|
||||
|
||||
// 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 { moveFileTool } from '../../../../src/tools/filesystem/move_file.js';
|
||||
import * as fs from 'fs/promises';
|
||||
import { getPermissionManager } from '../../../../src/permission/index.js';
|
||||
|
||||
describe('moveFileTool - 文件移动工具', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('工具定义', () => {
|
||||
it('有正确的名称', () => {
|
||||
expect(moveFileTool.name).toBe('move_file');
|
||||
});
|
||||
|
||||
it('有正确的元数据', () => {
|
||||
expect(moveFileTool.metadata.category).toBe('filesystem');
|
||||
expect(moveFileTool.metadata.keywords).toContain('move');
|
||||
expect(moveFileTool.metadata.keywords).toContain('rename');
|
||||
expect(moveFileTool.metadata.keywords).toContain('mv');
|
||||
});
|
||||
|
||||
it('定义了必需参数', () => {
|
||||
expect(moveFileTool.parameters.source.required).toBe(true);
|
||||
expect(moveFileTool.parameters.destination.required).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute - 执行', () => {
|
||||
it('成功移动文件', async () => {
|
||||
vi.mocked(fs.stat).mockRejectedValue(new Error('ENOENT')); // 目标不存在
|
||||
|
||||
const result = await moveFileTool.execute({
|
||||
source: 'old.txt',
|
||||
destination: 'new.txt',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('已移动');
|
||||
expect(fs.rename).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('移动到已存在的目录', async () => {
|
||||
vi.mocked(fs.stat).mockResolvedValue({ isDirectory: () => true } as any);
|
||||
|
||||
const result = await moveFileTool.execute({
|
||||
source: 'file.txt',
|
||||
destination: '/target/dir',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('file.txt');
|
||||
});
|
||||
|
||||
it('源文件移动权限被拒绝', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: false,
|
||||
action: 'deny',
|
||||
reason: '不允许移动',
|
||||
}),
|
||||
} as any);
|
||||
|
||||
const result = await moveFileTool.execute({
|
||||
source: '/protected/file',
|
||||
destination: 'dest.txt',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('权限被拒绝');
|
||||
});
|
||||
|
||||
it('目标位置写入权限被拒绝', async () => {
|
||||
const mockCheck = vi.fn()
|
||||
.mockResolvedValueOnce({ allowed: true }) // 移动权限
|
||||
.mockResolvedValueOnce({ allowed: false, action: 'deny', reason: '不允许写入' }); // 写入权限
|
||||
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: mockCheck,
|
||||
} as any);
|
||||
|
||||
const result = await moveFileTool.execute({
|
||||
source: 'src.txt',
|
||||
destination: '/protected/dest.txt',
|
||||
});
|
||||
|
||||
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 moveFileTool.execute({
|
||||
source: 'file.txt',
|
||||
destination: 'new.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.access).mockRejectedValue(new Error('ENOENT'));
|
||||
|
||||
const result = await moveFileTool.execute({
|
||||
source: 'nonexistent.txt',
|
||||
destination: 'dest.txt',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('ENOENT');
|
||||
});
|
||||
|
||||
it('创建目标目录', async () => {
|
||||
// 确保权限检查通过
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({ allowed: true }),
|
||||
} as any);
|
||||
// 源文件存在
|
||||
vi.mocked(fs.access).mockResolvedValue(undefined);
|
||||
// 目标不存在
|
||||
vi.mocked(fs.stat).mockRejectedValue(new Error('ENOENT'));
|
||||
|
||||
const result = await moveFileTool.execute({
|
||||
source: 'file.txt',
|
||||
destination: '/new/path/file.txt',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(fs.mkdir).toHaveBeenCalledWith(
|
||||
expect.any(String),
|
||||
{ recursive: true }
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,199 +0,0 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
|
||||
// Mock fs/promises
|
||||
vi.mock('fs/promises', () => ({
|
||||
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 { searchFilesTool } from '../../../../src/tools/filesystem/search_files.js';
|
||||
import * as fs from 'fs/promises';
|
||||
import { getPermissionManager } from '../../../../src/permission/index.js';
|
||||
|
||||
describe('searchFilesTool - 文件搜索工具', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('工具定义', () => {
|
||||
it('有正确的名称', () => {
|
||||
expect(searchFilesTool.name).toBe('search_files');
|
||||
});
|
||||
|
||||
it('有正确的元数据', () => {
|
||||
expect(searchFilesTool.metadata.category).toBe('filesystem');
|
||||
expect(searchFilesTool.metadata.keywords).toContain('search');
|
||||
expect(searchFilesTool.metadata.keywords).toContain('find');
|
||||
expect(searchFilesTool.metadata.keywords).toContain('glob');
|
||||
});
|
||||
|
||||
it('定义了必需参数', () => {
|
||||
expect(searchFilesTool.parameters.directory.required).toBe(true);
|
||||
expect(searchFilesTool.parameters.pattern.required).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute - 执行', () => {
|
||||
it('成功搜索并返回匹配文件', async () => {
|
||||
vi.mocked(fs.readdir).mockResolvedValue([
|
||||
{ name: 'test.ts', isDirectory: () => false, isFile: () => true },
|
||||
{ name: 'test.js', isDirectory: () => false, isFile: () => true },
|
||||
] as any);
|
||||
|
||||
const result = await searchFilesTool.execute({
|
||||
directory: '.',
|
||||
pattern: '*.ts',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('test.ts');
|
||||
expect(result.output).not.toContain('test.js');
|
||||
});
|
||||
|
||||
it('没有匹配时返回提示', async () => {
|
||||
vi.mocked(fs.readdir).mockResolvedValue([
|
||||
{ name: 'test.js', isDirectory: () => false, isFile: () => true },
|
||||
] as any);
|
||||
|
||||
const result = await searchFilesTool.execute({
|
||||
directory: '.',
|
||||
pattern: '*.tsx',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('没有找到匹配的文件');
|
||||
});
|
||||
|
||||
it('递归搜索子目录', async () => {
|
||||
vi.mocked(fs.readdir)
|
||||
.mockResolvedValueOnce([
|
||||
{ name: 'src', isDirectory: () => true, isFile: () => false },
|
||||
{ name: 'index.ts', isDirectory: () => false, isFile: () => true },
|
||||
] as any)
|
||||
.mockResolvedValueOnce([
|
||||
{ name: 'app.ts', isDirectory: () => false, isFile: () => true },
|
||||
] as any);
|
||||
|
||||
const result = await searchFilesTool.execute({
|
||||
directory: '.',
|
||||
pattern: '*.ts',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('index.ts');
|
||||
expect(result.output).toContain('app.ts');
|
||||
});
|
||||
|
||||
it('跳过隐藏文件和 node_modules', async () => {
|
||||
// 第一次调用返回根目录内容,第二次调用返回 src 目录内容
|
||||
vi.mocked(fs.readdir)
|
||||
.mockResolvedValueOnce([
|
||||
{ name: '.git', isDirectory: () => true, isFile: () => false },
|
||||
{ name: 'node_modules', isDirectory: () => true, isFile: () => false },
|
||||
{ name: 'src', isDirectory: () => true, isFile: () => false },
|
||||
] as any)
|
||||
.mockResolvedValueOnce([
|
||||
{ name: 'index.ts', isDirectory: () => false, isFile: () => true },
|
||||
] as any);
|
||||
|
||||
await searchFilesTool.execute({
|
||||
directory: '.',
|
||||
pattern: '*',
|
||||
});
|
||||
|
||||
// 不应该进入隐藏目录或 node_modules,只进入 src
|
||||
expect(fs.readdir).toHaveBeenCalledTimes(2); // 根目录 + src
|
||||
});
|
||||
|
||||
it('权限被拒绝时返回错误', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: false,
|
||||
action: 'deny',
|
||||
reason: '不允许搜索',
|
||||
}),
|
||||
} as any);
|
||||
|
||||
const result = await searchFilesTool.execute({
|
||||
directory: '/protected',
|
||||
pattern: '*',
|
||||
});
|
||||
|
||||
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 searchFilesTool.execute({
|
||||
directory: '.',
|
||||
pattern: '*',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('需要用户确认');
|
||||
});
|
||||
|
||||
it('支持 glob 模式匹配', async () => {
|
||||
// 恢复权限检查
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({ allowed: true }),
|
||||
} as any);
|
||||
vi.mocked(fs.readdir).mockResolvedValue([
|
||||
{ name: 'component.tsx', isDirectory: () => false, isFile: () => true },
|
||||
{ name: 'helper.ts', isDirectory: () => false, isFile: () => true },
|
||||
{ name: 'style.css', isDirectory: () => false, isFile: () => true },
|
||||
] as any);
|
||||
|
||||
// *.tsx 模式会匹配 component.tsx
|
||||
const result = await searchFilesTool.execute({
|
||||
directory: '.',
|
||||
pattern: '*.tsx',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('component.tsx');
|
||||
expect(result.output).not.toContain('style.css');
|
||||
});
|
||||
|
||||
it('传递正确参数给权限检查', async () => {
|
||||
const mockCheck = vi.fn().mockResolvedValue({ allowed: true });
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: mockCheck,
|
||||
} as any);
|
||||
vi.mocked(fs.readdir).mockResolvedValue([]);
|
||||
|
||||
await searchFilesTool.execute({
|
||||
directory: 'src',
|
||||
pattern: '*.ts',
|
||||
});
|
||||
|
||||
expect(mockCheck).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
operation: 'search',
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -165,13 +165,9 @@ describe('loadDescription', () => {
|
||||
{ tool: 'write_file', category: 'filesystem' },
|
||||
{ tool: 'edit_file', category: 'filesystem' },
|
||||
{ tool: 'list_directory', category: 'filesystem' },
|
||||
{ tool: 'create_directory', category: 'filesystem' },
|
||||
{ tool: 'search_files', category: 'filesystem' },
|
||||
{ tool: 'glob', category: 'filesystem' },
|
||||
{ tool: 'grep', category: 'filesystem' },
|
||||
{ tool: 'get_file_info', category: 'filesystem' },
|
||||
{ tool: 'move_file', category: 'filesystem' },
|
||||
{ tool: 'copy_file', category: 'filesystem' },
|
||||
{ tool: 'delete_file', category: 'filesystem' },
|
||||
// web
|
||||
{ tool: 'web_search', category: 'web' },
|
||||
{ tool: 'web_extract', category: 'web' },
|
||||
|
||||
Reference in New Issue
Block a user