import { beforeEach, describe, expect, it, vi } from 'vitest' import { http } from '@/shared/api/http' import { listPlatformTenants } from '@/modules/iam' import { listPlatformUnifiedOrders } from '@/modules/unified-orders' import { getActiveCustomerCount, getDashboardOperationalMetrics, getTodayNewOrderCount, } from './dashboard' vi.mock('@/modules/iam', () => ({ listPlatformTenants: vi.fn() })) vi.mock('@/modules/unified-orders', () => ({ listPlatformUnifiedOrders: vi.fn() })) vi.mock('@/shared/api/http', async (importOriginal) => ({ ...await importOriginal(), http: { get: vi.fn() }, })) describe('dashboard metrics api', () => { beforeEach(() => vi.clearAllMocks()) it('normalizes the operational metrics response', async () => { vi.mocked(http.get).mockResolvedValue({ code: '200', message: 'ok', data: { inTransitContainerCount: '3', inTransitTeu: '4.25', unknownContainerTypeCount: '1', pendingCustomsOrderCount: '7', asOf: '2026-09-11T12:00:00', } }) await expect(getDashboardOperationalMetrics()).resolves.toEqual({ inTransitContainerCount: 3, inTransitTeu: 4.25, unknownContainerTypeCount: 1, pendingCustomsOrderCount: 7, asOf: '2026-09-11T12:00:00', }) expect(http.get).toHaveBeenCalledWith('/platform/v1/first-leg/dashboard/operational-metrics') }) it('uses list totals for today orders and active customers', async () => { vi.mocked(listPlatformUnifiedOrders).mockResolvedValue({ records: [], page: 1, size: 1, total: 12, pages: 12 }) vi.mocked(listPlatformTenants).mockResolvedValue({ records: [], page: 1, size: 1, total: 8, pages: 8 }) await expect(getTodayNewOrderCount()).resolves.toBe(12) await expect(getActiveCustomerCount()).resolves.toBe(8) expect(listPlatformUnifiedOrders).toHaveBeenCalledWith(expect.objectContaining({ page: 1, size: 1 })) const orderQuery = vi.mocked(listPlatformUnifiedOrders).mock.calls[0]![0] expect(orderQuery.createdFrom).toMatch(/T00:00:00\.000$/) expect(orderQuery.createdTo).toMatch(/T23:59:59\.999$/) expect(listPlatformTenants).toHaveBeenCalledWith({ page: 1, size: 1, status: 'ACTIVE' }) }) })