import { cleanup, render, screen } from '@testing-library/react'; import { afterEach, describe, expect, it, vi } from 'vitest'; import { ServicePage } from '../../src/ServicePage'; afterEach(() => { cleanup(); vi.unstubAllGlobals(); }); describe('ServicePage API boundary', () => { it('renders a stable empty state when PostgreSQL serializes an empty service list as null', async () => { vi.stubGlobal('fetch', vi.fn().mockResolvedValue(new Response(JSON.stringify({ contractVersion: 'v1', observedAt: '2026-08-10T04:38:27Z', services: null, total: 0, }), { status: 200, headers: { 'Content-Type': 'application/json' } }))); render(); expect(await screen.findByRole('heading', { level: 1, name: 'Services' })).toBeVisible(); expect(screen.getByRole('heading', { level: 2, name: 'Nog geen services geconfigureerd' })).toBeVisible(); expect(screen.getByRole('link', { name: 'Open eerste configuratie' })).toHaveAttribute('href', '/onboarding'); }); it('does not present an unavailable source as an empty configuration', async () => { vi.stubGlobal('fetch', vi.fn().mockResolvedValue(new Response(JSON.stringify({ contractVersion: 'v1', observedAt: '2026-08-10T04:38:27Z', capabilityState: 'unavailable', configurationState: 'unknown', reason: 'source_unavailable', services: [], total: 0, }), { status: 200, headers: { 'Content-Type': 'application/json' } }))); render(); expect(await screen.findByRole('heading', { level: 2, name: 'Servicebron niet beschikbaar' })).toBeVisible(); expect(screen.queryByRole('link', { name: 'Open eerste configuratie' })).not.toBeInTheDocument(); }); it('renders the service-level TLS certificate when bounded probe history no longer contains the TLS sample', async () => { vi.stubGlobal('fetch', vi.fn((input: RequestInfo | URL) => { const url = String(input); if (url.endsWith('/dependencies?limit=100')) { return Promise.resolve(new Response(JSON.stringify({ serviceId: 'service-1', dependencies: [] }), { status: 200, headers: { 'Content-Type': 'application/json' } })); } return Promise.resolve(new Response(JSON.stringify({ service: { id: 'service-1', name: 'Pulse', state: 'up', sampleCount: 5, successfulSampleCount: 5, history: [{ probeId: 'http-probe', observedAt: '2026-08-12T10:20:00Z', state: 'up' }], certificate: { expiresAt: '2026-11-01T00:00:00Z', issuer: 'Pulse test issuer', subject: 'CN=pulse.test', hostnameValid: true, verificationState: 'valid' }, }, }), { status: 200, headers: { 'Content-Type': 'application/json' } })); })); render(); expect(await screen.findByRole('heading', { level: 2, name: 'TLS-certificaat' })).toBeVisible(); expect(screen.getByText('Pulse test issuer')).toBeVisible(); expect(screen.queryByText('Geen TLS-certificaat waargenomen.')).not.toBeInTheDocument(); }); });