@geocoding-ai/mcp
Version:
Model Context Protocol server for geocoding
99 lines (98 loc) • 3.51 kB
JavaScript
import { describe, it, expect } from 'bun:test';
import { handleGeocodeResult } from '../../tools/prepareResponse.js';
describe('handleGeocodeResult', () => {
it('should return a message if the result is null', () => {
const result = null;
const expected = {
content: [
{
type: 'text',
text: 'This service is unable to find an address for the given query.',
},
],
};
expect(handleGeocodeResult(result)).toEqual(expected);
});
it('should return a message if the result is an empty array', () => {
const result = [];
const expected = {
content: [
{
type: 'text',
text: 'This service is unable to find an address for the given query.',
},
],
};
expect(handleGeocodeResult(result)).toEqual(expected);
});
it('should return a message if the result is undefined', () => {
const result = undefined;
const expected = {
content: [
{
type: 'text',
text: 'This service is unable to find an address for the given query.',
},
],
};
expect(handleGeocodeResult(result)).toEqual(expected);
});
it('should return the stringified result if it is a valid array', () => {
const result = [
{ place_id: 1, lat: '10.0', lon: '20.0', display_name: 'Test Location' },
];
const expected = {
content: [
{
type: 'text',
text: JSON.stringify(result),
},
],
};
expect(handleGeocodeResult(result)).toEqual(expected);
});
it('should return an error message if JSON.stringify fails', () => {
const circularResult = {};
circularResult.self = circularResult; // Create a circular reference
// We need to catch the error message, which might vary slightly.
// So we'll check if the text includes the base error message.
const response = handleGeocodeResult(circularResult);
expect(response.content[0]?.type).toBe('text');
// The current implementation will identify a non-array object as an invalid input
// before attempting to stringify it. So, it falls into the first conditional block.
const expected = {
content: [
{
type: 'text',
text: 'This service is unable to find an address for the given query.',
},
],
};
expect(handleGeocodeResult(circularResult)).toEqual(expected);
});
it('should handle an array with multiple valid objects', () => {
const result = [
{
place_id: 1,
lat: '10.0',
lon: '20.0',
display_name: 'Test Location 1',
},
{
place_id: 2,
lat: '10.1',
lon: '20.1',
display_name: 'Test Location 2',
},
];
const expected = {
content: [
{
type: 'text',
text: JSON.stringify(result),
},
],
};
expect(handleGeocodeResult(result)).toEqual(expected);
});
});