feat: 添加完整的单元测试套件
- 新增 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
This commit is contained in:
@@ -0,0 +1,173 @@
|
||||
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');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,156 @@
|
||||
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');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,173 @@
|
||||
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',
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,201 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
|
||||
// Mock fs/promises
|
||||
vi.mock('fs/promises', () => ({
|
||||
readFile: vi.fn(),
|
||||
writeFile: 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(() => '编辑文件'),
|
||||
}));
|
||||
|
||||
// Mock LSP
|
||||
vi.mock('../../../../src/lsp/index.js', () => ({
|
||||
touchFile: vi.fn().mockResolvedValue(false),
|
||||
getFormattedFileDiagnostics: vi.fn().mockResolvedValue(null),
|
||||
isLanguageSupported: vi.fn().mockReturnValue(false),
|
||||
}));
|
||||
|
||||
import { editFileTool } from '../../../../src/tools/filesystem/edit_file.js';
|
||||
import * as fs from 'fs/promises';
|
||||
import { getPermissionManager } from '../../../../src/permission/index.js';
|
||||
import { isLanguageSupported, touchFile, getFormattedFileDiagnostics } from '../../../../src/lsp/index.js';
|
||||
|
||||
describe('editFileTool - 文件编辑工具', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
vi.mocked(fs.readFile).mockResolvedValue('original content here');
|
||||
});
|
||||
|
||||
describe('工具定义', () => {
|
||||
it('有正确的名称', () => {
|
||||
expect(editFileTool.name).toBe('edit_file');
|
||||
});
|
||||
|
||||
it('有正确的元数据', () => {
|
||||
expect(editFileTool.metadata.category).toBe('filesystem');
|
||||
expect(editFileTool.metadata.keywords).toContain('edit');
|
||||
expect(editFileTool.metadata.keywords).toContain('replace');
|
||||
});
|
||||
|
||||
it('定义了必需参数', () => {
|
||||
expect(editFileTool.parameters.path.required).toBe(true);
|
||||
expect(editFileTool.parameters.old_string.required).toBe(true);
|
||||
expect(editFileTool.parameters.new_string.required).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute - 执行', () => {
|
||||
it('成功编辑文件', async () => {
|
||||
vi.mocked(fs.readFile).mockResolvedValue('hello world');
|
||||
|
||||
const result = await editFileTool.execute({
|
||||
path: 'test.txt',
|
||||
old_string: 'world',
|
||||
new_string: 'universe',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('文件已编辑');
|
||||
expect(fs.writeFile).toHaveBeenCalledWith(
|
||||
expect.any(String),
|
||||
'hello universe',
|
||||
'utf-8'
|
||||
);
|
||||
});
|
||||
|
||||
it('old_string 不存在返回错误', async () => {
|
||||
vi.mocked(fs.readFile).mockResolvedValue('hello world');
|
||||
|
||||
const result = await editFileTool.execute({
|
||||
path: 'test.txt',
|
||||
old_string: 'notfound',
|
||||
new_string: 'replacement',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('未找到要替换的字符串');
|
||||
});
|
||||
|
||||
it('old_string 多次出现返回错误', async () => {
|
||||
vi.mocked(fs.readFile).mockResolvedValue('hello hello hello');
|
||||
|
||||
const result = await editFileTool.execute({
|
||||
path: 'test.txt',
|
||||
old_string: 'hello',
|
||||
new_string: 'hi',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('3 处匹配');
|
||||
expect(result.error).toContain('必须唯一');
|
||||
});
|
||||
|
||||
it('权限被拒绝时返回错误', async () => {
|
||||
vi.mocked(fs.readFile).mockResolvedValue('content');
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: false,
|
||||
action: 'deny',
|
||||
reason: '不允许编辑',
|
||||
}),
|
||||
} as any);
|
||||
|
||||
const result = await editFileTool.execute({
|
||||
path: 'test.txt',
|
||||
old_string: 'content',
|
||||
new_string: 'new',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('权限被拒绝');
|
||||
});
|
||||
|
||||
it('需要确认时返回提示', async () => {
|
||||
vi.mocked(fs.readFile).mockResolvedValue('content');
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: false,
|
||||
action: 'ask',
|
||||
needsConfirmation: true,
|
||||
}),
|
||||
} as any);
|
||||
|
||||
const result = await editFileTool.execute({
|
||||
path: 'test.txt',
|
||||
old_string: 'content',
|
||||
new_string: 'new',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('需要用户确认');
|
||||
});
|
||||
|
||||
it('文件不存在返回错误', async () => {
|
||||
vi.mocked(fs.readFile).mockRejectedValue(new Error('ENOENT: no such file'));
|
||||
|
||||
const result = await editFileTool.execute({
|
||||
path: 'nonexistent.txt',
|
||||
old_string: 'text',
|
||||
new_string: 'new',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('ENOENT');
|
||||
});
|
||||
|
||||
it('支持 LSP 时获取诊断信息', async () => {
|
||||
// 确保权限检查通过
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({ allowed: true }),
|
||||
} as any);
|
||||
vi.mocked(fs.readFile).mockResolvedValue('const x = 1');
|
||||
vi.mocked(isLanguageSupported).mockReturnValue(true);
|
||||
vi.mocked(touchFile).mockResolvedValue(false);
|
||||
vi.mocked(getFormattedFileDiagnostics).mockResolvedValue('\n错误: 类型不匹配');
|
||||
|
||||
const result = await editFileTool.execute({
|
||||
path: 'test.ts',
|
||||
old_string: 'const x = 1',
|
||||
new_string: 'const x: string = 1',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('代码检查发现问题');
|
||||
});
|
||||
|
||||
it('传递正确参数给权限检查', async () => {
|
||||
vi.mocked(fs.readFile).mockResolvedValue('old text');
|
||||
const mockCheck = vi.fn().mockResolvedValue({ allowed: true });
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: mockCheck,
|
||||
} as any);
|
||||
|
||||
await editFileTool.execute({
|
||||
path: 'test.txt',
|
||||
old_string: 'old text',
|
||||
new_string: 'new text',
|
||||
});
|
||||
|
||||
expect(mockCheck).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
operation: 'edit',
|
||||
oldContent: 'old text',
|
||||
newContent: 'new text',
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,188 @@
|
||||
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',
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,225 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
|
||||
// Mock fs/promises
|
||||
vi.mock('fs/promises', () => ({
|
||||
readdir: vi.fn(),
|
||||
readFile: 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 { grepContentTool } from '../../../../src/tools/filesystem/grep_content.js';
|
||||
import * as fs from 'fs/promises';
|
||||
import { getPermissionManager } from '../../../../src/permission/index.js';
|
||||
|
||||
describe('grepContentTool - 内容搜索工具', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('工具定义', () => {
|
||||
it('有正确的名称', () => {
|
||||
expect(grepContentTool.name).toBe('grep_content');
|
||||
});
|
||||
|
||||
it('有正确的元数据', () => {
|
||||
expect(grepContentTool.metadata.category).toBe('filesystem');
|
||||
expect(grepContentTool.metadata.keywords).toContain('grep');
|
||||
expect(grepContentTool.metadata.keywords).toContain('search');
|
||||
expect(grepContentTool.metadata.keywords).toContain('content');
|
||||
});
|
||||
|
||||
it('定义了必需参数', () => {
|
||||
expect(grepContentTool.parameters.directory.required).toBe(true);
|
||||
expect(grepContentTool.parameters.pattern.required).toBe(true);
|
||||
});
|
||||
|
||||
it('定义了可选参数', () => {
|
||||
expect(grepContentTool.parameters.file_pattern.required).toBe(false);
|
||||
expect(grepContentTool.parameters.max_results.required).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute - 执行', () => {
|
||||
it('成功搜索并返回匹配结果', async () => {
|
||||
vi.mocked(fs.readdir).mockResolvedValue([
|
||||
{ name: 'test.ts', isDirectory: () => false, isFile: () => true },
|
||||
] as any);
|
||||
vi.mocked(fs.readFile).mockResolvedValue('const hello = "world";\nconst foo = "bar";');
|
||||
|
||||
const result = await grepContentTool.execute({
|
||||
directory: '.',
|
||||
pattern: 'hello',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('找到');
|
||||
expect(result.output).toContain('hello');
|
||||
});
|
||||
|
||||
it('没有匹配时返回提示', async () => {
|
||||
vi.mocked(fs.readdir).mockResolvedValue([
|
||||
{ name: 'test.ts', isDirectory: () => false, isFile: () => true },
|
||||
] as any);
|
||||
vi.mocked(fs.readFile).mockResolvedValue('const foo = "bar";');
|
||||
|
||||
const result = await grepContentTool.execute({
|
||||
directory: '.',
|
||||
pattern: 'notfound',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('没有找到匹配的内容');
|
||||
});
|
||||
|
||||
it('按文件模式过滤', async () => {
|
||||
vi.mocked(fs.readdir).mockResolvedValue([
|
||||
{ name: 'test.ts', isDirectory: () => false, isFile: () => true },
|
||||
{ name: 'test.js', isDirectory: () => false, isFile: () => true },
|
||||
] as any);
|
||||
vi.mocked(fs.readFile).mockResolvedValue('const hello = "world";');
|
||||
|
||||
const result = await grepContentTool.execute({
|
||||
directory: '.',
|
||||
pattern: 'hello',
|
||||
file_pattern: '*.ts',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
// 只搜索 .ts 文件
|
||||
expect(fs.readFile).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('限制最大结果数', async () => {
|
||||
vi.mocked(fs.readdir).mockResolvedValue([
|
||||
{ name: 'test.ts', isDirectory: () => false, isFile: () => true },
|
||||
] as any);
|
||||
// 多行匹配内容
|
||||
vi.mocked(fs.readFile).mockResolvedValue(
|
||||
'hello1\nhello2\nhello3\nhello4\nhello5'
|
||||
);
|
||||
|
||||
const result = await grepContentTool.execute({
|
||||
directory: '.',
|
||||
pattern: 'hello',
|
||||
max_results: 2,
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('已达上限');
|
||||
});
|
||||
|
||||
it('跳过隐藏文件和 node_modules', async () => {
|
||||
// 第一次调用返回根目录内容,第二次调用返回 src 目录内容
|
||||
vi.mocked(fs.readdir)
|
||||
.mockResolvedValueOnce([
|
||||
{ name: '.hidden', 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);
|
||||
vi.mocked(fs.readFile).mockResolvedValue('const test = 1;');
|
||||
|
||||
await grepContentTool.execute({
|
||||
directory: '.',
|
||||
pattern: 'test',
|
||||
});
|
||||
|
||||
// 不应该进入隐藏目录或 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 grepContentTool.execute({
|
||||
directory: '/protected',
|
||||
pattern: 'test',
|
||||
});
|
||||
|
||||
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 grepContentTool.execute({
|
||||
directory: '.',
|
||||
pattern: 'test',
|
||||
});
|
||||
|
||||
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.readdir)
|
||||
.mockResolvedValueOnce([
|
||||
{ name: 'src', isDirectory: () => true, isFile: () => false },
|
||||
] as any)
|
||||
.mockResolvedValueOnce([
|
||||
{ name: 'index.ts', isDirectory: () => false, isFile: () => true },
|
||||
] as any);
|
||||
vi.mocked(fs.readFile).mockResolvedValue('const test = 1;');
|
||||
|
||||
const result = await grepContentTool.execute({
|
||||
directory: '.',
|
||||
pattern: 'test',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(fs.readdir).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it('传递正确参数给权限检查', async () => {
|
||||
const mockCheck = vi.fn().mockResolvedValue({ allowed: true });
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: mockCheck,
|
||||
} as any);
|
||||
vi.mocked(fs.readdir).mockResolvedValue([]);
|
||||
|
||||
await grepContentTool.execute({
|
||||
directory: 'src',
|
||||
pattern: 'test',
|
||||
});
|
||||
|
||||
expect(mockCheck).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
operation: 'grep',
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,143 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { listDirTool } from '../../../../src/tools/filesystem/list_directory.js';
|
||||
|
||||
// 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 * as fs from 'fs/promises';
|
||||
import { getPermissionManager } from '../../../../src/permission/index.js';
|
||||
|
||||
describe('listDirTool - 列出目录工具', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('工具定义', () => {
|
||||
it('有正确的名称', () => {
|
||||
expect(listDirTool.name).toBe('list_directory');
|
||||
});
|
||||
|
||||
it('有正确的元数据', () => {
|
||||
expect(listDirTool.metadata.category).toBe('filesystem');
|
||||
expect(listDirTool.metadata.keywords).toContain('list');
|
||||
expect(listDirTool.metadata.keywords).toContain('directory');
|
||||
expect(listDirTool.metadata.keywords).toContain('ls');
|
||||
});
|
||||
|
||||
it('定义了必需的 path 参数', () => {
|
||||
expect(listDirTool.parameters.path).toBeDefined();
|
||||
expect(listDirTool.parameters.path.required).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute - 执行', () => {
|
||||
it('成功列出目录内容', async () => {
|
||||
vi.mocked(fs.readdir).mockResolvedValue([
|
||||
{ name: 'file1.txt', isDirectory: () => false },
|
||||
{ name: 'folder', isDirectory: () => true },
|
||||
{ name: 'file2.js', isDirectory: () => false },
|
||||
] as any);
|
||||
|
||||
const result = await listDirTool.execute({ path: './' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('file1.txt');
|
||||
expect(result.output).toContain('folder');
|
||||
expect(result.output).toContain('file2.js');
|
||||
});
|
||||
|
||||
it('使用正确的图标区分文件和目录', async () => {
|
||||
vi.mocked(fs.readdir).mockResolvedValue([
|
||||
{ name: 'file.txt', isDirectory: () => false },
|
||||
{ name: 'folder', isDirectory: () => true },
|
||||
] as any);
|
||||
|
||||
const result = await listDirTool.execute({ path: './' });
|
||||
|
||||
expect(result.output).toMatch(/📄.*file\.txt/);
|
||||
expect(result.output).toMatch(/📁.*folder/);
|
||||
});
|
||||
|
||||
it('空目录显示提示', async () => {
|
||||
vi.mocked(fs.readdir).mockResolvedValue([]);
|
||||
|
||||
const result = await listDirTool.execute({ path: './' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toBe('(空目录)');
|
||||
});
|
||||
|
||||
it('权限被拒绝时返回错误', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: false,
|
||||
action: 'deny',
|
||||
reason: '不允许列出此目录',
|
||||
}),
|
||||
} as any);
|
||||
|
||||
const result = await listDirTool.execute({ path: '/etc' });
|
||||
|
||||
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 listDirTool.execute({ path: '/home/user' });
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('需要用户确认');
|
||||
});
|
||||
|
||||
it('目录不存在时返回错误', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: true,
|
||||
action: 'allow',
|
||||
}),
|
||||
} as any);
|
||||
vi.mocked(fs.readdir).mockRejectedValue(new Error('ENOENT: no such directory'));
|
||||
|
||||
const result = await listDirTool.execute({ path: './nonexistent' });
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('ENOENT');
|
||||
});
|
||||
|
||||
it('使用 withFileTypes 选项调用 readdir', async () => {
|
||||
vi.mocked(fs.readdir).mockResolvedValue([]);
|
||||
|
||||
await listDirTool.execute({ path: './' });
|
||||
|
||||
expect(fs.readdir).toHaveBeenCalledWith(
|
||||
expect.any(String),
|
||||
{ withFileTypes: true }
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,171 @@
|
||||
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 }
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,142 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { readFileTool } from '../../../../src/tools/filesystem/read_file.js';
|
||||
|
||||
// Mock fs/promises
|
||||
vi.mock('fs/promises', () => ({
|
||||
readFile: 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 * as fs from 'fs/promises';
|
||||
import { getPermissionManager } from '../../../../src/permission/index.js';
|
||||
|
||||
describe('readFileTool - 读取文件工具', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('工具定义', () => {
|
||||
it('有正确的名称', () => {
|
||||
expect(readFileTool.name).toBe('read_file');
|
||||
});
|
||||
|
||||
it('有正确的元数据', () => {
|
||||
expect(readFileTool.metadata.category).toBe('filesystem');
|
||||
expect(readFileTool.metadata.keywords).toContain('read');
|
||||
expect(readFileTool.metadata.keywords).toContain('file');
|
||||
});
|
||||
|
||||
it('定义了必需的 path 参数', () => {
|
||||
expect(readFileTool.parameters.path).toBeDefined();
|
||||
expect(readFileTool.parameters.path.required).toBe(true);
|
||||
expect(readFileTool.parameters.path.type).toBe('string');
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute - 执行', () => {
|
||||
it('成功读取文件', async () => {
|
||||
const mockContent = 'Hello, World!';
|
||||
vi.mocked(fs.readFile).mockResolvedValue(mockContent);
|
||||
|
||||
const result = await readFileTool.execute({ path: './test.txt' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toBe(mockContent);
|
||||
});
|
||||
|
||||
it('处理绝对路径', async () => {
|
||||
vi.mocked(fs.readFile).mockResolvedValue('content');
|
||||
|
||||
await readFileTool.execute({ path: '/absolute/path/file.txt' });
|
||||
|
||||
expect(fs.readFile).toHaveBeenCalledWith('/absolute/path/file.txt', 'utf-8');
|
||||
});
|
||||
|
||||
it('处理相对路径', async () => {
|
||||
vi.mocked(fs.readFile).mockResolvedValue('content');
|
||||
|
||||
await readFileTool.execute({ path: './relative/file.txt' });
|
||||
|
||||
// 应该解析为绝对路径
|
||||
expect(fs.readFile).toHaveBeenCalled();
|
||||
const calledPath = vi.mocked(fs.readFile).mock.calls[0][0] as string;
|
||||
expect(calledPath.endsWith('relative/file.txt')).toBe(true);
|
||||
});
|
||||
|
||||
it('权限被拒绝时返回错误', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: false,
|
||||
action: 'deny',
|
||||
reason: '不允许读取此文件',
|
||||
}),
|
||||
} as any);
|
||||
|
||||
const result = await readFileTool.execute({ path: '/etc/passwd' });
|
||||
|
||||
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,
|
||||
reason: '需要确认',
|
||||
}),
|
||||
} as any);
|
||||
|
||||
const result = await readFileTool.execute({ path: './sensitive.txt' });
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('需要用户确认');
|
||||
});
|
||||
|
||||
it('文件不存在时返回错误', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: true,
|
||||
action: 'allow',
|
||||
}),
|
||||
} as any);
|
||||
vi.mocked(fs.readFile).mockRejectedValue(new Error('ENOENT: no such file'));
|
||||
|
||||
const result = await readFileTool.execute({ path: './nonexistent.txt' });
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('ENOENT');
|
||||
});
|
||||
|
||||
it('读取大文件', async () => {
|
||||
const largeContent = 'x'.repeat(10000);
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: true,
|
||||
action: 'allow',
|
||||
}),
|
||||
} as any);
|
||||
vi.mocked(fs.readFile).mockResolvedValue(largeContent);
|
||||
|
||||
const result = await readFileTool.execute({ path: './large.txt' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output.length).toBe(10000);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,199 @@
|
||||
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',
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,158 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { writeFileTool } from '../../../../src/tools/filesystem/write_file.js';
|
||||
|
||||
// Mock fs/promises
|
||||
vi.mock('fs/promises', () => ({
|
||||
mkdir: vi.fn().mockResolvedValue(undefined),
|
||||
writeFile: 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(() => '写入文件内容'),
|
||||
}));
|
||||
|
||||
// Mock LSP
|
||||
vi.mock('../../../../src/lsp/index.js', () => ({
|
||||
touchFile: vi.fn().mockResolvedValue(false),
|
||||
getFormattedFileDiagnostics: vi.fn().mockResolvedValue(null),
|
||||
isLanguageSupported: vi.fn().mockReturnValue(false),
|
||||
}));
|
||||
|
||||
import * as fs from 'fs/promises';
|
||||
import { getPermissionManager } from '../../../../src/permission/index.js';
|
||||
|
||||
describe('writeFileTool - 写入文件工具', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('工具定义', () => {
|
||||
it('有正确的名称', () => {
|
||||
expect(writeFileTool.name).toBe('write_file');
|
||||
});
|
||||
|
||||
it('有正确的元数据', () => {
|
||||
expect(writeFileTool.metadata.category).toBe('filesystem');
|
||||
expect(writeFileTool.metadata.keywords).toContain('write');
|
||||
expect(writeFileTool.metadata.keywords).toContain('save');
|
||||
});
|
||||
|
||||
it('定义了必需的参数', () => {
|
||||
expect(writeFileTool.parameters.path).toBeDefined();
|
||||
expect(writeFileTool.parameters.path.required).toBe(true);
|
||||
expect(writeFileTool.parameters.content).toBeDefined();
|
||||
expect(writeFileTool.parameters.content.required).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute - 执行', () => {
|
||||
it('成功写入文件', async () => {
|
||||
const result = await writeFileTool.execute({
|
||||
path: './test.txt',
|
||||
content: 'Hello, World!',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.output).toContain('文件已写入');
|
||||
});
|
||||
|
||||
it('创建必要的目录', async () => {
|
||||
await writeFileTool.execute({
|
||||
path: './deep/nested/file.txt',
|
||||
content: 'content',
|
||||
});
|
||||
|
||||
expect(fs.mkdir).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 writeFileTool.execute({
|
||||
path: '/etc/passwd',
|
||||
content: 'malicious',
|
||||
});
|
||||
|
||||
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 writeFileTool.execute({
|
||||
path: './new-file.txt',
|
||||
content: 'content',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('需要用户确认');
|
||||
});
|
||||
|
||||
it('写入失败时返回错误', async () => {
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: vi.fn().mockResolvedValue({
|
||||
allowed: true,
|
||||
action: 'allow',
|
||||
}),
|
||||
} as any);
|
||||
vi.mocked(fs.writeFile).mockRejectedValue(new Error('Write failed'));
|
||||
|
||||
const result = await writeFileTool.execute({
|
||||
path: './test.txt',
|
||||
content: 'content',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.error).toContain('Write failed');
|
||||
});
|
||||
|
||||
it('传递 newContent 给权限检查', async () => {
|
||||
const mockCheck = vi.fn().mockResolvedValue({
|
||||
allowed: true,
|
||||
action: 'allow',
|
||||
});
|
||||
vi.mocked(getPermissionManager).mockReturnValue({
|
||||
checkFilePermission: mockCheck,
|
||||
} as any);
|
||||
|
||||
await writeFileTool.execute({
|
||||
path: './test.txt',
|
||||
content: 'new content',
|
||||
});
|
||||
|
||||
expect(mockCheck).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
operation: 'write',
|
||||
newContent: 'new content',
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user