Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
TypeScript agentic-aiagentsaiai-agentsaiagentsdeveloper-toolsfunction-callinggpt-4javascriptjsllmllmopsmcppythonremote-mcp-serverssetypescript
Update tests for mastra:
haxzie1be8e3778af1
diff --git a/packages/core/src/index.ts b/packages/core/src/index.tsindex e42bd2bd3..a920ab95c 100644--- a/packages/core/src/index.ts+++ b/packages/core/src/index.ts@@ -5,7 +5,7 @@ export { BaseAgenticProvider, BaseNonAgenticProvider } from './provider/BaseProvexport type { BaseComposioProvider } from './provider/BaseProvider';export { OpenAIProvider } from './provider/OpenAIProvider';export { BaseTelemetryTransport, ConsoleTelemetryTransport } from './telemetry/TelemetryTransport';-export { jsonSchemaToModel } from './utils/jsonSchema';+export { jsonSchemaToZodSchema } from './utils/jsonSchema';export { ToolListParamsSchema } from './types/tool.types';export type { Tool, ToolListParams, ToolExecuteParams } from './types/tool.types';export type { Provider, ExecuteToolFn, ExecuteToolFnOptions } from './types/provider.types';diff --git a/packages/core/src/utils/jsonSchema.ts b/packages/core/src/utils/jsonSchema.tsindex bb92bbb6c..91ec300ce 100644--- a/packages/core/src/utils/jsonSchema.ts+++ b/packages/core/src/utils/jsonSchema.ts@@ -30,7 +30,7 @@ export function jsonSchemaToTsType(jsonSchema: Record<string, unknown>): unknownif (type === 'object') {const properties = jsonSchema.properties;if (properties) {- const nestedModel = jsonSchemaToModel(jsonSchema);+ const nestedModel = jsonSchemaToZodSchema(jsonSchema);return nestedModel;}return Object;@@ -125,7 +125,7 @@ function jsonSchemaPropertiesToTSTypes(value: {);break;case 'object':- zodType = jsonSchemaToModel(value).describe(+ zodType = jsonSchemaToZodSchema(value).describe((value.description || '') +(value.examples ? `\nExamples: ${value.examples.join(', ')}` : ''));@@ -147,7 +147,7 @@ function jsonSchemaPropertiesToTSTypes(value: {** @example* ```ts- * const zodSchema = jsonSchemaToModel({+ * const zodSchema = jsonSchemaToZodSchema({* type: 'object',* properties: {* name: { type: 'string' },@@ -157,7 +157,7 @@ function jsonSchemaPropertiesToTSTypes(value: {* console.log(zodSchema);* ```*/-export function jsonSchemaToModel(+export function jsonSchemaToZodSchema(jsonSchema: Record<string, unknown>): z.ZodObject<Record<string, z.ZodTypeAny>> {const properties = jsonSchema.properties as Record<string, unknown>;diff --git a/packages/providers/langchain/src/index.ts b/packages/providers/langchain/src/index.tsindex 461800437..3eb2131bf 100644--- a/packages/providers/langchain/src/index.ts+++ b/packages/providers/langchain/src/index.ts@@ -9,7 +9,7 @@* @packageDocumentation* @module providers/langchain*/-import { BaseAgenticProvider, jsonSchemaToModel, Tool, ExecuteToolFn } from '@composio/core';+import { BaseAgenticProvider, jsonSchemaToZodSchema, Tool, ExecuteToolFn } from '@composio/core';import { DynamicStructuredTool } from '@langchain/core/tools';export type LangChainToolCollection = Array<DynamicStructuredTool>;@@ -38,7 +38,7 @@ export class LangchainProvider extends BaseAgenticProvider<if (!tool.inputParameters) {throw new Error('Tool input parameters are not defined');}- const parameters = jsonSchemaToModel(tool.inputParameters);+ const parameters = jsonSchemaToZodSchema(tool.inputParameters);return new DynamicStructuredTool({name: toolName,description: description || '',diff --git a/packages/providers/langchain/test/langchain.test.ts b/packages/providers/langchain/test/langchain.test.tsindex 30625367f..670e81250 100644--- a/packages/providers/langchain/test/langchain.test.ts+++ b/packages/providers/langchain/test/langchain.test.ts@@ -23,12 +23,12 @@ vi.mock('@langchain/core/tools', () => {};});-// Mock the jsonSchemaToModel function from @composio/core+// Mock the jsonSchemaToZodSchema function from @composio/corevi.mock('@composio/core', async () => {const actual = await vi.importActual('@composio/core');return {...(actual as object),- jsonSchemaToModel: vi.fn().mockImplementation(schema => {+ jsonSchemaToZodSchema: vi.fn().mockImplementation(schema => {return { type: 'mock-schema', originalSchema: schema };}),};diff --git a/packages/providers/mastra/package.json b/packages/providers/mastra/package.jsonindex ee547a8fa..8da06b69a 100644--- a/packages/providers/mastra/package.json+++ b/packages/providers/mastra/package.json@@ -39,6 +39,7 @@"devDependencies": {"@composio/core": "workspace:*","tsup": "^8.4.0",- "typescript": "^5.8.3"+ "typescript": "^5.8.3",+ "vitest": "^3.1.3"}}diff --git a/packages/providers/mastra/src/index.ts b/packages/providers/mastra/src/index.tsindex e75fd2ee0..162a46821 100644--- a/packages/providers/mastra/src/index.ts+++ b/packages/providers/mastra/src/index.ts@@ -6,7 +6,7 @@* @packageDocumentation* @module providers/mastra*/-import { BaseAgenticProvider, Tool, ExecuteToolFn, jsonSchemaToModel } from '@composio/core';+import { BaseAgenticProvider, Tool, ExecuteToolFn, jsonSchemaToZodSchema } from '@composio/core';import { createTool } from '@mastra/core';export type MastraTool = ReturnType<typeof createTool>;@@ -22,8 +22,10 @@ export class MastraProvider extends BaseAgenticProvider<MastraToolCollection, Maconst mastraTool = createTool({id: tool.slug,description: tool.description ?? '',- inputSchema: tool.inputParameters ? jsonSchemaToModel(tool.inputParameters) : undefined,- outputSchema: tool.outputParameters ? jsonSchemaToModel(tool.outputParameters) : undefined,+ inputSchema: tool.inputParameters ? jsonSchemaToZodSchema(tool.inputParameters) : undefined,+ outputSchema: tool.outputParameters+ ? jsonSchemaToZodSchema(tool.outputParameters)+ : undefined,execute: async ({ context }) => {const result = await executeTool(tool.slug, context);return result;diff --git a/packages/providers/mastra/test/README.md b/packages/providers/mastra/test/README.mdnew file mode 100644index 000000000..42de8b204--- /dev/null+++ b/packages/providers/mastra/test/README.md@@ -0,0 +1,72 @@+# Mastra Provider Tests++This directory contains comprehensive unit tests for the Mastra provider implementation.++## Test Coverage++The test suite covers the following areas:++### 1. Provider Properties++- **Name verification**: Ensures the provider has the correct name "mastra"+- **Agentic nature**: Confirms the provider is marked as agentic (supports tool execution)++### 2. Tool Wrapping (`wrapTool`)++- **Basic wrapping**: Tests conversion of Composio tools to Mastra createTool format+- **Schema handling**: Tests proper conversion of input/output parameters using jsonSchemaToModel+- **Edge cases**: Handles tools without descriptions, input parameters, or output parameters+- **Execution context**: Tests different context parameter scenarios (empty, missing, populated)++### 3. Tools Collection (`wrapTools`)++- **Multiple tools**: Tests wrapping arrays of tools into key-value collections+- **Empty arrays**: Handles empty tool arrays gracefully+- **Key mapping**: Uses tool slugs as collection keys+- **Duplicate handling**: Manages duplicate tool slugs by overwriting++### 4. Tool Execution (`executeTool`)++- **Global execution**: Tests integration with the global tool execution function+- **Modifiers support**: Tests passing of beforeExecute/afterExecute modifiers+- **Error handling**: Tests graceful handling of execution errors++### 5. Mastra Integration++- **Compatibility**: Ensures generated tools are compatible with Mastra's createTool function+- **Type safety**: Validates correct typing for MastraTool and MastraToolCollection+- **Schema mapping**: Tests proper mapping from Composio tool schemas to Mastra schemas++### 6. Error Handling++- **Execution failures**: Tests behavior when tool execution fails+- **Malformed schemas**: Handles tools with null or malformed schema definitions+- **Type safety**: Maintains type safety even with edge cases++## Test Structure++The tests follow vitest conventions and include:++- Comprehensive mocking of `@mastra/core` and `@composio/core`+- Setup and teardown with `beforeEach` for clean test isolation+- Detailed assertions for both successful and error scenarios+- Type-safe test implementations++## Running Tests++```bash+# From the mastra provider directory+npm test++# From the workspace root+pnpm test --filter=@composio/mastra+```++## Mock Strategy++The tests use vi.mock() to mock:++- `@mastra/core` - Specifically the `createTool` function+- `@composio/core` - The `jsonSchemaToModel` function++This ensures tests are isolated and don't depend on external dependencies while validating the integration contracts.diff --git a/packages/providers/mastra/test/mastra.test.ts b/packages/providers/mastra/test/mastra.test.tsnew file mode 100644index 000000000..4f0727dfa--- /dev/null+++ b/packages/providers/mastra/test/mastra.test.ts@@ -0,0 +1,461 @@+import { describe, it, expect, vi, beforeEach } from 'vitest';+import { MastraProvider } from '../src';+import { Tool } from '@composio/core';+import { createTool } from '@mastra/core';++// Define an interface for our mocked Mastra tool+interface MockedMastraTool {+ id: string;+ description: string;+ inputSchema?: any;+ outputSchema?: any;+ execute: Function;+ _isMockedMastraTool: boolean;+}++// Mock the @mastra/core module+vi.mock('@mastra/core', () => {+ return {+ createTool: vi.fn().mockImplementation(toolConfig => {+ return {+ id: toolConfig.id,+ description: toolConfig.description,+ inputSchema: toolConfig.inputSchema,+ outputSchema: toolConfig.outputSchema,+ execute: toolConfig.execute,+ _isMockedMastraTool: true,+ } as MockedMastraTool;+ }),+ };+});++// Mock the jsonSchemaToZodSchema function from @composio/core+vi.mock('@composio/core', async () => {+ const actual = await vi.importActual('@composio/core');+ return {+ ...(actual as object),+ jsonSchemaToZodSchema: vi.fn().mockImplementation(schema => {+ return { type: 'mock-zod-schema', originalSchema: schema };+ }),+ };+});++describe('MastraProvider', () => {+ let provider: MastraProvider;+ let mockTool: Tool;+ let mockExecuteToolFn: any;++ beforeEach(() => {+ provider = new MastraProvider();++ // Mock the global execute tool function+ mockExecuteToolFn = vi.fn().mockResolvedValue({+ data: { result: 'success' },+ error: null,+ successful: true,+ });+ provider._setExecuteToolFn(mockExecuteToolFn);++ // Create a mock Composio tool+ mockTool = {+ slug: 'test-tool',+ name: 'Test Tool',+ description: 'A tool for testing',+ inputParameters: {+ type: 'object',+ properties: {+ input: {+ type: 'string',+ description: 'Test input',+ },+ },+ required: ['input'],+ },+ outputParameters: {+ type: 'object',+ properties: {+ result: {+ type: 'string',+ description: 'Test result',+ },+ },+ },+ toolkit: {+ slug: 'test-toolkit',+ name: 'Test Toolkit',+ },+ tags: [],+ };++ // Reset mocks before each test+ vi.clearAllMocks();+ });++ describe('name property', () => {+ it('should have the correct name', () => {+ expect(provider.name).toBe('mastra');+ });+ });++ describe('_isAgentic property', () => {+ it('should be agentic', () => {+ expect(provider._isAgentic).toBe(true);+ });+ });++ describe('wrapTool', () => {+ it('should wrap a tool in Mastra createTool format', () => {+ const wrapped = provider.wrapTool(mockTool, mockExecuteToolFn) as unknown as MockedMastraTool;++ expect(createTool).toHaveBeenCalledWith({+ id: mockTool.slug,+ description: mockTool.description,+ inputSchema: { type: 'mock-zod-schema', originalSchema: mockTool.inputParameters },+ outputSchema: { type: 'mock-zod-schema', originalSchema: mockTool.outputParameters },+ execute: expect.any(Function),+ });++ expect(wrapped._isMockedMastraTool).toBe(true);+ });++ it('should handle tools without input parameters', () => {+ const toolWithoutInputParams: Tool = {+ ...mockTool,+ inputParameters: undefined,+ };++ const wrapped = provider.wrapTool(+ toolWithoutInputParams,+ mockExecuteToolFn+ ) as unknown as MockedMastraTool;++ expect(createTool).toHaveBeenCalledWith({+ id: toolWithoutInputParams.slug,+ description: toolWithoutInputParams.description,+ inputSchema: undefined,+ outputSchema: { type: 'mock-zod-schema', originalSchema: mockTool.outputParameters },+ execute: expect.any(Function),+ });++ expect(wrapped._isMockedMastraTool).toBe(true);+ });++ it('should handle tools without output parameters', () => {+ const toolWithoutOutputParams: Tool = {+ ...mockTool,+ outputParameters: undefined,+ };++ const wrapped = provider.wrapTool(+ toolWithoutOutputParams,+ mockExecuteToolFn+ ) as unknown as MockedMastraTool;++ expect(createTool).toHaveBeenCalledWith({+ id: toolWithoutOutputParams.slug,+ description: toolWithoutOutputParams.description,+ inputSchema: { type: 'mock-zod-schema', originalSchema: mockTool.inputParameters },+ outputSchema: undefined,+ execute: expect.any(Function),+ });++ expect(wrapped._isMockedMastraTool).toBe(true);+ });++ it('should handle tools without description', () => {+ const toolWithoutDescription: Tool = {+ ...mockTool,+ description: undefined,+ };++ const wrapped = provider.wrapTool(+ toolWithoutDescription,+ mockExecuteToolFn+ ) as unknown as MockedMastraTool;++ expect(createTool).toHaveBeenCalledWith({+ id: toolWithoutDescription.slug,+ description: '',+ inputSchema: { type: 'mock-zod-schema', originalSchema: mockTool.inputParameters },+ outputSchema: { type: 'mock-zod-schema', originalSchema: mockTool.outputParameters },+ execute: expect.any(Function),+ });++ expect(wrapped._isMockedMastraTool).toBe(true);+ });++ it('should create a function that executes the tool with the right parameters', async () => {+ provider.wrapTool(mockTool, mockExecuteToolFn) as unknown as MockedMastraTool;++ // Extract the execute function from the call to createTool()+ const executeFunction = (createTool as any).mock.calls[0][0].execute;++ // Test the execute function+ const context = { input: 'test-value' };+ const result = await executeFunction({ context });++ expect(mockExecuteToolFn).toHaveBeenCalledWith(mockTool.slug, context);+ expect(result).toEqual({+ data: { result: 'success' },+ error: null,+ successful: true,+ });+ });++ it('should handle empty context parameter', async () => {+ provider.wrapTool(mockTool, mockExecuteToolFn) as unknown as MockedMastraTool;++ // Extract the execute function from the call to createTool()+ const executeFunction = (createTool as any).mock.calls[0][0].execute;++ // Test the execute function with empty context+ const result = await executeFunction({ context: {} });++ expect(mockExecuteToolFn).toHaveBeenCalledWith(mockTool.slug, {});+ expect(result).toEqual({+ data: { result: 'success' },+ error: null,+ successful: true,+ });+ });++ it('should handle missing context parameter', async () => {+ provider.wrapTool(mockTool, mockExecuteToolFn) as unknown as MockedMastraTool;++ // Extract the execute function from the call to createTool()+ const executeFunction = (createTool as any).mock.calls[0][0].execute;++ // Test the execute function without context+ const result = await executeFunction({});++ expect(mockExecuteToolFn).toHaveBeenCalledWith(mockTool.slug, undefined);+ expect(result).toEqual({+ data: { result: 'success' },+ error: null,+ successful: true,+ });+ });+ });++ describe('wrapTools', () => {+ it('should wrap multiple tools in a collection', () => {+ const anotherTool: Tool = {+ ...mockTool,+ slug: 'another-tool',+ name: 'Another Tool',+ };+ const tools = [mockTool, anotherTool];++ const wrapped = provider.wrapTools(tools, mockExecuteToolFn);++ // Verify the result has the expected properties+ expect(Object.keys(wrapped)).toHaveLength(2);+ expect(wrapped['test-tool']).toBeDefined();+ expect(wrapped['another-tool']).toBeDefined();++ // Verify createTool was called with the right parameters for each tool+ expect(createTool).toHaveBeenCalledTimes(2);+ expect(createTool).toHaveBeenCalledWith({+ id: mockTool.slug,+ description: mockTool.description,+ inputSchema: { type: 'mock-zod-schema', originalSchema: mockTool.inputParameters },+ outputSchema: { type: 'mock-zod-schema', originalSchema: mockTool.outputParameters },+ execute: expect.any(Function),+ });+ expect(createTool).toHaveBeenCalledWith({+ id: anotherTool.slug,+ description: anotherTool.description,+ inputSchema: { type: 'mock-zod-schema', originalSchema: anotherTool.inputParameters },+ outputSchema: { type: 'mock-zod-schema', originalSchema: anotherTool.outputParameters },+ execute: expect.any(Function),+ });+ });++ it('should return an empty object for empty tools array', () => {+ const wrapped = provider.wrapTools([], mockExecuteToolFn);+ expect(wrapped).toEqual({});+ expect(createTool).not.toHaveBeenCalled();+ });++ it('should use tool slugs as keys in the collection', () => {+ const tools = [+ { ...mockTool, slug: 'first-tool' },+ { ...mockTool, slug: 'second-tool' },+ { ...mockTool, slug: 'third-tool' },+ ];++ const wrapped = provider.wrapTools(tools, mockExecuteToolFn);++ expect(Object.keys(wrapped)).toEqual(['first-tool', 'second-tool', 'third-tool']);+ expect(wrapped['first-tool']).toBeDefined();+ expect(wrapped['second-tool']).toBeDefined();+ expect(wrapped['third-tool']).toBeDefined();+ });++ it('should handle duplicate tool slugs by overwriting', () => {+ const tools = [+ { ...mockTool, slug: 'duplicate-tool', name: 'First Tool' },+ { ...mockTool, slug: 'duplicate-tool', name: 'Second Tool' },+ ];++ const wrapped = provider.wrapTools(tools, mockExecuteToolFn);++ expect(Object.keys(wrapped)).toHaveLength(1);+ expect(wrapped['duplicate-tool']).toBeDefined();+ expect(createTool).toHaveBeenCalledTimes(2);+ });+ });++ describe('executeTool', () => {+ it('should execute a tool using the global execute function', async () => {+ const toolSlug = 'test-tool';+ const toolParams = {+ userId: 'test-user',+ arguments: { input: 'test-value' },+ };++ const result = await provider.executeTool(toolSlug, toolParams);++ expect(mockExecuteToolFn).toHaveBeenCalledWith(toolSlug, toolParams, undefined);+ expect(result).toEqual({+ data: { result: 'success' },+ error: null,+ successful: true,+ });+ });++ it('should pass modifiers to the global execute function', async () => {+ const toolSlug = 'test-tool';+ const toolParams = {+ userId: 'test-user',+ arguments: { input: 'test-value' },+ };++ const modifiers = {+ beforeExecute: vi.fn(params => params),+ afterExecute: vi.fn(response => response),+ };++ await provider.executeTool(toolSlug, toolParams, modifiers);++ expect(mockExecuteToolFn).toHaveBeenCalledWith(toolSlug, toolParams, modifiers);+ });++ it('should handle execution errors gracefully', async () => {+ const toolSlug = 'test-tool';+ const toolParams = {+ userId: 'test-user',+ arguments: { input: 'test-value' },+ };++ const errorResponse = {+ data: null,+ error: { message: 'Tool execution failed' },+ successful: false,+ };++ mockExecuteToolFn.mockResolvedValueOnce(errorResponse);++ const result = await provider.executeTool(toolSlug, toolParams);++ expect(result).toEqual(errorResponse);+ });+ });++ describe('integration with Mastra', () => {+ it('should produce tools compatible with Mastra createTool', () => {+ const wrapped = provider.wrapTool(mockTool, mockExecuteToolFn) as unknown as MockedMastraTool;++ // Verify the wrapped tool has the expected structure+ expect(wrapped).toHaveProperty('id');+ expect(wrapped).toHaveProperty('description');+ expect(wrapped).toHaveProperty('inputSchema');+ expect(wrapped).toHaveProperty('outputSchema');+ expect(wrapped).toHaveProperty('execute');++ // The execute property should be a function+ expect(typeof wrapped.execute).toBe('function');+ });++ it('should create tools with correct id mapping from slug', () => {+ const wrapped = provider.wrapTool(mockTool, mockExecuteToolFn) as unknown as MockedMastraTool;++ expect(createTool).toHaveBeenCalledWith(+ expect.objectContaining({+ id: mockTool.slug,+ })+ );+ });++ it('should handle tools without schemas gracefully', () => {+ const minimalTool: Tool = {+ slug: 'minimal-tool',+ name: 'Minimal Tool',+ description: 'A minimal tool',+ tags: [],+ };++ const wrapped = provider.wrapTool(+ minimalTool,+ mockExecuteToolFn+ ) as unknown as MockedMastraTool;++ expect(createTool).toHaveBeenCalledWith({+ id: 'minimal-tool',+ description: 'A minimal tool',+ inputSchema: undefined,+ outputSchema: undefined,+ execute: expect.any(Function),+ });++ expect(wrapped._isMockedMastraTool).toBe(true);+ });+ });++ describe('error handling', () => {+ it('should handle execution failures in wrapped tools', async () => {+ const errorExecuteToolFn = vi.fn().mockRejectedValue(new Error('Execution failed'));++ const wrapped = provider.wrapTool(+ mockTool,+ errorExecuteToolFn+ ) as unknown as MockedMastraTool;+ const executeFunction = (createTool as any).mock.calls[0][0].execute;++ await expect(executeFunction({ context: { input: 'test' } })).rejects.toThrow(+ 'Execution failed'+ );+ });++ it('should handle tools with malformed schemas', () => {+ const toolWithMalformedSchema: Tool = {+ ...mockTool,+ inputParameters: null as any,+ outputParameters: undefined,+ };++ expect(() => {+ provider.wrapTool(toolWithMalformedSchema, mockExecuteToolFn);+ }).not.toThrow();+ });+ });++ describe('type safety', () => {+ it('should maintain correct typing for MastraTool', () => {+ const wrapped = provider.wrapTool(mockTool, mockExecuteToolFn);++ // Type assertion should work+ expect(wrapped).toBeDefined();+ expect(typeof wrapped).toBe('object');+ });++ it('should maintain correct typing for MastraToolCollection', () => {+ const tools = [mockTool];+ const wrapped = provider.wrapTools(tools, mockExecuteToolFn);++ // Should be an object with string keys+ expect(typeof wrapped).toBe('object');+ expect(Array.isArray(wrapped)).toBe(false);+ });+ });+});