Files
aios/tests/test_projection_recovery_patcher.py
T

47 lines
3.2 KiB
Python

from __future__ import annotations
from pathlib import Path
import subprocess
import sys
FIXTURE_SOURCE = '"""x"""\nimport re\nfrom aios_online_agent_execution import (\n GovernedExecutionBinding,\n validate_execution_envelope,\n)\n\nclass InitialExecutionProjectionHandler:\n store = None\n def _authorized(self):\n return True\n def _json(self, value, status):\n pass\n def do_GET(self):\n if self.path == "/health":\n self._json(\n {"status": "ok", "service": "aios-agent-task-initial-execution-projection"},\n 200,\n )\n else:\n self._json({"error": "NOT_FOUND"}, 404)\n\n def do_POST(self):\n initial_match = re.fullmatch(\n r"/v1/tasks/([^/]+)/initial-execution-projection", self.path\n )\n recovery_match = re.fullmatch(\n r"/v1/tasks/([^/]+)/source-capture-projection-recovery", self.path\n )\n if initial_match is None and recovery_match is None:\n self._json({"error": "NOT_FOUND"}, 404)\n return\n if not self._authorized():\n self._json({"error": "UNAUTHORIZED"}, 401)\n return\n try:\n value = {}\n if self.store is None:\n raise RuntimeError("RUNTIME_UNAVAILABLE")\n if initial_match is not None:\n request = InitialExecutionProjectionRequest.from_mapping(value)\n result = self.store.project(initial_match.group(1), request)\n else:\n recovery_request = SourceCaptureProjectionRecoveryRequest.from_mapping(value)\n result = self.store.project_from_source_capture(\n recovery_match.group(1), recovery_request\n )\n self._json(result, 200 if result["idempotent"] else 201)\n except Exception:\n pass\n'
def test_patcher_is_strict_and_idempotent(tmp_path):
source = tmp_path / "aios_agent_task_initial_execution_projection.py"
source.write_text(FIXTURE_SOURCE, encoding="utf-8")
patcher = Path(__file__).resolve().parents[1] / "tools" / "integrate_projection_recovery.py"
first = subprocess.run(
[sys.executable, str(patcher), str(source)],
capture_output=True,
text=True,
)
assert first.returncode == 0, first.stderr
assert first.stdout.strip() == "CHANGED"
text = source.read_text()
assert "/canonical-projection-recovery" in text
assert "/canonical-projection" in text
assert "recover_canonical_projection" in text
second = subprocess.run(
[sys.executable, str(patcher), str(source)],
capture_output=True,
text=True,
)
assert second.returncode == 0
assert second.stdout.strip() == "ALREADY_APPLIED"
def test_patcher_fails_closed_on_unexpected_source(tmp_path):
source = tmp_path / "wrong.py"
source.write_text("print('different')\n")
patcher = Path(__file__).resolve().parents[1] / "tools" / "integrate_projection_recovery.py"
result = subprocess.run(
[sys.executable, str(patcher), str(source)],
capture_output=True,
text=True,
)
assert result.returncode == 2
assert "ANCHOR_COUNT" in result.stderr