|
| 1 | +import { render, screen, waitFor, fireEvent } from '@testing-library/react'; |
| 2 | +import ProofPage from '@/app/proof/page'; |
| 3 | +import { localStorageMock } from '../../utils/test-helpers'; |
| 4 | + |
| 5 | +jest.mock('@/lib/api', () => ({ |
| 6 | + proofApi: { |
| 7 | + listRequirements: jest.fn(), |
| 8 | + waive: jest.fn(), |
| 9 | + }, |
| 10 | +})); |
| 11 | + |
| 12 | +jest.mock('@/lib/workspace-storage', () => ({ |
| 13 | + getSelectedWorkspacePath: jest.fn(() => '/test/workspace'), |
| 14 | +})); |
| 15 | + |
| 16 | +jest.mock('swr', () => ({ __esModule: true, default: jest.fn() })); |
| 17 | + |
| 18 | +import useSWR from 'swr'; |
| 19 | +import { proofApi } from '@/lib/api'; |
| 20 | + |
| 21 | +const mockUseSWR = useSWR as jest.MockedFunction<typeof useSWR>; |
| 22 | +const mockWaive = proofApi.waive as jest.MockedFunction<typeof proofApi.waive>; |
| 23 | + |
| 24 | +const openReq = { |
| 25 | + id: 'REQ-001', |
| 26 | + title: 'Test requirement', |
| 27 | + description: 'A test requirement', |
| 28 | + severity: 'high', |
| 29 | + status: 'open', |
| 30 | + glitch_type: 'regression', |
| 31 | + obligations: [], |
| 32 | + evidence_rules: [], |
| 33 | + waiver: null, |
| 34 | + created_at: '2026-01-01T00:00:00Z', |
| 35 | + satisfied_at: null, |
| 36 | + created_by: 'tester', |
| 37 | + source_issue: null, |
| 38 | + related_reqs: [], |
| 39 | + source: 'manual', |
| 40 | +}; |
| 41 | + |
| 42 | +const waivedReq = { |
| 43 | + ...openReq, |
| 44 | + id: 'REQ-002', |
| 45 | + title: 'Waived requirement', |
| 46 | + status: 'waived', |
| 47 | + waiver: { |
| 48 | + reason: 'Not applicable for this release', |
| 49 | + expires: null, |
| 50 | + manual_checklist: [], |
| 51 | + approved_by: 'frank', |
| 52 | + waived_at: '2026-03-01T12:00:00Z', |
| 53 | + }, |
| 54 | +}; |
| 55 | + |
| 56 | +describe('ProofPage', () => { |
| 57 | + beforeEach(() => { |
| 58 | + jest.clearAllMocks(); |
| 59 | + localStorageMock.clear(); |
| 60 | + mockUseSWR.mockReturnValue({ |
| 61 | + data: { |
| 62 | + requirements: [openReq, waivedReq], |
| 63 | + total: 2, |
| 64 | + by_status: { open: 1, waived: 1, satisfied: 0 }, |
| 65 | + }, |
| 66 | + error: undefined, |
| 67 | + isLoading: false, |
| 68 | + mutate: jest.fn(), |
| 69 | + } as any); |
| 70 | + }); |
| 71 | + |
| 72 | + describe('waived row visual treatment', () => { |
| 73 | + it('renders waived rows with muted/strikethrough styling', async () => { |
| 74 | + render(<ProofPage />); |
| 75 | + await waitFor(() => screen.getByText('Waived requirement')); |
| 76 | + |
| 77 | + const waivedRow = screen.getByText('Waived requirement').closest('tr'); |
| 78 | + expect(waivedRow).toHaveClass('opacity-60'); |
| 79 | + }); |
| 80 | + |
| 81 | + it('does not apply muted styling to open rows', async () => { |
| 82 | + render(<ProofPage />); |
| 83 | + await waitFor(() => screen.getByText('Test requirement')); |
| 84 | + |
| 85 | + const openRow = screen.getByText('Test requirement').closest('tr'); |
| 86 | + expect(openRow).not.toHaveClass('opacity-60'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('does not show Waive button for waived requirements', async () => { |
| 90 | + render(<ProofPage />); |
| 91 | + await waitFor(() => screen.getByText('Waived requirement')); |
| 92 | + |
| 93 | + const buttons = screen.getAllByRole('button', { name: /waive/i }); |
| 94 | + // Only one Waive button for the open req |
| 95 | + expect(buttons).toHaveLength(1); |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + describe('WaiveDialog — 2-step confirmation flow', () => { |
| 100 | + it('opens the form step when Waive is clicked', async () => { |
| 101 | + render(<ProofPage />); |
| 102 | + await waitFor(() => screen.getByRole('button', { name: /^waive$/i })); |
| 103 | + |
| 104 | + fireEvent.click(screen.getByRole('button', { name: /^waive$/i })); |
| 105 | + |
| 106 | + await waitFor(() => { |
| 107 | + expect(screen.getByText(/waive req-001/i)).toBeInTheDocument(); |
| 108 | + expect(screen.getByLabelText(/reason/i)).toBeInTheDocument(); |
| 109 | + expect(screen.getByRole('button', { name: /continue/i })).toBeInTheDocument(); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + it('shows error if Continue is clicked without a reason', async () => { |
| 114 | + render(<ProofPage />); |
| 115 | + await waitFor(() => screen.getByRole('button', { name: /^waive$/i })); |
| 116 | + fireEvent.click(screen.getByRole('button', { name: /^waive$/i })); |
| 117 | + |
| 118 | + await waitFor(() => screen.getByRole('button', { name: /continue/i })); |
| 119 | + fireEvent.click(screen.getByRole('button', { name: /continue/i })); |
| 120 | + |
| 121 | + await waitFor(() => { |
| 122 | + expect(screen.getByText(/reason is required/i)).toBeInTheDocument(); |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + it('advances to confirmation step when reason is provided', async () => { |
| 127 | + render(<ProofPage />); |
| 128 | + await waitFor(() => screen.getByRole('button', { name: /^waive$/i })); |
| 129 | + fireEvent.click(screen.getByRole('button', { name: /^waive$/i })); |
| 130 | + |
| 131 | + await waitFor(() => screen.getByLabelText(/reason/i)); |
| 132 | + fireEvent.change(screen.getByLabelText(/reason/i), { |
| 133 | + target: { value: 'Not needed this cycle' }, |
| 134 | + }); |
| 135 | + fireEvent.click(screen.getByRole('button', { name: /continue/i })); |
| 136 | + |
| 137 | + await waitFor(() => { |
| 138 | + expect(screen.getByText(/marked satisfied without evidence/i)).toBeInTheDocument(); |
| 139 | + expect(screen.getByRole('button', { name: /confirm waive/i })).toBeInTheDocument(); |
| 140 | + expect(screen.getByRole('button', { name: /back/i })).toBeInTheDocument(); |
| 141 | + }); |
| 142 | + }); |
| 143 | + |
| 144 | + it('shows the entered reason in the confirmation summary', async () => { |
| 145 | + render(<ProofPage />); |
| 146 | + await waitFor(() => screen.getByRole('button', { name: /^waive$/i })); |
| 147 | + fireEvent.click(screen.getByRole('button', { name: /^waive$/i })); |
| 148 | + |
| 149 | + await waitFor(() => screen.getByLabelText(/reason/i)); |
| 150 | + fireEvent.change(screen.getByLabelText(/reason/i), { |
| 151 | + target: { value: 'Accepted risk for v1' }, |
| 152 | + }); |
| 153 | + fireEvent.click(screen.getByRole('button', { name: /continue/i })); |
| 154 | + |
| 155 | + await waitFor(() => { |
| 156 | + expect(screen.getByText('Accepted risk for v1')).toBeInTheDocument(); |
| 157 | + }); |
| 158 | + }); |
| 159 | + |
| 160 | + it('goes back to form when Back is clicked', async () => { |
| 161 | + render(<ProofPage />); |
| 162 | + await waitFor(() => screen.getByRole('button', { name: /^waive$/i })); |
| 163 | + fireEvent.click(screen.getByRole('button', { name: /^waive$/i })); |
| 164 | + |
| 165 | + await waitFor(() => screen.getByLabelText(/reason/i)); |
| 166 | + fireEvent.change(screen.getByLabelText(/reason/i), { |
| 167 | + target: { value: 'Temporary waiver' }, |
| 168 | + }); |
| 169 | + fireEvent.click(screen.getByRole('button', { name: /continue/i })); |
| 170 | + |
| 171 | + await waitFor(() => screen.getByRole('button', { name: /back/i })); |
| 172 | + fireEvent.click(screen.getByRole('button', { name: /back/i })); |
| 173 | + |
| 174 | + await waitFor(() => { |
| 175 | + expect(screen.getByLabelText(/reason/i)).toBeInTheDocument(); |
| 176 | + expect(screen.getByDisplayValue('Temporary waiver')).toBeInTheDocument(); |
| 177 | + }); |
| 178 | + }); |
| 179 | + |
| 180 | + it('calls proofApi.waive and closes on Confirm Waive', async () => { |
| 181 | + mockWaive.mockResolvedValueOnce(undefined as any); |
| 182 | + const mutate = jest.fn(); |
| 183 | + mockUseSWR.mockReturnValue({ |
| 184 | + data: { |
| 185 | + requirements: [openReq, waivedReq], |
| 186 | + total: 2, |
| 187 | + by_status: { open: 1, waived: 1 }, |
| 188 | + }, |
| 189 | + error: undefined, |
| 190 | + isLoading: false, |
| 191 | + mutate, |
| 192 | + } as any); |
| 193 | + |
| 194 | + render(<ProofPage />); |
| 195 | + await waitFor(() => screen.getByRole('button', { name: /^waive$/i })); |
| 196 | + fireEvent.click(screen.getByRole('button', { name: /^waive$/i })); |
| 197 | + |
| 198 | + await waitFor(() => screen.getByLabelText(/reason/i)); |
| 199 | + fireEvent.change(screen.getByLabelText(/reason/i), { |
| 200 | + target: { value: 'Risk accepted' }, |
| 201 | + }); |
| 202 | + fireEvent.click(screen.getByRole('button', { name: /continue/i })); |
| 203 | + |
| 204 | + await waitFor(() => screen.getByRole('button', { name: /confirm waive/i })); |
| 205 | + fireEvent.click(screen.getByRole('button', { name: /confirm waive/i })); |
| 206 | + |
| 207 | + await waitFor(() => { |
| 208 | + expect(mockWaive).toHaveBeenCalledWith('/test/workspace', 'REQ-001', { |
| 209 | + reason: 'Risk accepted', |
| 210 | + expires: null, |
| 211 | + manual_checklist: [], |
| 212 | + approved_by: '', |
| 213 | + }); |
| 214 | + expect(mutate).toHaveBeenCalled(); |
| 215 | + }); |
| 216 | + }); |
| 217 | + }); |
| 218 | +}); |
0 commit comments