File size: 12,144 Bytes
f440f03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
"""Tests koda ģenerēšanai."""

import sys
from pathlib import Path
from typing import Any
from unittest.mock import AsyncMock, patch

import pytest
from fastapi import HTTPException

from maris_core.code.generate_code import (
    CodeRequest,
    FixCodeRequest,
    ProjectFile,
    _detect_stack,
    _extract_code_block,
    _extract_project_files,
    fix_code,
    generate_code,
)


def test_extract_code_block_with_fences() -> None:
    text = "Šeit ir kods:\n```\nprint('hello')\n```\nPaskaidrojums."
    code, explanation = _extract_code_block(text, "Python")
    assert "print" in code
    assert "Paskaidrojums" in explanation


def test_extract_code_block_no_fences() -> None:
    text = "print('hello world')"
    code, explanation = _extract_code_block(text, "Python")
    assert "print" in code


def test_extract_project_files_from_structured_json_payload() -> None:
    text = """```json
{
  "explanation": "Pilns kalkulatora projekts.",
  "entrypoint": "index.html",
  "files": [
    {"path": "index.html", "content": "<html></html>"},
    {"path": "assets/app.js", "content": "console.log('ok');"}
  ]
}
```"""

    files, entrypoint, explanation = _extract_project_files(text, language="HTML/CSS/JavaScript")

    assert files == [
        ProjectFile(path="index.html", content="<html></html>", absolute_path=None),
        ProjectFile(path="assets/app.js", content="console.log('ok');", absolute_path=None),
    ]
    assert entrypoint == "index.html"
    assert explanation == "Pilns kalkulatora projekts."


def test_detect_stack_prefers_nextjs_from_prompt() -> None:
    detected = _detect_stack(
        "Uztaisi Next.js dashboard ar app router un TypeScript",
        "Python",
        None,
    )

    assert detected == "nextjs"


@pytest.mark.asyncio
async def test_generate_code_requires_text_model() -> None:
    with (
        patch("maris_core.code.generate_code.get_pipeline", return_value=None),
        patch(
            "maris_core.utils.hf_integration.HFIntegration.save_generation",
            new_callable=AsyncMock,
        ),
    ):
        req = CodeRequest(prompt="Hello world skripts", language="Python")
        with pytest.raises(HTTPException) as exc_info:
            await generate_code(req)
        assert exc_info.value.status_code == 503


@pytest.mark.asyncio
async def test_generate_code_uses_requested_hf_fallback_model_when_text_runtime_is_unavailable() -> (
    None
):
    class FakeClient:
        def chat_completion(
            self,
            *,
            model: str,
            messages: list[dict[str, str]],
            max_tokens: int,
            temperature: float,
        ) -> dict[str, Any]:
            del messages, max_tokens, temperature
            assert model == "Qwen/Qwen2.5-Coder-32B-Instruct"
            return {
                "choices": [
                    {
                        "message": {
                            "content": "```python\ndef normalize_email(value: str) -> str:\n    return value.strip().lower()\n```"
                        }
                    }
                ]
            }

    fake_hf_module = type("FakeHFModule", (), {"InferenceClient": object})()
    fake_hf_utils = type("FakeHFUtils", (), {"HfHubHTTPError": RuntimeError})()

    with (
        patch("maris_core.code.generate_code.get_pipeline", return_value=None),
        patch("maris_core.text.generate.create_hf_inference_client", return_value=FakeClient()),
        patch.dict(
            sys.modules, {"huggingface_hub": fake_hf_module, "huggingface_hub.utils": fake_hf_utils}
        ),
        patch(
            "maris_core.utils.hf_integration.HFIntegration.save_generation",
            new_callable=AsyncMock,
        ),
    ):
        response = await generate_code(
            CodeRequest(
                prompt="Uzraksti Python helperi normalize_email",
                language="Python",
                fallback_model="Qwen/Qwen2.5-Coder-32B-Instruct",
            )
        )

    assert "normalize_email" in response.code


