Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions codeframe/core/proof/ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,21 @@ def _waiver_to_json(waiver: Optional[Waiver]) -> Optional[str]:
"expires": waiver.expires.isoformat() if waiver.expires else None,
"manual_checklist": waiver.manual_checklist,
"approved_by": waiver.approved_by,
"waived_at": waiver.waived_at.isoformat() if waiver.waived_at else None,
})


def _waiver_from_json(raw: Optional[str]) -> Optional[Waiver]:
if not raw:
return None
data = json.loads(raw)
waived_at_raw = data.get("waived_at")
return Waiver(
reason=data["reason"],
expires=date.fromisoformat(data["expires"]) if data.get("expires") else None,
manual_checklist=data.get("manual_checklist", []),
approved_by=data.get("approved_by", ""),
waived_at=datetime.fromisoformat(waived_at_raw) if waived_at_raw else None,
)


Expand Down Expand Up @@ -317,6 +320,14 @@ def waive_requirement(
workspace: Workspace, req_id: str, waiver: Waiver
) -> Optional[Requirement]:
"""Waive a requirement with reason and optional expiry."""
if waiver.waived_at is None:
waiver = Waiver(
reason=waiver.reason,
expires=waiver.expires,
manual_checklist=waiver.manual_checklist,
approved_by=waiver.approved_by,
waived_at=datetime.now(timezone.utc),
)
_ensure_tables(workspace)
conn = get_db_connection(workspace)
cursor = conn.cursor()
Expand Down
1 change: 1 addition & 0 deletions codeframe/core/proof/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class Waiver:
expires: Optional[date] = None
manual_checklist: list[str] = field(default_factory=list)
approved_by: str = ""
waived_at: Optional[datetime] = None


@dataclass
Expand Down
2 changes: 2 additions & 0 deletions codeframe/ui/routers/proof_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class WaiverOut(BaseModel):
expires: Optional[str]
manual_checklist: list[str]
approved_by: str
waived_at: Optional[str] = None


class RequirementResponse(BaseModel):
Expand Down Expand Up @@ -195,6 +196,7 @@ def _req_to_response(req) -> RequirementResponse:
expires=req.waiver.expires.isoformat() if req.waiver.expires else None,
manual_checklist=req.waiver.manual_checklist,
approved_by=req.waiver.approved_by,
waived_at=req.waiver.waived_at.isoformat() if req.waiver.waived_at else None,
) if req.waiver else None,
created_at=req.created_at.isoformat() if req.created_at else None,
satisfied_at=req.satisfied_at.isoformat() if req.satisfied_at else None,
Expand Down
2 changes: 2 additions & 0 deletions web-ui/__mocks__/@hugeicons/react.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,6 @@ module.exports = {
Alert01Icon: createIconMock('Alert01Icon'),
// SplitPane
ArrowLeft01Icon: createIconMock('ArrowLeft01Icon'),
// Proof page
InformationCircleIcon: createIconMock('InformationCircleIcon'),
};
218 changes: 218 additions & 0 deletions web-ui/__tests__/app/proof/page.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
import { render, screen, waitFor, fireEvent } from '@testing-library/react';
import ProofPage from '@/app/proof/page';
import { localStorageMock } from '../../utils/test-helpers';

jest.mock('@/lib/api', () => ({
proofApi: {
listRequirements: jest.fn(),
waive: jest.fn(),
},
}));

jest.mock('@/lib/workspace-storage', () => ({
getSelectedWorkspacePath: jest.fn(() => '/test/workspace'),
}));

jest.mock('swr', () => ({ __esModule: true, default: jest.fn() }));

import useSWR from 'swr';
import { proofApi } from '@/lib/api';

const mockUseSWR = useSWR as jest.MockedFunction<typeof useSWR>;
const mockWaive = proofApi.waive as jest.MockedFunction<typeof proofApi.waive>;

const openReq = {
id: 'REQ-001',
title: 'Test requirement',
description: 'A test requirement',
severity: 'high',
status: 'open',
glitch_type: 'regression',
obligations: [],
evidence_rules: [],
waiver: null,
created_at: '2026-01-01T00:00:00Z',
satisfied_at: null,
created_by: 'tester',
source_issue: null,
related_reqs: [],
source: 'manual',
};

const waivedReq = {
...openReq,
id: 'REQ-002',
title: 'Waived requirement',
status: 'waived',
waiver: {
reason: 'Not applicable for this release',
expires: null,
manual_checklist: [],
approved_by: 'frank',
waived_at: '2026-03-01T12:00:00Z',
},
};

