File size: 6,205 Bytes
afd56bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import pytest
from httpx import AsyncClient
from unittest.mock import patch, AsyncMock, MagicMock


@pytest.mark.asyncio
async def test_match_program_success(async_client: AsyncClient, auth_headers):
    with patch("endpoints.projects.get_llm") as mock_get_llm, patch(
        "core.ncbr_client.ncbr_client.get_active_nabory", new_callable=AsyncMock
    ) as mock_ncbr, patch(
        "core.parp_client.parp_client.get_active_nabory", new_callable=AsyncMock
    ) as mock_parp:
        mock_ncbr.return_value = []
        mock_parp.return_value = []

        mock_llm = MagicMock()
        mock_structured_llm = MagicMock()

        # Build fake programs list
        class DummyExplanation:
            def dict(self):
                return {"reason": "It matches", "criteria": ["R&D"], "risks": "None"}

        class DummyProgram:
            def __init__(self):
                self.id = 1
                self.name = "FENG.01.01-IP.02-001/23"
                self.type = "smart"
                self.match = 95
                self.chance = 80
                self.amount = "10-50 mln PLN"
                self.shortDesc = "Ścieżka SMART"
                self.fullDesc = "Full description"
                self.explanation = DummyExplanation()
                self.criteria = ["R&D"]
                self.risks = "None"

            def dict(self):
                return {
                    "id": self.id,
                    "name": self.name,
                    "type": self.type,
                    "match": self.match,
                    "chance": self.chance,
                    "amount": self.amount,
                    "shortDesc": self.shortDesc,
                    "fullDesc": self.fullDesc,
                    "explanation": self.explanation.dict(),
                    "criteria": self.criteria,
                    "risks": self.risks,
                }

        program_mock = DummyProgram()

        mock_output = MagicMock()
        mock_output.programs = [program_mock]
        mock_output.clarifying_questions = ["What is the TRL level?"]
        mock_output.model_dump.return_value = {
            "programs": [
                {
                    "id": 1,
                    "name": "FENG.01.01-IP.02-001/23",
                    "type": "smart",
                    "match": 95,
                    "chance": 80,
                    "amount": "10-50 mln PLN",
                    "shortDesc": "Ścieżka SMART",
                    "fullDesc": "Full description",
                    "explanation": {
                        "reason": "It matches",
                        "criteria": ["R&D"],
                        "risks": "None",
                    },
                    "criteria": ["R&D"],
                    "risks": "None",
                }
            ],
            "clarifying_questions": ["What is the TRL level?"],
        }

        mock_structured_llm.invoke.return_value = mock_output
        mock_structured_llm.return_value = mock_output
        mock_get_llm.return_value = mock_structured_llm

        payload = {
            "description": "We are building an AI software.",
            "company_type": "sme",
            "company_size": "small",
            "voivodeship": "mazowieckie",
            "innovation_type": "product",
        }

        response = await async_client.post(
            "/api/projects/match-program", json=payload, headers=auth_headers
        )

        assert response.status_code == 200
        data = response.json()
        assert "programs" in data
        assert "clarifying_questions" in data
        assert len(data["programs"]) == 1
        assert "What is the TRL level?" in data["clarifying_questions"]


@pytest.mark.asyncio
async def test_match_program_empty_description(async_client: AsyncClient, auth_headers):
    payload = {
        "description": "",
    }

    with patch("endpoints.projects.get_llm") as mock_get_llm, patch(
        "core.ncbr_client.ncbr_client.get_active_nabory", new_callable=AsyncMock
    ) as mock_ncbr, patch(
        "core.parp_client.parp_client.get_active_nabory", new_callable=AsyncMock
    ) as mock_parp:
        mock_ncbr.return_value = []
        mock_parp.return_value = []

        mock_llm = MagicMock()
        mock_structured_llm = MagicMock()

        mock_output = MagicMock()
        mock_output.programs = []
        mock_output.clarifying_questions = ["Proszę opisać swój projekt."]
        mock_output.model_dump.return_value = {
            "programs": [],
            "clarifying_questions": ["Proszę opisać swój projekt."],
        }

        mock_structured_llm.invoke.return_value = mock_output
        mock_structured_llm.return_value = mock_output
        mock_get_llm.return_value = mock_structured_llm

        response = await async_client.post(
            "/api/projects/match-program", json=payload, headers=auth_headers
        )

        assert response.status_code == 200
        data = response.json()
        assert "programs" in data
        assert "clarifying_questions" in data


@pytest.mark.asyncio
async def test_match_program_llm_fallback(async_client: AsyncClient, auth_headers):
    payload = {
        "description": "Fallback test.",
    }

    with patch("endpoints.projects.get_llm") as mock_get_llm, patch(
        "core.ncbr_client.ncbr_client.get_active_nabory", new_callable=AsyncMock
    ) as mock_ncbr, patch(
        "core.parp_client.parp_client.get_active_nabory", new_callable=AsyncMock
    ) as mock_parp:
        mock_ncbr.return_value = []
        mock_parp.return_value = []

        mock_llm = MagicMock()
        mock_structured_llm = MagicMock()
        mock_structured_llm.invoke.side_effect = Exception("LLM Error")
        mock_structured_llm.side_effect = Exception("LLM Error")
        mock_get_llm.return_value = mock_structured_llm

        response = await async_client.post(
            "/api/projects/match-program", json=payload, headers=auth_headers
        )

        assert response.status_code == 200
        data = response.json()
        assert "programs" in data
        assert len(data["programs"]) == 3
        assert data["programs"][0]["name"] == "Ścieżka SMART (FENG)"