import json
import os

from scripts.mindmodel import validator


def write_manifest(path, data: str):
    p = path
    p.write_text(data, encoding="utf-8")
    return str(p)


def test_validate_ok(tmp_path):
    # manifest with one constraint and evidence pointing to an existing file
    evidence_file = tmp_path / "file.txt"
    evidence_file.write_text("hello")

    manifest = {
        "constraints": [
            {"id": "c1", "evidence": [{"file": "file.txt", "text": "complete content"}]}
        ]
    }

    manifest_path = tmp_path / "manifest.json"
    manifest_path.write_text(json.dumps(manifest))

    code, report = validator.validate_manifest(
        str(manifest_path), base_dir=str(tmp_path)
    )
    assert code == 0
    assert report["missing_files"] == []
    assert report["secrets"] == []


def test_missing_file_flags_failure(tmp_path):
    # manifest refers to missing file
    manifest = {
        "constraints": [{"id": "c2", "evidence": [{"file": "nope.txt", "text": "foo"}]}]
    }
    manifest_path = tmp_path / "manifest.json"
    manifest_path.write_text(json.dumps(manifest))

    code, report = validator.validate_manifest(
        str(manifest_path), base_dir=str(tmp_path)
    )
    assert code == 2
    assert "nope.txt" in report["missing_files"]


def test_truncated_produces_warning(tmp_path):
    # evidence text is truncated -> warning
    f = tmp_path / "manifest.json"
    manifest = {
        "constraints": [{"id": "c3", "evidence": [{"text": "This is truncated..."}]}]
    }
    f.write_text(json.dumps(manifest))

    code, report = validator.validate_manifest(str(f), base_dir=str(tmp_path))
    assert code == 1
    assert report["truncated"] >= 1


def test_manifest_scanned_for_secrets(tmp_path):
    # manifest text contains an api_key pattern
    f = tmp_path / "manifest.json"
    f.write_text('api_key = "secretVALUE1234"')

    code, report = validator.validate_manifest(str(f), base_dir=str(tmp_path))
    assert code == 2
    assert any("secretVALUE1234" in s for s in report["secrets"]) or report["secrets"]
