import json

from project_brain.build.parser_audit import audit_project, load_expectations


def test_parser_audit_reports_coverage_facts_and_manifest_recall(tmp_path):
    (tmp_path / "model.py").write_text(
        "class User:\n"
        "    name = ''\n"
        "    def save(self): pass\n"
        "def run(user: User):\n"
        "    user.save()\n",
        encoding="utf-8",
    )
    expected = [{
        "name": "run", "kind": "function", "file": "model.py",
        "line_start": 4, "is_definition": True, "language": "python",
    }]

    report = audit_project(
        tmp_path, languages=["python"], expectations=expected,
        include_inventory=True,
    ).to_dict()

    assert report["schema_version"] == 1
    assert report["files"]["total"] == 1
    assert report["files"]["by_status"] == {"ok": 1}
    assert report["files"]["weighted_coverage"] == 1.0
    assert report["nodes"]["functions"] == 2
    assert report["relation_facts"]["reference_call"] == 1
    assert report["expectations"]["recall"] == 1.0
    assert any(item["name"] == "run" for item in report["inventory"])


def test_parser_audit_reports_missing_occurrences_and_loads_object_manifest(tmp_path):
    manifest = tmp_path / "expected.json"
    manifest.write_text(json.dumps({
        "occurrences": [{"name": "missing", "kind": "function"}],
    }), encoding="utf-8")

    expectations = load_expectations(manifest)
    report = audit_project(tmp_path, expectations=expectations).to_dict()

    assert report["expectations"]["recall"] == 0.0
    assert report["expectations"]["missing"] == expectations