@pytest.mark.asyncio
async def test_fix_code_delegates() -> None:
    with (
        patch("maris_core.code.generate_code.get_pipeline", return_value=None),
        patch(
            "maris_core.utils.hf_integration.HFIntegration.save_generation",
            new_callable=AsyncMock,
        ),
    ):
        req = FixCodeRequest(code="prit('hello')", error_message="NameError", language="Python")
        with pytest.raises(HTTPException) as exc_info:
            await fix_code(req)
        assert exc_info.value.status_code == 503


@pytest.mark.asyncio
async def test_generate_code_passes_large_max_new_tokens_to_pipeline() -> None:
    captured_max_new_tokens: int | None = None

    def fake_pipeline(
        messages: list[dict[str, Any]], *, max_new_tokens: int, temperature: float
    ) -> list[dict[str, list[dict[str, str]]]]:
        nonlocal captured_max_new_tokens
        del messages, temperature
        captured_max_new_tokens = max_new_tokens
        return [{"generated_text": [{"role": "assistant", "content": "print('hello world')"}]}]

    with (
        patch("maris_core.code.generate_code.get_pipeline", return_value=fake_pipeline),
        patch(
            "maris_core.utils.hf_integration.HFIntegration.save_generation",
            new_callable=AsyncMock,
        ),
    ):
        response = await generate_code(
            CodeRequest(
                prompt="Uzraksti pilnu Python servisu",
                language="Python",
                max_new_tokens=20_000,
            )
        )

    assert captured_max_new_tokens == 20_000
    assert response.code == "print('hello world')"


@pytest.mark.asyncio
async def test_generate_code_uses_stronger_engineering_system_prompt() -> None:
    captured_messages: list[dict[str, Any]] = []

    def fake_pipeline(
        messages: list[dict[str, Any]], *, max_new_tokens: int, temperature: float
    ) -> list[dict[str, list[dict[str, str]]]]:
        nonlocal captured_messages
        del max_new_tokens, temperature
        captured_messages = messages
        return [{"generated_text": [{"role": "assistant", "content": "print('hello world')"}]}]

    with (
        patch("maris_core.code.generate_code.get_pipeline", return_value=fake_pipeline),
        patch(
            "maris_core.utils.hf_integration.HFIntegration.save_generation",
            new_callable=AsyncMock,
        ),
    ):
        await generate_code(CodeRequest(prompt="Uzraksti Python skriptu", language="Python"))

    assert "production-ready" in captured_messages[0]["content"]
    assert "edge cases" in captured_messages[0]["content"]
    assert "izpildāmu artefaktu" in captured_messages[0]["content"]


@pytest.mark.asyncio
async def test_generate_code_materializes_workspace_artifacts_from_structured_payload(
    tmp_path,
) -> None:
    def fake_pipeline(
        messages: list[dict[str, Any]], *, max_new_tokens: int, temperature: float
    ) -> list[dict[str, list[dict[str, str]]]]:
        del messages, max_new_tokens, temperature
        return [
            {
                "generated_text": [
                    {
                        "role": "assistant",
                        "content": """```json
{
  "explanation": "Pilns kalkulatora projekts.",
  "entrypoint": "index.html",
  "files": [
    {"path": "index.html", "content": "<!doctype html><title>Kalkulators</title>"},
    {"path": "assets/app.js", "content": "console.log('calc');"}
  ]
}
```""",
                    }
                ]
            }
        ]

    with (
        patch("maris_core.code.generate_code.get_pipeline", return_value=fake_pipeline),
        patch("maris_core.code.generate_code.WORKSPACE_ARTIFACT_ROOT", tmp_path),
        patch(
            "maris_core.utils.hf_integration.HFIntegration.save_generation",
            new_callable=AsyncMock,
        ),
    ):
        response = await generate_code(
            CodeRequest(prompt="Uzprogrammē kalkulatora projektu", language="HTML/CSS/JavaScript")
        )

    bundle_path = Path(response.bundle_path or "")
    assert response.entrypoint == "index.html"
    assert response.workspace_artifact_dir is not None
    assert response.detected_stack == "HTML/CSS/JavaScript"
    assert Path(response.workspace_artifact_dir).exists()
    assert [file.path for file in response.files] == ["index.html", "assets/app.js"]
    assert response.files[0].absolute_path is not None
    assert bundle_path.exists()
    assert (
        Path(response.files[0].absolute_path or "")
        .read_text(encoding="utf-8")
        .startswith("<!doctype html>")
    )


