Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions hc_agent.py
Original file line number Diff line number Diff line change
@@ -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
38 changes: 38 additions & 0 deletions tests/test_hc_agent_smoke.py
Original file line number Diff line number Diff line change
@@ -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()