CascadingDev/tests/test_patcher.py

76 lines
2.5 KiB
Python

import subprocess
from pathlib import Path
import pytest
from automation.config import RulesConfig
from automation.ai_config import DEFAULT_SENTINEL
from automation.patcher import (
ModelConfig,
PatchGenerationError,
generate_output,
_ensure_codex_json,
)
@pytest.fixture()
def temp_repo(tmp_path: Path) -> Path:
repo = tmp_path / "repo"
repo.mkdir()
run(["git", "init"], cwd=repo)
run(["git", "config", "user.email", "dev@example.com"], cwd=repo)
run(["git", "config", "user.name", "Dev"], cwd=repo)
return repo
def run(args: list[str], cwd: Path) -> None:
subprocess.run(args, cwd=cwd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
def test_generate_output_creates_new_file(temp_repo: Path, tmp_path: Path) -> None:
src = temp_repo / "Docs/features/FR_1/discussions/example.discussion.md"
src.parent.mkdir(parents=True, exist_ok=True)
src.write_text("- Alice: initial note\n", encoding="utf-8")
run(["git", "add", src.relative_to(temp_repo).as_posix()], cwd=temp_repo)
patch_text = """<<<AI_DIFF_START>>>
diff --git a/Docs/features/FR_1/discussions/example.discussion.sum.md b/Docs/features/FR_1/discussions/example.discussion.sum.md
--- /dev/null
+++ b/Docs/features/FR_1/discussions/example.discussion.sum.md
@@ -0,0 +1,2 @@
+Line one
+Line two
<<<AI_DIFF_END>>>
"""
patch_file = tmp_path / "patch.txt"
patch_file.write_text(patch_text, encoding="utf-8")
model = ModelConfig(
commands=[f"bash -lc 'cat {patch_file.as_posix()}'"],
sentinel=DEFAULT_SENTINEL,
)
rules = RulesConfig(root=temp_repo, global_rules={"file_associations": {}, "rules": {}})
generate_output(
repo_root=temp_repo,
rules=rules,
model=model,
source_rel=Path("Docs/features/FR_1/discussions/example.discussion.md"),
output_rel=Path("Docs/features/FR_1/discussions/example.discussion.sum.md"),
instruction="Generate summary",
)
output_file = temp_repo / "Docs/features/FR_1/discussions/example.discussion.sum.md"
assert output_file.exists()
assert output_file.read_text(encoding="utf-8") == "Line one\nLine two\n"
def test_ensure_codex_json_adds_exec_and_stdin() -> None:
cmd = "codex --model gpt-5"
rewritten = _ensure_codex_json(cmd)
# Should include exec, --json, --color=never, and '-' for stdin
assert "codex exec" in rewritten
assert "--json" in rewritten
assert "--color=never" in rewritten
assert rewritten.rstrip().endswith(" -") or " - " in rewritten