Skip to content

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Fix toolkit tests

haxziec7ff44c5b0fb
diff --git a/package.json b/package.json
index 6a7f9e8bb..9ce135895 100644
--- a/package.json
+++ b/package.json
@@ -11,6 +11,7 @@
],
"scripts": {
"build": "turbo build",
+ "build:packages": "turbo build --filter=./packages/**",
"test": "turbo test --filter=./packages/**",
"test:ui": "vitest --ui",
"lint": "eslint . --ext .ts,.tsx",
@@ -23,10 +24,10 @@
"create:example": "bash scripts/create-example.sh",
"check:peer-deps": "tsx scripts/check-peer-deps.ts",
"update:peer-deps": "tsx scripts/update-peer-deps.ts",
- "prepublish": "pnpm run build",
+ "prepublish": "pnpm run build:packages",
"changeset": "changeset",
"changeset:version": "changeset version",
- "changeset:release": "pnpm run build && changeset publish",
+ "changeset:release": "pnpm run build:packages && changeset publish",
"changeset:pre-enter": "changeset pre enter alpha",
"changeset:pre-exit": "changeset pre exit alpha"
},
diff --git a/packages/core/src/models/Toolkits.ts b/packages/core/src/models/Toolkits.ts
index bba38d791..fac708d42 100644
--- a/packages/core/src/models/Toolkits.ts
+++ b/packages/core/src/models/Toolkits.ts
@@ -58,22 +58,34 @@ export class Toolkits {
sort_by: parsedQuery.data.sortBy,
});
- const parsedResult = ToolKitListResponseSchema.safeParse({
- items: result.items.map(item => {
- const parsedItem = ToolKitItemSchema.parse({
+ const parsedResult = ToolKitListResponseSchema.safeParse(
+ result.items.map(item => {
+ const parsedItem = ToolKitItemSchema.safeParse({
name: item.name,
slug: item.slug,
- meta: item.meta,
+ meta: {
+ ...item.meta,
+ createdAt: item.meta.created_at,
+ updatedAt: item.meta.updated_at,
+ toolsCount: item.meta.tools_count,
+ triggersCount: item.meta.triggers_count,
+ categories: item.meta.categories,
+ appUrl: item.meta.app_url,
+ logo: item.meta.logo,
+ },
isLocalToolkit: item.is_local_toolkit,
authSchemes: item.auth_schemes,
composioManagedAuthSchemes: item.composio_managed_auth_schemes,
noAuth: item.no_auth,
});
- return parsedItem;
- }),
- nextCursor: result.next_cursor,
- totalPages: result.total_pages,
- });
+ if (!parsedItem.success) {
+ throw new ValidationError('Failed to parse toolkit response', {
+ cause: parsedItem.error,
+ });
+ }
+ return parsedItem.data;
+ })
+ );
if (!parsedResult.success) {
throw new ValidationError('Failed to parse toolkit list response', {
@@ -226,7 +238,7 @@ export class Toolkits {
*/
async listCategories(): Promise<ToolkitRetrieveCategoriesResponse> {
const result = await this.client.toolkits.retrieveCategories();
- const parsedResult = ToolkitRetrieveCategoriesResponseSchema.parse({
+ const parsedResult = ToolkitRetrieveCategoriesResponseSchema.safeParse({
items: result.items.map(item => {
const parsedItem = ToolkitCategorySchema.parse({
id: item.id,
@@ -237,7 +249,12 @@ export class Toolkits {
nextCursor: result.next_cursor,
totalPages: result.total_pages,
});
- return parsedResult;
+ if (!parsedResult.success) {
+ throw new ValidationError('Failed to parse toolkit categories response', {
+ cause: parsedResult.error,
+ });
+ }
+ return parsedResult.data;
}
/**
diff --git a/packages/core/src/types/toolkit.types.ts b/packages/core/src/types/toolkit.types.ts
index b945db970..35ce8aaea 100644
--- a/packages/core/src/types/toolkit.types.ts
+++ b/packages/core/src/types/toolkit.types.ts
@@ -47,11 +47,7 @@ export const ToolKitItemSchema = z.object({
noAuth: z.boolean().optional(),
});
-export const ToolKitListResponseSchema = z.object({
- items: z.array(ToolKitItemSchema),
- nextCursor: z.string().nullable(),
- totalPages: z.number(),
-});
+export const ToolKitListResponseSchema = z.array(ToolKitItemSchema);
export type ToolKitMeta = z.infer<typeof ToolKitMetaSchema>;
export type ToolKitItem = z.infer<typeof ToolKitItemSchema>;
diff --git a/packages/core/test/models/toolkits.test.ts b/packages/core/test/models/toolkits.test.ts
new file mode 100644
index 000000000..fc4febc66
--- /dev/null
+++ b/packages/core/test/models/toolkits.test.ts
@@ -0,0 +1,449 @@
+import { describe, it, expect, vi, beforeEach } from 'vitest';
+import { Toolkits } from '../../src/models/toolkits';
+import ComposioClient from '@composio/client';
+import { ValidationError } from '../../src/errors/ValidationErrors';
+import { ComposioToolkitFetchError, ComposioToolkitNotFoundError } from '../../src/errors';
+import { telemetry } from '../../src/telemetry/Telemetry';
+import { ConnectionRequest } from '../../src/models/ConnectionRequest';
+import { ComposioAuthConfigNotFoundError } from '../../src/errors/AuthConfigErrors';
+
+// Mock dependencies
+vi.mock('../../src/telemetry/Telemetry', () => ({
+ telemetry: {
+ instrument: vi.fn(),
+ },
+}));
+
+// Create mock client with toolkit-related methods
+const createMockClient = () => ({
+ baseURL: 'https://api.composio.dev',
+ apiKey: 'test-api-key',
+ toolkits: {
+ list: vi.fn(),
+ retrieve: vi.fn(),
+ retrieveCategories: vi.fn(),
+ listCategories: vi.fn(),
+ authorize: vi.fn(),
+ },
+ authConfigs: {
+ list: vi.fn(),
+ create: vi.fn(),
+ },
+ connectedAccounts: {
+ initiate: vi.fn(),
+ create: vi.fn(),
+ },
+});
+
+// Mock response data
+const mockToolkitListResponse = {
+ items: [
+ {
+ name: 'GitHub',
+ slug: 'github',
+ meta: {
+ created_at: '2024-01-01',
+ updated_at: '2024-01-02',
+ tools_count: 10,
+ triggers_count: 5,
+ },
+ is_local_toolkit: false,
+ auth_schemes: ['oauth2'],
+ composio_managed_auth_schemes: ['oauth2'],
+ no_auth: false,
+ },
+ ],
+};
+
+const mockToolkitRetrieveResponse = {
+ name: 'GitHub',
+ slug: 'github',
+ meta: {
+ created_at: '2024-01-01',
+ updated_at: '2024-01-02',
+ tools_count: 10,
+ triggers_count: 5,
+ },
+ is_local_toolkit: false,
+ composio_managed_auth_schemes: ['oauth2'],
+ auth_config_details: [
+ {
+ name: 'oauth2',
+ mode: 'oauth2',
+ fields: {
+ auth_config_creation: {
+ optional: [],
+ required: [],
+ },
+ connected_account_initiation: {
+ optional: [],
+ required: [],
+ },
+ },
+ proxy: {
+ base_url: 'https://api.github.com',
+ },
+ },
+ ],
+};
+
+describe('Toolkits', () => {
+ let toolkits: Toolkits;
+ let mockClient: ReturnType<typeof createMockClient>;
+
+ beforeEach(() => {
+ vi.clearAllMocks();
+ mockClient = createMockClient();
+ toolkits = new Toolkits(mockClient as unknown as ComposioClient);
+ });
+
+ describe('constructor', () => {
+ it('should create an instance successfully', () => {
+ expect(toolkits).toBeInstanceOf(Toolkits);
+ expect(telemetry.instrument).toHaveBeenCalledWith(toolkits);
+ });
+
+ it('should store the client reference', () => {
+ expect(toolkits['client']).toBe(mockClient);
+ });
+ });
+
+ describe('get', () => {
+ it('should get a list of toolkits', async () => {
+ mockClient.toolkits.list.mockResolvedValue(mockToolkitListResponse);
+
+ const result = await toolkits.get({});
+
+ expect(result).toEqual([
+ {
+ name: 'GitHub',
+ slug: 'github',
+ meta: {
+ createdAt: '2024-01-01',
+ updatedAt: '2024-01-02',
+ toolsCount: 10,
+ triggersCount: 5,
+ },
+ isLocalToolkit: false,
+ authSchemes: ['oauth2'],
+ composioManagedAuthSchemes: ['oauth2'],
+ noAuth: false,
+ },
+ ]);
+ expect(mockClient.toolkits.list).toHaveBeenCalledWith({});
+ });
+
+ it('should get a single toolkit by slug', async () => {
+ mockClient.toolkits.retrieve.mockResolvedValue(mockToolkitRetrieveResponse);
+
+ const result = await toolkits.get('github');
+
+ expect(result).toEqual({
+ name: 'GitHub',
+ slug: 'github',
+ meta: {
+ createdAt: '2024-01-01',
+ updatedAt: '2024-01-02',
+ toolsCount: 10,
+ triggersCount: 5,
+ },
+ isLocalToolkit: false,
+ composioManagedAuthSchemes: ['oauth2'],
+ authConfigDetails: [
+ {
+ name: 'oauth2',
+ mode: 'oauth2',
+ fields: {
+ authConfigCreation: {
+ optional: [],
+ required: [],
+ },
+ connectedAccountInitiation: {
+ optional: [],
+ required: [],
+ },
+ },
+ proxy: {
+ baseUrl: 'https://api.github.com',
+ },
+ },
+ ],
+ });
+ expect(mockClient.toolkits.retrieve).toHaveBeenCalledWith('github');
+ });
+
+ it('should throw ValidationError for invalid list query', async () => {
+ const promise = toolkits.get({ category: 123 } as any);
+ await expect(promise).rejects.toThrowError('Failed to fetch toolkits');
+ });
+
+ it('should throw ComposioToolkitNotFoundError when toolkit not found', async () => {
+ mockClient.toolkits.retrieve.mockRejectedValue(
+ new Error('Toolkit with slug non-existent not found')
+ );
+
+ const promise = toolkits.get('non-existent');
+ await expect(promise).rejects.toThrowError("Couldn't fetch Toolkit with slug: non-existent");
+ });
+
+ it('should throw ComposioToolkitFetchError for other errors', async () => {
+ mockClient.toolkits.list.mockRejectedValue(new Error('Network error'));
+
+ const promise = toolkits.get({});
+ await expect(promise).rejects.toThrowError('Failed to fetch toolkits');
+ });
+ });
+
+ describe('listCategories', () => {
+ it('should list toolkit categories', async () => {
+ const mockResponse = {
+ items: [
+ {
+ id: 'cat-1',
+ name: 'Category 1',
+ },
+ ],
+ next_cursor: null,
+ total_pages: 1,
+ };
+
+ mockClient.toolkits.retrieveCategories.mockResolvedValue(mockResponse);
+
+ const result = await toolkits.listCategories();
+
+ expect(result).toEqual({
+ items: mockResponse.items,
+ nextCursor: mockResponse.next_cursor,
+ totalPages: mockResponse.total_pages,
+ });
+ });
+ });
+
+ describe('authorize', () => {
+ it('should create auth config and initiate connection request when no auth config exists', async () => {
+ // Mock toolkit retrieval
+ mockClient.toolkits.retrieve.mockResolvedValue({
+ name: 'Test Toolkit',
+ slug: 'test-toolkit',
+ meta: {
+ created_at: new Date().toISOString(),
+ updated_at: new Date().toISOString(),
+ tools_count: 1,
+ triggers_count: 0,
+ },
+ is_local_toolkit: false,
+ auth_config_details: [
+ {
+ name: 'OAuth2',
+ mode: 'OAUTH2',
+ fields: {
+ auth_config_creation: {
+ optional: [],
+ required: [],
+ },
+ connected_account_initiation: {
+ optional: [],
+ required: [],
+ },
+ },
+ },
+ ],
+ });
+
+ // Mock empty auth config list
+ mockClient.authConfigs.list.mockResolvedValue({
+ items: [],
+ next_cursor: null,
+ total_pages: 1,
+ });
+
+ // Mock auth config creation
+ mockClient.authConfigs.create.mockResolvedValue({
+ auth_config: {
+ id: 'new-auth-config-id',
+ auth_scheme: 'OAUTH2',
+ is_composio_managed: true,
+ toolkit: {
+ slug: 'test-toolkit',
+ logo: 'https://example.com/logo.png',
+ },
+ },
+ toolkit: {
+ slug: 'test-toolkit',
+ },
+ });
+
+ // Mock connection request initiation
+ mockClient.connectedAccounts.create.mockResolvedValue({
+ id: 'connection-request-id',
+ connectionData: {
+ val: {
+ status: 'INITIATED',
+ redirectUrl: 'https://auth.url',
+ },
+ },
+ });
+
+ const result = await toolkits.authorize('user-123', 'test-toolkit');
+
+ expect(mockClient.authConfigs.create).toHaveBeenCalledWith({
+ toolkit: {
+ slug: 'test-toolkit',
+ },
+ auth_config: {
+ type: 'use_composio_managed_auth',
+ name: 'Test Toolkit Auth Config',
+ },
+ });
+
+ expect(mockClient.connectedAccounts.create).toHaveBeenCalledWith({
+ auth_config: {
+ id: 'new-auth-config-id',
+ },
+ connection: {
+ user_id: 'user-123',
+ },
+ });
+
+ expect(result.toJSON()).toEqual({
+ id: 'connection-request-id',
+ status: 'INITIATED',
+ redirectUrl: 'https://auth.url',
+ });
+ });
+
+ it('should use existing auth config to initiate connection request', async () => {
+ // Mock toolkit retrieval
+ mockClient.toolkits.retrieve.mockResolvedValue({
+ name: 'Test Toolkit',
+ slug: 'test-toolkit',
+ meta: {
+ created_at: new Date().toISOString(),
+ updated_at: new Date().toISOString(),
+ tools_count: 1,
+ triggers_count: 0,
+ },
+ is_local_toolkit: false,
+ auth_config_details: [
+ {
+ name: 'OAuth2',
+ mode: 'OAUTH2',
+ fields: {
+ auth_config_creation: {
+ optional: [],
+ required: [],
+ },
+ connected_account_initiation: {
+ optional: [],
+ required: [],
+ },
+ },
+ },
+ ],
+ });
+
+ // Mock existing auth config
+ mockClient.authConfigs.list.mockResolvedValue({
+ items: [
+ {
+ id: 'existing-auth-config-id',
+ auth_scheme: 'OAUTH2',
+ is_composio_managed: true,
+ name: 'Existing Auth Config',
+ no_of_connections: 0,
+ status: 'ENABLED',
+ toolkit: {
+ logo: 'https://example.com/logo.png',
+ slug: 'test-toolkit',
+ },
+ uuid: 'uuid-1',
+ credentials: {},
+ expected_input_fields: [],
+ created_by: 'user-1',
+ created_at: new Date().toISOString(),
+ last_updated_at: new Date().toISOString(),
+ },
+ ],
+ next_cursor: null,
+ total_pages: 1,
+ });
+
+ // Mock connection request initiation
+ mockClient.connectedAccounts.create.mockResolvedValue({
+ id: 'connection-request-id',
+ connectionData: {
+ val: {
+ status: 'INITIATED',
+ redirectUrl: 'https://auth.url',
+ },
+ },
+ });
+
+ const result = await toolkits.authorize('user-123', 'test-toolkit');
+
+ expect(mockClient.authConfigs.create).not.toHaveBeenCalled();
+ expect(mockClient.connectedAccounts.create).toHaveBeenCalledWith({
+ auth_config: {
+ id: 'existing-auth-config-id',
+ },
+ connection: {
+ user_id: 'user-123',
+ },
+ });
+
+ expect(result.toJSON()).toEqual({
+ id: 'connection-request-id',
+ status: 'INITIATED',
+ redirectUrl: 'https://auth.url',
+ });
+ });
+
+ it('should throw ComposioAuthConfigNotFoundError when toolkit has no auth config details', async () => {
+ // Mock toolkit retrieval with no auth config details
+ mockClient.toolkits.retrieve.mockResolvedValue({
+ name: 'Test Toolkit',
+ slug: 'test-toolkit',
+ meta: {
+ created_at: new Date().toISOString(),
+ updated_at: new Date().toISOString(),
+ tools_count: 1,
+ triggers_count: 0,
+ },
+ is_local_toolkit: false,
+ auth_config_details: [],
+ });
+
+ // Mock empty auth config list
+ mockClient.authConfigs.list.mockResolvedValue({
+ items: [],
+ next_cursor: null,
+ total_pages: 1,
+ });
+
+ // Mock the error to be thrown
+ mockClient.authConfigs.create.mockRejectedValue(
+ new ComposioAuthConfigNotFoundError('No auth config found for toolkit')
+ );
+
+ const promise = toolkits.authorize('user-123', 'test-toolkit');
+
+ await expect(promise).rejects.toThrow(ComposioAuthConfigNotFoundError);
+ await expect(promise).rejects.toThrow('No auth config found for toolkit');
+ expect(mockClient.authConfigs.create).not.toHaveBeenCalled();
+ expect(mockClient.connectedAccounts.initiate).not.toHaveBeenCalled();
+ });
+
+ it('should throw ComposioToolkitNotFoundError when toolkit does not exist', async () => {
+ mockClient.toolkits.retrieve.mockRejectedValue(
+ new Error('Toolkit with slug non-existent not found')
+ );
+
+ const promise = toolkits.authorize('user-123', 'non-existent');
+
+ await expect(promise).rejects.toThrow("Couldn't fetch Toolkit with slug: non-existent");
+ expect(mockClient.authConfigs.list).not.toHaveBeenCalled();
+ expect(mockClient.authConfigs.create).not.toHaveBeenCalled();
+ expect(mockClient.connectedAccounts.initiate).not.toHaveBeenCalled();
+ });
+ });
+});