File size: 3,418 Bytes
339ddd8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | """Tests for PythonWorkspaceSession transactional editing, verification, and rollback."""
from __future__ import annotations
from pathlib import Path
from lgpython.session import PythonWorkspaceSession
def test_verify_clean_workspace_reaches_verified_assurance_3(sample_pkg: Path) -> None:
session = PythonWorkspaceSession(sample_pkg)
res = session.verify("src/mathops.py")
assert res.verdict == "VERIFIED"
assert res.assurance == 3
assert res.committed is False
def test_edit_file_commits_only_when_verified(sample_pkg: Path) -> None:
session = PythonWorkspaceSession(sample_pkg)
res = session.edit_file(
"src/mathops.py",
"return left + right",
"return int(left) + int(right)",
commit=True,
)
assert res.verdict == "VERIFIED"
assert res.assurance == 3
assert res.committed is True
assert "return int(left) + int(right)" in (sample_pkg / "src" / "mathops.py").read_text(
encoding="utf-8"
)
def test_edit_file_does_not_touch_disk_when_tests_fail(sample_pkg: Path) -> None:
session = PythonWorkspaceSession(sample_pkg)
before = (sample_pkg / "src" / "mathops.py").read_text(encoding="utf-8")
res = session.edit_file(
"src/mathops.py",
"return left + right",
"return left - right",
commit=True,
)
after = (sample_pkg / "src" / "mathops.py").read_text(encoding="utf-8")
assert res.verdict == "UNKNOWN"
assert res.reason == "GATE_REJECTED"
assert res.committed is False
assert before == after
def test_edit_file_refuses_protected_and_traversal_paths(sample_pkg: Path) -> None:
(sample_pkg / ".env").write_text("KEY=1\n", encoding="utf-8")
session = PythonWorkspaceSession(sample_pkg)
env_res = session.edit_file(".env", "KEY=1", "KEY=2", commit=True)
assert env_res.verdict == "UNKNOWN"
assert env_res.reason == "PROTECTED_PATH"
assert env_res.committed is False
assert (sample_pkg / ".env").read_text(encoding="utf-8") == "KEY=1\n"
esc_res = session.edit_file("../outside.py", "a = 1", "a = 2", commit=True)
assert esc_res.verdict == "UNKNOWN"
assert esc_res.reason in ("PROTECTED_PATH", "NOT_APPLIED")
assert esc_res.committed is False
def test_create_and_delete_file_transactions(sample_pkg: Path) -> None:
session = PythonWorkspaceSession(sample_pkg)
created = session.create_file(
"src/helper.py",
'"""Extra module."""\n\n\ndef square(x: int) -> int:\n return x * x\n',
commit=True,
)
assert created.verdict == "VERIFIED"
assert created.committed is True
assert (sample_pkg / "src" / "helper.py").is_file()
deleted = session.delete_file("src/helper.py", commit=True)
assert deleted.verdict == "VERIFIED"
assert deleted.committed is True
assert not (sample_pkg / "src" / "helper.py").exists()
def test_retry_loop_stops_on_identical_failure_stall(sample_pkg: Path) -> None:
session = PythonWorkspaceSession(sample_pkg)
def repeated_bad_edit(attempt: int, last_result: object) -> tuple[str, str]:
return ("return left + right", "return left - right")
final, attempts = session.run_retry_loop("src/mathops.py", repeated_bad_edit, max_attempts=5)
assert final.verdict == "UNKNOWN"
assert len(attempts) == 2
assert any(c.get("role") == "adversary_loop" for c in final.dossier.get("checkers", []))
|