"""Pull-flow client: parse the scanner's list_findings result + safe no-config path."""
import asyncio

from compliance.services.scanner_mcp_client import fetch_findings, parse_findings_text


def test_parse_plain_array():
    out = parse_findings_text('[{"_id":"a","title":"x"},{"_id":"b"}]')
    assert len(out) == 2
    assert out[0]["_id"] == "a"


def test_parse_wrapped_findings_key():
    assert parse_findings_text('{"findings":[{"_id":"a"}]}') == [{"_id": "a"}]


def test_parse_wrapped_results_key():
    assert parse_findings_text('{"results":[{"_id":"a"}]}') == [{"_id": "a"}]


def test_parse_garbage_returns_empty():
    assert parse_findings_text("not json") == []
    assert parse_findings_text("") == []
    assert parse_findings_text('{"x":1}') == []


def test_fetch_findings_no_url_returns_empty():
    # Unconfigured + no override -> [] (no MCP lib needed; transport is lazy-imported).
    assert asyncio.run(fetch_findings(base_url="")) == []
