summaryrefslogtreecommitdiff
path: root/packages/core/src/tools/tool-registry.test.ts
diff options
context:
space:
mode:
authorjoshualitt <[email protected]>2025-08-21 14:40:18 -0700
committerGitHub <[email protected]>2025-08-21 21:40:18 +0000
commitec41b8db8e714867ea354c29c07f009cd837ac23 (patch)
tree4bb01ac90a25d6d82cfc005d68aae336be192744 /packages/core/src/tools/tool-registry.test.ts
parent299bf58309a0950ac81ae051b02ec64463ebd153 (diff)
feat(core): Annotate remaining error paths in tools with type. (#6699)
Diffstat (limited to 'packages/core/src/tools/tool-registry.test.ts')
-rw-r--r--packages/core/src/tools/tool-registry.test.ts76
1 files changed, 76 insertions, 0 deletions
diff --git a/packages/core/src/tools/tool-registry.test.ts b/packages/core/src/tools/tool-registry.test.ts
index 6db60377..0084ddff 100644
--- a/packages/core/src/tools/tool-registry.test.ts
+++ b/packages/core/src/tools/tool-registry.test.ts
@@ -24,6 +24,7 @@ import fs from 'node:fs';
import { MockTool } from '../test-utils/tools.js';
import { McpClientManager } from './mcp-client-manager.js';
+import { ToolErrorType } from './tool-error.js';
vi.mock('node:fs');
@@ -311,6 +312,81 @@ describe('ToolRegistry', () => {
});
});
+ it('should return a DISCOVERED_TOOL_EXECUTION_ERROR on tool failure', async () => {
+ const discoveryCommand = 'my-discovery-command';
+ mockConfigGetToolDiscoveryCommand.mockReturnValue(discoveryCommand);
+ vi.spyOn(config, 'getToolCallCommand').mockReturnValue('my-call-command');
+
+ const toolDeclaration: FunctionDeclaration = {
+ name: 'failing-tool',
+ description: 'A tool that fails',
+ parametersJsonSchema: {
+ type: 'object',
+ properties: {},
+ },
+ };
+
+ const mockSpawn = vi.mocked(spawn);
+ // --- Discovery Mock ---
+ const discoveryProcess = {
+ stdout: { on: vi.fn(), removeListener: vi.fn() },
+ stderr: { on: vi.fn(), removeListener: vi.fn() },
+ on: vi.fn(),
+ };
+ mockSpawn.mockReturnValueOnce(discoveryProcess as any);
+
+ discoveryProcess.stdout.on.mockImplementation((event, callback) => {
+ if (event === 'data') {
+ callback(
+ Buffer.from(
+ JSON.stringify([{ functionDeclarations: [toolDeclaration] }]),
+ ),
+ );
+ }
+ });
+ discoveryProcess.on.mockImplementation((event, callback) => {
+ if (event === 'close') {
+ callback(0);
+ }
+ });
+
+ await toolRegistry.discoverAllTools();
+ const discoveredTool = toolRegistry.getTool('failing-tool');
+ expect(discoveredTool).toBeDefined();
+
+ // --- Execution Mock ---
+ const executionProcess = {
+ stdout: { on: vi.fn(), removeListener: vi.fn() },
+ stderr: { on: vi.fn(), removeListener: vi.fn() },
+ stdin: { write: vi.fn(), end: vi.fn() },
+ on: vi.fn(),
+ connected: true,
+ disconnect: vi.fn(),
+ removeListener: vi.fn(),
+ };
+ mockSpawn.mockReturnValueOnce(executionProcess as any);
+
+ executionProcess.stderr.on.mockImplementation((event, callback) => {
+ if (event === 'data') {
+ callback(Buffer.from('Something went wrong'));
+ }
+ });
+ executionProcess.on.mockImplementation((event, callback) => {
+ if (event === 'close') {
+ callback(1); // Non-zero exit code
+ }
+ });
+
+ const invocation = (discoveredTool as DiscoveredTool).build({});
+ const result = await invocation.execute(new AbortController().signal);
+
+ expect(result.error?.type).toBe(
+ ToolErrorType.DISCOVERED_TOOL_EXECUTION_ERROR,
+ );
+ expect(result.llmContent).toContain('Stderr: Something went wrong');
+ expect(result.llmContent).toContain('Exit Code: 1');
+ });
+
it('should discover tools using MCP servers defined in getMcpServers', async () => {
const discoverSpy = vi.spyOn(
McpClientManager.prototype,