Skip to main content
Glama
by sinch
list-email-templates.test.ts2.99 kB
import * as mailgunHelper from '../../../src/tools/email/utils/mailgun-service-helper'; import { PromptResponse } from '../../../src/types'; import { listEmailTemplatesHandler } from '../../../src/tools/email/list-email-templates'; // Mock fetch global.fetch = jest.fn(); describe('listEmailTemplatesHandler', () => { const mockCredentials = { domain: 'example.com', apiKey: 'test-api-key' }; beforeEach(() => { jest.resetAllMocks(); jest.spyOn(mailgunHelper, 'getMailgunCredentials').mockReturnValue(mockCredentials); }); it('returns formatted prompt response with templates', async () => { // Given const mockResponse = { items: [ { id: 'template1', name: 'my Template 1', description: 'The template #1', createdAt: 'Fri, 28 Mar 2025 17:42:30 UTC' }, { id: 'template2', name: 'my Template 2', description: 'The template #2', createdAt: 'Wed, 26 Mar 2025 11:11:48 UTC' } ] }; (fetch as jest.Mock).mockResolvedValue({ ok: true, json: async () => mockResponse }); // When const result = await listEmailTemplatesHandler({}); // Then const expectedText = [ 'The following templates must be presented as an array ALL their data in 3 columns: Name, Description and Creation Date. Do not omit anything.', '', 'Found 2 Email templates for domain "example.com":', '', '| Name | Description | Creation Date |', '|------|-------------|---------------|', '| my Template 1 | The template #1 | 2025-03-28T17:42:30.000Z |', '| my Template 2 | The template #2 | 2025-03-26T11:11:48.000Z |\n' ].join('\n'); expect(result.role).toBe('assistant'); expect(result.content[0].text).toBe(expectedText); }); it('handles Mailgun API error', async () => { // Given (fetch as jest.Mock).mockResolvedValue({ ok: false, status: 403, statusText: 'Forbidden' }); // When const result = await listEmailTemplatesHandler({}); // Then expect(result).toEqual(new PromptResponse('Mailgun API error: 403 Forbidden').promptResponse); }); it('handles JSON parse error', async () => { // Given (fetch as jest.Mock).mockResolvedValue({ ok: true, json: async () => { throw new Error('invalid json'); } }); // When const result = await listEmailTemplatesHandler({}); // Then expect(result).toEqual(new PromptResponse('Failed to parse JSON response: Error: invalid json').promptResponse); }); it('returns early on credential fetch error', async () => { // Given jest.spyOn(mailgunHelper, 'getMailgunCredentials').mockReturnValue(new PromptResponse('Missing credentials')); // When const result = await listEmailTemplatesHandler({}); // Then expect(result).toEqual(new PromptResponse('Missing credentials').promptResponse); }); });

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/sinch/sinch-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server