import { describe, it, expect, vi } from 'vitest'; import { aggregateIncidentKpisFromSummary } from '@/lib/stats-data'; import type { StatsSummaryPayload } from '@/lib/stats-data'; import { fetchOperationalStats } from '@/lib/stats-data'; vi.mock('next/headers', () => ({ cookies: vi.fn().mockResolvedValue({ get: vi.fn().mockReturnValue(undefined) }), })); describe('fetchOperationalStats — not_configured', () => { it('returns error when API is not configured', async () => { delete process.env.ERROR_REPORTING_ADMIN_API_BASE; const result = await fetchOperationalStats(); expect(result.ok).toBe(false); if (!result.ok) { expect(typeof result.message).toBe('string'); } }); }); describe('aggregateIncidentKpisFromSummary', () => { it('returns zeros for empty byProject list', () => { const summary: StatsSummaryPayload = { byProject: [] }; const result = aggregateIncidentKpisFromSummary(summary); expect(result).toEqual({ total: 0, open: 0, investigating: 0, resolved: 0, ignored: 0 }); }); it('sums across a single project', () => { const summary: StatsSummaryPayload = { byProject: [ { projectSlug: 'proj-a', total: 10, open: 3, investigating: 2, resolved: 4, ignored: 1 }, ], }; const result = aggregateIncidentKpisFromSummary(summary); expect(result.total).toBe(10); expect(result.open).toBe(3); expect(result.investigating).toBe(2); expect(result.resolved).toBe(4); expect(result.ignored).toBe(1); }); it('sums across multiple projects', () => { const summary: StatsSummaryPayload = { byProject: [ { projectSlug: 'proj-a', total: 5, open: 2, investigating: 1, resolved: 1, ignored: 1 }, { projectSlug: 'proj-b', total: 8, open: 4, investigating: 2, resolved: 2, ignored: 0 }, ], }; const result = aggregateIncidentKpisFromSummary(summary); expect(result.total).toBe(13); expect(result.open).toBe(6); expect(result.investigating).toBe(3); expect(result.resolved).toBe(3); expect(result.ignored).toBe(1); }); it('handles projects with all zeros', () => { const summary: StatsSummaryPayload = { byProject: [ { projectSlug: 'empty', total: 0, open: 0, investigating: 0, resolved: 0, ignored: 0 }, ], }; const result = aggregateIncidentKpisFromSummary(summary); expect(result.total).toBe(0); }); });