Files
repomix-mirror/tests/mcp/tools/packRemoteRepositoryTool.test.ts
Kazuki Yamada e5f7a1f311 fix(shared): Address PR review feedback
- shared/errorHandle: recognize duck-typed OperationCancelledError from
  worker boundaries in isRepomixError (it extends RepomixError but the
  name was missing from the structured-clone fallback comparison).
  Add a regression test for the worker-boundary case.

Test improvements per coderabbit / claude review:
- cliReport: assert skill-directory + relative path on the same log line.
- processConcurrency: restore process.versions.bun by removing the property
  when it didn't originally exist, instead of leaving it defined-as-undefined.
- logger: drop the no-op `process.env.REPOMIX_LOG_LEVEL = undefined` (it
  coerces to the string "undefined" and is overwritten by the next delete).
- unifiedWorker: replace the tautological cache test with one that proves
  cache uniqueness via onWorkerTermination cleanup count; add a test for
  task-based inference overriding workerData (bundled-env reuse).
- calculateMetricsWorker: new direct test for the default export's items
  vs. single-mode dispatch — unifiedWorker mocks this module so the branch
  was otherwise untested.
- packRemoteRepositoryTool: hard-code the expected output path instead of
  expect.any(String) to catch arg-swap regressions.
- memoryUtils: tighten getMemoryStats assertions with sanity bounds
  (heapUsed <= heapTotal, rss > 0, heapUsagePercent <= 100) so a
  unit-conversion regression (bytes vs MB) would fail the test.
2026-04-26 22:20:42 +09:00

148 lines
4.8 KiB
TypeScript

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 { runCli } from '../../../src/cli/cliRun.js';
import { createToolWorkspace, formatPackToolResponse } from '../../../src/mcp/tools/mcpToolRuntime.js';
import { registerPackRemoteRepositoryTool } from '../../../src/mcp/tools/packRemoteRepositoryTool.js';
import { createMockConfig } from '../../testing/testUtils.js';
vi.mock('node:path');
vi.mock('../../../src/cli/cliRun.js');
vi.mock('../../../src/mcp/tools/mcpToolRuntime.js', async () => {
const actual = await vi.importActual('../../../src/mcp/tools/mcpToolRuntime.js');
return {
...actual,
createToolWorkspace: vi.fn(),
formatPackToolResponse: vi.fn(),
};
});
describe('PackRemoteRepositoryTool', () => {
const mockServer = {
registerTool: vi.fn().mockReturnThis(),
} as unknown as McpServer;
let toolHandler: (args: {
remote: string;
compress?: boolean;
includePatterns?: string;
ignorePatterns?: string;
topFilesLength?: number;
style?: 'xml' | 'markdown' | 'json' | 'plain';
}) => Promise<CallToolResult>;
const defaultPackResult = {
totalFiles: 10,
totalCharacters: 1000,
totalTokens: 500,
fileCharCounts: { 'test.js': 100 },
fileTokenCounts: { 'test.js': 50 },
suspiciousFilesResults: [],
gitDiffTokenCount: 0,
gitLogTokenCount: 0,
suspiciousGitDiffResults: [],
suspiciousGitLogResults: [],
processedFiles: [],
safeFilePaths: [],
skippedFiles: [],
};
beforeEach(() => {
vi.resetAllMocks();
registerPackRemoteRepositoryTool(mockServer);
toolHandler = (mockServer.registerTool as ReturnType<typeof vi.fn>).mock.calls[0][2];
vi.mocked(path.join).mockImplementation((...args) => args.join('/'));
vi.mocked(createToolWorkspace).mockResolvedValue('/temp/dir');
vi.mocked(formatPackToolResponse).mockResolvedValue({
content: [{ type: 'text', text: 'Success response' }],
});
vi.mocked(runCli).mockImplementation(async (_directories, cwd, opts = {}) => ({
packResult: defaultPackResult,
config: createMockConfig({
output: {
filePath: opts.output ?? '/temp/dir/repomix-output.xml',
style: opts.style ?? 'xml',
},
cwd,
}),
}));
});
test('registers the tool with the expected name', () => {
expect(mockServer.registerTool).toHaveBeenCalledWith(
'pack_remote_repository',
expect.any(Object),
expect.any(Function),
);
});
test('forwards user options to runCli with the correct shape', async () => {
await toolHandler({
remote: 'yamadashy/repomix',
compress: false,
includePatterns: '**/*.ts',
ignorePatterns: 'tests/**',
topFilesLength: 7,
style: 'markdown',
});
expect(runCli).toHaveBeenCalledWith(
['.'],
process.cwd(),
expect.objectContaining({
remote: 'yamadashy/repomix',
compress: false,
include: '**/*.ts',
ignore: 'tests/**',
topFilesLen: 7,
style: 'markdown',
securityCheck: true,
quiet: true,
}),
);
// path is fully determined by mocked createToolWorkspace + mocked path.join
// (see beforeEach: '/temp/dir' + '/' + defaultFilePathMap[style]).
// Hard-coding instead of expect.any(String) catches arg-swap regressions.
expect(formatPackToolResponse).toHaveBeenCalledWith(
{ repository: 'yamadashy/repomix' },
defaultPackResult,
'/temp/dir/repomix-output.md',
7,
);
});
test('returns an error response when runCli yields no result', async () => {
vi.mocked(runCli).mockResolvedValue(undefined);
const result = await toolHandler({ remote: 'user/repo' });
expect(result.isError).toBe(true);
const content = result.content[0] as { type: 'text'; text: string };
expect(JSON.parse(content.text).errorMessage).toBe('Failed to return a result');
});
test('returns an error response when runCli throws', async () => {
vi.mocked(runCli).mockRejectedValue(new Error('Clone failed'));
const result = await toolHandler({ remote: 'user/repo' });
expect(result.isError).toBe(true);
const content = result.content[0] as { type: 'text'; text: string };
expect(JSON.parse(content.text).errorMessage).toBe('Clone failed');
});
test('returns an error response when workspace creation fails', async () => {
vi.mocked(createToolWorkspace).mockRejectedValue(new Error('mkdtemp failed'));
const result = await toolHandler({ remote: 'user/repo' });
expect(result.isError).toBe(true);
const content = result.content[0] as { type: 'text'; text: string };
expect(JSON.parse(content.text).errorMessage).toBe('mkdtemp failed');
});
});