719032b19f
Update Schema / Update configuration json schema (push) Has been cancelled
Memory Benchmark / Memory Test (Full Analysis) (push) Has been cancelled
CI Quality / Lint GitHub Actions (actionlint) (push) Has been cancelled
CI Quality / Lint GitHub Actions (zizmor) (push) Has been cancelled
CI Quality / Check typos (push) Has been cancelled
CI / Test coverage (push) Has been cancelled
CI / Test (22.x, windows-latest) (push) Has been cancelled
CI / Test (24.x, macos-latest) (push) Has been cancelled
CI / Test (24.x, ubuntu-latest) (push) Has been cancelled
CI / Test (24.x, windows-latest) (push) Has been cancelled
CI / Test (26.x, macos-latest) (push) Has been cancelled
CI / Test (26.x, ubuntu-latest) (push) Has been cancelled
CI / Test (26.x, windows-latest) (push) Has been cancelled
CI / Test with Bun (latest, macos-latest) (push) Has been cancelled
CI / Test with Bun (latest, ubuntu-latest) (push) Has been cancelled
CI / Test with Bun (latest, windows-latest) (push) Has been cancelled
CI / Build and run (22.x, macos-latest) (push) Has been cancelled
CI / Build and run (22.x, ubuntu-latest) (push) Has been cancelled
CI / Build and run (22.x, windows-latest) (push) Has been cancelled
autofix.ci / autofix (push) Has been cancelled
CI Browser Extension / Lint Browser Extension (push) Has been cancelled
CI Browser Extension / Test Browser Extension (push) Has been cancelled
Memory Benchmark / Memory Test (push) Has been cancelled
CI Website / Lint Website Client (push) Has been cancelled
CI Website / Lint Website Server (push) Has been cancelled
CI Website / Test Website Server (push) Has been cancelled
CI Website / Bundle Website Server (push) Has been cancelled
CI / Lint Biome (push) Has been cancelled
CI / Lint oxlint (push) Has been cancelled
CI / Lint TypeScript (push) Has been cancelled
CI / Lint Secretlint (push) Has been cancelled
CI / Test (22.x, macos-latest) (push) Has been cancelled
CI / Test (22.x, ubuntu-latest) (push) Has been cancelled
CI / Build and run (24.x, macos-latest) (push) Has been cancelled
CI / Build and run (24.x, ubuntu-latest) (push) Has been cancelled
CI / Build and run (24.x, windows-latest) (push) Has been cancelled
CI / Build and run (26.x, macos-latest) (push) Has been cancelled
CI / Build and run (26.x, ubuntu-latest) (push) Has been cancelled
CI / Build and run (26.x, windows-latest) (push) Has been cancelled
CI / Build and run with Bun (latest, macos-latest) (push) Has been cancelled
CI / Build and run with Bun (latest, ubuntu-latest) (push) Has been cancelled
CI / Build and run with Bun (latest, windows-latest) (push) Has been cancelled
CodeQL / Analyze (javascript-typescript) (push) Has been cancelled
Docker / build (linux/amd64, ubuntu-latest) (push) Has been cancelled
Docker / build (linux/arm/v7, ubuntu-24.04-arm) (push) Has been cancelled
Docker / build (linux/arm64, ubuntu-24.04-arm) (push) Has been cancelled
Docker / merge (push) Has been cancelled
Pack repository with Repomix / pack-repo (push) Has been cancelled
Performance Benchmark History / Benchmark (macos-latest) (push) Has been cancelled
Performance Benchmark History / Benchmark (ubuntu-latest) (push) Has been cancelled
Performance Benchmark History / Benchmark (windows-latest) (push) Has been cancelled
Performance Benchmark History / Store Results (push) Has been cancelled
Test Repomix Action / Test Node.js 22 (push) Has been cancelled
Test Repomix Action / Test Node.js 24 (push) Has been cancelled
Test Repomix Action / Test Node.js 26 (push) Has been cancelled
130 lines
4.9 KiB
TypeScript
130 lines
4.9 KiB
TypeScript
import fs from 'node:fs/promises';
|
|
import path from 'node:path';
|
|
import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
import type { CallToolResult } from '@modelcontextprotocol/sdk/types.js';
|
|
import { beforeEach, describe, expect, test, vi } from 'vitest';
|
|
import { registerFileSystemReadDirectoryTool } from '../../../src/mcp/tools/fileSystemReadDirectoryTool.js';
|
|
|
|
vi.mock('node:fs/promises');
|
|
vi.mock('node:path');
|
|
|
|
describe('FileSystemReadDirectoryTool', () => {
|
|
const mockServer = {
|
|
registerTool: vi.fn().mockReturnThis(),
|
|
} as unknown as McpServer;
|
|
|
|
let toolHandler: (args: { path: string }) => Promise<CallToolResult>;
|
|
|
|
beforeEach(() => {
|
|
vi.resetAllMocks();
|
|
registerFileSystemReadDirectoryTool(mockServer);
|
|
toolHandler = (mockServer.registerTool as ReturnType<typeof vi.fn>).mock.calls[0][2];
|
|
|
|
// Default mock for path.isAbsolute
|
|
vi.mocked(path.isAbsolute).mockImplementation((p: string) => p.startsWith('/'));
|
|
});
|
|
|
|
test('should register tool with correct parameters', () => {
|
|
expect(mockServer.registerTool).toHaveBeenCalledWith(
|
|
'file_system_read_directory',
|
|
expect.any(Object), // tool spec
|
|
expect.any(Function),
|
|
);
|
|
});
|
|
|
|
test('should handle relative path error', async () => {
|
|
const testPath = 'relative/path';
|
|
vi.mocked(path.isAbsolute).mockReturnValue(false);
|
|
|
|
const result = await toolHandler({ path: testPath });
|
|
|
|
expect(result).toEqual({
|
|
isError: true,
|
|
content: [
|
|
{
|
|
type: 'text',
|
|
text: JSON.stringify({ errorMessage: `Error: Path must be absolute. Received: ${testPath}` }, null, 2),
|
|
},
|
|
],
|
|
});
|
|
});
|
|
|
|
test('should handle non-existent directory', async () => {
|
|
const testPath = '/non/existent/dir';
|
|
vi.mocked(path.isAbsolute).mockReturnValue(true);
|
|
vi.mocked(fs.stat).mockRejectedValue(new Error('ENOENT'));
|
|
|
|
const result = await toolHandler({ path: testPath });
|
|
|
|
expect(result).toEqual({
|
|
isError: true,
|
|
content: [
|
|
{
|
|
type: 'text',
|
|
text: JSON.stringify({ errorMessage: `Error: Directory not found at path: ${testPath}` }, null, 2),
|
|
},
|
|
],
|
|
});
|
|
});
|
|
|
|
test('should error when path points to a file', async () => {
|
|
const testPath = '/some/file.txt';
|
|
vi.mocked(path.isAbsolute).mockReturnValue(true);
|
|
vi.mocked(fs.stat).mockResolvedValue({ isDirectory: () => false } as Awaited<ReturnType<typeof fs.stat>>);
|
|
|
|
const result = await toolHandler({ path: testPath });
|
|
|
|
expect(result.isError).toBe(true);
|
|
const text = (result.content[0] as { type: 'text'; text: string }).text;
|
|
expect(JSON.parse(text).errorMessage).toContain('not a directory');
|
|
});
|
|
|
|
test('should list directory contents with [FILE]/[DIR] prefixes', async () => {
|
|
const testPath = '/some/dir';
|
|
vi.mocked(path.isAbsolute).mockReturnValue(true);
|
|
vi.mocked(fs.stat).mockResolvedValue({ isDirectory: () => true } as Awaited<ReturnType<typeof fs.stat>>);
|
|
vi.mocked(fs.readdir).mockResolvedValue([
|
|
{ name: 'a.ts', isFile: () => true, isDirectory: () => false },
|
|
{ name: 'subdir', isFile: () => false, isDirectory: () => true },
|
|
{ name: 'b.md', isFile: () => true, isDirectory: () => false },
|
|
] as unknown as Awaited<ReturnType<typeof fs.readdir>>);
|
|
|
|
const result = await toolHandler({ path: testPath });
|
|
|
|
expect(result.isError).toBeUndefined();
|
|
const text = (result.content[0] as { type: 'text'; text: string }).text;
|
|
const parsed = JSON.parse(text);
|
|
expect(parsed.contents).toEqual(['[FILE] a.ts', '[DIR] subdir', '[FILE] b.md']);
|
|
expect(parsed.fileCount).toBe(2);
|
|
expect(parsed.directoryCount).toBe(1);
|
|
expect(parsed.totalItems).toBe(3);
|
|
});
|
|
|
|
test('should report empty directory placeholder', async () => {
|
|
const testPath = '/empty/dir';
|
|
vi.mocked(path.isAbsolute).mockReturnValue(true);
|
|
vi.mocked(fs.stat).mockResolvedValue({ isDirectory: () => true } as Awaited<ReturnType<typeof fs.stat>>);
|
|
vi.mocked(fs.readdir).mockResolvedValue([] as unknown as Awaited<ReturnType<typeof fs.readdir>>);
|
|
|
|
const result = await toolHandler({ path: testPath });
|
|
|
|
const text = (result.content[0] as { type: 'text'; text: string }).text;
|
|
const parsed = JSON.parse(text);
|
|
expect(parsed.contents).toEqual(['(empty directory)']);
|
|
expect(parsed.totalItems).toBe(0);
|
|
});
|
|
|
|
test('should report readdir failures via the outer catch', async () => {
|
|
const testPath = '/some/dir';
|
|
vi.mocked(path.isAbsolute).mockReturnValue(true);
|
|
vi.mocked(fs.stat).mockResolvedValue({ isDirectory: () => true } as Awaited<ReturnType<typeof fs.stat>>);
|
|
vi.mocked(fs.readdir).mockRejectedValue(new Error('EACCES'));
|
|
|
|
const result = await toolHandler({ path: testPath });
|
|
|
|
expect(result.isError).toBe(true);
|
|
const text = (result.content[0] as { type: 'text'; text: string }).text;
|
|
expect(JSON.parse(text).errorMessage).toContain('EACCES');
|
|
});
|
|
});
|