import { act, renderHook } from '@testing-library/react' import { describe, expect, it, vi } from 'vitest' import type { TemporalComparisonResponse, VectorSelectionBBox } from '../types' const mocks = vi.hoisted(() => ({ compare: vi.fn(), })) vi.mock('../services/api/temporal', () => ({ temporalApi: { compare: mocks.compare, }, })) import { useTemporalComparison } from './useTemporalComparison' const bbox: VectorSelectionBBox = { min_x: 4.98, min_y: 51.15, max_x: 5.02, max_y: 51.19, crs: 'EPSG:4326', } describe('useTemporalComparison', () => { it('fails locally when no project is active', async () => { const { result } = renderHook(() => useTemporalComparison(null)) let response: TemporalComparisonResponse | null = null await act(async () => { response = await result.current.compareTemporalSnapshots('earlier', 'later', bbox) }) expect(response).toBeNull() expect(result.current.temporalComparisonError).toContain('project') expect(mocks.compare).not.toHaveBeenCalled() }) it('requires two explicit snapshots before calling the API', async () => { const { result } = renderHook(() => useTemporalComparison('project-1')) await act(async () => { await result.current.compareTemporalSnapshots('', 'later', bbox) }) expect(result.current.temporalComparisonError).toContain('twee meetmomenten') expect(mocks.compare).not.toHaveBeenCalled() }) it('persists the compatible comparison result in hook state', async () => { const comparison = { project_id: 'project-1', earlier_dataset_id: 'earlier', later_dataset_id: 'later', warnings: [], } as unknown as TemporalComparisonResponse mocks.compare.mockResolvedValueOnce(comparison) const { result } = renderHook(() => useTemporalComparison('project-1')) let response: TemporalComparisonResponse | null = null await act(async () => { response = await result.current.compareTemporalSnapshots('earlier', 'later', bbox, 'area-1') }) expect(response).toEqual(comparison) expect(result.current.temporalComparison).toEqual(comparison) expect(result.current.temporalComparisonError).toBeNull() expect(mocks.compare).toHaveBeenCalledWith('project-1', { earlier_dataset_id: 'earlier', later_dataset_id: 'later', bbox, area_id: 'area-1', preview_limit: 500, }) }) })