from pathlib import Path from afk_developer.config import LintMode from afk_developer.scheduler.lint_baseline import ( compare_ruff_baseline, capture_ruff_baseline, load_baseline, save_baseline, ) from afk_developer.scheduler.reviewer import _run_linter def test_baseline_mode_accepts_unchanged_ruff_debt(tmp_path: Path) -> None: (tmp_path / "legacy.py").write_text("import os\n", encoding="utf-8") (tmp_path / "changed.py").write_text("value = 1\n", encoding="utf-8") baseline = capture_ruff_baseline(tmp_path, "example") result = _run_linter( tmp_path, "ruff", [Path("changed.py")], LintMode.BASELINE, baseline, ) assert result.passed assert result.local_passed assert result.baseline_loaded assert result.baseline_diagnostic_count == 1 assert result.introduced_diagnostic_count == 0 def test_baseline_mode_rejects_introduced_ruff_diagnostics(tmp_path: Path) -> None: (tmp_path / "legacy.py").write_text("import os\n", encoding="utf-8") baseline = capture_ruff_baseline(tmp_path, "example") (tmp_path / "changed.py").write_text("import sys\n", encoding="utf-8") result = _run_linter( tmp_path, "ruff", [Path("changed.py")], LintMode.BASELINE, baseline, ) assert not result.passed assert not result.local_passed assert result.baseline_diagnostic_count == 1 assert result.introduced_diagnostic_count == 1 assert "F401" in result.output def test_baseline_fingerprint_tolerates_an_unrelated_line_shift(tmp_path: Path) -> None: (tmp_path / "legacy.py").write_text("import os\n", encoding="utf-8") (tmp_path / "changed.py").write_text("value = 1\n", encoding="utf-8") baseline = capture_ruff_baseline(tmp_path, "example") (tmp_path / "legacy.py").write_text("\n\nimport os\n", encoding="utf-8") result = _run_linter( tmp_path, "ruff", [Path("changed.py")], LintMode.BASELINE, baseline, ) assert result.passed assert result.introduced_diagnostic_count == 0 def test_baseline_round_trip_preserves_metadata(tmp_path: Path) -> None: (tmp_path / "legacy.py").write_text("import os\n", encoding="utf-8") baseline = capture_ruff_baseline(tmp_path, "example") path = tmp_path / "state" / "lint-baseline.json" save_baseline(path, baseline) loaded = load_baseline(path) assert loaded == baseline def test_read_only_baseline_comparison_reports_zero_introduced_diagnostics(tmp_path: Path) -> None: (tmp_path / "legacy.py").write_text("import os\n", encoding="utf-8") baseline = capture_ruff_baseline(tmp_path, "example") path = tmp_path / "state" / "lint-baseline.json" save_baseline(path, baseline) result = compare_ruff_baseline(path, tmp_path, "example") assert "Baseline diagnostics: 1" in result assert "Introduced diagnostics: 0" in result