import { fireEvent, screen, waitFor } from '@testing-library/react'; import { afterEach, describe, expect, it, vi } from 'vitest'; import type { ModuleReadiness } from '../../api/types'; import { ReleaseLine } from '../../components/shell/cockpit'; import { jsonResponse, renderUi } from '../../test/utils'; import { ModuleCard } from '../home/ReadinessPage'; import { SignOffReport } from '../run/SignOffReport'; const urlOf = (input: RequestInfo | URL) => (typeof input === 'string' ? input : input instanceof URL ? input.href : input.url); const mod = (module: string, verdict: string, run_id: string | null): ModuleReadiness => ({ module, verdict, rule_result: null, run_id, run_status: null, counts: { scenarios: 0, passed: 0, passed_on_retry: 0, failing: 0, not_run: 0, waiting_for_you: 0, library: 3 }, confirmed_defects: [], steps: [], unmapped_scenarios: 0, changes: [], basis: '' }); describe('step 5: result, report, readiness cockpit', () => { afterEach(() => vi.restoreAllMocks()); it('readiness: an area shows its last finished verdict, and a newer waiting run as a second line with Open your review', async () => { vi.spyOn(globalThis, 'fetch').mockImplementation(async (input) => { const u = urlOf(input); if (u.includes('/api/runs/old/readiness')) return jsonResponse({ modules: [{ ...mod('P2P', 'Not ready', 'old'), counts: { scenarios: 65, passed: 64, passed_on_retry: 0, failing: 1, not_run: 0, waiting_for_you: 0, library: 3 }, steps: [{ key: 'req', name: 'Requisition', state: 'failing', scenarios: 7, passed: 6, failed: 1, flaky: 0, not_run: 0, library: 7, touched: true, cross_cutting: false, group: null }] }], signoff: { by: 'admin', role: 'admin', at: '2026-10-18T23:51:00Z', decisions: { P2P: 'NO-GO' }, comment: null } }); if (u.endsWith('/api/auth/me')) return jsonResponse({ id: 1, username: 'c', role: 'consultant', permissions: ['read', 'run.start', 'gate.approve_cases'] }); return jsonResponse({}); }); const newer = { ...mod('P2P', 'Waiting for you', 'new'), run_status: 'waiting_approval' }; const last = { run_id: 'old', status: 'completed', created_at: '2026-10-18T23:40:00Z', modules: ['P2P'], decisions: { P2P: 'NO-GO' }, orchestrator: 'langgraph', playwright_mode: 'stub', trigger: 'manual', counts: {} }; renderUi(, { auth: true }); expect(screen.getByTestId('verdict-P2P')).toHaveTextContent(/^Not ready$/); await waitFor(() => expect(screen.getByTestId('summary-P2P')).toHaveTextContent('signed off by admin. 1 scenario failing in Requisition.')); expect(screen.getByText('A newer test run is waiting for your approval.')).toBeInTheDocument(); expect(screen.getByRole('link', { name: 'Open your review' })).toHaveAttribute('href', '/runs/new'); expect(screen.getByRole('link', { name: 'Report' })).toHaveAttribute('href', '/runs/old/report'); }); it('readiness: an area with no finished run says "Not tested yet"; a testing run is a neutral line', () => { vi.spyOn(globalThis, 'fetch').mockImplementation(async () => jsonResponse({})); renderUi(, { auth: true }); expect(screen.getByTestId('verdict-O2C')).toHaveTextContent('Not tested yet'); expect(screen.getByTestId('summary-O2C')).toHaveTextContent('Not tested yet'); expect(screen.getByText('A newer test run is testing now.')).toBeInTheDocument(); expect(screen.queryByRole('link', { name: 'Open your review' })).not.toBeInTheDocument(); }); it('the release line never guesses "Next Oracle update"', async () => { vi.spyOn(globalThis, 'fetch').mockImplementation(async (input) => { const u = urlOf(input); if (u.includes('/api/pods')) return jsonResponse([{ id: 1, name: 'Local mock pod', kind: 'mock', runnable: true, is_default: true, base_url: '', environment: 'test', modules: [], credentials: [], enabled: true }]); if (u.includes('/api/pod/profile')) return jsonResponse({ about: { release: '26C', environment: 'test', is_mock: true, source: 'x' }, modules: [] }); if (u.includes('/api/patch-watch')) return jsonResponse({ enabled: false, interval_s: 300, autostart: false, modules: [], last_seen_release: '26C', last_checked_at: null }); return jsonResponse({}); }); const { unmount } = renderUi(); await waitFor(() => expect(screen.getByTestId('release-line')).toHaveTextContent('Patch 26C. Test environment release 26C.')); expect(screen.queryByText(/Next Oracle update/)).not.toBeInTheDocument(); unmount(); renderUi(); expect(await screen.findByText(/Next Oracle update:/)).toBeInTheDocument(); }); it('an area with no test run offers "Start a test run" into the intake for that area', async () => { vi.spyOn(globalThis, 'fetch').mockImplementation(async (input) => (urlOf(input).endsWith('/api/auth/me') ? jsonResponse({ id: 1, username: 'c', role: 'consultant', permissions: ['read', 'run.start'] }) : jsonResponse({}))); renderUi(, { auth: true }); expect(await screen.findByRole('link', { name: 'Start a test run' })).toHaveAttribute('href', '/runs/new?area=O2C'); }); it('the report keeps every download: one toolbar with Sign off, PDF, Share and Downloads; file names under Audit details', async () => { vi.spyOn(globalThis, 'fetch').mockImplementation(async (input) => { const u = urlOf(input); if (u.includes('/readiness')) return jsonResponse({ modules: [], signoff: null }); if (u.includes('/report')) return jsonResponse({ decisions: { P2P: 'NO-GO' }, decision: 'NO-GO', decision_source: 'rule', criteria: { P2P: { C1: { ok: false, detail: 'x' } } }, gaps: {}, counts: {}, executive_summary: { P2P: 'Mock app, not Oracle Fusion.' }, files: { P2P: { pdf: true } }, target: 'local mock app' }); if (u.includes('/api/tickets')) return jsonResponse({ items: [], total: 0, limit: 50, offset: 0 }); if (u.includes('/api/runs/r1')) return jsonResponse({ run_id: 'r1', status: 'completed', modules: ['P2P'], decisions: { P2P: 'NO-GO' } }); if (u.endsWith('/api/auth/me')) return jsonResponse({ id: 1, username: 'q', role: 'qa_approver', permissions: ['read', 'run.signoff'] }); return jsonResponse({}); }); renderUi(, { auth: true }); expect(await screen.findByTestId('pdf-link')).toHaveAttribute('href', '/api/runs/r1/report/P2P/pdf'); expect(screen.getByRole('button', { name: 'Share' })).toBeInTheDocument(); expect(screen.getByText('Practice environment: these results come from the mock app, not Oracle Fusion.')).toBeInTheDocument(); expect(screen.getByRole('toolbar', { name: 'Report actions' })).toBeInTheDocument(); fireEvent.click(screen.getByRole('button', { name: 'Downloads' })); const href = async (name: string | RegExp) => (await screen.findByRole('menuitem', { name })).getAttribute('href'); expect(await href('PDF report')).toBe('/api/runs/r1/report/P2P/pdf'); expect(await href('Web page report (HTML)')).toBe('/api/runs/r1/report/P2P/html?download=true'); expect(await href('Open the report in a new tab')).toBe('/api/runs/r1/report/P2P/bundle'); expect(await href('Text report (Markdown)')).toBe('/api/runs/r1/report/P2P/md?download=true'); expect(screen.queryByRole('menuitem', { name: 'run-summary.json' })).not.toBeInTheDocument(); // engineering file names live under Audit details expect(await href('Results (Excel)')).toBe('/api/runs/r1/test-script.xlsx'); await waitFor(() => expect(screen.getByRole('button', { name: 'Sign off this report' })).toBeInTheDocument()); expect(screen.getAllByRole('button', { name: 'Sign off' })).toHaveLength(1); }); });