Spaces:
Running
Running
| """Tests for functional audit implementation (P0/P1 improvement plan).""" | |
| from types import SimpleNamespace | |
| from unittest.mock import patch | |
| import pytest | |
| from fastapi import HTTPException | |
| from sqlalchemy.exc import IntegrityError | |
| from core.projects.company_profile import ( | |
| COMPANY_PROFILES_MIGRATION, | |
| upsert_company_profile, | |
| ) | |
| from core.projects.export_gate import evaluate_export_gate | |
| from core.projects.project_creation import validate_grant_eligibility | |
| import grantforge_bootstrap | |
| def _project(**kwargs): | |
| defaults = { | |
| "external_context": {}, | |
| "final_document_audit_result": None, | |
| } | |
| defaults.update(kwargs) | |
| return SimpleNamespace(**defaults) | |
| def test_export_gate_blocks_missing_audit(): | |
| gate = evaluate_export_gate(_project()) | |
| assert gate["blocked"] is True | |
| assert any("brak ukończonego audytu" in r for r in gate["reasons"]) | |
| def test_export_gate_blocks_pending_audit(): | |
| gate = evaluate_export_gate( | |
| _project(final_document_audit_result={"status": "pending"}) | |
| ) | |
| assert gate["blocked"] is True | |
| def test_export_gate_blocks_error_audit(): | |
| gate = evaluate_export_gate( | |
| _project(final_document_audit_result={"status": "error"}) | |
| ) | |
| assert gate["blocked"] is True | |
| assert any("fail-closed" in r for r in gate["reasons"]) | |
| def test_export_gate_allows_clean_audit(): | |
| gate = evaluate_export_gate( | |
| _project( | |
| final_document_audit_result={ | |
| "status": "completed", | |
| "export_status": "ok", | |
| "is_approved": True, | |
| "human_review_required": False, | |
| } | |
| ) | |
| ) | |
| assert gate["blocked"] is False | |
| def test_validate_grant_eligibility_blocks_closed(): | |
| with pytest.raises(HTTPException) as exc: | |
| validate_grant_eligibility( | |
| {"selected_grant": {"status": "closed", "name": "Test"}} | |
| ) | |
| assert exc.value.status_code == 400 | |
| def test_validate_grant_eligibility_blocks_outdated_warning(): | |
| with pytest.raises(HTTPException) as exc: | |
| validate_grant_eligibility( | |
| {"selected_grant": {"is_outdated_warning": True}} | |
| ) | |
| assert exc.value.status_code == 400 | |
| def test_bootstrap_skips_disabled_patches(monkeypatch): | |
| grantforge_bootstrap.reset_bootstrap_for_tests() | |
| monkeypatch.setenv("ENABLE_PROJECTS_MATCH_PATCH", "false") | |
| monkeypatch.setenv("ENABLE_GRANTS_MATCH_FIX", "false") | |
| monkeypatch.setenv("RUN_BOOTSTRAP_CATALOG", "false") | |
| status = grantforge_bootstrap.bootstrap_startup() | |
| disabled = status.get("disabled_patches") or [] | |
| assert "ENABLE_PROJECTS_MATCH_PATCH" in disabled | |
| assert "ENABLE_GRANTS_MATCH_FIX" in disabled | |
| def test_company_profile_migration_constant(): | |
| assert COMPANY_PROFILES_MIGRATION == "20260703_multi_company_profiles" | |
| def test_upsert_company_profile_handles_unique_violation(): | |
| from core.subscription.db import SessionLocal, init_models, Base, engine | |
| from core.projects.models import CompanyProfile | |
| init_models() | |
| Base.metadata.create_all(bind=engine) | |
| db = SessionLocal() | |
| try: | |
| db.query(CompanyProfile).delete() | |
| db.commit() | |
| ctx = {"company_data": {"nip": "1234567890", "name": "Test Sp. z o.o."}} | |
| p1 = upsert_company_profile(db, "user_a", ctx) | |
| assert p1 is not None | |
| assert p1.nip == "1234567890" | |
| original_commit = db.commit | |
| def commit_with_race(): | |
| if not getattr(commit_with_race, "called", False): | |
| commit_with_race.called = True | |
| raise IntegrityError("dup", params=None, orig=Exception("uq_company_profile_user_nip")) | |
| return original_commit() | |
| db.commit = commit_with_race | |
| p2 = upsert_company_profile(db, "user_a", {**ctx, "company_data": {**ctx["company_data"], "name": "Updated"}}) | |
| db.commit = original_commit | |
| assert p2 is not None | |
| assert p2.name == "Updated" | |
| finally: | |
| db.close() | |
| def test_orchestration_entry_prefers_gsd(): | |
| from core.orchestration_entry import ( | |
| LEGACY_LANGGRAPH_DEPRECATED, | |
| PREFERRED_ORCHESTRATION_PATH, | |
| start_project_orchestration, | |
| ) | |
| assert LEGACY_LANGGRAPH_DEPRECATED is True | |
| assert PREFERRED_ORCHESTRATION_PATH == "gsd" | |
| with patch("gsd.integration.start_gsd_for_project", return_value={"gsd_mode": True}) as mock: | |
| result = start_project_orchestration("proj-1", "user-1", program_type="SMART") | |
| assert result["gsd_mode"] is True | |
| mock.assert_called_once() | |
| def test_endpoint_patch_registry_lists_patches(): | |
| from endpoints.patches.registry import ENDPOINT_PATCH_REGISTRY, list_endpoint_patches | |
| assert len(ENDPOINT_PATCH_REGISTRY) >= 4 | |
| flags = {p["flag"] for p in list_endpoint_patches()} | |
| assert "ENABLE_PROJECTS_MATCH_PATCH" in flags | |