import { fireEvent, screen, waitFor, within } from '@testing-library/react'; import { afterEach, describe, expect, it, vi } from 'vitest'; import { jsonResponse, renderUi } from '../../test/utils'; const urlOf = (input: RequestInfo | URL) => (typeof input === 'string' ? input : input instanceof URL ? input.href : input.url); const ADMIN = { id: 1, username: 'a', role: 'admin', permissions: ['read', 'admin', 'run.start'] }; const pod = { id: 3, name: 'Acme test', kind: 'fusion', base_url: 'https://acme-test.fa.ocs.oraclecloud.com', environment: 'test', enabled: true, is_default: false, runnable: true, from_server_settings: false, credentials: [{ module: 'ALL', set_by: 'a', set_at: '2026-11-08T10:00:00Z' }], release: '26C', modules: ['AP'], run_areas: ['AP'], discovered_at: '2026-11-08T10:05:00Z', discovery: { areas: [] }, test_data: {}, test_data_overrides: {}, }; const coverage = { pod_id: 3, read_at: '2026-11-09T10:00:00Z', scenarios: 12, covered_share: 0.4, areas: [{ code: 'AP', name: 'Payables', status: 'read' }, { code: 'PO', name: 'Purchasing', status: 'not_readable' }], variants: [ { area: 'AP', dimension: 'invoice_type', label: 'Invoice type', value: 'Standard', share: 0.6, volume: 60, status: 'covered', matches: [{ id: 'AP-FUS-001', title: 'Standard invoice', why: "its title says 'standard invoice'" }] }, { area: 'AP', dimension: 'invoice_type', label: 'Invoice type', value: 'Credit memo', share: 0.25, volume: 25, status: 'not_covered', matches: [] }, ], gaps: [{ area: 'AP', dimension: 'invoice_type', label: 'Invoice type', value: 'Credit memo', share: 0.25, volume: 25, status: 'not_covered' }], }; function mockApi(pods: unknown[], calls: { method: string; url: string }[] = []) { vi.spyOn(globalThis, 'fetch').mockImplementation(async (input, init) => { const u = urlOf(input); const req = input instanceof Request ? input : null; const method = req?.method ?? init?.method ?? 'GET'; if (method !== 'GET') { calls.push({ method, url: u }); return jsonResponse({ pod_id: 3, read_at: '2026-11-09T10:00:00Z', days: 90, areas: [] }); } if (u.endsWith('/api/auth/me')) return jsonResponse(ADMIN); if (u.endsWith('/api/pods/3/coverage')) return jsonResponse(coverage); if (u.endsWith('/api/pods')) return jsonResponse(pods); return jsonResponse({}); }); } describe('Test environments: usage and coverage of an Oracle Fusion pod', () => { afterEach(() => vi.restoreAllMocks()); it('a pod whose usage was never read offers Read usage, which calls the server', async () => { const calls: { method: string; url: string }[] = []; mockApi([{ ...pod, usage_read_at: null }], calls); const { PodsPage } = await import('./PodsPage'); renderUi(, { auth: true, route: '/pods' }); const panel = await screen.findByTestId('pod-usage'); expect(panel).toHaveTextContent('Not read yet'); fireEvent.click(within(panel).getByTestId('pod-read-usage')); await waitFor(() => expect(calls.some((c) => c.method === 'POST' && c.url.endsWith('/api/pods/3/usage'))).toBe(true)); }); it('shows the covered share, the gaps most used first, each variant with its share, and the areas not counted', async () => { mockApi([{ ...pod, usage_read_at: '2026-11-09T10:00:00Z' }]); const { PodsPage } = await import('./PodsPage'); renderUi(, { auth: true, route: '/pods' }); expect(await screen.findByTestId('pod-usage-covered')).toHaveTextContent('The library covers 40% of the volume read (12 scenarios compared)'); const gaps = screen.getByTestId('pod-usage-gaps'); expect(within(gaps).getByText(/invoice type Credit memo/)).toBeInTheDocument(); expect(within(gaps).getByText('25%, Not covered')).toBeInTheDocument(); const rows = within(screen.getByTestId('pod-usage-table')).getAllByRole('row'); expect(rows.map((r) => r.getAttribute('data-status'))).toEqual(['covered', 'not_covered']); expect(screen.getByText(/Not counted: .*\(the test user cannot read it\)/)).toBeInTheDocument(); expect(screen.getByTestId('pod-read-usage')).toHaveTextContent('Read usage again'); }); });