@sinch/mcp
Version:
Sinch MCP server
222 lines • 8.94 kB
JavaScript
"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 list_email_events_1 = require("../../../src/tools/email/list-email-events");
const mailgunHelper = __importStar(require("../../../src/tools/email/utils/mailgun-service-helper"));
const types_1 = require("../../../src/types");
global.fetch = jest.fn();
describe('listEmailEventsHandler', () => {
const mockCredentials = {
domain: 'example.com',
apiKey: 'test-api-key'
};
beforeEach(() => {
jest.resetAllMocks();
jest.spyOn(mailgunHelper, 'getMailgunCredentials').mockReturnValue(mockCredentials);
});
it('returns formatted prompt response with grouped events', async () => {
// Given
const mockResponse = {
items: [
{
id: 'event1',
timestamp: 1710000000.7552662,
event: 'accepted',
recipient: 'user@example.com',
message: {
headers: {
'message-id': 'abc123@example.com',
from: 'sender@example.com',
to: 'user@example.com',
subject: 'Test Subject'
}
}
},
{
id: 'event2',
timestamp: 1710000100.1638649,
event: 'delivered',
recipient: 'user@example.com',
message: {
headers: {
'message-id': 'abc123@example.com',
from: 'sender@example.com',
to: 'user@example.com',
subject: 'Test Subject'
}
}
},
{
id: 'event3',
timestamp: 1710000200.745685,
event: 'opened',
recipient: 'user@example.com',
message: {
headers: {
'message-id': 'abc123@example.com',
}
}
},
{
id: 'event3',
timestamp: 1710000300.8591378,
event: 'opened',
recipient: 'other@example.com',
message: {
headers: {
'message-id': 'xyz789@example.com',
}
}
}
]
};
fetch.mockResolvedValue({
ok: true,
json: async () => mockResponse
});
// When
const result = await (0, list_email_events_1.listEmailEventsHandler)({});
// Then
const expectedText = [
'The following events must be presented with ALL their data, even if the ID is long, it MUST be displayed as this information can be used to get subsequent information on other API endpoints.',
'Found 4 email events for domain "example.com":',
'**Message ID:** abc123@example.com',
'From: sender@example.com',
'To: user@example.com',
'Subject: Test Subject',
'| Event | Timestamp (UTC) |',
'|-------|-----------------|',
'| accepted | 2024-03-09T16:00:00.755Z |',
'| delivered | 2024-03-09T16:01:40.163Z |',
'| opened | 2024-03-09T16:03:20.745Z |',
'',
'**Message ID:** xyz789@example.com',
'| Event | Timestamp (UTC) |',
'|-------|-----------------|',
'| opened | 2024-03-09T16:05:00.859Z |\n'
].join('\n');
expect(result.role).toBe('assistant');
expect(result.content[0].text).toBe(expectedText);
});
it('returns formatted prompt response with grouped events filtered by input parameters', async () => {
// Given
const mockResponse = {
items: [
{
id: 'event3',
timestamp: 1710000200.745685,
event: 'opened',
recipient: 'user@example.com',
message: {
headers: {
'message-id': 'abc123@example.com',
}
}
},
{
id: 'event3',
timestamp: 1710000300.8591378,
event: 'opened',
recipient: 'other@example.com',
message: {
headers: {
'message-id': 'xyz789@example.com',
}
}
}
]
};
fetch.mockResolvedValue({
ok: true,
json: async () => mockResponse
});
// When
const result = await (0, list_email_events_1.listEmailEventsHandler)({
domain: 'example.com',
event: 'opened',
limit: 50,
beginSearchPeriod: '2024-03-09T16:00:00Z',
endSearchPeriod: '2024-03-09T17:00:00Z'
});
// Then
const expectedText = [
'The following events must be presented with ALL their data, even if the ID is long, it MUST be displayed as this information can be used to get subsequent information on other API endpoints.',
'Found 2 email events for domain "example.com" (filtered by event: opened):',
'**Message ID:** abc123@example.com',
'| Event | Timestamp (UTC) |',
'|-------|-----------------|',
'| opened | 2024-03-09T16:03:20.745Z |',
'',
'**Message ID:** xyz789@example.com',
'| Event | Timestamp (UTC) |',
'|-------|-----------------|',
'| opened | 2024-03-09T16:05:00.859Z |\n'
].join('\n');
expect(result.role).toBe('assistant');
expect(result.content[0].text).toBe(expectedText);
});
it('handles Mailgun API error', async () => {
// Given
fetch.mockResolvedValue({
ok: false,
status: 403,
statusText: 'Forbidden'
});
// When
const result = await (0, list_email_events_1.listEmailEventsHandler)({});
// 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('invalid json'); }
});
// When
const result = await (0, list_email_events_1.listEmailEventsHandler)({});
// Then
expect(result).toEqual(new types_1.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 types_1.PromptResponse('Missing credentials'));
// When
const result = await (0, list_email_events_1.listEmailEventsHandler)({});
// Then
expect(result).toEqual(new types_1.PromptResponse('Missing credentials').promptResponse);
});
});
//# sourceMappingURL=list-email-events.test.js.map