File size: 1,340 Bytes
9237011
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from app.schemas import Severity
from app.services.json_parser import parse_agent_output, parse_json_object


def test_parse_json_object_accepts_fenced_json():
    data = parse_json_object('```json\n{"findings": []}\n```')

    assert data == {"findings": []}


def test_parse_json_object_extracts_object_from_extra_text():
    data = parse_json_object('Here is JSON: {"findings": []} done.')

    assert data == {"findings": []}


def test_parse_agent_output_returns_empty_output_for_invalid_json():
    output = parse_agent_output("not json", "Security Agent")

    assert output.findings == []
    assert output.metadata["parse_error"] is True


def test_parse_agent_output_validates_findings():
    output = parse_agent_output(
        {
            "findings": [
                {
                    "title": "Unsafe eval",
                    "severity": "HIGH",
                    "file_path": "app.py",
                    "line_start": 1,
                    "line_end": 1,
                    "description": "eval is used",
                    "why_it_matters": "Arbitrary code execution",
                    "suggested_fix": "Remove eval",
                    "agent_source": "Security Agent",
                }
            ]
        },
        "Security Agent",
    )

    assert output.findings[0].severity == Severity.high