get-timezone-info.tool.spec.tsβ’2.92 kB
import { describe, it, expect, beforeEach, vi } from 'vitest'
import { GetTimezoneInfoTool } from './get-timezone-info.tool'
import { TimezoneService } from '../../timezone/timezone.service'
describe('GetTimezoneInfoTool', () => {
let tool: GetTimezoneInfoTool
let timezoneService: TimezoneService
beforeEach(() => {
timezoneService = new TimezoneService()
tool = new GetTimezoneInfoTool(timezoneService)
})
describe('execute', () => {
it('should return timezone info for valid region and city', async () => {
const result = await tool.execute({ region: 'Europe', city: 'London' })
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 timezone information', async () => {
const result = await tool.execute({ region: 'America', city: 'New_York' })
const data = JSON.parse(result.content[0].text)
expect(data).toHaveProperty('timezone')
expect(data).toHaveProperty('datetime_local')
expect(data).toHaveProperty('datetime_utc')
expect(data).toHaveProperty('timezone_offset')
expect(data).toHaveProperty('timestamp')
})
it('should format timezone as region/city', async () => {
const result = await tool.execute({ region: 'Asia', city: 'Tokyo' })
const data = JSON.parse(result.content[0].text)
expect(data.timezone).toBe('Asia/Tokyo')
})
it('should throw error when region parameter is missing', async () => {
await expect(tool.execute({ region: '', city: 'London' })).rejects.toThrow(
'Region and city parameters are required',
)
})
it('should throw error when city parameter is missing', async () => {
await expect(tool.execute({ region: 'Europe', city: '' })).rejects.toThrow(
'Region and city parameters are required',
)
})
it('should throw error for invalid timezone', async () => {
await expect(tool.execute({ region: 'Invalid', city: 'City' })).rejects.toThrow(
'Invalid timezone',
)
})
it('should call timezoneService.getTimeInTimezone with correct parameter', async () => {
const spy = vi.spyOn(timezoneService, 'getTimeInTimezone')
await tool.execute({ region: 'Australia', city: 'Sydney' })
expect(spy).toHaveBeenCalledWith('Australia/Sydney')
})
it('should return current timestamp', async () => {
const before = Date.now()
const result = await tool.execute({ region: 'Europe', city: 'London' })
const after = Date.now()
const data = JSON.parse(result.content[0].text)
expect(data.timestamp).toBeGreaterThanOrEqual(before)
expect(data.timestamp).toBeLessThanOrEqual(after)
})
})
})