1
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
2
+import ComposioClient from '@composio/client';
3
import { mockClient } from '../utils/mocks/client.mock';
4
import { toolMocks } from '../utils/mocks/data.mock';
5
import { Tool, ToolListParams, ToolExecuteParams } from '../../src/types/tool.types';
13
} from '../utils/toolExecuteUtils';
14
import { MockProvider } from '../utils/mocks/provider.mock';
15
import { ValidationError } from '../../src/errors/ValidationErrors';
16
+import { ComposioToolFetchError, ComposioToolNotFoundError } from '../../src/errors/ToolErrors';
17
18
// Minimal structural shape for a ComposioError-like value (possibly wrapping
19
// another error as its `cause`), used to narrow `catch (error: unknown)`
457
expect(result.slug).toEqual(toolMocks.transformedTool.slug);
458
});
459
458
- it('should throw an error if tool is not found', async () => {
460
+ it('should throw ComposioToolNotFoundError when the API returns 404', async () => {
461
const slug = 'NONEXISTENT_TOOL';
462
+ const notFound = new ComposioClient.NotFoundError(404, undefined, undefined, new Headers());
463
461
- mockClient.tools.retrieve.mockRejectedValue(null);
464
+ mockClient.tools.retrieve.mockRejectedValueOnce(notFound);
465
463
- await expect(context.tools.getRawComposioToolBySlug(slug)).rejects.toThrow(
464
- `Unable to retrieve tool with slug ${slug}`
466
+ const error = await context.tools.getRawComposioToolBySlug(slug).catch(e => e);
467
+
468
+ expect(error).toBeInstanceOf(ComposioToolNotFoundError);
469
+ expect(error.message).toBe(`Tool with slug ${slug} not found`);
470
+ expect(error.cause).toBe(notFound);
471
+ });
472
+
473
+ it('should throw ComposioToolNotFoundError when the API returns 400', async () => {
474
+ const slug = 'malformed slug';
475
+ const badRequest = new ComposioClient.BadRequestError(
476
+ 400,
477
+ undefined,
478
+ undefined,
479
+ new Headers()
480
+ );
481
+
482
+ mockClient.tools.retrieve.mockRejectedValueOnce(badRequest);
483
+
484
+ const error = await context.tools.getRawComposioToolBySlug(slug).catch(e => e);
485
+
486
+ expect(error).toBeInstanceOf(ComposioToolNotFoundError);
487
+ expect(error.cause).toBe(badRequest);
488
+ });
489
+
490
+ it('should not report an invalid API key (401) as tool not found', async () => {
491
+ const slug = 'SLACK_FETCH_CONVERSATION_HISTORY';
492
+ const unauthorized = new ComposioClient.AuthenticationError(
493
+ 401,
494
+ { error: { message: 'Invalid API key', code: 801, status: 401 } },
495
+ undefined,
496
+ new Headers()
497
);
498
+
499
+ mockClient.tools.retrieve.mockRejectedValueOnce(unauthorized);
500
+
501
+ const error = await context.tools.getRawComposioToolBySlug(slug).catch(e => e);
502
+
503
+ expect(error).toBeInstanceOf(ComposioToolFetchError);
504
+ expect(error).not.toBeInstanceOf(ComposioToolNotFoundError);
505
+ expect(error.name).toBe('ComposioToolFetchError');
506
+ expect(error.message).toBe(`Unable to retrieve tool with slug ${slug}`);
507
+ expect(error.cause).toBe(unauthorized);
508
+ expect(error.cause.status).toBe(401);
509
+ });
510
+
511
+ it('should wrap non-API failures in ComposioToolFetchError', async () => {
512
+ const slug = 'TOOL_SLUG';
513
+ const networkError = new Error('socket hang up');
514
+
515
+ mockClient.tools.retrieve.mockRejectedValueOnce(networkError);
516
+
517
+ const error = await context.tools.getRawComposioToolBySlug(slug).catch(e => e);
518
+
519
+ expect(error).toBeInstanceOf(ComposioToolFetchError);
520
+ expect(error.cause).toBe(networkError);
521
});
522
523
it('should apply schema modifiers when provided', async () => {