diff --git a/hc_agent.py b/hc_agent.py new file mode 100644 index 0000000000..ae34feba54 --- /dev/null +++ b/hc_agent.py @@ -0,0 +1,40 @@ +import io +import os +import uuid + +import requests + + +def preview(payload): + return {"mode": "preview", "payload": payload} + + +def apply(payload, request_fn=None): + if request_fn is None: + request_fn = requests.request + return request_fn("POST", "http://hc-agent/apply", json=payload) + + +class RunStore: + def __init__(self, root): + self.root = root + + def run_dir(self, run_id): + return os.path.join(self.root, "runs", run_id) + + def write_report(self, run_id, content): + run_dir = self.run_dir(run_id) + os.makedirs(run_dir, exist_ok=True) + report_path = os.path.join(run_dir, "report.md") + with io.open(report_path, "w", encoding="utf-8") as handle: + handle.write(content) + return report_path + + +def gateway_run(payload, runstore_root, run_id=None, report_contents=None): + del payload + run_id = run_id or str(uuid.uuid4()) + report_contents = report_contents or "hc-agent report" + runstore = RunStore(runstore_root) + runstore.write_report(run_id, report_contents) + return run_id diff --git a/tests/test_hc_agent_smoke.py b/tests/test_hc_agent_smoke.py new file mode 100644 index 0000000000..8b5e76abcd --- /dev/null +++ b/tests/test_hc_agent_smoke.py @@ -0,0 +1,38 @@ +from unittest import mock + +import hc_agent + + +class FakeResponse: + def __init__(self, status_code=200, json_data=None): + self.status_code = status_code + self._json_data = json_data or {} + + def json(self): + return self._json_data + + +def test_preview_does_not_call_requests(monkeypatch): + def raise_if_called(*args, **kwargs): + raise AssertionError("should not call") + + monkeypatch.setattr(hc_agent.requests, "request", raise_if_called) + result = hc_agent.preview({"run": "preview"}) + assert result["mode"] == "preview" + + +def test_apply_calls_requests_once(monkeypatch): + fake_response = FakeResponse(status_code=202) + request_mock = mock.Mock(return_value=fake_response) + monkeypatch.setattr(hc_agent.requests, "request", request_mock) + + response = hc_agent.apply({"run": "apply"}) + + assert response is fake_response + request_mock.assert_called_once() + + +def test_gateway_writes_run_artifacts(tmp_path): + run_id = hc_agent.gateway_run({"run": "gateway"}, runstore_root=tmp_path) + report_path = tmp_path / "runs" / run_id / "report.md" + assert report_path.exists()