From 05ad02d2841929f476dccb302153ac9ea1581e58 Mon Sep 17 00:00:00 2001 From: Nalajmah <55954113+Alajmah@users.noreply.github.com> Date: Sat, 6 Jun 2026 04:46:34 +0300 Subject: [PATCH] Add API endpoint behavior tests --- tests/test_api.py | 51 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 9 deletions(-) diff --git a/tests/test_api.py b/tests/test_api.py index dfee510..1d1be92 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -1,18 +1,17 @@ from datetime import date -from sovereign_cortex.api import build_report_result, create_app +from sovereign_cortex.api import CommandRequest, build_report_result, create_app from sovereign_cortex.events import EventType -def test_app_exposes_expected_routes(): - app = create_app() - paths = {route.path for route in app.routes} - - assert "/health" in paths - assert "/commands" in paths +def _endpoint(app, path: str): + for route in app.routes: + if route.path == path: + return route.endpoint + raise AssertionError(f"Route not found: {path}") -def test_project_report_result_builds_api_events(tmp_path): +def _write_demo_workspace(tmp_path): vault = tmp_path / "vault" / "demo-project" / "tasks" vault.mkdir(parents=True) (tmp_path / "workspaces.yaml").write_text( @@ -20,10 +19,44 @@ def test_project_report_result_builds_api_events(tmp_path): encoding="utf-8", ) (vault / "launch.md").write_text( - "---\ntitle: Launch\nstatus: planned\ndue: 2026-06-05\ndependencies: []\n---\n# Launch\n", + "---\nid: launch\ntitle: Launch\nstatus: planned\ndue: 2026-06-05\ndependencies: []\n---\n# Launch\n", encoding="utf-8", ) + +def test_app_exposes_expected_routes(): + app = create_app() + paths = {route.path for route in app.routes} + + assert "/health" in paths + assert "/commands" in paths + + +def test_health_endpoint_returns_ok(): + app = create_app() + health = _endpoint(app, "/health") + + assert health() == {"status": "ok"} + + +def test_command_endpoint_runs_read_only_report_and_records_events(tmp_path): + _write_demo_workspace(tmp_path) + app = create_app(tmp_path) + run_command = _endpoint(app, "/commands") + + response = run_command(CommandRequest(text="Project summary", date=date(2026, 6, 6))) + + assert response.status == "no_action" + assert response.approval_id is None + assert "Project summary for demo-project:" in response.message + assert response.events[0]["event_type"] == "CommandReceived" + records = list((tmp_path / "approval_requests" / "records").glob("*.jsonl")) + assert records + + +def test_project_report_result_builds_api_events(tmp_path): + _write_demo_workspace(tmp_path) + result = build_report_result(tmp_path, "Project summary", "demo-project", date(2026, 6, 6)) assert result is not None