UNPKG

@sinch/mcp

Version:

Sinch MCP server

135 lines 5.78 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); const analytics_metrics_1 = require("../../../src/tools/email/analytics-metrics"); const mailgunHelper = __importStar(require("../../../src/tools/email/utils/mailgun-service-helper")); const types_1 = require("../../../src/types"); const mailgun_tools_helper_1 = require("../../../src/tools/email/utils/mailgun-tools-helper"); const utils_1 = require("../../../src/utils"); global.fetch = jest.fn(); describe('analyticsMetricsHandler', () => { const mockApiKey = 'test-api-key'; beforeEach(() => { jest.resetAllMocks(); jest.spyOn(mailgunHelper, 'getMailgunApiKey').mockReturnValue(mockApiKey); }); it('returns metrics data in prompt response', async () => { // Given const mockResponse = { start: 'Mon, 02 Jun 2025 00:00:00 +0000', end: 'Mon, 09 Jun 2025 00:00:00 +0000', resolution: 'day', dimensions: [ 'time' ], items: [], aggregates: { metrics: { accepted_count: 10, delivered_count: 8, failed_count: 2, opened_count: 5, clicked_count: 3, unsubscribed_count: 1, complained_count: 0 } } }; const mockedFetch = fetch; mockedFetch.mockResolvedValue({ ok: true, json: async () => mockResponse }); // When const result = await (0, analytics_metrics_1.analyticsMetricsHandler)({}); // Then const expectedText = [ 'The following data must be presented graphically. Mailgun Analytics Metrics for domain "all":', '', '{"metrics":{"accepted_count":10,"delivered_count":8,"failed_count":2,"opened_count":5,"clicked_count":3,"unsubscribed_count":1,"complained_count":0}}' ].join('\n'); expect(result.role).toBe('assistant'); expect(result.content[0].text).toBe(expectedText); expect(mockedFetch).toHaveBeenCalledTimes(1); // eslint-disable-next-line @typescript-eslint/no-unused-vars const [_url, options] = mockedFetch.mock.calls[0]; expect(options?.method).toBe('POST'); const encodedAuth = 'Basic ' + Buffer.from(`api:${mockApiKey}`).toString('base64'); expect(options?.headers).toMatchObject({ Authorization: encodedAuth, 'Content-Type': 'application/json', }); const apiKeyHash = '4c806362b613f7496abf284146efd31da90e4b16169fe001841ca17290f427c4'; expect((0, mailgun_tools_helper_1.sha256)(mockApiKey)).toBe(apiKeyHash); const expectedUserAgent = (0, utils_1.formatUserAgent)('analytics-metrics', apiKeyHash); expect((options?.headers)['User-Agent']).toBe(expectedUserAgent); }); it('handles Mailgun API error gracefully', async () => { // Given fetch.mockResolvedValue({ ok: false, status: 403, statusText: 'Forbidden' }); // When const result = await (0, analytics_metrics_1.analyticsMetricsHandler)({ domain: 'testdomain.com', }); // Then expect(result).toEqual(new types_1.PromptResponse('Mailgun API error: 403 Forbidden').promptResponse); }); it('handles JSON parse error', async () => { // Given fetch.mockResolvedValue({ ok: true, json: async () => { throw new Error('Unexpected token'); } }); // When const result = await (0, analytics_metrics_1.analyticsMetricsHandler)({ domain: 'testdomain.com', }); // Then expect(result).toEqual(new types_1.PromptResponse('Failed to parse JSON response: Error: Unexpected token').promptResponse); }); it('returns early on credential fetch error', async () => { // Given jest.spyOn(mailgunHelper, 'getMailgunApiKey').mockReturnValue(new types_1.PromptResponse('Missing API key')); // When const result = await (0, analytics_metrics_1.analyticsMetricsHandler)({}); // Then expect(result).toEqual(new types_1.PromptResponse('Missing API key').promptResponse); }); }); //# sourceMappingURL=analytics-metrics.test.js.map