import os
import tempfile
from pathlib import Path

import pytest

from src.validators.mindmodel_validator import validate_manifest


def _write_temp_manifest(contents: str) -> str:
    fd, path = tempfile.mkstemp(prefix="manifest_", suffix=".yaml")
    os.close(fd)
    with open(path, "w", encoding="utf-8") as f:
        f.write(contents)
    return path


def test_validator_reports_missing_file(tmp_path):
    # manifest referencing a non-existent file
    missing = str(tmp_path / "no_such_file.txt")
    manifest = f"""
files:
  - path: {missing}
"""
    mpath = _write_temp_manifest(manifest)
    try:
        report = validate_manifest(mpath)
        assert "missing_files" in report
        assert missing in report["missing_files"]
    finally:
        Path(mpath).unlink()


def test_validator_detects_potential_secret(tmp_path):
    # manifest with evidence_excerpt containing PASSWORD
    evidence = "This shows a PASSWORD=hunter2 in the output"
    manifest = f'files:\n  - path: some_file.txt\n    evidence_excerpt: "{evidence}"\n'
    mpath = _write_temp_manifest(manifest)
    try:
        report = validate_manifest(mpath)
        assert "potential_secrets" in report
        items = report["potential_secrets"]
        assert any(evidence in (item.get("evidence_excerpt") or "") for item in items)
    finally:
        Path(mpath).unlink()
