import { generateOpenAPIConnectorPromptText, openAPIConnectorPromptCallback, registerOpenAPIConnectorPrompt } from '../../src/prompts/openapi-connector-prompt.js';
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { SimplifierClient } from '../../src/client/simplifier-client.js';
describe('OpenAPI Connector Prompt', () => {
describe('generateOpenAPIConnectorPromptText', () => {
it('should generate prompt text with URL input', () => {
const result = generateOpenAPIConnectorPromptText(
'https://api.example.com/openapi.yaml',
'ExampleAPI'
);
expect(result).toContain('Create Simplifier Connector from API Specification or Description');
expect(result).toContain('https://api.example.com/openapi.yaml');
expect(result).toContain('Fetch the API specification or description from:');
expect(result).toContain('**Name**: ExampleAPI');
expect(result).toContain('**Namespace**: con/ExampleAPI');
});
it('should generate prompt text with inline spec input', () => {
const result = generateOpenAPIConnectorPromptText(
'openapi: 3.0.0\ninfo:\n title: Test API'
);
expect(result).toContain('Analyze the provided API specification or description:');
expect(result).not.toContain('Fetch the API specification or description from:');
});
it('should use default values for optional parameters', () => {
const result = generateOpenAPIConnectorPromptText(
'https://api.example.com/openapi.yaml'
);
expect(result).toContain('**Name**: <will be derived from API specification>');
expect(result).toContain('**Namespace**: con/<will be derived from API specification>');
});
it('should include all phases in the prompt', () => {
const result = generateOpenAPIConnectorPromptText(
'https://api.example.com/openapi.yaml'
);
expect(result).toContain('## Phase 1: Fetch and Analyze the API Specification or Description');
expect(result).toContain('## Phase 2: Authentication');
expect(result).toContain('## Phase 3: User Selections for Authentication');
expect(result).toContain('## Phase 4: Analyze and Present Endpoints');
expect(result).toContain('## Phase 5: User Selection for Endpoints');
expect(result).toContain('## Phase 6: Create Connector and Components');
});
it('should include guidelines and available tools', () => {
const result = generateOpenAPIConnectorPromptText(
'https://api.example.com/openapi.yaml'
);
expect(result).toContain('## Important Guidelines:');
expect(result).toContain('## Available Tools:');
expect(result).toContain('datatype-update');
expect(result).toContain('connector-update');
expect(result).toContain('connector-call-update');
});
});
describe('openAPIConnectorPromptCallback', () => {
it('should return GetPromptResult with correct structure', () => {
const args = {
openapi_url_or_spec: 'https://api.example.com/openapi.yaml',
connector_name: 'ExampleAPI'
};
const result = openAPIConnectorPromptCallback(args);
expect(result.description).toBe('Multi-phase workflow for creating a Simplifier connector from an API specification (OpenAPI/Swagger) or informal REST API description');
expect(result.messages).toHaveLength(1);
expect(result.messages[0].role).toBe('user');
expect(result.messages[0].content.type).toBe('text');
expect(typeof result.messages[0].content.text).toBe('string');
});
it('should handle missing optional parameters', () => {
const args = {
openapi_url_or_spec: 'https://api.example.com/openapi.yaml'
};
const result = openAPIConnectorPromptCallback(args);
expect(result.messages[0].content.text).toContain('<will be derived from API specification>');
expect(result.messages[0].content.text).toContain('**Namespace**: con/<will be derived from API specification>');
});
});
describe('registerOpenAPIConnectorPrompt', () => {
let mockServer: McpServer;
let mockSimplifier: SimplifierClient;
let mockPrompt: jest.Mock;
beforeEach(() => {
mockPrompt = jest.fn();
mockServer = {
prompt: mockPrompt
} as unknown as McpServer;
mockSimplifier = {} as SimplifierClient;
});
it('should register prompt with server', () => {
registerOpenAPIConnectorPrompt(mockServer, mockSimplifier);
expect(mockPrompt).toHaveBeenCalledTimes(1);
expect(mockPrompt).toHaveBeenCalledWith(
'create-connector-from-openapi',
expect.any(String),
expect.any(Object),
expect.any(Function)
);
});
it('should register prompt with correct description', () => {
registerOpenAPIConnectorPrompt(mockServer, mockSimplifier);
const description = mockPrompt.mock.calls[0][1];
expect(description).toContain('Guided workflow');
expect(description).toContain('API specification');
});
it('should register prompt with correct argument schema', () => {
registerOpenAPIConnectorPrompt(mockServer, mockSimplifier);
const argsSchema = mockPrompt.mock.calls[0][2];
expect(argsSchema).toHaveProperty('openapi_url_or_spec');
expect(argsSchema).toHaveProperty('connector_name');
});
it('should register prompt with callback function', () => {
registerOpenAPIConnectorPrompt(mockServer, mockSimplifier);
const callback = mockPrompt.mock.calls[0][3];
expect(typeof callback).toBe('function');
});
});
});