Files
errors.bitreport.pt/admin-ui/test/lib/incidents-data.test.ts
David SantosandPaperclip 3d4444eb21 BIT-15744: add remaining unit tests for admin-ui, api routes, collector-worker db, ingest-api services
Complete test coverage implementation for errors.bitreport.pt:

admin-ui (98 tests):
- test/components/: AppShell, DataAlert, IncidentNotes, KpiCard, SeverityBadge, StateBadge
- test/lib/: format, demo-incidents, incidents-data, incidents-data-api, stats-data, stats-data-api
- vitest.config.ts with jsdom + v8 coverage + @vitejs/plugin-react

api route tests (54 tests):
- src/routes/admin/auth.test.ts (15 tests)
- src/routes/admin/incidents.test.ts (11 tests)
- src/routes/admin/stats.test.ts (18 tests)
- src/routes/public/errorMonitoringStats.test.ts (10 tests)

collector-worker db/scheduler tests (22 tests):
- src/db/collectorRunWriter.test.ts (10 tests)
- src/db/loadActiveSources.test.ts (6 tests)
- src/scheduler/runHttpSourceOnce.test.ts (6 tests)

ingest-api service tests (13 tests):
- src/buildApp.test.ts, plugins/prometheus.test.ts
- services/ingestAuth.test.ts, services/processIngestBatch.utils.test.ts

Total: 187 additional tests (255 total across all modules). All pass.

Paperclip-Issue: BIT-15744
Co-Authored-By: Paperclip <[email protected]>
2026-05-03 07:24:38 +00:00

177 lines
5.6 KiB
TypeScript

import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { computeKpis, fetchIncidentList, fetchIncidentDetail } from '@/lib/incidents-data';
import { getDemoIncidentSummaries } from '@/lib/demo-incidents';
import type { IncidentSummary } from '@/lib/types';
vi.mock('next/headers', () => ({
cookies: vi.fn().mockResolvedValue({ get: vi.fn().mockReturnValue(undefined) }),
}));
const makeIncident = (state: IncidentSummary['currentState']): IncidentSummary => ({
id: Math.random().toString(),
projectSlug: 'test',
fingerprint: 'fp',
title: 'Error',
severity: 'high',
currentState: state,
firstSeenAt: '2024-01-01T00:00:00Z',
lastSeenAt: '2024-01-01T00:00:00Z',
occurrencesCount: 1,
});
describe('computeKpis', () => {
it('returns all zeros for empty list', () => {
const result = computeKpis([]);
expect(result).toEqual({ total: 0, open: 0, investigating: 0, resolved: 0, ignored: 0 });
});
it('counts total correctly', () => {
const incidents = [makeIncident('open'), makeIncident('resolved'), makeIncident('ignored')];
expect(computeKpis(incidents).total).toBe(3);
});
it('counts each state correctly', () => {
const incidents = [
makeIncident('open'),
makeIncident('open'),
makeIncident('investigating'),
makeIncident('resolved'),
makeIncident('ignored'),
];
const result = computeKpis(incidents);
expect(result.open).toBe(2);
expect(result.investigating).toBe(1);
expect(result.resolved).toBe(1);
expect(result.ignored).toBe(1);
expect(result.total).toBe(5);
});
it('handles all incidents in a single state', () => {
const incidents = [makeIncident('resolved'), makeIncident('resolved')];
const result = computeKpis(incidents);
expect(result.resolved).toBe(2);
expect(result.open).toBe(0);
});
});
describe('fetchIncidentList — not_configured', () => {
beforeEach(() => {
delete process.env.ERROR_REPORTING_ADMIN_API_BASE;
delete process.env.ERROR_REPORTING_UI_DEMO;
});
it('returns not_configured when API base is missing and demo is off', async () => {
const result = await fetchIncidentList({});
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.error).toBe('not_configured');
}
});
});
describe('fetchIncidentList — demo mode', () => {
beforeEach(() => {
delete process.env.ERROR_REPORTING_ADMIN_API_BASE;
process.env.ERROR_REPORTING_UI_DEMO = '1';
});
afterEach(() => {
delete process.env.ERROR_REPORTING_UI_DEMO;
});
it('returns demo incidents when no API base is set', async () => {
const result = await fetchIncidentList({});
expect(result.ok).toBe(true);
if (result.ok) {
expect(result.source).toBe('demo');
expect(result.incidents.length).toBeGreaterThan(0);
}
});
it('filters demo incidents by state', async () => {
const allDemo = getDemoIncidentSummaries();
const openCount = allDemo.filter((i) => i.currentState === 'open').length;
const result = await fetchIncidentList({ state: 'open' });
if (result.ok) {
expect(result.incidents.every((i) => i.currentState === 'open')).toBe(true);
expect(result.incidents.length).toBe(openCount);
}
});
it('filters demo incidents by severity', async () => {
const result = await fetchIncidentList({ severity: 'high' });
if (result.ok) {
expect(result.incidents.every((i) => i.severity === 'high')).toBe(true);
}
});
it('filters demo incidents by project slug', async () => {
const allDemo = getDemoIncidentSummaries();
const firstSlug = allDemo[0].projectSlug;
const result = await fetchIncidentList({ project: firstSlug });
if (result.ok) {
expect(result.incidents.every((i) => i.projectSlug === firstSlug)).toBe(true);
}
});
it('filters demo incidents by query string', async () => {
const allDemo = getDemoIncidentSummaries();
const word = allDemo[0].title.split(' ')[0].toLowerCase();
const result = await fetchIncidentList({ q: word });
if (result.ok) {
expect(result.incidents.length).toBeGreaterThan(0);
}
});
it('returns empty list when no demo incidents match filter', async () => {
const result = await fetchIncidentList({ q: 'xyzzy_no_match_abc_999' });
if (result.ok) {
expect(result.incidents).toHaveLength(0);
}
});
});
describe('fetchIncidentDetail — demo mode', () => {
beforeEach(() => {
delete process.env.ERROR_REPORTING_ADMIN_API_BASE;
process.env.ERROR_REPORTING_UI_DEMO = '1';
});
afterEach(() => {
delete process.env.ERROR_REPORTING_UI_DEMO;
});
it('returns detail for a known demo incident', async () => {
const demos = getDemoIncidentSummaries();
const result = await fetchIncidentDetail(demos[0].id);
expect(result.ok).toBe(true);
if (result.ok) {
expect(result.incident.id).toBe(demos[0].id);
expect(result.source).toBe('demo');
}
});
it('returns not_found for an unknown demo id', async () => {
const result = await fetchIncidentDetail('no-such-incident-id');
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.error).toBe('not_found');
}
});
});
describe('fetchIncidentDetail — not_configured', () => {
beforeEach(() => {
delete process.env.ERROR_REPORTING_ADMIN_API_BASE;
delete process.env.ERROR_REPORTING_UI_DEMO;
});
it('returns not_configured when API is not set and demo is off', async () => {
const result = await fetchIncidentDetail('some-id');
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.error).toBe('not_configured');
}
});
});