describe('ProofPage', () => {
beforeEach(() => {
jest.clearAllMocks();
localStorageMock.clear();
mockUseSWR.mockReturnValue({
data: {
requirements: [openReq, waivedReq],
total: 2,
by_status: { open: 1, waived: 1, satisfied: 0 },
},
error: undefined,
isLoading: false,
mutate: jest.fn(),
} as any);
});

describe('waived row visual treatment', () => {
it('renders waived rows with muted/strikethrough styling', async () => {
render(<ProofPage />);
await waitFor(() => screen.getByText('Waived requirement'));

const waivedRow = screen.getByText('Waived requirement').closest('tr');
expect(waivedRow).toHaveClass('opacity-60');
});

it('does not apply muted styling to open rows', async () => {
render(<ProofPage />);
await waitFor(() => screen.getByText('Test requirement'));

const openRow = screen.getByText('Test requirement').closest('tr');
expect(openRow).not.toHaveClass('opacity-60');
});

it('does not show Waive button for waived requirements', async () => {
render(<ProofPage />);
await waitFor(() => screen.getByText('Waived requirement'));

const buttons = screen.getAllByRole('button', { name: /waive/i });
// Only one Waive button for the open req
expect(buttons).toHaveLength(1);
});
});

describe('WaiveDialog — 2-step confirmation flow', () => {
it('opens the form step when Waive is clicked', async () => {
render(<ProofPage />);
await waitFor(() => screen.getByRole('button', { name: /^waive$/i }));

fireEvent.click(screen.getByRole('button', { name: /^waive$/i }));

await waitFor(() => {
expect(screen.getByText(/waive req-001/i)).toBeInTheDocument();
expect(screen.getByLabelText(/reason/i)).toBeInTheDocument();
expect(screen.getByRole('button', { name: /continue/i })).toBeInTheDocument();
});
});

it('shows error if Continue is clicked without a reason', async () => {
render(<ProofPage />);
await waitFor(() => screen.getByRole('button', { name: /^waive$/i }));
fireEvent.click(screen.getByRole('button', { name: /^waive$/i }));

await waitFor(() => screen.getByRole('button', { name: /continue/i }));
fireEvent.click(screen.getByRole('button', { name: /continue/i }));

await waitFor(() => {
expect(screen.getByText(/reason is required/i)).toBeInTheDocument();
});
});

it('advances to confirmation step when reason is provided', async () => {
render(<ProofPage />);
await waitFor(() => screen.getByRole('button', { name: /^waive$/i }));
fireEvent.click(screen.getByRole('button', { name: /^waive$/i }));

await waitFor(() => screen.getByLabelText(/reason/i));
fireEvent.change(screen.getByLabelText(/reason/i), {
target: { value: 'Not needed this cycle' },
});
fireEvent.click(screen.getByRole('button', { name: /continue/i }));

await waitFor(() => {
expect(screen.getByText(/marked satisfied without evidence/i)).toBeInTheDocument();
expect(screen.getByRole('button', { name: /confirm waive/i })).toBeInTheDocument();
expect(screen.getByRole('button', { name: /back/i })).toBeInTheDocument();
});
});

it('shows the entered reason in the confirmation summary', async () => {
render(<ProofPage />);
await waitFor(() => screen.getByRole('button', { name: /^waive$/i }));
fireEvent.click(screen.getByRole('button', { name: /^waive$/i }));

await waitFor(() => screen.getByLabelText(/reason/i));
fireEvent.change(screen.getByLabelText(/reason/i), {
target: { value: 'Accepted risk for v1' },
});
fireEvent.click(screen.getByRole('button', { name: /continue/i }));

await waitFor(() => {
expect(screen.getByText('Accepted risk for v1')).toBeInTheDocument();
});
});

it('goes back to form when Back is clicked', async () => {
render(<ProofPage />);
await waitFor(() => screen.getByRole('button', { name: /^waive$/i }));
fireEvent.click(screen.getByRole('button', { name: /^waive$/i }));

await waitFor(() => screen.getByLabelText(/reason/i));
fireEvent.change(screen.getByLabelText(/reason/i), {
target: { value: 'Temporary waiver' },
});
fireEvent.click(screen.getByRole('button', { name: /continue/i }));

await waitFor(() => screen.getByRole('button', { name: /back/i }));
fireEvent.click(screen.getByRole('button', { name: /back/i }));

await waitFor(() => {
expect(screen.getByLabelText(/reason/i)).toBeInTheDocument();
expect(screen.getByDisplayValue('Temporary waiver')).toBeInTheDocument();
});
});

it('calls proofApi.waive and closes on Confirm Waive', async () => {
mockWaive.mockResolvedValueOnce(undefined as any);
const mutate = jest.fn();
mockUseSWR.mockReturnValue({
data: {
requirements: [openReq, waivedReq],
total: 2,
by_status: { open: 1, waived: 1 },
},
error: undefined,
isLoading: false,
mutate,
} as any);

render(<ProofPage />);
await waitFor(() => screen.getByRole('button', { name: /^waive$/i }));
fireEvent.click(screen.getByRole('button', { name: /^waive$/i }));

await waitFor(() => screen.getByLabelText(/reason/i));
fireEvent.change(screen.getByLabelText(/reason/i), {
target: { value: 'Risk accepted' },
});
fireEvent.click(screen.getByRole('button', { name: /continue/i }));

await waitFor(() => screen.getByRole('button', { name: /confirm waive/i }));
fireEvent.click(screen.getByRole('button', { name: /confirm waive/i }));

await waitFor(() => {
expect(mockWaive).toHaveBeenCalledWith('/test/workspace', 'REQ-001', {
reason: 'Risk accepted',
expires: null,
manual_checklist: [],
approved_by: '',
});
expect(mutate).toHaveBeenCalled();
});
});
});
});
Loading
Loading