import { describe, expect, it, vi, beforeEach } from 'vitest'; import { api } from './client'; import { fetchRawMeasurements, downloadRawMeasurementsExport } from './measurements'; vi.mock('./client', () => ({ api: { get: vi.fn(), }, })); describe('measurements API client', () => { beforeEach(() => { vi.mocked(api.get).mockReset(); }); it('passes the device-id filter as a comma-separated string', async () => { vi.mocked(api.get).mockResolvedValueOnce({ data: { totalCount: 0, limit: 200, offset: 0, rows: [] }, }); await fetchRawMeasurements({ fromUtc: '2026-05-01T00:00:00Z', toUtc: '2026-05-02T00:00:00Z', deviceIds: ['aaa', 'bbb', 'ccc'], limit: 200, offset: 0, }); expect(api.get).toHaveBeenCalledWith( '/measurements/raw', expect.objectContaining({ params: expect.objectContaining({ deviceIds: 'aaa,bbb,ccc' }), }), ); }); it('omits the device-id filter when the list is empty', async () => { vi.mocked(api.get).mockResolvedValueOnce({ data: { totalCount: 0, limit: 200, offset: 0, rows: [] }, }); await fetchRawMeasurements({ fromUtc: '2026-05-01T00:00:00Z', toUtc: '2026-05-02T00:00:00Z', deviceIds: [], limit: 200, offset: 0, }); const call = vi.mocked(api.get).mock.calls[0][1] as { params: Record }; expect(call.params.deviceIds).toBeUndefined(); }); it('builds the download URL with the full filter set', () => { const originalLocation = window.location; const setHref = vi.fn(); Object.defineProperty(window, 'location', { writable: true, value: { ...originalLocation, set href(v: string) { setHref(v); }, }, }); downloadRawMeasurementsExport({ fromUtc: '2026-05-01T00:00:00.000Z', toUtc: '2026-05-02T00:00:00.000Z', deviceIds: ['device-1', 'device-2'], rowCap: 50000, }); expect(setHref).toHaveBeenCalledWith( expect.stringContaining('/api/measurements/raw/export.xlsx?'), ); const url = setHref.mock.calls[0][0] as string; expect(url).toContain('from=2026-05-01T00%3A00%3A00.000Z'); expect(url).toContain('to=2026-05-02T00%3A00%3A00.000Z'); expect(url).toContain('deviceIds=device-1%2Cdevice-2'); expect(url).toContain('rowCap=50000'); Object.defineProperty(window, 'location', { writable: true, value: originalLocation }); }); });