"""Tests for the SAST scanning components."""

from pathlib import Path

from web_sentinel.checks.source_code.scanner import evaluate_source_code
from web_sentinel.model import ScanRequest


def test_source_scanner_detects_hardcoded_secret(tmp_path):
    """Ensure the scanner flags obvious hardcoded credentials."""
    from web_sentinel.localization import set_language
    set_language("en")  # Force English for test assertions
    
    project_dir = tmp_path / "project"
    project_dir.mkdir()
    (project_dir / "app.py").write_text("password = 'realpassword123'\n", encoding="utf-8")

    request = ScanRequest(domain="example.com", source_path=str(project_dir))
    findings = list(evaluate_source_code(request))
        
    assert any("Hardcoded secrets" in finding.title for finding in findings)


def test_evaluate_source_code_requires_source_path():
    """evaluate_source_code should guide the user when no source path is provided."""
    request = ScanRequest(domain="example.com")
    findings = list(evaluate_source_code(request))

    assert len(findings) == 1
    finding = findings[0]
    assert finding.severity == "info"
    assert "not configured" in finding.title.lower()


def test_source_scanner_obeys_file_limit(tmp_path):
    """Respect max_files licensing constraint."""
    from web_sentinel.localization import set_language
    set_language("en")  # Force English for test assertions
    
    for index in range(3):
        (tmp_path / f"file{index}.py").write_text(
            f"password = 'mypassword{index}123'\n", encoding="utf-8"
        )

    request = ScanRequest(domain="example.com", source_path=str(tmp_path), source_max_files=1)
    findings = list(evaluate_source_code(request))

    # We expect at least the limit warning finding
    assert any("File limit exceeded" in finding.title for finding in findings)
