Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/filesystem/__tests__/structured-content.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,25 @@ describe('structuredContent schema compliance', () => {
// The content should contain success message
expect(structuredContent.content).toContain('Successfully moved');
});

it('should reject an existing destination without overwriting it', async () => {
const sourcePath = path.join(testDir, 'test.txt');
const destPath = path.join(testDir, 'existing.txt');
await fs.writeFile(destPath, 'keep this content');

const result = await client.callTool({
name: 'move_file',
arguments: { source: sourcePath, destination: destPath }
});
expect(result.isError).toBe(true);
expect(result.content[0]).toMatchObject({
type: 'text',
text: expect.stringContaining('Destination already exists'),
});

await expect(fs.readFile(sourcePath, 'utf-8')).resolves.toBe('test content');
await expect(fs.readFile(destPath, 'utf-8')).resolves.toBe('keep this content');
});
});

describe('list_directory (control - already working)', () => {
Expand Down
11 changes: 11 additions & 0 deletions src/filesystem/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,17 @@ server.registerTool(
async (args: z.infer<typeof MoveFileArgsSchema>) => {
const validSourcePath = await validatePath(args.source);
const validDestPath = await validatePath(args.destination);
// fs.rename replaces an existing destination on POSIX (and has platform-
// dependent overwrite semantics elsewhere). The tool contract promises a
// failed move instead, so make the check explicit to prevent data loss.
try {
await fs.lstat(validDestPath);
throw new Error(`Destination already exists: ${args.destination}`);
} catch (error) {
if ((error as NodeJS.ErrnoException).code !== 'ENOENT') {
throw error;
}
}
await fs.rename(validSourcePath, validDestPath);
const text = `Successfully moved ${args.source} to ${args.destination}`;
const contentBlock = { type: "text" as const, text };
Expand Down