get-cities.tool.spec.tsβ’2.17 kB
import { describe, it, expect, beforeEach, vi } from 'vitest'
import { GetCitiesTool } from './get-cities.tool'
import { TimezoneService } from '../../timezone/timezone.service'
describe('GetCitiesTool', () => {
let tool: GetCitiesTool
let timezoneService: TimezoneService
beforeEach(() => {
timezoneService = new TimezoneService()
tool = new GetCitiesTool(timezoneService)
})
describe('execute', () => {
it('should return cities for a valid region', async () => {
const result = await tool.execute({ region: 'America' })
expect(result).toHaveProperty('content')
expect(result.content).toBeInstanceOf(Array)
expect(result.content).toHaveLength(1)
expect(result.content[0]).toHaveProperty('type', 'text')
expect(result.content[0]).toHaveProperty('text')
})
it('should return valid JSON string with region, cities, and count', async () => {
const result = await tool.execute({ region: 'Europe' })
const data = JSON.parse(result.content[0].text)
expect(data).toHaveProperty('region', 'Europe')
expect(data).toHaveProperty('cities')
expect(data).toHaveProperty('count')
expect(data.cities).toBeInstanceOf(Array)
expect(data.count).toBe(data.cities.length)
})
it('should include expected cities for Europe', async () => {
const result = await tool.execute({ region: 'Europe' })
const data = JSON.parse(result.content[0].text)
expect(data.cities).toContain('London')
expect(data.cities).toContain('Paris')
})
it('should throw error when region parameter is missing', async () => {
await expect(tool.execute({ region: '' })).rejects.toThrow('Region parameter is required')
})
it('should throw error for invalid region', async () => {
await expect(tool.execute({ region: 'InvalidRegion' })).rejects.toThrow('Invalid region')
})
it('should call timezoneService.getCitiesInRegion with correct parameter', async () => {
const spy = vi.spyOn(timezoneService, 'getCitiesInRegion')
await tool.execute({ region: 'Asia' })
expect(spy).toHaveBeenCalledWith('Asia')
})
})
})