@pytest.mark.asyncio
async def test_generate_code_auto_scaffolds_nextjs_project_from_single_code_block(tmp_path) -> None:
    def fake_pipeline(
        messages: list[dict[str, Any]], *, max_new_tokens: int, temperature: float
    ) -> list[dict[str, list[dict[str, str]]]]:
        del messages, max_new_tokens, temperature
        return [
            {
                "generated_text": [
                    {
                        "role": "assistant",
                        "content": """```tsx
export default function HomePage() {
  return <main>Analytics dashboard</main>;
}
```""",
                    }
                ]
            }
        ]

    with (
        patch("maris_core.code.generate_code.get_pipeline", return_value=fake_pipeline),
        patch("maris_core.code.generate_code.WORKSPACE_ARTIFACT_ROOT", tmp_path),
        patch(
            "maris_core.utils.hf_integration.HFIntegration.save_generation",
            new_callable=AsyncMock,
        ),
    ):
        response = await generate_code(
            CodeRequest(
                prompt="Uztaisi Next.js app router dashboard ar TypeScript",
                language="Python",
            )
        )

    paths = [file.path for file in response.files]
    assert response.detected_stack == "Next.js (TypeScript)"
    assert response.language == "Next.js (TypeScript)"
    assert response.entrypoint == "app/page.tsx"
    assert "app/page.tsx" in paths
    assert "package.json" in paths
    assert Path(response.bundle_path or "").exists()


@pytest.mark.asyncio
async def test_generate_code_uses_repo_aware_entrypoint_for_existing_project(tmp_path) -> None:
    project_root = tmp_path / "existing-project"
    (project_root / "src").mkdir(parents=True)
    (project_root / "pyproject.toml").write_text(
        "[project]\nname = 'demo'\nversion = '0.1.0'\n",
        encoding="utf-8",
    )
    (project_root / "src/main.py").write_text(
        "def main() -> None:\n    print('old')\n", encoding="utf-8"
    )

    captured_messages: list[dict[str, Any]] = []

    def fake_pipeline(
        messages: list[dict[str, Any]], *, max_new_tokens: int, temperature: float
    ) -> list[dict[str, list[dict[str, str]]]]:
        nonlocal captured_messages
        del max_new_tokens, temperature
        captured_messages = messages
        return [
            {
                "generated_text": [
                    {
                        "role": "assistant",
                        "content": """```python
def main() -> None:
    print('updated')
```""",
                    }
                ]
            }
        ]

    with (
        patch("maris_core.code.generate_code.get_pipeline", return_value=fake_pipeline),
        patch("maris_core.code.generate_code.WORKSPACE_ARTIFACT_ROOT", tmp_path / "artifacts"),
        patch(
            "maris_core.utils.hf_integration.HFIntegration.save_generation",
            new_callable=AsyncMock,
        ),
    ):
        response = await generate_code(
            CodeRequest(
                prompt="Salabo esošo servisu un atjauno starta loģiku",
                language="Python",
                repo_path=str(project_root),
            )
        )

    assert response.repo_path == str(project_root)
    assert response.detected_stack == "Python"
    assert response.entrypoint == "src/main.py"
    assert response.files[0].path == "src/main.py"
    assert "Repo sakne" in captured_messages[1]["content"]
    assert "src/main.py" in captured_messages[1]["content"]