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]>
191 lines
5.9 KiB
TypeScript
191 lines
5.9 KiB
TypeScript
/**
|
|
* Tests for fetchIncidentList and fetchIncidentDetail covering the API-backed paths.
|
|
*/
|
|
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
import { fetchIncidentList, fetchIncidentDetail } from '@/lib/incidents-data';
|
|
import type { IncidentSummary } from '@/lib/types';
|
|
|
|
vi.mock('next/headers', () => ({
|
|
cookies: vi.fn().mockResolvedValue({ get: vi.fn().mockReturnValue(undefined) }),
|
|
}));
|
|
|
|
const PREV_API_BASE = process.env.ERROR_REPORTING_ADMIN_API_BASE;
|
|
|
|
const mockIncident: IncidentSummary = {
|
|
id: 'inc-001',
|
|
projectSlug: 'wdmi-store',
|
|
fingerprint: 'fp-abc',
|
|
title: 'Uncaught TypeError',
|
|
severity: 'high',
|
|
currentState: 'open',
|
|
firstSeenAt: '2026-01-01T00:00:00Z',
|
|
lastSeenAt: '2026-01-02T00:00:00Z',
|
|
occurrencesCount: 5,
|
|
};
|
|
|
|
beforeEach(() => {
|
|
process.env.ERROR_REPORTING_ADMIN_API_BASE = 'http://test-api.local';
|
|
delete process.env.ERROR_REPORTING_UI_DEMO;
|
|
});
|
|
|
|
afterEach(() => {
|
|
if (PREV_API_BASE === undefined) {
|
|
delete process.env.ERROR_REPORTING_ADMIN_API_BASE;
|
|
} else {
|
|
process.env.ERROR_REPORTING_ADMIN_API_BASE = PREV_API_BASE;
|
|
}
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
describe('fetchIncidentList — API mode', () => {
|
|
it('returns incidents from API on success', async () => {
|
|
global.fetch = vi.fn().mockResolvedValue({
|
|
ok: true,
|
|
json: () => Promise.resolve({ incidents: [mockIncident] }),
|
|
});
|
|
const result = await fetchIncidentList({});
|
|
expect(result.ok).toBe(true);
|
|
if (result.ok) {
|
|
expect(result.incidents).toHaveLength(1);
|
|
expect(result.incidents[0].id).toBe('inc-001');
|
|
expect(result.source).toBe('api');
|
|
}
|
|
});
|
|
|
|
it('passes state/severity/project/q as query params', async () => {
|
|
global.fetch = vi.fn().mockResolvedValue({
|
|
ok: true,
|
|
json: () => Promise.resolve({ incidents: [] }),
|
|
});
|
|
await fetchIncidentList({ state: 'open', severity: 'high', project: 'wdmi', q: 'TypeError' });
|
|
const calledUrl = (global.fetch as ReturnType<typeof vi.fn>).mock.calls[0][0] as string;
|
|
expect(calledUrl).toContain('state=open');
|
|
expect(calledUrl).toContain('severity=high');
|
|
expect(calledUrl).toContain('project=wdmi');
|
|
expect(calledUrl).toContain('q=TypeError');
|
|
});
|
|
|
|
it('omits empty params from query string', async () => {
|
|
global.fetch = vi.fn().mockResolvedValue({
|
|
ok: true,
|
|
json: () => Promise.resolve({ incidents: [] }),
|
|
});
|
|
await fetchIncidentList({});
|
|
const calledUrl = (global.fetch as ReturnType<typeof vi.fn>).mock.calls[0][0] as string;
|
|
expect(calledUrl).not.toContain('state=');
|
|
expect(calledUrl).not.toContain('severity=');
|
|
});
|
|
|
|
it('returns unreachable error when fetch throws', async () => {
|
|
global.fetch = vi.fn().mockRejectedValue(new Error('ECONNREFUSED'));
|
|
const result = await fetchIncidentList({});
|
|
expect(result.ok).toBe(false);
|
|
if (!result.ok) {
|
|
expect(result.error).toBe('unreachable');
|
|
expect(result.message).toContain('ECONNREFUSED');
|
|
}
|
|
});
|
|
|
|
it('returns unreachable error when API returns non-ok status', async () => {
|
|
global.fetch = vi.fn().mockResolvedValue({
|
|
ok: false,
|
|
status: 503,
|
|
json: () => Promise.resolve({}),
|
|
});
|
|
const result = await fetchIncidentList({});
|
|
expect(result.ok).toBe(false);
|
|
if (!result.ok) {
|
|
expect(result.error).toBe('unreachable');
|
|
}
|
|
});
|
|
|
|
it('returns empty incidents array when payload has no incidents field', async () => {
|
|
global.fetch = vi.fn().mockResolvedValue({
|
|
ok: true,
|
|
json: () => Promise.resolve({}),
|
|
});
|
|
const result = await fetchIncidentList({});
|
|
expect(result.ok).toBe(true);
|
|
if (result.ok) {
|
|
expect(result.incidents).toHaveLength(0);
|
|
}
|
|
});
|
|
});
|
|
|
|
describe('fetchIncidentDetail — API mode', () => {
|
|
const detailPayload = {
|
|
incident: {
|
|
id: 'inc-001',
|
|
projectSlug: 'wdmi-store',
|
|
fingerprint: 'fp-abc',
|
|
title: 'Uncaught TypeError',
|
|
severity: 'high',
|
|
currentState: 'open',
|
|
firstSeenAt: '2026-01-01T00:00:00Z',
|
|
lastSeenAt: '2026-01-02T00:00:00Z',
|
|
occurrencesCount: 5,
|
|
recentOccurrences: [],
|
|
stateHistory: [],
|
|
},
|
|
};
|
|
|
|
it('returns incident detail on success', async () => {
|
|
global.fetch = vi.fn().mockResolvedValue({
|
|
ok: true,
|
|
json: () => Promise.resolve(detailPayload),
|
|
});
|
|
const result = await fetchIncidentDetail('inc-001');
|
|
expect(result.ok).toBe(true);
|
|
if (result.ok) {
|
|
expect(result.incident.id).toBe('inc-001');
|
|
expect(result.source).toBe('api');
|
|
}
|
|
});
|
|
|
|
it('returns not_found on 404', async () => {
|
|
global.fetch = vi.fn().mockResolvedValue({
|
|
ok: false,
|
|
status: 404,
|
|
json: () => Promise.resolve({}),
|
|
});
|
|
const result = await fetchIncidentDetail('unknown-id');
|
|
expect(result.ok).toBe(false);
|
|
if (!result.ok) {
|
|
expect(result.error).toBe('not_found');
|
|
}
|
|
});
|
|
|
|
it('returns unreachable when API throws', async () => {
|
|
global.fetch = vi.fn().mockRejectedValue(new Error('timeout'));
|
|
const result = await fetchIncidentDetail('inc-001');
|
|
expect(result.ok).toBe(false);
|
|
if (!result.ok) {
|
|
expect(result.error).toBe('unreachable');
|
|
expect(result.message).toContain('timeout');
|
|
}
|
|
});
|
|
|
|
it('returns unreachable on non-404 HTTP error', async () => {
|
|
global.fetch = vi.fn().mockResolvedValue({
|
|
ok: false,
|
|
status: 500,
|
|
json: () => Promise.resolve({}),
|
|
});
|
|
const result = await fetchIncidentDetail('inc-001');
|
|
expect(result.ok).toBe(false);
|
|
if (!result.ok) {
|
|
expect(result.error).toBe('unreachable');
|
|
}
|
|
});
|
|
|
|
it('URL-encodes the incident id', async () => {
|
|
global.fetch = vi.fn().mockResolvedValue({
|
|
ok: true,
|
|
json: () => Promise.resolve(detailPayload),
|
|
});
|
|
await fetchIncidentDetail('inc/with/slashes');
|
|
const calledUrl = (global.fetch as ReturnType<typeof vi.fn>).mock.calls[0][0] as string;
|
|
expect(calledUrl).toContain('inc%2Fwith%2Fslashes');
|
|
});
|
|
});
|