@sinch/mcp
Version:
Sinch MCP server
93 lines • 3.86 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const axios_1 = __importDefault(require("axios"));
const geocoding_1 = require("../../../../src/tools/conversation/utils/geocoding");
jest.mock('axios');
const mockedAxios = axios_1.default;
const mockAddress = 'Phare d\'Eckmühl';
const mockSuccessResponse = {
data: {
status: 'OK',
results: [{
formatted_address: 'Pl. du Maréchal Davout, 29760 Penmarch, France',
geometry: {
location: {
lat: 47.7981899,
lng: -4.372768499999999
}
}
}]
}
};
beforeEach(() => {
process.env.GEOCODING_API_KEY = 'test-api-key';
jest.clearAllMocks();
});
test('returns coordinates when API responds with OK', async () => {
mockedAxios.get.mockResolvedValueOnce(mockSuccessResponse);
const result = await (0, geocoding_1.getLatitudeLongitudeFromAddress)(mockAddress);
expect(result).toEqual({
latitude: 47.7981899,
longitude: -4.372768499999999,
formattedAddress: 'Pl. du Maréchal Davout, 29760 Penmarch, France'
});
expect(mockedAxios.get).toHaveBeenCalledWith(expect.stringContaining('geocode/json'), {
params: { address: mockAddress, key: 'test-api-key' }
});
});
test('returns fallback when API status is not OK', async () => {
mockedAxios.get.mockResolvedValueOnce({ data: { results: [], status: 'ZERO_RESULTS' } });
const consoleSpy = jest.spyOn(console, 'error').mockImplementation(() => { });
const result = await (0, geocoding_1.getLatitudeLongitudeFromAddress)('nowhere');
expect(result).toEqual({
latitude: 0,
longitude: 0,
formattedAddress: 'Unknown'
});
expect(consoleSpy).toHaveBeenCalledWith('Geocoding failed:', 'ZERO_RESULTS');
consoleSpy.mockRestore();
});
test('returns fallback when axios throws', async () => {
mockedAxios.get.mockRejectedValueOnce(new Error('Network error'));
const consoleSpy = jest.spyOn(console, 'error').mockImplementation(() => { });
const result = await (0, geocoding_1.getLatitudeLongitudeFromAddress)('some address');
expect(result).toEqual({
latitude: 0,
longitude: 0,
formattedAddress: 'Unknown'
});
expect(consoleSpy).toHaveBeenCalledWith('Request failed:', new Error('Network error'));
consoleSpy.mockRestore();
});
test('includes GEOCODING_API_KEY in query params', async () => {
mockedAxios.get.mockResolvedValueOnce(mockSuccessResponse);
await (0, geocoding_1.getLatitudeLongitudeFromAddress)(mockAddress);
expect(mockedAxios.get).toHaveBeenCalledWith(expect.any(String), expect.objectContaining({
params: expect.objectContaining({
key: 'test-api-key'
})
}));
});
test('returns fallback when GEOCODING_API_KEY is not set', async () => {
delete process.env.GEOCODING_API_KEY;
const consoleSpy = jest.spyOn(console, 'error').mockImplementation(() => { });
const mockError = new Error('API key missing');
mockedAxios.get.mockRejectedValueOnce(mockError);
const result = await (0, geocoding_1.getLatitudeLongitudeFromAddress)(mockAddress);
expect(result).toEqual({
latitude: 0,
longitude: 0,
formattedAddress: 'Unknown'
});
expect(mockedAxios.get).toHaveBeenCalledWith(expect.any(String), expect.objectContaining({
params: expect.objectContaining({
key: undefined
})
}));
expect(consoleSpy).toHaveBeenCalledWith('Request failed:', new Error('API key missing'));
consoleSpy.mockRestore();
});
//# sourceMappingURL=geocoding.test.js.map